blob: 187c28a9d00655b722f28a88aa39e8e0a0e09f29 [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,
2053 * and 13 + 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 Beckercab87e62019-04-29 13:52:53 +01002080 add_data[11 + rec->cid_len + 0] = ( rec->data_len >> 8 ) & 0xFF;
2081 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 0 ) & 0xFF;
Hanno Beckercab87e62019-04-29 13:52:53 +01002082 *add_data_len = 13 + rec->cid_len;
Hanno Becker43c24b82019-05-01 09:45:57 +01002083#else /* MBEDTLS_SSL_CID */
2084 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2085 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
Hanno Beckercab87e62019-04-29 13:52:53 +01002086 *add_data_len = 13;
2087#endif /* MBEDTLS_SSL_CID */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002088}
2089
Hanno Beckera18d1322018-01-03 14:27:32 +00002090int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2091 mbedtls_ssl_transform *transform,
2092 mbedtls_record *rec,
2093 int (*f_rng)(void *, unsigned char *, size_t),
2094 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002097 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002098 unsigned char * data;
Hanno Becker75f080f2019-04-30 15:01:51 +01002099 unsigned char add_data[13 + MBEDTLS_SSL_CID_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002100 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002101 size_t post_avail;
2102
2103 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002104#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105 ((void) ssl);
2106#endif
2107
2108 /* The PRNG is used for dynamic IV generation that's used
2109 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2110#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2111 ( defined(MBEDTLS_AES_C) || \
2112 defined(MBEDTLS_ARIA_C) || \
2113 defined(MBEDTLS_CAMELLIA_C) ) && \
2114 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2115 ((void) f_rng);
2116 ((void) p_rng);
2117#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002121 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002122 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2125 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002126 if( rec == NULL
2127 || rec->buf == NULL
2128 || rec->buf_len < rec->data_offset
2129 || rec->buf_len - rec->data_offset < rec->data_len
2130#if defined(MBEDTLS_SSL_CID)
2131 || rec->cid_len != 0
2132#endif
2133 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 {
2135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002137 }
2138
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002140 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002142 data, rec->data_len );
2143
2144 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2145
2146 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2147 {
2148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2149 (unsigned) rec->data_len,
2150 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2152 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002153
Hanno Beckercab87e62019-04-29 13:52:53 +01002154#if defined(MBEDTLS_SSL_CID)
2155 /*
2156 * Add CID information
2157 */
2158 rec->cid_len = transform->out_cid_len;
2159 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2160 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002161
2162 if( rec->cid_len != 0 )
2163 {
2164 /*
2165 * Wrap plaintext into DTLSInnerPlaintext structure
2166 *
2167 * struct {
2168 * opaque content[DTLSPlaintext.length];
2169 * ContentType real_type;
2170 * uint8 zeros[length_of_padding];
2171 * } DTLSInnerPlaintext;
2172 *
2173 * and change the record content type.
2174 *
2175 * The rest of the record encryption stays
2176 * unmodified (apart from the inclusion of
2177 * the CID into the additional data for the
2178 * record MAC).
2179 */
2180 if( ssl_cid_build_inner_plaintext( data,
2181 &rec->data_len,
2182 post_avail,
2183 rec->type ) != 0 )
2184 {
2185 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2186 }
2187
2188 rec->type = MBEDTLS_SSL_MSG_CID;
2189 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002190#endif /* MBEDTLS_SSL_CID */
2191
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002192 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2193
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002195 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 */
Hanno Becker52344c22018-01-03 15:24:20 +00002197#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 if( mode == MBEDTLS_MODE_STREAM ||
2199 ( mode == MBEDTLS_MODE_CBC
2200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002201 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002202#endif
2203 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002205 if( post_avail < transform->maclen )
2206 {
2207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2208 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2209 }
2210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002213 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002214 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002215 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2216 data, rec->data_len, rec->ctr, rec->type, mac );
2217 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002218 }
2219 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2222 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002223 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002224 {
Hanno Becker992b6872017-11-09 18:57:39 +00002225 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2226
Hanno Beckercab87e62019-04-29 13:52:53 +01002227 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002228
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002230 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002231 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2232 data, rec->data_len );
2233 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2234 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2235
2236 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002237 }
2238 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002239#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002243 }
2244
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2246 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002248 rec->data_len += transform->maclen;
2249 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002250 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002251 }
Hanno Becker52344c22018-01-03 15:24:20 +00002252#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002254 /*
2255 * Encrypt
2256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2258 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002260 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002261 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002263 "including %d bytes of padding",
2264 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2267 transform->iv_enc, transform->ivlen,
2268 data, rec->data_len,
2269 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002272 return( ret );
2273 }
2274
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002275 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 }
Paul Bakker68884e32013-01-07 18:20:04 +01002281 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002283
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002284#if defined(MBEDTLS_GCM_C) || \
2285 defined(MBEDTLS_CCM_C) || \
2286 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002288 mode == MBEDTLS_MODE_CCM ||
2289 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002290 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002291 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002292 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002293 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002294
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002295 /* Check that there's space for both the authentication tag
2296 * and the explicit IV before and after the record content. */
2297 if( post_avail < transform->taglen ||
2298 rec->data_offset < explicit_iv_len )
2299 {
2300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2301 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2302 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002303
Paul Bakker68884e32013-01-07 18:20:04 +01002304 /*
2305 * Generate IV
2306 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002307 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2308 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002309 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002310 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002311 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2312 explicit_iv_len );
2313 /* Prefix record content with explicit IV. */
2314 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002315 }
2316 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2317 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002318 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002319 unsigned char i;
2320
2321 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2322
2323 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002324 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 }
2326 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002327 {
2328 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002331 }
2332
Hanno Beckercab87e62019-04-29 13:52:53 +01002333 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002334
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2336 iv, transform->ivlen );
2337 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002338 data - explicit_iv_len, explicit_iv_len );
2339 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002340 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002342 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002344
Paul Bakker68884e32013-01-07 18:20:04 +01002345 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002346 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002347 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002348
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002349 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002350 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002351 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 data, rec->data_len, /* source */
2353 data, &rec->data_len, /* destination */
2354 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002357 return( ret );
2358 }
2359
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002360 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2361 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002362
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363 rec->data_len += transform->taglen + explicit_iv_len;
2364 rec->data_offset -= explicit_iv_len;
2365 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002366 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002368 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2370#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002371 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002374 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 size_t padlen, i;
2376 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002377
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 /* Currently we're always using minimal padding
2379 * (up to 255 bytes would be allowed). */
2380 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2381 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 padlen = 0;
2383
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002384 /* Check there's enough space in the buffer for the padding. */
2385 if( post_avail < padlen + 1 )
2386 {
2387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2388 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2389 }
2390
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002392 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002394 rec->data_len += padlen + 1;
2395 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002398 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002399 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2400 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002401 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002402 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002403 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002404 if( f_rng == NULL )
2405 {
2406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2408 }
2409
2410 if( rec->data_offset < transform->ivlen )
2411 {
2412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2413 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2414 }
2415
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 /*
2417 * Generate IV
2418 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002420 if( ret != 0 )
2421 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002423 memcpy( data - transform->ivlen, transform->iv_enc,
2424 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002432 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2435 transform->iv_enc,
2436 transform->ivlen,
2437 data, rec->data_len,
2438 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002441 return( ret );
2442 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002443
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002444 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002448 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002452 {
2453 /*
2454 * Save IV in SSL3 and TLS1
2455 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2457 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002460#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002461 {
2462 data -= transform->ivlen;
2463 rec->data_offset -= transform->ivlen;
2464 rec->data_len += transform->ivlen;
2465 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002468 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002469 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002470 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2471
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002472 /*
2473 * MAC(MAC_write_key, seq_num +
2474 * TLSCipherText.type +
2475 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002476 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002477 * IV + // except for TLS 1.0
2478 * ENC(content + padding + padding_length));
2479 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480
2481 if( post_avail < transform->maclen)
2482 {
2483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2484 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2485 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486
Hanno Beckercab87e62019-04-29 13:52:53 +01002487 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002490 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002491 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002492
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002493 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002494 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002495 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2496 data, rec->data_len );
2497 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2498 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002502 rec->data_len += transform->maclen;
2503 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002504 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002508 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002510 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002516 /* Make extra sure authentication was performed, exactly once */
2517 if( auth_done != 1 )
2518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 }
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525 return( 0 );
2526}
2527
Hanno Beckera18d1322018-01-03 14:27:32 +00002528int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2529 mbedtls_ssl_transform *transform,
2530 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002531{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002532 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002534 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002535#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002536 size_t padlen = 0, correct = 1;
2537#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002538 unsigned char* data;
Hanno Becker75f080f2019-04-30 15:01:51 +01002539 unsigned char add_data[13 + MBEDTLS_SSL_CID_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002540 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002541
Hanno Beckera18d1322018-01-03 14:27:32 +00002542#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 ((void) ssl);
2544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002547 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002548 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2551 }
2552 if( rec == NULL ||
2553 rec->buf == NULL ||
2554 rec->buf_len < rec->data_offset ||
2555 rec->buf_len - rec->data_offset < rec->data_len )
2556 {
2557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002559 }
2560
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 data = rec->buf + rec->data_offset;
2562 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Beckercab87e62019-04-29 13:52:53 +01002564#if defined(MBEDTLS_SSL_CID)
2565 /*
2566 * Match record's CID with incoming CID.
2567 */
2568
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002569 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002570 /* if( rec->cid_len != transform->in_cid_len || */
2571 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2572 /* { */
2573 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2574 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002575
2576 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002577 rec->cid_len = transform->in_cid_len;
2578 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2579 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2580#endif /* MBEDTLS_SSL_CID */
2581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2583 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002584 {
2585 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002586 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2587 transform->iv_dec,
2588 transform->ivlen,
2589 data, rec->data_len,
2590 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 return( ret );
2594 }
2595
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002596 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
Paul Bakker68884e32013-01-07 18:20:04 +01002602 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604#if defined(MBEDTLS_GCM_C) || \
2605 defined(MBEDTLS_CCM_C) || \
2606 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002608 mode == MBEDTLS_MODE_CCM ||
2609 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002610 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002611 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002613
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002614 /*
2615 * Compute and update sizes
2616 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 "+ taglen (%d)", rec->data_len,
2621 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002623 }
Paul Bakker68884e32013-01-07 18:20:04 +01002624
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625 /*
2626 * Prepare IV
2627 */
2628 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2629 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002630 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002631 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002632 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002633
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002634 }
2635 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2636 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002637 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 unsigned char i;
2639
2640 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2641
2642 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002643 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002644 }
2645 else
2646 {
2647 /* Reminder if we ever add an AEAD mode with a different size */
2648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2650 }
2651
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 data += explicit_iv_len;
2653 rec->data_offset += explicit_iv_len;
2654 rec->data_len -= explicit_iv_len + transform->taglen;
2655
Hanno Beckercab87e62019-04-29 13:52:53 +01002656 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002658 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659
2660 memcpy( transform->iv_dec + transform->fixed_ivlen,
2661 data - explicit_iv_len, explicit_iv_len );
2662
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002663 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002665 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002666
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002668 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002669 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002670 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2672 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002673 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674 data, rec->data_len,
2675 data, &olen,
2676 data + rec->data_len,
2677 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2682 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002683
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002684 return( ret );
2685 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002686 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002687
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002692 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002693
Paul Bakkerca4ab492012-04-18 14:23:57 +00002694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2697#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002698 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002701 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002702
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 /*
Paul Bakker45829992013-01-03 14:52:21 +01002704 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002707 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2708 {
2709 /* The ciphertext is prefixed with the CBC IV. */
2710 minlen += transform->ivlen;
2711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002712#endif
Paul Bakker45829992013-01-03 14:52:21 +01002713
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 /* Size considerations:
2715 *
2716 * - The CBC cipher text must not be empty and hence
2717 * at least of size transform->ivlen.
2718 *
2719 * Together with the potential IV-prefix, this explains
2720 * the first of the two checks below.
2721 *
2722 * - The record must contain a MAC, either in plain or
2723 * encrypted, depending on whether Encrypt-then-MAC
2724 * is used or not.
2725 * - If it is, the message contains the IV-prefix,
2726 * the CBC ciphertext, and the MAC.
2727 * - If it is not, the padded plaintext, and hence
2728 * the CBC ciphertext, has at least length maclen + 1
2729 * because there is at least the padding length byte.
2730 *
2731 * As the CBC ciphertext is not empty, both cases give the
2732 * lower bound minlen + maclen + 1 on the record size, which
2733 * we test for in the second check below.
2734 */
2735 if( rec->data_len < minlen + transform->ivlen ||
2736 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002739 "+ 1 ) ( + expl IV )", rec->data_len,
2740 transform->ivlen,
2741 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002743 }
2744
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002745 /*
2746 * Authenticate before decrypt if enabled
2747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002750 {
Hanno Becker992b6872017-11-09 18:57:39 +00002751 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002754
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2756 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002757
Hanno Beckercab87e62019-04-29 13:52:53 +01002758 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002759
Hanno Beckercab87e62019-04-29 13:52:53 +01002760 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2761 add_data_len );
2762 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2763 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2765 data, rec->data_len );
2766 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2767 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002768
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2770 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002771 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002772 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2775 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002780 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783
2784 /*
2785 * Check length sanity
2786 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002790 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002792 }
2793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002796 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002797 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002798 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 /* This is safe because data_len >= minlen + maclen + 1 initially,
2801 * and at this point we have at most subtracted maclen (note that
2802 * minlen == transform->ivlen here). */
2803 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 data += transform->ivlen;
2806 rec->data_offset += transform->ivlen;
2807 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002808 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002810
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2812 transform->iv_dec, transform->ivlen,
2813 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002816 return( ret );
2817 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002818
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002819 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002823 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002827 {
2828 /*
2829 * Save IV in SSL3 and TLS1
2830 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2832 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002834#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 /* Safe since data_len >= minlen + maclen + 1, so after having
2837 * subtracted at most minlen and maclen up to this point,
2838 * data_len > 0. */
2839 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002840
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 if( auth_done == 1 )
2842 {
2843 correct *= ( rec->data_len >= padlen + 1 );
2844 padlen *= ( rec->data_len >= padlen + 1 );
2845 }
2846 else
Paul Bakker45829992013-01-03 14:52:21 +01002847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002849 if( rec->data_len < transform->maclen + padlen + 1 )
2850 {
2851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2852 rec->data_len,
2853 transform->maclen,
2854 padlen + 1 ) );
2855 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002856#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857
2858 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2859 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002862 padlen++;
2863
2864 /* Regardless of the validity of the padding,
2865 * we have data_len >= padlen here. */
2866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002868 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872#if defined(MBEDTLS_SSL_DEBUG_ALL)
2873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 "should be no more than %d",
2875 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002876#endif
Paul Bakker45829992013-01-03 14:52:21 +01002877 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 }
2879 }
2880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2882#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2883 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002884 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 /* The padding check involves a series of up to 256
2887 * consecutive memory reads at the end of the record
2888 * plaintext buffer. In order to hide the length and
2889 * validity of the padding, always perform exactly
2890 * `min(256,plaintext_len)` reads (but take into account
2891 * only the last `padlen` bytes for the padding check). */
2892 size_t pad_count = 0;
2893 size_t real_count = 0;
2894 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002895
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002896 /* Index of first padding byte; it has been ensured above
2897 * that the subtraction is safe. */
2898 size_t const padding_idx = rec->data_len - padlen;
2899 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2900 size_t const start_idx = rec->data_len - num_checks;
2901 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002902
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002904 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905 real_count |= ( idx >= padding_idx );
2906 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002907 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002908 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002911 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002913#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002914 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2918 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002922 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002923
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002924 /* If the padding was found to be invalid, padlen == 0
2925 * and the subtraction is safe. If the padding was found valid,
2926 * padlen hasn't been changed and the previous assertion
2927 * data_len >= padlen still holds. */
2928 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002932 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002938#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002941#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
2943 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002944 * Authenticate if not done yet.
2945 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 */
Hanno Becker52344c22018-01-03 15:24:20 +00002947#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002948 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002949 {
Hanno Becker992b6872017-11-09 18:57:39 +00002950 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002951
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002952 /* If the initial value of padlen was such that
2953 * data_len < maclen + padlen + 1, then padlen
2954 * got reset to 1, and the initial check
2955 * data_len >= minlen + maclen + 1
2956 * guarantees that at this point we still
2957 * have at least data_len >= maclen.
2958 *
2959 * If the initial value of padlen was such that
2960 * data_len >= maclen + padlen + 1, then we have
2961 * subtracted either padlen + 1 (if the padding was correct)
2962 * or 0 (if the padding was incorrect) since then,
2963 * hence data_len >= maclen in any case.
2964 */
2965 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Hanno Beckercab87e62019-04-29 13:52:53 +01002967 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002970 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002971 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002972 ssl_mac( &transform->md_ctx_dec,
2973 transform->mac_dec,
2974 data, rec->data_len,
2975 rec->ctr, rec->type,
2976 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002977 }
2978 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2980#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2981 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002982 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002983 {
2984 /*
2985 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002986 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002987 *
2988 * Known timing attacks:
2989 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2990 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002991 * To compensate for different timings for the MAC calculation
2992 * depending on how much padding was removed (which is determined
2993 * by padlen), process extra_run more blocks through the hash
2994 * function.
2995 *
2996 * The formula in the paper is
2997 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2998 * where L1 is the size of the header plus the decrypted message
2999 * plus CBC padding and L2 is the size of the header plus the
3000 * decrypted message. This is for an underlying hash function
3001 * with 64-byte blocks.
3002 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3003 * correctly. We round down instead of up, so -56 is the correct
3004 * value for our calculations instead of -55.
3005 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003006 * Repeat the formula rather than defining a block_size variable.
3007 * This avoids requiring division by a variable at runtime
3008 * (which would be marginally less efficient and would require
3009 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003010 */
3011 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003013
3014 /*
3015 * The next two sizes are the minimum and maximum values of
3016 * in_msglen over all padlen values.
3017 *
3018 * They're independent of padlen, since we previously did
3019 * in_msglen -= padlen.
3020 *
3021 * Note that max_len + maclen is never more than the buffer
3022 * length, as we previously did in_msglen -= maclen too.
3023 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003025 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3026
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003027 memset( tmp, 0, sizeof( tmp ) );
3028
3029 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003030 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003031#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3032 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003033 case MBEDTLS_MD_MD5:
3034 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003035 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003036 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003037 extra_run =
3038 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3039 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 break;
3041#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003042#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003044 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003045 extra_run =
3046 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3047 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 break;
3049#endif
3050 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3053 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003054
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003055 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003056
Hanno Beckercab87e62019-04-29 13:52:53 +01003057 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3058 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3060 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003061 /* Make sure we access everything even when padlen > 0. This
3062 * makes the synchronisation requirements for just-in-time
3063 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003064 ssl_read_memory( data + rec->data_len, padlen );
3065 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003066
3067 /* Call mbedtls_md_process at least once due to cache attacks
3068 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003069 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003071
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003073
3074 /* Make sure we access all the memory that could contain the MAC,
3075 * before we check it in the next code block. This makes the
3076 * synchronisation requirements for just-in-time Prime+Probe
3077 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 ssl_read_memory( data + min_len,
3079 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003080 }
3081 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3083 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003089#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3091 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3095 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#if defined(MBEDTLS_SSL_DEBUG_ALL)
3098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003099#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 correct = 0;
3101 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003102 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003103 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003104
3105 /*
3106 * Finally check the correct flag
3107 */
3108 if( correct == 0 )
3109 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003110#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003111
3112 /* Make extra sure authentication was performed, exactly once */
3113 if( auth_done != 1 )
3114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003119#if defined(MBEDTLS_SSL_CID)
3120 if( rec->cid_len != 0 )
3121 {
3122 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3123 &rec->type );
3124 if( ret != 0 )
3125 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3126 }
3127#endif /* MBEDTLS_SSL_CID */
3128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
3131 return( 0 );
3132}
3133
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003134#undef MAC_NONE
3135#undef MAC_PLAINTEXT
3136#undef MAC_CIPHERTEXT
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003139/*
3140 * Compression/decompression functions
3141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003143{
3144 int ret;
3145 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003146 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003147 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003148 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003151
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003152 if( len_pre == 0 )
3153 return( 0 );
3154
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155 memcpy( msg_pre, ssl->out_msg, len_pre );
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158 ssl->out_msglen ) );
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003161 ssl->out_msg, ssl->out_msglen );
3162
Paul Bakker48916f92012-09-16 19:57:18 +00003163 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3164 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3165 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003166 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167
Paul Bakker48916f92012-09-16 19:57:18 +00003168 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003169 if( ret != Z_OK )
3170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3172 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173 }
3174
Angus Grattond8213d02016-05-25 20:56:48 +10003175 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003176 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179 ssl->out_msglen ) );
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003182 ssl->out_msg, ssl->out_msglen );
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185
3186 return( 0 );
3187}
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190{
3191 int ret;
3192 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003193 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003195 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003199 if( len_pre == 0 )
3200 return( 0 );
3201
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 memcpy( msg_pre, ssl->in_msg, len_pre );
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205 ssl->in_msglen ) );
3206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003208 ssl->in_msg, ssl->in_msglen );
3209
Paul Bakker48916f92012-09-16 19:57:18 +00003210 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3211 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3212 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003213 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003214 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215
Paul Bakker48916f92012-09-16 19:57:18 +00003216 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003217 if( ret != Z_OK )
3218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3220 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221 }
3222
Angus Grattond8213d02016-05-25 20:56:48 +10003223 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003224 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227 ssl->in_msglen ) );
3228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003230 ssl->in_msg, ssl->in_msglen );
3231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233
3234 return( 0 );
3235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3239static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if defined(MBEDTLS_SSL_PROTO_DTLS)
3242static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003243{
3244 /* If renegotiation is not enforced, retransmit until we would reach max
3245 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003246 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003247 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003248 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003249 unsigned char doublings = 1;
3250
3251 while( ratio != 0 )
3252 {
3253 ++doublings;
3254 ratio >>= 1;
3255 }
3256
3257 if( ++ssl->renego_records_seen > doublings )
3258 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003260 return( 0 );
3261 }
3262 }
3263
3264 return( ssl_write_hello_request( ssl ) );
3265}
3266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003268
Paul Bakker5121ce52009-01-03 21:22:43 +00003269/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003270 * Fill the input message buffer by appending data to it.
3271 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003272 *
3273 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3274 * available (from this read and/or a previous one). Otherwise, an error code
3275 * is returned (possibly EOF or WANT_READ).
3276 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003277 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3278 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3279 * since we always read a whole datagram at once.
3280 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003281 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003282 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003285{
Paul Bakker23986e52011-04-24 08:57:21 +00003286 int ret;
3287 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003291 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003294 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003296 }
3297
Angus Grattond8213d02016-05-25 20:56:48 +10003298 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003302 }
3303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003307 uint32_t timeout;
3308
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003309 /* Just to be sure */
3310 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3311 {
3312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3313 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3315 }
3316
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003317 /*
3318 * The point is, we need to always read a full datagram at once, so we
3319 * sometimes read more then requested, and handle the additional data.
3320 * It could be the rest of the current record (while fetching the
3321 * header) and/or some other records in the same datagram.
3322 */
3323
3324 /*
3325 * Move to the next record in the already read datagram if applicable
3326 */
3327 if( ssl->next_record_offset != 0 )
3328 {
3329 if( ssl->in_left < ssl->next_record_offset )
3330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003333 }
3334
3335 ssl->in_left -= ssl->next_record_offset;
3336
3337 if( ssl->in_left != 0 )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003340 ssl->next_record_offset ) );
3341 memmove( ssl->in_hdr,
3342 ssl->in_hdr + ssl->next_record_offset,
3343 ssl->in_left );
3344 }
3345
3346 ssl->next_record_offset = 0;
3347 }
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003351
3352 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003353 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003354 */
3355 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003359 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003360
3361 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003362 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003363 * are not at the beginning of a new record, the caller did something
3364 * wrong.
3365 */
3366 if( ssl->in_left != 0 )
3367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3369 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003370 }
3371
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003372 /*
3373 * Don't even try to read if time's out already.
3374 * This avoids by-passing the timer when repeatedly receiving messages
3375 * that will end up being dropped.
3376 */
3377 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003378 {
3379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003380 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003381 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003382 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003383 {
Angus Grattond8213d02016-05-25 20:56:48 +10003384 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003387 timeout = ssl->handshake->retransmit_timeout;
3388 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003389 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003393 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003394 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3395 timeout );
3396 else
3397 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400
3401 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003403 }
3404
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003405 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003408 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003411 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003412 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003415 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416 }
3417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003421 return( ret );
3422 }
3423
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003424 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003427 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003429 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003430 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003433 return( ret );
3434 }
3435
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003436 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003439 }
3440
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 if( ret < 0 )
3442 return( ret );
3443
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003444 ssl->in_left = ret;
3445 }
3446 else
3447#endif
3448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003450 ssl->in_left, nb_want ) );
3451
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003452 while( ssl->in_left < nb_want )
3453 {
3454 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003455
3456 if( ssl_check_timer( ssl ) != 0 )
3457 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3458 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003459 {
3460 if( ssl->f_recv_timeout != NULL )
3461 {
3462 ret = ssl->f_recv_timeout( ssl->p_bio,
3463 ssl->in_hdr + ssl->in_left, len,
3464 ssl->conf->read_timeout );
3465 }
3466 else
3467 {
3468 ret = ssl->f_recv( ssl->p_bio,
3469 ssl->in_hdr + ssl->in_left, len );
3470 }
3471 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003474 ssl->in_left, nb_want ) );
3475 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003476
3477 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003479
3480 if( ret < 0 )
3481 return( ret );
3482
mohammad160352aecb92018-03-28 23:41:40 -07003483 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003484 {
Darryl Green11999bb2018-03-13 15:22:58 +00003485 MBEDTLS_SSL_DEBUG_MSG( 1,
3486 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003487 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3489 }
3490
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003491 ssl->in_left += ret;
3492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 }
3494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
3497 return( 0 );
3498}
3499
3500/*
3501 * Flush any data not yet written
3502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003504{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003505 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003506 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003510 if( ssl->f_send == NULL )
3511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003513 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003515 }
3516
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003517 /* Avoid incrementing counter if data is flushed */
3518 if( ssl->out_left == 0 )
3519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003521 return( 0 );
3522 }
3523
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 while( ssl->out_left > 0 )
3525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3527 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Hanno Becker2b1e3542018-08-06 11:19:13 +01003529 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003530 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
3534 if( ret <= 0 )
3535 return( ret );
3536
mohammad160352aecb92018-03-28 23:41:40 -07003537 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003538 {
Darryl Green11999bb2018-03-13 15:22:58 +00003539 MBEDTLS_SSL_DEBUG_MSG( 1,
3540 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003541 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3543 }
3544
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 ssl->out_left -= ret;
3546 }
3547
Hanno Becker2b1e3542018-08-06 11:19:13 +01003548#if defined(MBEDTLS_SSL_PROTO_DTLS)
3549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003550 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003551 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003552 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003553 else
3554#endif
3555 {
3556 ssl->out_hdr = ssl->out_buf + 8;
3557 }
3558 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003561
3562 return( 0 );
3563}
3564
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003565/*
3566 * Functions to handle the DTLS retransmission state machine
3567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003569/*
3570 * Append current handshake message to current outgoing flight
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3576 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3577 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578
3579 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003580 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585 }
3586
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003587 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003588 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 }
3593
3594 /* Copy current handshake message with headers */
3595 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3596 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003597 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 msg->next = NULL;
3599
3600 /* Append to the current flight */
3601 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003602 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603 else
3604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606 while( cur->next != NULL )
3607 cur = cur->next;
3608 cur->next = msg;
3609 }
3610
Hanno Becker3b235902018-08-06 09:54:53 +01003611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 return( 0 );
3613}
3614
3615/*
3616 * Free the current flight of handshake messages
3617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 mbedtls_ssl_flight_item *cur = flight;
3621 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003622
3623 while( cur != NULL )
3624 {
3625 next = cur->next;
3626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 mbedtls_free( cur->p );
3628 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003629
3630 cur = next;
3631 }
3632}
3633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3635static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003636#endif
3637
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003638/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639 * Swap transform_out and out_ctr with the alternative ones
3640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003642{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003644 unsigned char tmp_out_ctr[8];
3645
3646 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649 return;
3650 }
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003653
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003654 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003655 tmp_transform = ssl->transform_out;
3656 ssl->transform_out = ssl->handshake->alt_transform_out;
3657 ssl->handshake->alt_transform_out = tmp_transform;
3658
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003659 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003660 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3661 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003663
3664 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003665 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3668 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3673 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003674 }
3675 }
3676#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003677}
3678
3679/*
3680 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003681 */
3682int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3683{
3684 int ret = 0;
3685
3686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3687
3688 ret = mbedtls_ssl_flight_transmit( ssl );
3689
3690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3691
3692 return( ret );
3693}
3694
3695/*
3696 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697 *
3698 * Need to remember the current message in case flush_output returns
3699 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003700 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003702int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003704 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003710
3711 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003712 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003713 ssl_swap_epochs( ssl );
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003716 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717
3718 while( ssl->handshake->cur_msg != NULL )
3719 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003720 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003721 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003722
Hanno Beckere1dcb032018-08-17 16:47:58 +01003723 int const is_finished =
3724 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3725 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3726
Hanno Becker04da1892018-08-14 13:22:10 +01003727 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3728 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3729
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003730 /* Swap epochs before sending Finished: we can't do it after
3731 * sending ChangeCipherSpec, in case write returns WANT_READ.
3732 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003733 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003734 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003736 ssl_swap_epochs( ssl );
3737 }
3738
Hanno Becker67bc7c32018-08-06 11:33:50 +01003739 ret = ssl_get_remaining_payload_in_datagram( ssl );
3740 if( ret < 0 )
3741 return( ret );
3742 max_frag_len = (size_t) ret;
3743
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003744 /* CCS is copied as is, while HS messages may need fragmentation */
3745 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3746 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003747 if( max_frag_len == 0 )
3748 {
3749 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3750 return( ret );
3751
3752 continue;
3753 }
3754
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003755 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003756 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003757 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003758
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003759 /* Update position inside current message */
3760 ssl->handshake->cur_msg_p += cur->len;
3761 }
3762 else
3763 {
3764 const unsigned char * const p = ssl->handshake->cur_msg_p;
3765 const size_t hs_len = cur->len - 12;
3766 const size_t frag_off = p - ( cur->p + 12 );
3767 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003769
Hanno Beckere1dcb032018-08-17 16:47:58 +01003770 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003771 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003772 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003773 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003774
Hanno Becker67bc7c32018-08-06 11:33:50 +01003775 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3776 return( ret );
3777
3778 continue;
3779 }
3780 max_hs_frag_len = max_frag_len - 12;
3781
3782 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3783 max_hs_frag_len : rem_len;
3784
3785 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003788 (unsigned) cur_hs_frag_len,
3789 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003790 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003791
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003792 /* Messages are stored with handshake headers as if not fragmented,
3793 * copy beginning of headers then fill fragmentation fields.
3794 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3795 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003797 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3798 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3799 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3800
Hanno Becker67bc7c32018-08-06 11:33:50 +01003801 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3802 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3803 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804
3805 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3806
Hanno Becker3f7b9732018-08-28 09:53:25 +01003807 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3809 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003810 ssl->out_msgtype = cur->type;
3811
3812 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003813 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003814 }
3815
3816 /* If done with the current message move to the next one if any */
3817 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3818 {
3819 if( cur->next != NULL )
3820 {
3821 ssl->handshake->cur_msg = cur->next;
3822 ssl->handshake->cur_msg_p = cur->next->p + 12;
3823 }
3824 else
3825 {
3826 ssl->handshake->cur_msg = NULL;
3827 ssl->handshake->cur_msg_p = NULL;
3828 }
3829 }
3830
3831 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003832 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003835 return( ret );
3836 }
3837 }
3838
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3840 return( ret );
3841
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003842 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3844 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003845 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003848 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3849 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003850
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003852
3853 return( 0 );
3854}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003855
3856/*
3857 * To be called when the last message of an incoming flight is received.
3858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003860{
3861 /* We won't need to resend that one any more */
3862 ssl_flight_free( ssl->handshake->flight );
3863 ssl->handshake->flight = NULL;
3864 ssl->handshake->cur_msg = NULL;
3865
3866 /* The next incoming flight will start with this msg_seq */
3867 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3868
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003869 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003870 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003871
Hanno Becker0271f962018-08-16 13:23:47 +01003872 /* Clear future message buffering structure. */
3873 ssl_buffering_free( ssl );
3874
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003875 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003876 ssl_set_timer( ssl, 0 );
3877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3879 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003882 }
3883 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003885}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003886
3887/*
3888 * To be called when the last message of an outgoing flight is send.
3889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003891{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003892 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003893 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3896 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003899 }
3900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003904
Paul Bakker5121ce52009-01-03 21:22:43 +00003905/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003906 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003907 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003908
3909/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003910 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003911 *
3912 * - fill in handshake headers
3913 * - update handshake checksum
3914 * - DTLS: save message for resending
3915 * - then pass to the record layer
3916 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003917 * DTLS: except for HelloRequest, messages are only queued, and will only be
3918 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003919 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003920 * Inputs:
3921 * - ssl->out_msglen: 4 + actual handshake message len
3922 * (4 is the size of handshake headers for TLS)
3923 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3924 * - ssl->out_msg + 4: the handshake message body
3925 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003926 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003927 * - ssl->out_msglen: the length of the record contents
3928 * (including handshake headers but excluding record headers)
3929 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003930 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003931int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003932{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003933 int ret;
3934 const size_t hs_len = ssl->out_msglen - 4;
3935 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003936
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3938
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003939 /*
3940 * Sanity checks
3941 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003942 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003943 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3944 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003945 /* In SSLv3, the client might send a NoCertificate alert. */
3946#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3947 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3948 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3949 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3950#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3951 {
3952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3954 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003957 /* Whenever we send anything different from a
3958 * HelloRequest we should be in a handshake - double check. */
3959 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3960 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003961 ssl->handshake == NULL )
3962 {
3963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003968 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003971 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003974 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003976
Hanno Beckerb50a2532018-08-06 11:52:54 +01003977 /* Double-check that we did not exceed the bounds
3978 * of the outgoing record buffer.
3979 * This should never fail as the various message
3980 * writing functions must obey the bounds of the
3981 * outgoing record buffer, but better be safe.
3982 *
3983 * Note: We deliberately do not check for the MTU or MFL here.
3984 */
3985 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3986 {
3987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3988 "size %u, maximum %u",
3989 (unsigned) ssl->out_msglen,
3990 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3992 }
3993
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003994 /*
3995 * Fill handshake headers
3996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003999 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4000 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4001 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004002
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004003 /*
4004 * DTLS has additional fields in the Handshake layer,
4005 * between the length field and the actual payload:
4006 * uint16 message_seq;
4007 * uint24 fragment_offset;
4008 * uint24 fragment_length;
4009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004012 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004013 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004014 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004015 {
4016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4017 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004018 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004019 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4021 }
4022
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004023 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004024 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004025
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004026 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004027 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004028 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004029 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4030 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4031 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004032 }
4033 else
4034 {
4035 ssl->out_msg[4] = 0;
4036 ssl->out_msg[5] = 0;
4037 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004038
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004039 /* Handshake hashes are computed without fragmentation,
4040 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004041 memset( ssl->out_msg + 6, 0x00, 3 );
4042 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004045
Hanno Becker0207e532018-08-28 10:28:28 +01004046 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004047 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4048 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004049 }
4050
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004051 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004054 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4055 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056 {
4057 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004060 return( ret );
4061 }
4062 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004063 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004065 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004066 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004067 {
4068 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4069 return( ret );
4070 }
4071 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004072
4073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4074
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004075 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004076}
4077
4078/*
4079 * Record layer functions
4080 */
4081
4082/*
4083 * Write current record.
4084 *
4085 * Uses:
4086 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4087 * - ssl->out_msglen: length of the record content (excl headers)
4088 * - ssl->out_msg: record content
4089 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004090int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004091{
4092 int ret, done = 0;
4093 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004094 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004095
4096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004099 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004101 {
4102 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004105 return( ret );
4106 }
4107
4108 len = ssl->out_msglen;
4109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4113 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 ret = mbedtls_ssl_hw_record_write( ssl );
4118 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4121 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004122 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004123
4124 if( ret == 0 )
4125 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004126 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004128 if( !done )
4129 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004130 unsigned i;
4131 size_t protected_record_size;
4132
Paul Bakker05ef8352012-05-08 09:17:57 +00004133 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004135 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004136
Hanno Becker19859472018-08-06 09:40:20 +01004137 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004138 ssl->out_len[0] = (unsigned char)( len >> 8 );
4139 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004140
Paul Bakker48916f92012-09-16 19:57:18 +00004141 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004142 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004143 mbedtls_record rec;
4144
4145 rec.buf = ssl->out_iv;
4146 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4147 ( ssl->out_iv - ssl->out_buf );
4148 rec.data_len = ssl->out_msglen;
4149 rec.data_offset = ssl->out_msg - rec.buf;
4150
4151 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4152 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4153 ssl->conf->transport, rec.ver );
4154 rec.type = ssl->out_msgtype;
4155
Hanno Becker43c24b82019-05-01 09:45:57 +01004156#if defined(MBEDTLS_SSL_CID)
4157 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004158 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004159#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004160
Hanno Beckera18d1322018-01-03 14:27:32 +00004161 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004162 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004165 return( ret );
4166 }
4167
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004168 if( rec.data_offset != 0 )
4169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4172 }
4173
Hanno Becker78f839d2019-03-14 12:56:23 +00004174 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004175 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4176 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004177 }
4178
Hanno Becker2b1e3542018-08-06 11:19:13 +01004179 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4180
4181#if defined(MBEDTLS_SSL_PROTO_DTLS)
4182 /* In case of DTLS, double-check that we don't exceed
4183 * the remaining space in the datagram. */
4184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4185 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004186 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004187 if( ret < 0 )
4188 return( ret );
4189
4190 if( protected_record_size > (size_t) ret )
4191 {
4192 /* Should never happen */
4193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4194 }
4195 }
4196#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004199 "version = [%d:%d], msglen = %d",
4200 ssl->out_hdr[0], ssl->out_hdr[1],
4201 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004204 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004205
4206 ssl->out_left += protected_record_size;
4207 ssl->out_hdr += protected_record_size;
4208 ssl_update_out_pointers( ssl, ssl->transform_out );
4209
Hanno Becker04484622018-08-06 09:49:38 +01004210 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4211 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4212 break;
4213
4214 /* The loop goes to its end iff the counter is wrapping */
4215 if( i == ssl_ep_len( ssl ) )
4216 {
4217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4218 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004220 }
4221
Hanno Becker67bc7c32018-08-06 11:33:50 +01004222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4224 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004226 size_t remaining;
4227 ret = ssl_get_remaining_payload_in_datagram( ssl );
4228 if( ret < 0 )
4229 {
4230 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4231 ret );
4232 return( ret );
4233 }
4234
4235 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004236 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004237 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004238 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004239 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004240 else
4241 {
Hanno Becker513815a2018-08-20 11:56:09 +01004242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004243 }
4244 }
4245#endif /* MBEDTLS_SSL_PROTO_DTLS */
4246
4247 if( ( flush == SSL_FORCE_FLUSH ) &&
4248 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004251 return( ret );
4252 }
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004255
4256 return( 0 );
4257}
4258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004260
4261static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4262{
4263 if( ssl->in_msglen < ssl->in_hslen ||
4264 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4265 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4266 {
4267 return( 1 );
4268 }
4269 return( 0 );
4270}
Hanno Becker44650b72018-08-16 12:51:11 +01004271
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004272static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004273{
4274 return( ( ssl->in_msg[9] << 16 ) |
4275 ( ssl->in_msg[10] << 8 ) |
4276 ssl->in_msg[11] );
4277}
4278
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004279static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004280{
4281 return( ( ssl->in_msg[6] << 16 ) |
4282 ( ssl->in_msg[7] << 8 ) |
4283 ssl->in_msg[8] );
4284}
4285
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004286static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004287{
4288 uint32_t msg_len, frag_off, frag_len;
4289
4290 msg_len = ssl_get_hs_total_len( ssl );
4291 frag_off = ssl_get_hs_frag_off( ssl );
4292 frag_len = ssl_get_hs_frag_len( ssl );
4293
4294 if( frag_off > msg_len )
4295 return( -1 );
4296
4297 if( frag_len > msg_len - frag_off )
4298 return( -1 );
4299
4300 if( frag_len + 12 > ssl->in_msglen )
4301 return( -1 );
4302
4303 return( 0 );
4304}
4305
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004306/*
4307 * Mark bits in bitmask (used for DTLS HS reassembly)
4308 */
4309static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4310{
4311 unsigned int start_bits, end_bits;
4312
4313 start_bits = 8 - ( offset % 8 );
4314 if( start_bits != 8 )
4315 {
4316 size_t first_byte_idx = offset / 8;
4317
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004318 /* Special case */
4319 if( len <= start_bits )
4320 {
4321 for( ; len != 0; len-- )
4322 mask[first_byte_idx] |= 1 << ( start_bits - len );
4323
4324 /* Avoid potential issues with offset or len becoming invalid */
4325 return;
4326 }
4327
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004328 offset += start_bits; /* Now offset % 8 == 0 */
4329 len -= start_bits;
4330
4331 for( ; start_bits != 0; start_bits-- )
4332 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4333 }
4334
4335 end_bits = len % 8;
4336 if( end_bits != 0 )
4337 {
4338 size_t last_byte_idx = ( offset + len ) / 8;
4339
4340 len -= end_bits; /* Now len % 8 == 0 */
4341
4342 for( ; end_bits != 0; end_bits-- )
4343 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4344 }
4345
4346 memset( mask + offset / 8, 0xFF, len / 8 );
4347}
4348
4349/*
4350 * Check that bitmask is full
4351 */
4352static int ssl_bitmask_check( unsigned char *mask, size_t len )
4353{
4354 size_t i;
4355
4356 for( i = 0; i < len / 8; i++ )
4357 if( mask[i] != 0xFF )
4358 return( -1 );
4359
4360 for( i = 0; i < len % 8; i++ )
4361 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4362 return( -1 );
4363
4364 return( 0 );
4365}
4366
Hanno Becker56e205e2018-08-16 09:06:12 +01004367/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004368static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004369 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004370{
Hanno Becker56e205e2018-08-16 09:06:12 +01004371 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004372
Hanno Becker56e205e2018-08-16 09:06:12 +01004373 alloc_len = 12; /* Handshake header */
4374 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004375
Hanno Beckerd07df862018-08-16 09:14:58 +01004376 if( add_bitmap )
4377 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004378
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004379 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004380}
Hanno Becker56e205e2018-08-16 09:06:12 +01004381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004383
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004384static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004385{
4386 return( ( ssl->in_msg[1] << 16 ) |
4387 ( ssl->in_msg[2] << 8 ) |
4388 ssl->in_msg[3] );
4389}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004390
Simon Butcher99000142016-10-13 17:21:01 +01004391int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004392{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004396 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004398 }
4399
Hanno Becker12555c62018-08-16 12:47:53 +01004400 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004403 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004404 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004408 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004409 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004410 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004411
Hanno Becker44650b72018-08-16 12:51:11 +01004412 if( ssl_check_hs_header( ssl ) != 0 )
4413 {
4414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4415 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4416 }
4417
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004418 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004419 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4420 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4421 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4422 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004423 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004424 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4425 {
4426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4427 recv_msg_seq,
4428 ssl->handshake->in_msg_seq ) );
4429 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4430 }
4431
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004432 /* Retransmit only on last message from previous flight, to avoid
4433 * too many retransmissions.
4434 * Besides, No sane server ever retransmits HelloVerifyRequest */
4435 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004439 "message_seq = %d, start_of_flight = %d",
4440 recv_msg_seq,
4441 ssl->handshake->in_flight_start_seq ) );
4442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004446 return( ret );
4447 }
4448 }
4449 else
4450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004452 "message_seq = %d, expected = %d",
4453 recv_msg_seq,
4454 ssl->handshake->in_msg_seq ) );
4455 }
4456
Hanno Becker90333da2017-10-10 11:27:13 +01004457 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004458 }
4459 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004460
Hanno Becker6d97ef52018-08-16 13:09:04 +01004461 /* Message reassembly is handled alongside buffering of future
4462 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004463 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004464 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004465 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004468 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004469 }
4470 }
4471 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004473 /* With TLS we don't handle fragmentation (for now) */
4474 if( ssl->in_msglen < ssl->in_hslen )
4475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4477 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004478 }
4479
Simon Butcher99000142016-10-13 17:21:01 +01004480 return( 0 );
4481}
4482
4483void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4484{
Hanno Becker0271f962018-08-16 13:23:47 +01004485 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004486
Hanno Becker0271f962018-08-16 13:23:47 +01004487 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004488 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004489 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004490 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004491
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004492 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004494 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004495 ssl->handshake != NULL )
4496 {
Hanno Becker0271f962018-08-16 13:23:47 +01004497 unsigned offset;
4498 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004499
Hanno Becker0271f962018-08-16 13:23:47 +01004500 /* Increment handshake sequence number */
4501 hs->in_msg_seq++;
4502
4503 /*
4504 * Clear up handshake buffering and reassembly structure.
4505 */
4506
4507 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004508 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004509
4510 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004511 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4512 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004513 offset++, hs_buf++ )
4514 {
4515 *hs_buf = *(hs_buf + 1);
4516 }
4517
4518 /* Create a fresh last entry */
4519 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004520 }
4521#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004522}
4523
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004524/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004525 * DTLS anti-replay: RFC 6347 4.1.2.6
4526 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004527 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4528 * Bit n is set iff record number in_window_top - n has been seen.
4529 *
4530 * Usually, in_window_top is the last record number seen and the lsb of
4531 * in_window is set. The only exception is the initial state (record number 0
4532 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4535static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004536{
4537 ssl->in_window_top = 0;
4538 ssl->in_window = 0;
4539}
4540
4541static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4542{
4543 return( ( (uint64_t) buf[0] << 40 ) |
4544 ( (uint64_t) buf[1] << 32 ) |
4545 ( (uint64_t) buf[2] << 24 ) |
4546 ( (uint64_t) buf[3] << 16 ) |
4547 ( (uint64_t) buf[4] << 8 ) |
4548 ( (uint64_t) buf[5] ) );
4549}
4550
4551/*
4552 * Return 0 if sequence number is acceptable, -1 otherwise
4553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004555{
4556 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4557 uint64_t bit;
4558
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004559 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004560 return( 0 );
4561
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004562 if( rec_seqnum > ssl->in_window_top )
4563 return( 0 );
4564
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004565 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004566
4567 if( bit >= 64 )
4568 return( -1 );
4569
4570 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4571 return( -1 );
4572
4573 return( 0 );
4574}
4575
4576/*
4577 * Update replay window on new validated record
4578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580{
4581 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4582
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004583 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004584 return;
4585
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004586 if( rec_seqnum > ssl->in_window_top )
4587 {
4588 /* Update window_top and the contents of the window */
4589 uint64_t shift = rec_seqnum - ssl->in_window_top;
4590
4591 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004592 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004593 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004594 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004595 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004596 ssl->in_window |= 1;
4597 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004598
4599 ssl->in_window_top = rec_seqnum;
4600 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004601 else
4602 {
4603 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004604 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004605
4606 if( bit < 64 ) /* Always true, but be extra sure */
4607 ssl->in_window |= (uint64_t) 1 << bit;
4608 }
4609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004612#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004613/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004614static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4615
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004616/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004617 * Without any SSL context, check if a datagram looks like a ClientHello with
4618 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004619 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004620 *
4621 * - if cookie is valid, return 0
4622 * - if ClientHello looks superficially valid but cookie is not,
4623 * fill obuf and set olen, then
4624 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4625 * - otherwise return a specific error code
4626 */
4627static int ssl_check_dtls_clihlo_cookie(
4628 mbedtls_ssl_cookie_write_t *f_cookie_write,
4629 mbedtls_ssl_cookie_check_t *f_cookie_check,
4630 void *p_cookie,
4631 const unsigned char *cli_id, size_t cli_id_len,
4632 const unsigned char *in, size_t in_len,
4633 unsigned char *obuf, size_t buf_len, size_t *olen )
4634{
4635 size_t sid_len, cookie_len;
4636 unsigned char *p;
4637
4638 if( f_cookie_write == NULL || f_cookie_check == NULL )
4639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4640
4641 /*
4642 * Structure of ClientHello with record and handshake headers,
4643 * and expected values. We don't need to check a lot, more checks will be
4644 * done when actually parsing the ClientHello - skipping those checks
4645 * avoids code duplication and does not make cookie forging any easier.
4646 *
4647 * 0-0 ContentType type; copied, must be handshake
4648 * 1-2 ProtocolVersion version; copied
4649 * 3-4 uint16 epoch; copied, must be 0
4650 * 5-10 uint48 sequence_number; copied
4651 * 11-12 uint16 length; (ignored)
4652 *
4653 * 13-13 HandshakeType msg_type; (ignored)
4654 * 14-16 uint24 length; (ignored)
4655 * 17-18 uint16 message_seq; copied
4656 * 19-21 uint24 fragment_offset; copied, must be 0
4657 * 22-24 uint24 fragment_length; (ignored)
4658 *
4659 * 25-26 ProtocolVersion client_version; (ignored)
4660 * 27-58 Random random; (ignored)
4661 * 59-xx SessionID session_id; 1 byte len + sid_len content
4662 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4663 * ...
4664 *
4665 * Minimum length is 61 bytes.
4666 */
4667 if( in_len < 61 ||
4668 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4669 in[3] != 0 || in[4] != 0 ||
4670 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4671 {
4672 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4673 }
4674
4675 sid_len = in[59];
4676 if( sid_len > in_len - 61 )
4677 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4678
4679 cookie_len = in[60 + sid_len];
4680 if( cookie_len > in_len - 60 )
4681 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4682
4683 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4684 cli_id, cli_id_len ) == 0 )
4685 {
4686 /* Valid cookie */
4687 return( 0 );
4688 }
4689
4690 /*
4691 * If we get here, we've got an invalid cookie, let's prepare HVR.
4692 *
4693 * 0-0 ContentType type; copied
4694 * 1-2 ProtocolVersion version; copied
4695 * 3-4 uint16 epoch; copied
4696 * 5-10 uint48 sequence_number; copied
4697 * 11-12 uint16 length; olen - 13
4698 *
4699 * 13-13 HandshakeType msg_type; hello_verify_request
4700 * 14-16 uint24 length; olen - 25
4701 * 17-18 uint16 message_seq; copied
4702 * 19-21 uint24 fragment_offset; copied
4703 * 22-24 uint24 fragment_length; olen - 25
4704 *
4705 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4706 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4707 *
4708 * Minimum length is 28.
4709 */
4710 if( buf_len < 28 )
4711 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4712
4713 /* Copy most fields and adapt others */
4714 memcpy( obuf, in, 25 );
4715 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4716 obuf[25] = 0xfe;
4717 obuf[26] = 0xff;
4718
4719 /* Generate and write actual cookie */
4720 p = obuf + 28;
4721 if( f_cookie_write( p_cookie,
4722 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4723 {
4724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4725 }
4726
4727 *olen = p - obuf;
4728
4729 /* Go back and fill length fields */
4730 obuf[27] = (unsigned char)( *olen - 28 );
4731
4732 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4733 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4734 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4735
4736 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4737 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4738
4739 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4740}
4741
4742/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004743 * Handle possible client reconnect with the same UDP quadruplet
4744 * (RFC 6347 Section 4.2.8).
4745 *
4746 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4747 * that looks like a ClientHello.
4748 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004749 * - if the input looks like a ClientHello without cookies,
4750 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004751 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004752 * - if the input looks like a ClientHello with a valid cookie,
4753 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004754 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004755 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004756 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004757 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004758 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4759 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004760 */
4761static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4762{
4763 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004764 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004766 ret = ssl_check_dtls_clihlo_cookie(
4767 ssl->conf->f_cookie_write,
4768 ssl->conf->f_cookie_check,
4769 ssl->conf->p_cookie,
4770 ssl->cli_id, ssl->cli_id_len,
4771 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004772 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004773
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004774 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4775
4776 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004777 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004778 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004779 * If the error is permanent we'll catch it later,
4780 * if it's not, then hopefully it'll work next time. */
4781 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4782
4783 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004784 }
4785
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004786 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004787 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004788 /* Got a valid cookie, partially reset context */
4789 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4790 {
4791 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4792 return( ret );
4793 }
4794
4795 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004796 }
4797
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004798 return( ret );
4799}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004800#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004801
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004802/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004803 * ContentType type;
4804 * ProtocolVersion version;
4805 * uint16 epoch; // DTLS only
4806 * uint48 sequence_number; // DTLS only
4807 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004808 *
4809 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004810 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004811 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4812 *
4813 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004814 * 1. proceed with the record if this function returns 0
4815 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4816 * 3. return CLIENT_RECONNECT if this function return that value
4817 * 4. drop the whole datagram if this function returns anything else.
4818 * Point 2 is needed when the peer is resending, and we have already received
4819 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004822{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004823 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 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 +02004826
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004828 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004829 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004833 ssl->in_msgtype,
4834 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004836 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4838 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4839 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4840 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004843
4844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004845 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4846 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004847 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4848#endif /* MBEDTLS_SSL_PROTO_DTLS */
4849 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4850 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004853 }
4854
4855 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004856 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4859 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 }
4861
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004862 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4865 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 }
4867
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004868 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004869 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004870 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4873 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004874 }
4875
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004876 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004877 * DTLS-related tests.
4878 * Check epoch before checking length constraint because
4879 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4880 * message gets duplicated before the corresponding Finished message,
4881 * the second ChangeCipherSpec should be discarded because it belongs
4882 * to an old epoch, but not because its length is shorter than
4883 * the minimum record length for packets using the new record transform.
4884 * Note that these two kinds of failures are handled differently,
4885 * as an unexpected record is silently skipped but an invalid
4886 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004887 */
4888#if defined(MBEDTLS_SSL_PROTO_DTLS)
4889 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4890 {
4891 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4892
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004893 /* Check epoch (and sequence number) with DTLS */
4894 if( rec_epoch != ssl->in_epoch )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4897 "expected %d, received %d",
4898 ssl->in_epoch, rec_epoch ) );
4899
4900#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4901 /*
4902 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4903 * access the first byte of record content (handshake type), as we
4904 * have an active transform (possibly iv_len != 0), so use the
4905 * fact that the record header len is 13 instead.
4906 */
4907 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4908 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4909 rec_epoch == 0 &&
4910 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4911 ssl->in_left > 13 &&
4912 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4913 {
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4915 "from the same port" ) );
4916 return( ssl_handle_possible_reconnect( ssl ) );
4917 }
4918 else
4919#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004920 {
4921 /* Consider buffering the record. */
4922 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4923 {
4924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4925 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4926 }
4927
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004928 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004929 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004930 }
4931
4932#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4933 /* Replay detection only works for the current epoch */
4934 if( rec_epoch == ssl->in_epoch &&
4935 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4936 {
4937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4938 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4939 }
4940#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004941
Hanno Becker52c6dc62017-05-26 16:07:36 +01004942 /* Drop unexpected ApplicationData records,
4943 * except at the beginning of renegotiations */
4944 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4945 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4946#if defined(MBEDTLS_SSL_RENEGOTIATION)
4947 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4948 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4949#endif
4950 )
4951 {
4952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4953 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4954 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004955 }
4956#endif /* MBEDTLS_SSL_PROTO_DTLS */
4957
Hanno Becker52c6dc62017-05-26 16:07:36 +01004958
4959 /* Check length against bounds of the current transform and version */
4960 if( ssl->transform_in == NULL )
4961 {
4962 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004963 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004964 {
4965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4966 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4967 }
4968 }
4969 else
4970 {
4971 if( ssl->in_msglen < ssl->transform_in->minlen )
4972 {
4973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4974 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4975 }
4976
4977#if defined(MBEDTLS_SSL_PROTO_SSL3)
4978 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004979 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004980 {
4981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4982 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4983 }
4984#endif
4985#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4986 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4987 /*
4988 * TLS encrypted messages can have up to 256 bytes of padding
4989 */
4990 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4991 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004992 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004993 {
4994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4995 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4996 }
4997#endif
4998 }
4999
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000 return( 0 );
5001}
Paul Bakker5121ce52009-01-03 21:22:43 +00005002
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005003/*
5004 * If applicable, decrypt (and decompress) record content
5005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005007{
5008 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
5011 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5014 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 ret = mbedtls_ssl_hw_record_read( ssl );
5019 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5022 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005023 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005024
5025 if( ret == 0 )
5026 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005027 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005029 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005030 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005031 mbedtls_record rec;
5032
5033 rec.buf = ssl->in_iv;
5034 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5035 - ( ssl->in_iv - ssl->in_buf );
5036 rec.data_len = ssl->in_msglen;
5037 rec.data_offset = 0;
5038
5039 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5040 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5041 ssl->conf->transport, rec.ver );
5042 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005043 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5044 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005047 return( ret );
5048 }
5049
Hanno Becker29800d22018-08-07 14:30:18 +01005050 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
5051 {
5052 /* Should never happen */
5053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5054 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005055
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005056 ssl->in_msglen = rec.data_len;
5057 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5058 ssl->in_len[1] = (unsigned char)( rec.data_len );
5059
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005060 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5061 ssl->in_msg, ssl->in_msglen );
5062
Angus Grattond8213d02016-05-25 20:56:48 +10005063 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5066 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005067 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005068 else if( ssl->in_msglen == 0 )
5069 {
5070#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5071 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5072 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5073 {
5074 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5076 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5077 }
5078#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5079
5080 ssl->nb_zero++;
5081
5082 /*
5083 * Three or more empty messages may be a DoS attack
5084 * (excessive CPU consumption).
5085 */
5086 if( ssl->nb_zero > 3 )
5087 {
5088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
5089 "messages, possible DoS attack" ) );
5090 /* Q: Is that the right error code? */
5091 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5092 }
5093 }
5094 else
5095 ssl->nb_zero = 0;
5096
5097#if defined(MBEDTLS_SSL_PROTO_DTLS)
5098 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5099 {
5100 ; /* in_ctr read from peer, not maintained internally */
5101 }
5102 else
5103#endif
5104 {
5105 unsigned i;
5106 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5107 if( ++ssl->in_ctr[i - 1] != 0 )
5108 break;
5109
5110 /* The loop goes to its end iff the counter is wrapping */
5111 if( i == ssl_ep_len( ssl ) )
5112 {
5113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5114 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5115 }
5116 }
5117
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 }
5119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005121 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005123 {
5124 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005127 return( ret );
5128 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005133 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005136 }
5137#endif
5138
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005139 return( 0 );
5140}
5141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005143
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005144/*
5145 * Read a record.
5146 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005147 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5148 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5149 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005150 */
Hanno Becker1097b342018-08-15 14:09:41 +01005151
5152/* Helper functions for mbedtls_ssl_read_record(). */
5153static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005154static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5155static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005156
Hanno Becker327c93b2018-08-15 13:56:18 +01005157int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005158 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005159{
5160 int ret;
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005163
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005164 if( ssl->keep_current_message == 0 )
5165 {
5166 do {
Simon Butcher99000142016-10-13 17:21:01 +01005167
Hanno Becker26994592018-08-15 14:14:59 +01005168 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005169 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005170 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005171
Hanno Beckere74d5562018-08-15 14:26:08 +01005172 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005173 {
Hanno Becker40f50842018-08-15 14:48:01 +01005174#if defined(MBEDTLS_SSL_PROTO_DTLS)
5175 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005176
Hanno Becker40f50842018-08-15 14:48:01 +01005177 /* We only check for buffered messages if the
5178 * current datagram is fully consumed. */
5179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005180 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005181 {
Hanno Becker40f50842018-08-15 14:48:01 +01005182 if( ssl_load_buffered_message( ssl ) == 0 )
5183 have_buffered = 1;
5184 }
5185
5186 if( have_buffered == 0 )
5187#endif /* MBEDTLS_SSL_PROTO_DTLS */
5188 {
5189 ret = ssl_get_next_record( ssl );
5190 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5191 continue;
5192
5193 if( ret != 0 )
5194 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005195 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005196 return( ret );
5197 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005198 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005199 }
5200
5201 ret = mbedtls_ssl_handle_message_type( ssl );
5202
Hanno Becker40f50842018-08-15 14:48:01 +01005203#if defined(MBEDTLS_SSL_PROTO_DTLS)
5204 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5205 {
5206 /* Buffer future message */
5207 ret = ssl_buffer_message( ssl );
5208 if( ret != 0 )
5209 return( ret );
5210
5211 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5212 }
5213#endif /* MBEDTLS_SSL_PROTO_DTLS */
5214
Hanno Becker90333da2017-10-10 11:27:13 +01005215 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5216 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005217
5218 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005219 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005220 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005221 return( ret );
5222 }
5223
Hanno Becker327c93b2018-08-15 13:56:18 +01005224 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005225 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005226 {
5227 mbedtls_ssl_update_handshake_status( ssl );
5228 }
Simon Butcher99000142016-10-13 17:21:01 +01005229 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005230 else
Simon Butcher99000142016-10-13 17:21:01 +01005231 {
Hanno Becker02f59072018-08-15 14:00:24 +01005232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005233 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005234 }
5235
5236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5237
5238 return( 0 );
5239}
5240
Hanno Becker40f50842018-08-15 14:48:01 +01005241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005242static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005243{
Hanno Becker40f50842018-08-15 14:48:01 +01005244 if( ssl->in_left > ssl->next_record_offset )
5245 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005246
Hanno Becker40f50842018-08-15 14:48:01 +01005247 return( 0 );
5248}
5249
5250static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5251{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005252 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005253 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005254 int ret = 0;
5255
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005256 if( hs == NULL )
5257 return( -1 );
5258
Hanno Beckere00ae372018-08-20 09:39:42 +01005259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5260
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005261 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5262 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5263 {
5264 /* Check if we have seen a ChangeCipherSpec before.
5265 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005266 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005267 {
5268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5269 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005270 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005271 }
5272
Hanno Becker39b8bc92018-08-28 17:17:13 +01005273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005274 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5275 ssl->in_msglen = 1;
5276 ssl->in_msg[0] = 1;
5277
5278 /* As long as they are equal, the exact value doesn't matter. */
5279 ssl->in_left = 0;
5280 ssl->next_record_offset = 0;
5281
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005282 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005283 goto exit;
5284 }
Hanno Becker37f95322018-08-16 13:55:32 +01005285
Hanno Beckerb8f50142018-08-28 10:01:34 +01005286#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005287 /* Debug only */
5288 {
5289 unsigned offset;
5290 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5291 {
5292 hs_buf = &hs->buffering.hs[offset];
5293 if( hs_buf->is_valid == 1 )
5294 {
5295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5296 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005297 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005298 }
5299 }
5300 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005301#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005302
5303 /* Check if we have buffered and/or fully reassembled the
5304 * next handshake message. */
5305 hs_buf = &hs->buffering.hs[0];
5306 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5307 {
5308 /* Synthesize a record containing the buffered HS message. */
5309 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5310 ( hs_buf->data[2] << 8 ) |
5311 hs_buf->data[3];
5312
5313 /* Double-check that we haven't accidentally buffered
5314 * a message that doesn't fit into the input buffer. */
5315 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5316 {
5317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5319 }
5320
5321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5322 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5323 hs_buf->data, msg_len + 12 );
5324
5325 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5326 ssl->in_hslen = msg_len + 12;
5327 ssl->in_msglen = msg_len + 12;
5328 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5329
5330 ret = 0;
5331 goto exit;
5332 }
5333 else
5334 {
5335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5336 hs->in_msg_seq ) );
5337 }
5338
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005339 ret = -1;
5340
5341exit:
5342
5343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5344 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005345}
5346
Hanno Beckera02b0b42018-08-21 17:20:27 +01005347static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5348 size_t desired )
5349{
5350 int offset;
5351 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5353 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005354
Hanno Becker01315ea2018-08-21 17:22:17 +01005355 /* Get rid of future records epoch first, if such exist. */
5356 ssl_free_buffered_record( ssl );
5357
5358 /* Check if we have enough space available now. */
5359 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5360 hs->buffering.total_bytes_buffered ) )
5361 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005363 return( 0 );
5364 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005365
Hanno Becker4f432ad2018-08-28 10:02:32 +01005366 /* We don't have enough space to buffer the next expected handshake
5367 * message. Remove buffers used for future messages to gain space,
5368 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005369 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5370 offset >= 0; offset-- )
5371 {
5372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5373 offset ) );
5374
Hanno Beckerb309b922018-08-23 13:18:05 +01005375 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005376
5377 /* Check if we have enough space available now. */
5378 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5379 hs->buffering.total_bytes_buffered ) )
5380 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005382 return( 0 );
5383 }
5384 }
5385
5386 return( -1 );
5387}
5388
Hanno Becker40f50842018-08-15 14:48:01 +01005389static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5390{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005391 int ret = 0;
5392 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5393
5394 if( hs == NULL )
5395 return( 0 );
5396
5397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5398
5399 switch( ssl->in_msgtype )
5400 {
5401 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005403
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005404 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005405 break;
5406
5407 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005408 {
5409 unsigned recv_msg_seq_offset;
5410 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5411 mbedtls_ssl_hs_buffer *hs_buf;
5412 size_t msg_len = ssl->in_hslen - 12;
5413
5414 /* We should never receive an old handshake
5415 * message - double-check nonetheless. */
5416 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5417 {
5418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5420 }
5421
5422 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5423 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5424 {
5425 /* Silently ignore -- message too far in the future */
5426 MBEDTLS_SSL_DEBUG_MSG( 2,
5427 ( "Ignore future HS message with sequence number %u, "
5428 "buffering window %u - %u",
5429 recv_msg_seq, ssl->handshake->in_msg_seq,
5430 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5431
5432 goto exit;
5433 }
5434
5435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5436 recv_msg_seq, recv_msg_seq_offset ) );
5437
5438 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5439
5440 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005441 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005442 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005443 size_t reassembly_buf_sz;
5444
Hanno Becker37f95322018-08-16 13:55:32 +01005445 hs_buf->is_fragmented =
5446 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5447
5448 /* We copy the message back into the input buffer
5449 * after reassembly, so check that it's not too large.
5450 * This is an implementation-specific limitation
5451 * and not one from the standard, hence it is not
5452 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005453 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005454 {
5455 /* Ignore message */
5456 goto exit;
5457 }
5458
Hanno Beckere0b150f2018-08-21 15:51:03 +01005459 /* Check if we have enough space to buffer the message. */
5460 if( hs->buffering.total_bytes_buffered >
5461 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5462 {
5463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5465 }
5466
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005467 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5468 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005469
5470 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5471 hs->buffering.total_bytes_buffered ) )
5472 {
5473 if( recv_msg_seq_offset > 0 )
5474 {
5475 /* If we can't buffer a future message because
5476 * of space limitations -- ignore. */
5477 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",
5478 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5479 (unsigned) hs->buffering.total_bytes_buffered ) );
5480 goto exit;
5481 }
Hanno Beckere1801392018-08-21 16:51:05 +01005482 else
5483 {
5484 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",
5485 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5486 (unsigned) hs->buffering.total_bytes_buffered ) );
5487 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005488
Hanno Beckera02b0b42018-08-21 17:20:27 +01005489 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005490 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005491 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",
5492 (unsigned) msg_len,
5493 (unsigned) reassembly_buf_sz,
5494 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005495 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005496 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5497 goto exit;
5498 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005499 }
5500
5501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5502 msg_len ) );
5503
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005504 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5505 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005506 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005507 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005508 goto exit;
5509 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005510 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005511
5512 /* Prepare final header: copy msg_type, length and message_seq,
5513 * then add standardised fragment_offset and fragment_length */
5514 memcpy( hs_buf->data, ssl->in_msg, 6 );
5515 memset( hs_buf->data + 6, 0, 3 );
5516 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5517
5518 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005519
5520 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005521 }
5522 else
5523 {
5524 /* Make sure msg_type and length are consistent */
5525 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5528 /* Ignore */
5529 goto exit;
5530 }
5531 }
5532
Hanno Becker4422bbb2018-08-20 09:40:19 +01005533 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005534 {
5535 size_t frag_len, frag_off;
5536 unsigned char * const msg = hs_buf->data + 12;
5537
5538 /*
5539 * Check and copy current fragment
5540 */
5541
5542 /* Validation of header fields already done in
5543 * mbedtls_ssl_prepare_handshake_record(). */
5544 frag_off = ssl_get_hs_frag_off( ssl );
5545 frag_len = ssl_get_hs_frag_len( ssl );
5546
5547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5548 frag_off, frag_len ) );
5549 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5550
5551 if( hs_buf->is_fragmented )
5552 {
5553 unsigned char * const bitmask = msg + msg_len;
5554 ssl_bitmask_set( bitmask, frag_off, frag_len );
5555 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5556 msg_len ) == 0 );
5557 }
5558 else
5559 {
5560 hs_buf->is_complete = 1;
5561 }
5562
5563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5564 hs_buf->is_complete ? "" : "not yet " ) );
5565 }
5566
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005567 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005568 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005569
5570 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005571 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005572 break;
5573 }
5574
5575exit:
5576
5577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5578 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005579}
5580#endif /* MBEDTLS_SSL_PROTO_DTLS */
5581
Hanno Becker1097b342018-08-15 14:09:41 +01005582static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005583{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005584 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005585 * Consume last content-layer message and potentially
5586 * update in_msglen which keeps track of the contents'
5587 * consumption state.
5588 *
5589 * (1) Handshake messages:
5590 * Remove last handshake message, move content
5591 * and adapt in_msglen.
5592 *
5593 * (2) Alert messages:
5594 * Consume whole record content, in_msglen = 0.
5595 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005596 * (3) Change cipher spec:
5597 * Consume whole record content, in_msglen = 0.
5598 *
5599 * (4) Application data:
5600 * Don't do anything - the record layer provides
5601 * the application data as a stream transport
5602 * and consumes through mbedtls_ssl_read only.
5603 *
5604 */
5605
5606 /* Case (1): Handshake messages */
5607 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005608 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005609 /* Hard assertion to be sure that no application data
5610 * is in flight, as corrupting ssl->in_msglen during
5611 * ssl->in_offt != NULL is fatal. */
5612 if( ssl->in_offt != NULL )
5613 {
5614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5616 }
5617
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005618 /*
5619 * Get next Handshake message in the current record
5620 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005621
Hanno Becker4a810fb2017-05-24 16:27:30 +01005622 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005623 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005624 * current handshake content: If DTLS handshake
5625 * fragmentation is used, that's the fragment
5626 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005627 * size here is faulty and should be changed at
5628 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005629 * (2) While it doesn't seem to cause problems, one
5630 * has to be very careful not to assume that in_hslen
5631 * is always <= in_msglen in a sensible communication.
5632 * Again, it's wrong for DTLS handshake fragmentation.
5633 * The following check is therefore mandatory, and
5634 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005635 * Additionally, ssl->in_hslen might be arbitrarily out of
5636 * bounds after handling a DTLS message with an unexpected
5637 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005638 */
5639 if( ssl->in_hslen < ssl->in_msglen )
5640 {
5641 ssl->in_msglen -= ssl->in_hslen;
5642 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5643 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005644
Hanno Becker4a810fb2017-05-24 16:27:30 +01005645 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5646 ssl->in_msg, ssl->in_msglen );
5647 }
5648 else
5649 {
5650 ssl->in_msglen = 0;
5651 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005652
Hanno Becker4a810fb2017-05-24 16:27:30 +01005653 ssl->in_hslen = 0;
5654 }
5655 /* Case (4): Application data */
5656 else if( ssl->in_offt != NULL )
5657 {
5658 return( 0 );
5659 }
5660 /* Everything else (CCS & Alerts) */
5661 else
5662 {
5663 ssl->in_msglen = 0;
5664 }
5665
Hanno Becker1097b342018-08-15 14:09:41 +01005666 return( 0 );
5667}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005668
Hanno Beckere74d5562018-08-15 14:26:08 +01005669static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5670{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005671 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005672 return( 1 );
5673
5674 return( 0 );
5675}
5676
Hanno Becker5f066e72018-08-16 14:56:31 +01005677#if defined(MBEDTLS_SSL_PROTO_DTLS)
5678
5679static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5680{
5681 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5682 if( hs == NULL )
5683 return;
5684
Hanno Becker01315ea2018-08-21 17:22:17 +01005685 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005686 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005687 hs->buffering.total_bytes_buffered -=
5688 hs->buffering.future_record.len;
5689
5690 mbedtls_free( hs->buffering.future_record.data );
5691 hs->buffering.future_record.data = NULL;
5692 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005693}
5694
5695static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5696{
5697 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5698 unsigned char * rec;
5699 size_t rec_len;
5700 unsigned rec_epoch;
5701
5702 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5703 return( 0 );
5704
5705 if( hs == NULL )
5706 return( 0 );
5707
Hanno Becker5f066e72018-08-16 14:56:31 +01005708 rec = hs->buffering.future_record.data;
5709 rec_len = hs->buffering.future_record.len;
5710 rec_epoch = hs->buffering.future_record.epoch;
5711
5712 if( rec == NULL )
5713 return( 0 );
5714
Hanno Becker4cb782d2018-08-20 11:19:05 +01005715 /* Only consider loading future records if the
5716 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005717 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005718 return( 0 );
5719
Hanno Becker5f066e72018-08-16 14:56:31 +01005720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5721
5722 if( rec_epoch != ssl->in_epoch )
5723 {
5724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5725 goto exit;
5726 }
5727
5728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5729
5730 /* Double-check that the record is not too large */
5731 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5732 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5733 {
5734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5736 }
5737
5738 memcpy( ssl->in_hdr, rec, rec_len );
5739 ssl->in_left = rec_len;
5740 ssl->next_record_offset = 0;
5741
5742 ssl_free_buffered_record( ssl );
5743
5744exit:
5745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5746 return( 0 );
5747}
5748
5749static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5750{
5751 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5752 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005753 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005754
5755 /* Don't buffer future records outside handshakes. */
5756 if( hs == NULL )
5757 return( 0 );
5758
5759 /* Only buffer handshake records (we are only interested
5760 * in Finished messages). */
5761 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5762 return( 0 );
5763
5764 /* Don't buffer more than one future epoch record. */
5765 if( hs->buffering.future_record.data != NULL )
5766 return( 0 );
5767
Hanno Becker01315ea2018-08-21 17:22:17 +01005768 /* Don't buffer record if there's not enough buffering space remaining. */
5769 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5770 hs->buffering.total_bytes_buffered ) )
5771 {
5772 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",
5773 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5774 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005775 return( 0 );
5776 }
5777
Hanno Becker5f066e72018-08-16 14:56:31 +01005778 /* Buffer record */
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5780 ssl->in_epoch + 1 ) );
5781 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5782 rec_hdr_len + ssl->in_msglen );
5783
5784 /* ssl_parse_record_header() only considers records
5785 * of the next epoch as candidates for buffering. */
5786 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005787 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005788
5789 hs->buffering.future_record.data =
5790 mbedtls_calloc( 1, hs->buffering.future_record.len );
5791 if( hs->buffering.future_record.data == NULL )
5792 {
5793 /* If we run out of RAM trying to buffer a
5794 * record from the next epoch, just ignore. */
5795 return( 0 );
5796 }
5797
Hanno Becker01315ea2018-08-21 17:22:17 +01005798 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005799
Hanno Becker01315ea2018-08-21 17:22:17 +01005800 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005801 return( 0 );
5802}
5803
5804#endif /* MBEDTLS_SSL_PROTO_DTLS */
5805
Hanno Beckere74d5562018-08-15 14:26:08 +01005806static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005807{
5808 int ret;
5809
Hanno Becker5f066e72018-08-16 14:56:31 +01005810#if defined(MBEDTLS_SSL_PROTO_DTLS)
5811 /* We might have buffered a future record; if so,
5812 * and if the epoch matches now, load it.
5813 * On success, this call will set ssl->in_left to
5814 * the length of the buffered record, so that
5815 * the calls to ssl_fetch_input() below will
5816 * essentially be no-ops. */
5817 ret = ssl_load_buffered_record( ssl );
5818 if( ret != 0 )
5819 return( ret );
5820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005825 return( ret );
5826 }
5827
5828 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5832 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005833 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005834 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5835 {
5836 ret = ssl_buffer_future_record( ssl );
5837 if( ret != 0 )
5838 return( ret );
5839
5840 /* Fall through to handling of unexpected records */
5841 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5842 }
5843
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005844 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5845 {
5846 /* Skip unexpected record (but not whole datagram) */
5847 ssl->next_record_offset = ssl->in_msglen
5848 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005849
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5851 "(header)" ) );
5852 }
5853 else
5854 {
5855 /* Skip invalid record and the rest of the datagram */
5856 ssl->next_record_offset = 0;
5857 ssl->in_left = 0;
5858
5859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5860 "(header)" ) );
5861 }
5862
5863 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005864 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005865 }
5866#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005867 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005868 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005869
5870 /*
5871 * Read and optionally decrypt the message contents
5872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5874 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005877 return( ret );
5878 }
5879
5880 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005885 if( ssl->next_record_offset < ssl->in_left )
5886 {
5887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5888 }
5889 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005890 else
5891#endif
5892 ssl->in_left = 0;
5893
5894 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005898 {
5899 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5901 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005902 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005903 /* Except when waiting for Finished as a bad mac here
5904 * probably means something went wrong in the handshake
5905 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5906 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5907 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5908 {
5909#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5910 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5911 {
5912 mbedtls_ssl_send_alert_message( ssl,
5913 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5914 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5915 }
5916#endif
5917 return( ret );
5918 }
5919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005921 if( ssl->conf->badmac_limit != 0 &&
5922 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5925 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005926 }
5927#endif
5928
Hanno Becker4a810fb2017-05-24 16:27:30 +01005929 /* As above, invalid records cause
5930 * dismissal of the whole datagram. */
5931
5932 ssl->next_record_offset = 0;
5933 ssl->in_left = 0;
5934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005936 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005937 }
5938
5939 return( ret );
5940 }
5941 else
5942#endif
5943 {
5944 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5946 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 mbedtls_ssl_send_alert_message( ssl,
5949 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5950 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005951 }
5952#endif
5953 return( ret );
5954 }
5955 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005956
Simon Butcher99000142016-10-13 17:21:01 +01005957 return( 0 );
5958}
5959
5960int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5961{
5962 int ret;
5963
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005964 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005965 * Handle particular types of records
5966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 {
Simon Butcher99000142016-10-13 17:21:01 +01005969 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5970 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005971 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 }
5974
Hanno Beckere678eaa2018-08-21 14:57:46 +01005975 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005976 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005977 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005978 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5980 ssl->in_msglen ) );
5981 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005982 }
5983
Hanno Beckere678eaa2018-08-21 14:57:46 +01005984 if( ssl->in_msg[0] != 1 )
5985 {
5986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5987 ssl->in_msg[0] ) );
5988 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5989 }
5990
5991#if defined(MBEDTLS_SSL_PROTO_DTLS)
5992 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5993 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5994 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5995 {
5996 if( ssl->handshake == NULL )
5997 {
5998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5999 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6000 }
6001
6002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6003 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6004 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006005#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006006 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006009 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006010 if( ssl->in_msglen != 2 )
6011 {
6012 /* Note: Standard allows for more than one 2 byte alert
6013 to be packed in a single message, but Mbed TLS doesn't
6014 currently support this. */
6015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6016 ssl->in_msglen ) );
6017 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6018 }
6019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 ssl->in_msg[0], ssl->in_msg[1] ) );
6022
6023 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006024 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006029 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 }
6032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6034 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6037 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006038 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006039
6040#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6041 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6042 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6043 {
Hanno Becker90333da2017-10-10 11:27:13 +01006044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006045 /* Will be handled when trying to parse ServerHello */
6046 return( 0 );
6047 }
6048#endif
6049
6050#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6051 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6052 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6053 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6054 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6055 {
6056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6057 /* Will be handled in mbedtls_ssl_parse_certificate() */
6058 return( 0 );
6059 }
6060#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6061
6062 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006063 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 }
6065
Hanno Beckerc76c6192017-06-06 10:03:17 +01006066#if defined(MBEDTLS_SSL_PROTO_DTLS)
6067 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6068 ssl->handshake != NULL &&
6069 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6070 {
6071 ssl_handshake_wrapup_free_hs_transform( ssl );
6072 }
6073#endif
6074
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 return( 0 );
6076}
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006079{
6080 int ret;
6081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6083 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6084 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006085 {
6086 return( ret );
6087 }
6088
6089 return( 0 );
6090}
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006093 unsigned char level,
6094 unsigned char message )
6095{
6096 int ret;
6097
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006098 if( ssl == NULL || ssl->conf == NULL )
6099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006105 ssl->out_msglen = 2;
6106 ssl->out_msg[0] = level;
6107 ssl->out_msg[1] = message;
6108
Hanno Becker67bc7c32018-08-06 11:33:50 +01006109 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006112 return( ret );
6113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006115
6116 return( 0 );
6117}
6118
Hanno Beckerb9d44792019-02-08 07:19:04 +00006119#if defined(MBEDTLS_X509_CRT_PARSE_C)
6120static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6121{
6122#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6123 if( session->peer_cert != NULL )
6124 {
6125 mbedtls_x509_crt_free( session->peer_cert );
6126 mbedtls_free( session->peer_cert );
6127 session->peer_cert = NULL;
6128 }
6129#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6130 if( session->peer_cert_digest != NULL )
6131 {
6132 /* Zeroization is not necessary. */
6133 mbedtls_free( session->peer_cert_digest );
6134 session->peer_cert_digest = NULL;
6135 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6136 session->peer_cert_digest_len = 0;
6137 }
6138#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6139}
6140#endif /* MBEDTLS_X509_CRT_PARSE_C */
6141
Paul Bakker5121ce52009-01-03 21:22:43 +00006142/*
6143 * Handshake functions
6144 */
Hanno Becker21489932019-02-05 13:20:55 +00006145#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006146/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006148{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006149 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6150 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006153
Hanno Becker7177a882019-02-05 13:36:46 +00006154 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006157 ssl->state++;
6158 return( 0 );
6159 }
6160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006163}
6164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006166{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006167 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6168 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006171
Hanno Becker7177a882019-02-05 13:36:46 +00006172 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006175 ssl->state++;
6176 return( 0 );
6177 }
6178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006181}
Gilles Peskinef9828522017-05-03 12:28:43 +02006182
Hanno Becker21489932019-02-05 13:20:55 +00006183#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006184/* Some certificate support -> implement write and parse */
6185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006189 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6192 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006195
Hanno Becker7177a882019-02-05 13:36:46 +00006196 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006199 ssl->state++;
6200 return( 0 );
6201 }
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006204 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 {
6206 if( ssl->client_auth == 0 )
6207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006209 ssl->state++;
6210 return( 0 );
6211 }
6212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 /*
6215 * If using SSLv3 and got no cert, send an Alert message
6216 * (otherwise an empty Certificate message will be sent).
6217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6219 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 {
6221 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6223 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6224 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006227 goto write_msg;
6228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231#endif /* MBEDTLS_SSL_CLI_C */
6232#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006233 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6238 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 }
6240 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006244
6245 /*
6246 * 0 . 0 handshake type
6247 * 1 . 3 handshake length
6248 * 4 . 6 length of all certs
6249 * 7 . 9 length of cert. 1
6250 * 10 . n-1 peer certificate
6251 * n . n+2 length of cert. 2
6252 * n+3 . ... upper level cert, etc.
6253 */
6254 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006256
Paul Bakker29087132010-03-21 21:03:34 +00006257 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006258 {
6259 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006260 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006263 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 }
6266
6267 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6268 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6269 ssl->out_msg[i + 2] = (unsigned char)( n );
6270
6271 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6272 i += n; crt = crt->next;
6273 }
6274
6275 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6276 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6277 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6278
6279 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6281 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006283#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006284write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006285#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006286
6287 ssl->state++;
6288
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006289 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006292 return( ret );
6293 }
6294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296
Paul Bakkered27a042013-04-18 22:46:23 +02006297 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006298}
6299
Hanno Becker84879e32019-01-31 07:44:03 +00006300#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006301
6302#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006303static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6304 unsigned char *crt_buf,
6305 size_t crt_buf_len )
6306{
6307 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6308
6309 if( peer_crt == NULL )
6310 return( -1 );
6311
6312 if( peer_crt->raw.len != crt_buf_len )
6313 return( -1 );
6314
Hanno Becker46f34d02019-02-08 14:00:04 +00006315 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006316}
Hanno Becker177475a2019-02-05 17:02:46 +00006317#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6318static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6319 unsigned char *crt_buf,
6320 size_t crt_buf_len )
6321{
6322 int ret;
6323 unsigned char const * const peer_cert_digest =
6324 ssl->session->peer_cert_digest;
6325 mbedtls_md_type_t const peer_cert_digest_type =
6326 ssl->session->peer_cert_digest_type;
6327 mbedtls_md_info_t const * const digest_info =
6328 mbedtls_md_info_from_type( peer_cert_digest_type );
6329 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6330 size_t digest_len;
6331
6332 if( peer_cert_digest == NULL || digest_info == NULL )
6333 return( -1 );
6334
6335 digest_len = mbedtls_md_get_size( digest_info );
6336 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6337 return( -1 );
6338
6339 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6340 if( ret != 0 )
6341 return( -1 );
6342
6343 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6344}
6345#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006346#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006347
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006348/*
6349 * Once the certificate message is read, parse it into a cert chain and
6350 * perform basic checks, but leave actual verification to the caller
6351 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006352static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6353 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006354{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006355 int ret;
6356#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6357 int crt_cnt=0;
6358#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006359 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006360 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6366 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 }
6369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6371 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006374 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6375 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 }
6378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006380
Paul Bakker5121ce52009-01-03 21:22:43 +00006381 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006384 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006385
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006386 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006390 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6391 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 }
6394
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006395 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6396 i += 3;
6397
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006398 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006399 while( i < ssl->in_hslen )
6400 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006401 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006402 if ( i + 3 > ssl->in_hslen ) {
6403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006404 mbedtls_ssl_send_alert_message( ssl,
6405 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6406 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006407 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6408 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006409 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6410 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 if( ssl->in_msg[i] != 0 )
6412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006414 mbedtls_ssl_send_alert_message( ssl,
6415 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6416 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 }
6419
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006420 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006421 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6422 | (unsigned int) ssl->in_msg[i + 2];
6423 i += 3;
6424
6425 if( n < 128 || i + n > ssl->in_hslen )
6426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006428 mbedtls_ssl_send_alert_message( ssl,
6429 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 }
6433
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006434 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006435#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6436 if( crt_cnt++ == 0 &&
6437 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6438 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006439 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006440 /* During client-side renegotiation, check that the server's
6441 * end-CRTs hasn't changed compared to the initial handshake,
6442 * mitigating the triple handshake attack. On success, reuse
6443 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6445 if( ssl_check_peer_crt_unchanged( ssl,
6446 &ssl->in_msg[i],
6447 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006448 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6450 mbedtls_ssl_send_alert_message( ssl,
6451 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6452 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6453 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006454 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006455
6456 /* Now we can safely free the original chain. */
6457 ssl_clear_peer_cert( ssl->session );
6458 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006459#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6460
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006461 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006463 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006464#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006465 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006466 * it in-place from the input buffer instead of making a copy. */
6467 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6468#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006469 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006470 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006471 case 0: /*ok*/
6472 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6473 /* Ignore certificate with an unknown algorithm: maybe a
6474 prior certificate was already trusted. */
6475 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006476
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006477 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6478 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6479 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006480
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006481 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6482 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6483 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006484
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006485 default:
6486 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6487 crt_parse_der_failed:
6488 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6489 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6490 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 }
6492
6493 i += n;
6494 }
6495
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006496 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006497 return( 0 );
6498}
6499
Hanno Becker4a55f632019-02-05 12:49:06 +00006500#if defined(MBEDTLS_SSL_SRV_C)
6501static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6502{
6503 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6504 return( -1 );
6505
6506#if defined(MBEDTLS_SSL_PROTO_SSL3)
6507 /*
6508 * Check if the client sent an empty certificate
6509 */
6510 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6511 {
6512 if( ssl->in_msglen == 2 &&
6513 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6514 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6515 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6516 {
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6518 return( 0 );
6519 }
6520
6521 return( -1 );
6522 }
6523#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6524
6525#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6526 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6527 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6528 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6529 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6530 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6531 {
6532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6533 return( 0 );
6534 }
6535
6536 return( -1 );
6537#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6538 MBEDTLS_SSL_PROTO_TLS1_2 */
6539}
6540#endif /* MBEDTLS_SSL_SRV_C */
6541
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006542/* Check if a certificate message is expected.
6543 * Return either
6544 * - SSL_CERTIFICATE_EXPECTED, or
6545 * - SSL_CERTIFICATE_SKIP
6546 * indicating whether a Certificate message is expected or not.
6547 */
6548#define SSL_CERTIFICATE_EXPECTED 0
6549#define SSL_CERTIFICATE_SKIP 1
6550static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6551 int authmode )
6552{
6553 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006554 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006555
6556 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6557 return( SSL_CERTIFICATE_SKIP );
6558
6559#if defined(MBEDTLS_SSL_SRV_C)
6560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6561 {
6562 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6563 return( SSL_CERTIFICATE_SKIP );
6564
6565 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6566 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006567 ssl->session_negotiate->verify_result =
6568 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6569 return( SSL_CERTIFICATE_SKIP );
6570 }
6571 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006572#else
6573 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006574#endif /* MBEDTLS_SSL_SRV_C */
6575
6576 return( SSL_CERTIFICATE_EXPECTED );
6577}
6578
Hanno Becker68636192019-02-05 14:36:34 +00006579static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6580 int authmode,
6581 mbedtls_x509_crt *chain,
6582 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006583{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006584 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006585 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006586 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006587 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006588
Hanno Becker8927c832019-04-03 12:52:50 +01006589 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6590 void *p_vrfy;
6591
Hanno Becker68636192019-02-05 14:36:34 +00006592 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6593 return( 0 );
6594
Hanno Becker8927c832019-04-03 12:52:50 +01006595 if( ssl->f_vrfy != NULL )
6596 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006598 f_vrfy = ssl->f_vrfy;
6599 p_vrfy = ssl->p_vrfy;
6600 }
6601 else
6602 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006604 f_vrfy = ssl->conf->f_vrfy;
6605 p_vrfy = ssl->conf->p_vrfy;
6606 }
6607
Hanno Becker68636192019-02-05 14:36:34 +00006608 /*
6609 * Main check: verify certificate
6610 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006611#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6612 if( ssl->conf->f_ca_cb != NULL )
6613 {
6614 ((void) rs_ctx);
6615 have_ca_chain = 1;
6616
6617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006618 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006619 chain,
6620 ssl->conf->f_ca_cb,
6621 ssl->conf->p_ca_cb,
6622 ssl->conf->cert_profile,
6623 ssl->hostname,
6624 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006625 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006626 }
6627 else
6628#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6629 {
6630 mbedtls_x509_crt *ca_chain;
6631 mbedtls_x509_crl *ca_crl;
6632
6633#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6634 if( ssl->handshake->sni_ca_chain != NULL )
6635 {
6636 ca_chain = ssl->handshake->sni_ca_chain;
6637 ca_crl = ssl->handshake->sni_ca_crl;
6638 }
6639 else
6640#endif
6641 {
6642 ca_chain = ssl->conf->ca_chain;
6643 ca_crl = ssl->conf->ca_crl;
6644 }
6645
6646 if( ca_chain != NULL )
6647 have_ca_chain = 1;
6648
6649 ret = mbedtls_x509_crt_verify_restartable(
6650 chain,
6651 ca_chain, ca_crl,
6652 ssl->conf->cert_profile,
6653 ssl->hostname,
6654 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006655 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006656 }
Hanno Becker68636192019-02-05 14:36:34 +00006657
6658 if( ret != 0 )
6659 {
6660 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6661 }
6662
6663#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6664 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6665 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6666#endif
6667
6668 /*
6669 * Secondary checks: always done, but change 'ret' only if it was 0
6670 */
6671
6672#if defined(MBEDTLS_ECP_C)
6673 {
6674 const mbedtls_pk_context *pk = &chain->pk;
6675
6676 /* If certificate uses an EC key, make sure the curve is OK */
6677 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6678 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6679 {
6680 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6681
6682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6683 if( ret == 0 )
6684 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6685 }
6686 }
6687#endif /* MBEDTLS_ECP_C */
6688
6689 if( mbedtls_ssl_check_cert_usage( chain,
6690 ciphersuite_info,
6691 ! ssl->conf->endpoint,
6692 &ssl->session_negotiate->verify_result ) != 0 )
6693 {
6694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6695 if( ret == 0 )
6696 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6697 }
6698
6699 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6700 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6701 * with details encoded in the verification flags. All other kinds
6702 * of error codes, including those from the user provided f_vrfy
6703 * functions, are treated as fatal and lead to a failure of
6704 * ssl_parse_certificate even if verification was optional. */
6705 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6706 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6707 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6708 {
6709 ret = 0;
6710 }
6711
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006712 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006713 {
6714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6715 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6716 }
6717
6718 if( ret != 0 )
6719 {
6720 uint8_t alert;
6721
6722 /* The certificate may have been rejected for several reasons.
6723 Pick one and send the corresponding alert. Which alert to send
6724 may be a subject of debate in some cases. */
6725 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6726 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6727 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6728 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6729 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6730 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6731 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6732 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6733 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6734 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6735 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6736 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6737 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6738 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6739 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6740 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6741 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6742 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6743 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6744 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6745 else
6746 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6748 alert );
6749 }
6750
6751#if defined(MBEDTLS_DEBUG_C)
6752 if( ssl->session_negotiate->verify_result != 0 )
6753 {
6754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6755 ssl->session_negotiate->verify_result ) );
6756 }
6757 else
6758 {
6759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6760 }
6761#endif /* MBEDTLS_DEBUG_C */
6762
6763 return( ret );
6764}
6765
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006766#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6767static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6768 unsigned char *start, size_t len )
6769{
6770 int ret;
6771 /* Remember digest of the peer's end-CRT. */
6772 ssl->session_negotiate->peer_cert_digest =
6773 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6774 if( ssl->session_negotiate->peer_cert_digest == NULL )
6775 {
6776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6777 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6778 mbedtls_ssl_send_alert_message( ssl,
6779 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6780 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6781
6782 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6783 }
6784
6785 ret = mbedtls_md( mbedtls_md_info_from_type(
6786 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6787 start, len,
6788 ssl->session_negotiate->peer_cert_digest );
6789
6790 ssl->session_negotiate->peer_cert_digest_type =
6791 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6792 ssl->session_negotiate->peer_cert_digest_len =
6793 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6794
6795 return( ret );
6796}
6797
6798static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6799 unsigned char *start, size_t len )
6800{
6801 unsigned char *end = start + len;
6802 int ret;
6803
6804 /* Make a copy of the peer's raw public key. */
6805 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6806 ret = mbedtls_pk_parse_subpubkey( &start, end,
6807 &ssl->handshake->peer_pubkey );
6808 if( ret != 0 )
6809 {
6810 /* We should have parsed the public key before. */
6811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6812 }
6813
6814 return( 0 );
6815}
6816#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6817
Hanno Becker68636192019-02-05 14:36:34 +00006818int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6819{
6820 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006821 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006822#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6823 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6824 ? ssl->handshake->sni_authmode
6825 : ssl->conf->authmode;
6826#else
6827 const int authmode = ssl->conf->authmode;
6828#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006829 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006830 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006831
6832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6833
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006834 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6835 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006836 {
6837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006838 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006839 }
6840
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006841#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6842 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006843 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006844 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006845 chain = ssl->handshake->ecrs_peer_cert;
6846 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006847 goto crt_verify;
6848 }
6849#endif
6850
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006851 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006852 {
6853 /* mbedtls_ssl_read_record may have sent an alert already. We
6854 let it decide whether to alert. */
6855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006856 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006857 }
6858
Hanno Becker4a55f632019-02-05 12:49:06 +00006859#if defined(MBEDTLS_SSL_SRV_C)
6860 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6861 {
6862 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006863
6864 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006865 ret = 0;
6866 else
6867 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006868
Hanno Becker6bdfab22019-02-05 13:11:17 +00006869 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006870 }
6871#endif /* MBEDTLS_SSL_SRV_C */
6872
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006873 /* Clear existing peer CRT structure in case we tried to
6874 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006875 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006876
6877 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6878 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006879 {
6880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6881 sizeof( mbedtls_x509_crt ) ) );
6882 mbedtls_ssl_send_alert_message( ssl,
6883 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6884 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006885
Hanno Becker3dad3112019-02-05 17:19:52 +00006886 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6887 goto exit;
6888 }
6889 mbedtls_x509_crt_init( chain );
6890
6891 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006892 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006893 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006894
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006895#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6896 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006897 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006898
6899crt_verify:
6900 if( ssl->handshake->ecrs_enabled)
6901 rs_ctx = &ssl->handshake->ecrs_ctx;
6902#endif
6903
Hanno Becker68636192019-02-05 14:36:34 +00006904 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006905 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006906 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006907 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006908
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006909#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006910 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006911 unsigned char *crt_start, *pk_start;
6912 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006913
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006914 /* We parse the CRT chain without copying, so
6915 * these pointers point into the input buffer,
6916 * and are hence still valid after freeing the
6917 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006918
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006919 crt_start = chain->raw.p;
6920 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006921
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006922 pk_start = chain->pk_raw.p;
6923 pk_len = chain->pk_raw.len;
6924
6925 /* Free the CRT structures before computing
6926 * digest and copying the peer's public key. */
6927 mbedtls_x509_crt_free( chain );
6928 mbedtls_free( chain );
6929 chain = NULL;
6930
6931 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006932 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006933 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006934
6935 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6936 if( ret != 0 )
6937 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006938 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006939#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6940 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006941 ssl->session_negotiate->peer_cert = chain;
6942 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006943#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006946
Hanno Becker6bdfab22019-02-05 13:11:17 +00006947exit:
6948
Hanno Becker3dad3112019-02-05 17:19:52 +00006949 if( ret == 0 )
6950 ssl->state++;
6951
6952#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6953 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6954 {
6955 ssl->handshake->ecrs_peer_cert = chain;
6956 chain = NULL;
6957 }
6958#endif
6959
6960 if( chain != NULL )
6961 {
6962 mbedtls_x509_crt_free( chain );
6963 mbedtls_free( chain );
6964 }
6965
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 return( ret );
6967}
Hanno Becker21489932019-02-05 13:20:55 +00006968#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006971{
6972 int ret;
6973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 ssl->out_msglen = 1;
6978 ssl->out_msg[0] = 1;
6979
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 ssl->state++;
6981
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006982 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 return( ret );
6986 }
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006989
6990 return( 0 );
6991}
6992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006994{
6995 int ret;
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006998
Hanno Becker327c93b2018-08-15 13:56:18 +01006999 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007002 return( ret );
7003 }
7004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7009 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007011 }
7012
Hanno Beckere678eaa2018-08-21 14:57:46 +01007013 /* CCS records are only accepted if they have length 1 and content '1',
7014 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007016 /*
7017 * Switch to our negotiated transform and session parameters for inbound
7018 * data.
7019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007021 ssl->transform_in = ssl->transform_negotiate;
7022 ssl->session_in = ssl->session_negotiate;
7023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007028 ssl_dtls_replay_reset( ssl );
7029#endif
7030
7031 /* Increment epoch */
7032 if( ++ssl->in_epoch == 0 )
7033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007035 /* This is highly unlikely to happen for legitimate reasons, so
7036 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007038 }
7039 }
7040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007042 memset( ssl->in_ctr, 0, 8 );
7043
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007044 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7047 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7053 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007055 }
7056 }
7057#endif
7058
Paul Bakker5121ce52009-01-03 21:22:43 +00007059 ssl->state++;
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007062
7063 return( 0 );
7064}
7065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7067 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007068{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007069 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7072 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7073 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007074 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007075 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7078#if defined(MBEDTLS_SHA512_C)
7079 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007080 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7081 else
7082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#if defined(MBEDTLS_SHA256_C)
7084 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007085 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007086 else
7087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007091 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007092 }
Paul Bakker380da532012-04-18 16:10:25 +00007093}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7098 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007099 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7100 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7103#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007105 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007106 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7107#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007108 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007109#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007113 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007114 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007115#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007116 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007117#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007120}
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007123 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7126 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007127 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7128 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7131#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007132#if defined(MBEDTLS_USE_PSA_CRYPTO)
7133 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7134#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007135 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007136#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007140 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007141#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007142 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007143#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007146}
7147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7149 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7150static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007151 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007152{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007153 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7154 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007155}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007156#endif
Paul Bakker380da532012-04-18 16:10:25 +00007157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7159#if defined(MBEDTLS_SHA256_C)
7160static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007161 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007162{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007163#if defined(MBEDTLS_USE_PSA_CRYPTO)
7164 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7165#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007166 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007167#endif
Paul Bakker380da532012-04-18 16:10:25 +00007168}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007169#endif
Paul Bakker380da532012-04-18 16:10:25 +00007170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171#if defined(MBEDTLS_SHA512_C)
7172static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007173 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007174{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007176 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007177#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007178 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007179#endif
Paul Bakker380da532012-04-18 16:10:25 +00007180}
Paul Bakker769075d2012-11-24 11:26:46 +01007181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007185static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007187{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007188 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007189 mbedtls_md5_context md5;
7190 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007191
Paul Bakker5121ce52009-01-03 21:22:43 +00007192 unsigned char padbuf[48];
7193 unsigned char md5sum[16];
7194 unsigned char sha1sum[20];
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007197 if( !session )
7198 session = ssl->session;
7199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007201
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007202 mbedtls_md5_init( &md5 );
7203 mbedtls_sha1_init( &sha1 );
7204
7205 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7206 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007207
7208 /*
7209 * SSLv3:
7210 * hash =
7211 * MD5( master + pad2 +
7212 * MD5( handshake + sender + master + pad1 ) )
7213 * + SHA1( master + pad2 +
7214 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007215 */
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007218 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7219 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007223 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7224 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007225#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007228 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007229
Paul Bakker1ef83d62012-04-11 12:09:53 +00007230 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007231
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007232 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7233 mbedtls_md5_update_ret( &md5, session->master, 48 );
7234 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7235 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007236
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007237 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7238 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7239 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7240 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007241
Paul Bakker1ef83d62012-04-11 12:09:53 +00007242 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007243
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007244 mbedtls_md5_starts_ret( &md5 );
7245 mbedtls_md5_update_ret( &md5, session->master, 48 );
7246 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7247 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7248 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007249
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007250 mbedtls_sha1_starts_ret( &sha1 );
7251 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7252 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7253 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7254 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007258 mbedtls_md5_free( &md5 );
7259 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007260
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007261 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7262 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7263 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007270static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007273 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007274 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007275 mbedtls_md5_context md5;
7276 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007277 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007280 if( !session )
7281 session = ssl->session;
7282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007284
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007285 mbedtls_md5_init( &md5 );
7286 mbedtls_sha1_init( &sha1 );
7287
7288 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7289 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007290
Paul Bakker1ef83d62012-04-11 12:09:53 +00007291 /*
7292 * TLSv1:
7293 * hash = PRF( master, finished_label,
7294 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7295 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007298 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7299 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007300#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007303 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7304 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007305#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007308 ? "client finished"
7309 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007310
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007311 mbedtls_md5_finish_ret( &md5, padbuf );
7312 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007313
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007314 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007315 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007318
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007319 mbedtls_md5_free( &md5 );
7320 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007321
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007322 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7329#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007330static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007332{
7333 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007334 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007335 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007336#if defined(MBEDTLS_USE_PSA_CRYPTO)
7337 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007338 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007339 psa_status_t status;
7340#else
7341 mbedtls_sha256_context sha256;
7342#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007345 if( !session )
7346 session = ssl->session;
7347
Andrzej Kurekeb342242019-01-29 09:14:33 -05007348 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7349 ? "client finished"
7350 : "server finished";
7351
7352#if defined(MBEDTLS_USE_PSA_CRYPTO)
7353 sha256_psa = psa_hash_operation_init();
7354
7355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7356
7357 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7358 if( status != PSA_SUCCESS )
7359 {
7360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7361 return;
7362 }
7363
7364 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7365 if( status != PSA_SUCCESS )
7366 {
7367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7368 return;
7369 }
7370 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7371#else
7372
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007373 mbedtls_sha256_init( &sha256 );
7374
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007376
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007377 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378
7379 /*
7380 * TLSv1.2:
7381 * hash = PRF( master, finished_label,
7382 * Hash( handshake ) )[0.11]
7383 */
7384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if !defined(MBEDTLS_SHA256_ALT)
7386 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007387 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007388#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007390 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007391 mbedtls_sha256_free( &sha256 );
7392#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007393
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007394 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007395 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007398
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007399 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007406static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007408{
7409 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007410 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007411 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007412#if defined(MBEDTLS_USE_PSA_CRYPTO)
7413 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007414 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007415 psa_status_t status;
7416#else
7417 mbedtls_sha512_context sha512;
7418#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007421 if( !session )
7422 session = ssl->session;
7423
Andrzej Kurekeb342242019-01-29 09:14:33 -05007424 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7425 ? "client finished"
7426 : "server finished";
7427
7428#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007429 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430
7431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7432
Andrzej Kurek972fba52019-01-30 03:29:12 -05007433 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007434 if( status != PSA_SUCCESS )
7435 {
7436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7437 return;
7438 }
7439
Andrzej Kurek972fba52019-01-30 03:29:12 -05007440 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007441 if( status != PSA_SUCCESS )
7442 {
7443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7444 return;
7445 }
7446 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7447#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007448 mbedtls_sha512_init( &sha512 );
7449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007451
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007452 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007453
7454 /*
7455 * TLSv1.2:
7456 * hash = PRF( master, finished_label,
7457 * Hash( handshake ) )[0.11]
7458 */
7459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007461 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7462 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007463#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007465 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007466 mbedtls_sha512_free( &sha512 );
7467#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007468
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007469 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007470 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007473
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007474 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478#endif /* MBEDTLS_SHA512_C */
7479#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007484
7485 /*
7486 * Free our handshake params
7487 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007488 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007490 ssl->handshake = NULL;
7491
7492 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007493 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007494 */
7495 if( ssl->transform )
7496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 mbedtls_ssl_transform_free( ssl->transform );
7498 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007499 }
7500 ssl->transform = ssl->transform_negotiate;
7501 ssl->transform_negotiate = NULL;
7502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007504}
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007507{
7508 int resume = ssl->handshake->resume;
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SSL_RENEGOTIATION)
7513 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007516 ssl->renego_records_seen = 0;
7517 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007518#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007519
7520 /*
7521 * Free the previous session and switch in the current one
7522 */
Paul Bakker0a597072012-09-25 21:55:46 +00007523 if( ssl->session )
7524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007526 /* RFC 7366 3.1: keep the EtM state */
7527 ssl->session_negotiate->encrypt_then_mac =
7528 ssl->session->encrypt_then_mac;
7529#endif
7530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 mbedtls_ssl_session_free( ssl->session );
7532 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007533 }
7534 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007535 ssl->session_negotiate = NULL;
7536
Paul Bakker0a597072012-09-25 21:55:46 +00007537 /*
7538 * Add cache entry
7539 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007540 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007541 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007542 resume == 0 )
7543 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007544 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007546 }
Paul Bakker0a597072012-09-25 21:55:46 +00007547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007549 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007550 ssl->handshake->flight != NULL )
7551 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007552 /* Cancel handshake timer */
7553 ssl_set_timer( ssl, 0 );
7554
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007555 /* Keep last flight around in case we need to resend it:
7556 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007558 }
7559 else
7560#endif
7561 ssl_handshake_wrapup_free_hs_transform( ssl );
7562
Paul Bakker48916f92012-09-16 19:57:18 +00007563 ssl->state++;
7564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007566}
7567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007569{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007570 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007574 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007575
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007576 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007577
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007578 /*
7579 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7580 * may define some other value. Currently (early 2016), no defined
7581 * ciphersuite does this (and this is unlikely to change as activity has
7582 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007587 ssl->verify_data_len = hash_len;
7588 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007589#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007590
Paul Bakker5121ce52009-01-03 21:22:43 +00007591 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7593 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007594
7595 /*
7596 * In case of session resuming, invert the client and server
7597 * ChangeCipherSpec messages order.
7598 */
Paul Bakker0a597072012-09-25 21:55:46 +00007599 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007602 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007606 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007608#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007609 }
7610 else
7611 ssl->state++;
7612
Paul Bakker48916f92012-09-16 19:57:18 +00007613 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007614 * Switch to our negotiated transform and session parameters for outbound
7615 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007621 {
7622 unsigned char i;
7623
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007624 /* Remember current epoch settings for resending */
7625 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007626 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007627
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007628 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007629 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007630
7631 /* Increment epoch */
7632 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007633 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007634 break;
7635
7636 /* The loop goes to its end iff the counter is wrapping */
7637 if( i == 0 )
7638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7640 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007641 }
7642 }
7643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007645 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007646
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007647 ssl->transform_out = ssl->transform_negotiate;
7648 ssl->session_out = ssl->session_negotiate;
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7651 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7656 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007657 }
7658 }
7659#endif
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007662 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007664#endif
7665
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007666 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007667 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007669 return( ret );
7670 }
7671
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007672#if defined(MBEDTLS_SSL_PROTO_DTLS)
7673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7674 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7675 {
7676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7677 return( ret );
7678 }
7679#endif
7680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007682
7683 return( 0 );
7684}
7685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007687#define SSL_MAX_HASH_LEN 36
7688#else
7689#define SSL_MAX_HASH_LEN 12
7690#endif
7691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007693{
Paul Bakker23986e52011-04-24 08:57:21 +00007694 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007695 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007696 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007700 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Hanno Becker327c93b2018-08-15 13:56:18 +01007702 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007705 return( ret );
7706 }
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007711 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7712 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 }
7715
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007716 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717#if defined(MBEDTLS_SSL_PROTO_SSL3)
7718 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007719 hash_len = 36;
7720 else
7721#endif
7722 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7725 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007731 }
7732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007734 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 }
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007743 ssl->verify_data_len = hash_len;
7744 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007745#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007746
Paul Bakker0a597072012-09-25 21:55:46 +00007747 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007754 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007757 }
7758 else
7759 ssl->state++;
7760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007764#endif
7765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007767
7768 return( 0 );
7769}
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7776 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7777 mbedtls_md5_init( &handshake->fin_md5 );
7778 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007779 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7780 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7783#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007784#if defined(MBEDTLS_USE_PSA_CRYPTO)
7785 handshake->fin_sha256_psa = psa_hash_operation_init();
7786 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7787#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007789 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007790#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007794 handshake->fin_sha384_psa = psa_hash_operation_init();
7795 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007796#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007798 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007799#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007802
7803 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007804
7805#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7806 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7807 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7808#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810#if defined(MBEDTLS_DHM_C)
7811 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813#if defined(MBEDTLS_ECDH_C)
7814 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007815#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007816#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007817 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007818#if defined(MBEDTLS_SSL_CLI_C)
7819 handshake->ecjpake_cache = NULL;
7820 handshake->ecjpake_cache_len = 0;
7821#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007822#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007823
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007824#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007825 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007826#endif
7827
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007828#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7829 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7830#endif
Hanno Becker75173122019-02-06 16:18:31 +00007831
7832#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7833 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7834 mbedtls_pk_init( &handshake->peer_pubkey );
7835#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007836}
7837
Hanno Beckera18d1322018-01-03 14:27:32 +00007838void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7843 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007844
Hanno Beckerd56ed242018-01-03 15:32:51 +00007845#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 mbedtls_md_init( &transform->md_ctx_enc );
7847 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007848#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007849}
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007854}
7855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007857{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007858 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007859 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007861 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007863 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007864 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007865
7866 /*
7867 * Either the pointers are now NULL or cleared properly and can be freed.
7868 * Now allocate missing structures.
7869 */
7870 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007871 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007872 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007873 }
Paul Bakker48916f92012-09-16 19:57:18 +00007874
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007875 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007876 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007877 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007878 }
Paul Bakker48916f92012-09-16 19:57:18 +00007879
Paul Bakker82788fb2014-10-20 13:59:19 +02007880 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007881 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007882 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007883 }
Paul Bakker48916f92012-09-16 19:57:18 +00007884
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007885 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007886 if( ssl->handshake == NULL ||
7887 ssl->transform_negotiate == NULL ||
7888 ssl->session_negotiate == NULL )
7889 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 mbedtls_free( ssl->handshake );
7893 mbedtls_free( ssl->transform_negotiate );
7894 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007895
7896 ssl->handshake = NULL;
7897 ssl->transform_negotiate = NULL;
7898 ssl->session_negotiate = NULL;
7899
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007900 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007901 }
7902
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007903 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007905 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007906 ssl_handshake_params_init( ssl->handshake );
7907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7910 {
7911 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007912
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007913 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7914 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7915 else
7916 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007917
7918 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007919 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007920#endif
7921
Paul Bakker48916f92012-09-16 19:57:18 +00007922 return( 0 );
7923}
7924
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007925#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007926/* Dummy cookie callbacks for defaults */
7927static int ssl_cookie_write_dummy( void *ctx,
7928 unsigned char **p, unsigned char *end,
7929 const unsigned char *cli_id, size_t cli_id_len )
7930{
7931 ((void) ctx);
7932 ((void) p);
7933 ((void) end);
7934 ((void) cli_id);
7935 ((void) cli_id_len);
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007938}
7939
7940static int ssl_cookie_check_dummy( void *ctx,
7941 const unsigned char *cookie, size_t cookie_len,
7942 const unsigned char *cli_id, size_t cli_id_len )
7943{
7944 ((void) ctx);
7945 ((void) cookie);
7946 ((void) cookie_len);
7947 ((void) cli_id);
7948 ((void) cli_id_len);
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007951}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007952#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007953
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007954/* Once ssl->out_hdr as the address of the beginning of the
7955 * next outgoing record is set, deduce the other pointers.
7956 *
7957 * Note: For TLS, we save the implicit record sequence number
7958 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7959 * and the caller has to make sure there's space for this.
7960 */
7961
7962static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7963 mbedtls_ssl_transform *transform )
7964{
7965#if defined(MBEDTLS_SSL_PROTO_DTLS)
7966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7967 {
7968 ssl->out_ctr = ssl->out_hdr + 3;
7969 ssl->out_len = ssl->out_hdr + 11;
7970 ssl->out_iv = ssl->out_hdr + 13;
7971 }
7972 else
7973#endif
7974 {
7975 ssl->out_ctr = ssl->out_hdr - 8;
7976 ssl->out_len = ssl->out_hdr + 3;
7977 ssl->out_iv = ssl->out_hdr + 5;
7978 }
7979
7980 /* Adjust out_msg to make space for explicit IV, if used. */
7981 if( transform != NULL &&
7982 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7983 {
7984 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7985 }
7986 else
7987 ssl->out_msg = ssl->out_iv;
7988}
7989
7990/* Once ssl->in_hdr as the address of the beginning of the
7991 * next incoming record is set, deduce the other pointers.
7992 *
7993 * Note: For TLS, we save the implicit record sequence number
7994 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7995 * and the caller has to make sure there's space for this.
7996 */
7997
7998static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7999 mbedtls_ssl_transform *transform )
8000{
8001#if defined(MBEDTLS_SSL_PROTO_DTLS)
8002 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8003 {
8004 ssl->in_ctr = ssl->in_hdr + 3;
8005 ssl->in_len = ssl->in_hdr + 11;
8006 ssl->in_iv = ssl->in_hdr + 13;
8007 }
8008 else
8009#endif
8010 {
8011 ssl->in_ctr = ssl->in_hdr - 8;
8012 ssl->in_len = ssl->in_hdr + 3;
8013 ssl->in_iv = ssl->in_hdr + 5;
8014 }
8015
8016 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8017 if( transform != NULL &&
8018 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8019 {
8020 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8021 }
8022 else
8023 ssl->in_msg = ssl->in_iv;
8024}
8025
Paul Bakker5121ce52009-01-03 21:22:43 +00008026/*
8027 * Initialize an SSL context
8028 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008029void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8030{
8031 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8032}
8033
8034/*
8035 * Setup an SSL context
8036 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008037
8038static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8039{
8040 /* Set the incoming and outgoing record pointers. */
8041#if defined(MBEDTLS_SSL_PROTO_DTLS)
8042 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8043 {
8044 ssl->out_hdr = ssl->out_buf;
8045 ssl->in_hdr = ssl->in_buf;
8046 }
8047 else
8048#endif /* MBEDTLS_SSL_PROTO_DTLS */
8049 {
8050 ssl->out_hdr = ssl->out_buf + 8;
8051 ssl->in_hdr = ssl->in_buf + 8;
8052 }
8053
8054 /* Derive other internal pointers. */
8055 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8056 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8057}
8058
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008059int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008060 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008061{
Paul Bakker48916f92012-09-16 19:57:18 +00008062 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008063
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008064 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008065
8066 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008067 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008068 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008069
8070 /* Set to NULL in case of an error condition */
8071 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008072
Angus Grattond8213d02016-05-25 20:56:48 +10008073 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8074 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008075 {
Angus Grattond8213d02016-05-25 20:56:48 +10008076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008077 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008078 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008079 }
8080
8081 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8082 if( ssl->out_buf == NULL )
8083 {
8084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008085 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008086 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008087 }
8088
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008089 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008090
Paul Bakker48916f92012-09-16 19:57:18 +00008091 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008092 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008093
8094 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008095
8096error:
8097 mbedtls_free( ssl->in_buf );
8098 mbedtls_free( ssl->out_buf );
8099
8100 ssl->conf = NULL;
8101
8102 ssl->in_buf = NULL;
8103 ssl->out_buf = NULL;
8104
8105 ssl->in_hdr = NULL;
8106 ssl->in_ctr = NULL;
8107 ssl->in_len = NULL;
8108 ssl->in_iv = NULL;
8109 ssl->in_msg = NULL;
8110
8111 ssl->out_hdr = NULL;
8112 ssl->out_ctr = NULL;
8113 ssl->out_len = NULL;
8114 ssl->out_iv = NULL;
8115 ssl->out_msg = NULL;
8116
k-stachowiak9f7798e2018-07-31 16:52:32 +02008117 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008118}
8119
8120/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008121 * Reset an initialized and used SSL context for re-use while retaining
8122 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008123 *
8124 * If partial is non-zero, keep data in the input buffer and client ID.
8125 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008126 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008127static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008128{
Paul Bakker48916f92012-09-16 19:57:18 +00008129 int ret;
8130
Hanno Becker7e772132018-08-10 12:38:21 +01008131#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8132 !defined(MBEDTLS_SSL_SRV_C)
8133 ((void) partial);
8134#endif
8135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008137
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008138 /* Cancel any possibly running timer */
8139 ssl_set_timer( ssl, 0 );
8140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SSL_RENEGOTIATION)
8142 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008143 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008144
8145 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8147 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008150
Paul Bakker7eb013f2011-10-06 12:37:39 +00008151 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008152 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008153
8154 ssl->in_msgtype = 0;
8155 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008157 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008158 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008161 ssl_dtls_replay_reset( ssl );
8162#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008163
8164 ssl->in_hslen = 0;
8165 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008166
8167 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008168
8169 ssl->out_msgtype = 0;
8170 ssl->out_msglen = 0;
8171 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8173 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008174 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008175#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008176
Hanno Becker19859472018-08-06 09:40:20 +01008177 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8178
Paul Bakker48916f92012-09-16 19:57:18 +00008179 ssl->transform_in = NULL;
8180 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008181
Hanno Becker78640902018-08-13 16:35:15 +01008182 ssl->session_in = NULL;
8183 ssl->session_out = NULL;
8184
Angus Grattond8213d02016-05-25 20:56:48 +10008185 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008186
8187#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008188 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008189#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8190 {
8191 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008192 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008193 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8196 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8199 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8202 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008203 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008204 }
8205#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008206
Paul Bakker48916f92012-09-16 19:57:18 +00008207 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 mbedtls_ssl_transform_free( ssl->transform );
8210 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008211 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008212 }
Paul Bakker48916f92012-09-16 19:57:18 +00008213
Paul Bakkerc0463502013-02-14 11:19:38 +01008214 if( ssl->session )
8215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 mbedtls_ssl_session_free( ssl->session );
8217 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008218 ssl->session = NULL;
8219 }
8220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008222 ssl->alpn_chosen = NULL;
8223#endif
8224
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008225#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008226#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008227 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008228#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008229 {
8230 mbedtls_free( ssl->cli_id );
8231 ssl->cli_id = NULL;
8232 ssl->cli_id_len = 0;
8233 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008234#endif
8235
Paul Bakker48916f92012-09-16 19:57:18 +00008236 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8237 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008238
8239 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008240}
8241
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008242/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008243 * Reset an initialized and used SSL context for re-use while retaining
8244 * all application-set variables, function pointers and data.
8245 */
8246int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8247{
8248 return( ssl_session_reset_int( ssl, 0 ) );
8249}
8250
8251/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008252 * SSL set accessors
8253 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008254void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008256 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008257}
8258
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008259void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008261 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008262}
8263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008265void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008266{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008267 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008268}
8269#endif
8270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008272void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008273{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008274 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008275}
8276#endif
8277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008279
Hanno Becker1841b0a2018-08-24 11:13:57 +01008280void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8281 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008282{
8283 ssl->disable_datagram_packing = !allow_packing;
8284}
8285
8286void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8287 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008289 conf->hs_timeout_min = min;
8290 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008291}
8292#endif
8293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008294void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008296 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008297}
8298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008300void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008301 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008302 void *p_vrfy )
8303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008304 conf->f_vrfy = f_vrfy;
8305 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008309void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008310 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 void *p_rng )
8312{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008313 conf->f_rng = f_rng;
8314 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008315}
8316
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008317void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008318 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008319 void *p_dbg )
8320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008321 conf->f_dbg = f_dbg;
8322 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008323}
8324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008326 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008327 mbedtls_ssl_send_t *f_send,
8328 mbedtls_ssl_recv_t *f_recv,
8329 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008330{
8331 ssl->p_bio = p_bio;
8332 ssl->f_send = f_send;
8333 ssl->f_recv = f_recv;
8334 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008335}
8336
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008337#if defined(MBEDTLS_SSL_PROTO_DTLS)
8338void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8339{
8340 ssl->mtu = mtu;
8341}
8342#endif
8343
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008344void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008345{
8346 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008347}
8348
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008349void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8350 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008351 mbedtls_ssl_set_timer_t *f_set_timer,
8352 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008353{
8354 ssl->p_timer = p_timer;
8355 ssl->f_set_timer = f_set_timer;
8356 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008357
8358 /* Make sure we start with no timer running */
8359 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008360}
8361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008363void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008364 void *p_cache,
8365 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8366 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008367{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008368 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008369 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008370 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_CLI_C)
8375int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008376{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008377 int ret;
8378
8379 if( ssl == NULL ||
8380 session == NULL ||
8381 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008382 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008385 }
8386
Hanno Becker52055ae2019-02-06 14:30:46 +00008387 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8388 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008389 return( ret );
8390
Paul Bakker0a597072012-09-25 21:55:46 +00008391 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008392
8393 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008396
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008397void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008398 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8401 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8402 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8403 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008404}
8405
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008406void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008407 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008408 int major, int minor )
8409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008411 return;
8412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008414 return;
8415
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008416 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008417}
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008420void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008421 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008422{
8423 conf->cert_profile = profile;
8424}
8425
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008426/* Append a new keycert entry to a (possibly empty) list */
8427static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8428 mbedtls_x509_crt *cert,
8429 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008430{
niisato8ee24222018-06-25 19:05:48 +09008431 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008432
niisato8ee24222018-06-25 19:05:48 +09008433 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8434 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008435 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008436
niisato8ee24222018-06-25 19:05:48 +09008437 new_cert->cert = cert;
8438 new_cert->key = key;
8439 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008440
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008441 /* Update head is the list was null, else add to the end */
8442 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008443 {
niisato8ee24222018-06-25 19:05:48 +09008444 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008445 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008446 else
8447 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008448 mbedtls_ssl_key_cert *cur = *head;
8449 while( cur->next != NULL )
8450 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008451 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008452 }
8453
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008454 return( 0 );
8455}
8456
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008457int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008458 mbedtls_x509_crt *own_cert,
8459 mbedtls_pk_context *pk_key )
8460{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008461 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008462}
8463
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008464void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008465 mbedtls_x509_crt *ca_chain,
8466 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008467{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008468 conf->ca_chain = ca_chain;
8469 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008470
8471#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8472 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8473 * cannot be used together. */
8474 conf->f_ca_cb = NULL;
8475 conf->p_ca_cb = NULL;
8476#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008477}
Hanno Becker5adaad92019-03-27 16:54:37 +00008478
8479#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8480void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008481 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008482 void *p_ca_cb )
8483{
8484 conf->f_ca_cb = f_ca_cb;
8485 conf->p_ca_cb = p_ca_cb;
8486
8487 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8488 * cannot be used together. */
8489 conf->ca_chain = NULL;
8490 conf->ca_crl = NULL;
8491}
8492#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008494
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008495#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8496int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8497 mbedtls_x509_crt *own_cert,
8498 mbedtls_pk_context *pk_key )
8499{
8500 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8501 own_cert, pk_key ) );
8502}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008503
8504void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8505 mbedtls_x509_crt *ca_chain,
8506 mbedtls_x509_crl *ca_crl )
8507{
8508 ssl->handshake->sni_ca_chain = ca_chain;
8509 ssl->handshake->sni_ca_crl = ca_crl;
8510}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008511
8512void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8513 int authmode )
8514{
8515 ssl->handshake->sni_authmode = authmode;
8516}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008517#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8518
Hanno Becker8927c832019-04-03 12:52:50 +01008519#if defined(MBEDTLS_X509_CRT_PARSE_C)
8520void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8521 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8522 void *p_vrfy )
8523{
8524 ssl->f_vrfy = f_vrfy;
8525 ssl->p_vrfy = p_vrfy;
8526}
8527#endif
8528
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008529#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008530/*
8531 * Set EC J-PAKE password for current handshake
8532 */
8533int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8534 const unsigned char *pw,
8535 size_t pw_len )
8536{
8537 mbedtls_ecjpake_role role;
8538
Janos Follath8eb64132016-06-03 15:40:57 +01008539 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8541
8542 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8543 role = MBEDTLS_ECJPAKE_SERVER;
8544 else
8545 role = MBEDTLS_ECJPAKE_CLIENT;
8546
8547 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8548 role,
8549 MBEDTLS_MD_SHA256,
8550 MBEDTLS_ECP_DP_SECP256R1,
8551 pw, pw_len ) );
8552}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008553#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008556
8557static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8558{
8559 /* Remove reference to existing PSK, if any. */
8560#if defined(MBEDTLS_USE_PSA_CRYPTO)
8561 if( conf->psk_opaque != 0 )
8562 {
8563 /* The maintenance of the PSK key slot is the
8564 * user's responsibility. */
8565 conf->psk_opaque = 0;
8566 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008567 /* This and the following branch should never
8568 * be taken simultaenously as we maintain the
8569 * invariant that raw and opaque PSKs are never
8570 * configured simultaneously. As a safeguard,
8571 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008572#endif /* MBEDTLS_USE_PSA_CRYPTO */
8573 if( conf->psk != NULL )
8574 {
8575 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8576
8577 mbedtls_free( conf->psk );
8578 conf->psk = NULL;
8579 conf->psk_len = 0;
8580 }
8581
8582 /* Remove reference to PSK identity, if any. */
8583 if( conf->psk_identity != NULL )
8584 {
8585 mbedtls_free( conf->psk_identity );
8586 conf->psk_identity = NULL;
8587 conf->psk_identity_len = 0;
8588 }
8589}
8590
Hanno Becker7390c712018-11-15 13:33:04 +00008591/* This function assumes that PSK identity in the SSL config is unset.
8592 * It checks that the provided identity is well-formed and attempts
8593 * to make a copy of it in the SSL config.
8594 * On failure, the PSK identity in the config remains unset. */
8595static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8596 unsigned char const *psk_identity,
8597 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008598{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008599 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008600 if( psk_identity == NULL ||
8601 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008602 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008603 {
8604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8605 }
8606
Hanno Becker7390c712018-11-15 13:33:04 +00008607 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8608 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008609 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008610
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008611 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008612 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008613
8614 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008615}
8616
Hanno Becker7390c712018-11-15 13:33:04 +00008617int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8618 const unsigned char *psk, size_t psk_len,
8619 const unsigned char *psk_identity, size_t psk_identity_len )
8620{
8621 int ret;
8622 /* Remove opaque/raw PSK + PSK Identity */
8623 ssl_conf_remove_psk( conf );
8624
8625 /* Check and set raw PSK */
8626 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8628 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8629 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8630 conf->psk_len = psk_len;
8631 memcpy( conf->psk, psk, conf->psk_len );
8632
8633 /* Check and set PSK Identity */
8634 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8635 if( ret != 0 )
8636 ssl_conf_remove_psk( conf );
8637
8638 return( ret );
8639}
8640
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008641static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8642{
8643#if defined(MBEDTLS_USE_PSA_CRYPTO)
8644 if( ssl->handshake->psk_opaque != 0 )
8645 {
8646 ssl->handshake->psk_opaque = 0;
8647 }
8648 else
8649#endif /* MBEDTLS_USE_PSA_CRYPTO */
8650 if( ssl->handshake->psk != NULL )
8651 {
8652 mbedtls_platform_zeroize( ssl->handshake->psk,
8653 ssl->handshake->psk_len );
8654 mbedtls_free( ssl->handshake->psk );
8655 ssl->handshake->psk_len = 0;
8656 }
8657}
8658
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008659int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8660 const unsigned char *psk, size_t psk_len )
8661{
8662 if( psk == NULL || ssl->handshake == NULL )
8663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8664
8665 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8667
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008668 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008669
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008670 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008671 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008672
8673 ssl->handshake->psk_len = psk_len;
8674 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8675
8676 return( 0 );
8677}
8678
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008679#if defined(MBEDTLS_USE_PSA_CRYPTO)
8680int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008681 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008682 const unsigned char *psk_identity,
8683 size_t psk_identity_len )
8684{
Hanno Becker7390c712018-11-15 13:33:04 +00008685 int ret;
8686 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008687 ssl_conf_remove_psk( conf );
8688
Hanno Becker7390c712018-11-15 13:33:04 +00008689 /* Check and set opaque PSK */
8690 if( psk_slot == 0 )
8691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008692 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008693
8694 /* Check and set PSK Identity */
8695 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8696 psk_identity_len );
8697 if( ret != 0 )
8698 ssl_conf_remove_psk( conf );
8699
8700 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008701}
8702
8703int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008704 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008705{
8706 if( psk_slot == 0 || ssl->handshake == NULL )
8707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8708
8709 ssl_remove_psk( ssl );
8710 ssl->handshake->psk_opaque = psk_slot;
8711 return( 0 );
8712}
8713#endif /* MBEDTLS_USE_PSA_CRYPTO */
8714
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008715void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008717 size_t),
8718 void *p_psk )
8719{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008720 conf->f_psk = f_psk;
8721 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008724
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008725#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008726
8727#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008728int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008729{
8730 int ret;
8731
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008732 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8733 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8734 {
8735 mbedtls_mpi_free( &conf->dhm_P );
8736 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008737 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008739
8740 return( 0 );
8741}
Hanno Becker470a8c42017-10-04 15:28:46 +01008742#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008743
Hanno Beckera90658f2017-10-04 15:29:08 +01008744int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8745 const unsigned char *dhm_P, size_t P_len,
8746 const unsigned char *dhm_G, size_t G_len )
8747{
8748 int ret;
8749
8750 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8751 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8752 {
8753 mbedtls_mpi_free( &conf->dhm_P );
8754 mbedtls_mpi_free( &conf->dhm_G );
8755 return( ret );
8756 }
8757
8758 return( 0 );
8759}
Paul Bakker5121ce52009-01-03 21:22:43 +00008760
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008761int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008762{
8763 int ret;
8764
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008765 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8766 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8767 {
8768 mbedtls_mpi_free( &conf->dhm_P );
8769 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008770 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008771 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008772
8773 return( 0 );
8774}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008775#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008776
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008777#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8778/*
8779 * Set the minimum length for Diffie-Hellman parameters
8780 */
8781void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8782 unsigned int bitlen )
8783{
8784 conf->dhm_min_bitlen = bitlen;
8785}
8786#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8787
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008788#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008789/*
8790 * Set allowed/preferred hashes for handshake signatures
8791 */
8792void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8793 const int *hashes )
8794{
8795 conf->sig_hashes = hashes;
8796}
Hanno Becker947194e2017-04-07 13:25:49 +01008797#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008798
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008799#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008800/*
8801 * Set the allowed elliptic curves
8802 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008803void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008804 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008805{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008806 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008807}
Hanno Becker947194e2017-04-07 13:25:49 +01008808#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008809
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008810#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008812{
Hanno Becker947194e2017-04-07 13:25:49 +01008813 /* Initialize to suppress unnecessary compiler warning */
8814 size_t hostname_len = 0;
8815
8816 /* Check if new hostname is valid before
8817 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008818 if( hostname != NULL )
8819 {
8820 hostname_len = strlen( hostname );
8821
8822 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8824 }
8825
8826 /* Now it's clear that we will overwrite the old hostname,
8827 * so we can free it safely */
8828
8829 if( ssl->hostname != NULL )
8830 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008831 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008832 mbedtls_free( ssl->hostname );
8833 }
8834
8835 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008836
Paul Bakker5121ce52009-01-03 21:22:43 +00008837 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008838 {
8839 ssl->hostname = NULL;
8840 }
8841 else
8842 {
8843 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008844 if( ssl->hostname == NULL )
8845 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008846
Hanno Becker947194e2017-04-07 13:25:49 +01008847 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008848
Hanno Becker947194e2017-04-07 13:25:49 +01008849 ssl->hostname[hostname_len] = '\0';
8850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008851
8852 return( 0 );
8853}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008854#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008855
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008856#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008857void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008859 const unsigned char *, size_t),
8860 void *p_sni )
8861{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008862 conf->f_sni = f_sni;
8863 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008868int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008869{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008870 size_t cur_len, tot_len;
8871 const char **p;
8872
8873 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008874 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8875 * MUST NOT be truncated."
8876 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008877 */
8878 tot_len = 0;
8879 for( p = protos; *p != NULL; p++ )
8880 {
8881 cur_len = strlen( *p );
8882 tot_len += cur_len;
8883
8884 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008886 }
8887
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008888 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008889
8890 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008891}
8892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008894{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008895 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008898
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008899void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008901 conf->max_major_ver = major;
8902 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008903}
8904
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008905void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008906{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008907 conf->min_major_ver = major;
8908 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008909}
8910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008912void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008913{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008914 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008915}
8916#endif
8917
Janos Follath088ce432017-04-10 12:42:31 +01008918#if defined(MBEDTLS_SSL_SRV_C)
8919void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8920 char cert_req_ca_list )
8921{
8922 conf->cert_req_ca_list = cert_req_ca_list;
8923}
8924#endif
8925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008927void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008928{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008929 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008930}
8931#endif
8932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008934void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008936 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008937}
8938#endif
8939
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008940#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008941void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008942{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008943 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008944}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008945#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008948int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008951 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008954 }
8955
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008956 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008957
8958 return( 0 );
8959}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008963void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008964{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008965 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008970void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008971{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008972 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008973}
8974#endif
8975
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008976void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008978 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008979}
8980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008982void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008984 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008985}
8986
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008987void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008989 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008990}
8991
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008992void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008993 const unsigned char period[8] )
8994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008995 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009000#if defined(MBEDTLS_SSL_CLI_C)
9001void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009002{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009003 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009004}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009005#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009006
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009007#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009008void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9009 mbedtls_ssl_ticket_write_t *f_ticket_write,
9010 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9011 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009012{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009013 conf->f_ticket_write = f_ticket_write;
9014 conf->f_ticket_parse = f_ticket_parse;
9015 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009016}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009017#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009019
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009020#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9021void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9022 mbedtls_ssl_export_keys_t *f_export_keys,
9023 void *p_export_keys )
9024{
9025 conf->f_export_keys = f_export_keys;
9026 conf->p_export_keys = p_export_keys;
9027}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009028
9029void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9030 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9031 void *p_export_keys )
9032{
9033 conf->f_export_keys_ext = f_export_keys_ext;
9034 conf->p_export_keys = p_export_keys;
9035}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009036#endif
9037
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009038#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009039void mbedtls_ssl_conf_async_private_cb(
9040 mbedtls_ssl_config *conf,
9041 mbedtls_ssl_async_sign_t *f_async_sign,
9042 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9043 mbedtls_ssl_async_resume_t *f_async_resume,
9044 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009045 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009046{
9047 conf->f_async_sign_start = f_async_sign;
9048 conf->f_async_decrypt_start = f_async_decrypt;
9049 conf->f_async_resume = f_async_resume;
9050 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009051 conf->p_async_config_data = async_config_data;
9052}
9053
Gilles Peskine8f97af72018-04-26 11:46:10 +02009054void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9055{
9056 return( conf->p_async_config_data );
9057}
9058
Gilles Peskine1febfef2018-04-30 11:54:39 +02009059void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009060{
9061 if( ssl->handshake == NULL )
9062 return( NULL );
9063 else
9064 return( ssl->handshake->user_async_ctx );
9065}
9066
Gilles Peskine1febfef2018-04-30 11:54:39 +02009067void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009068 void *ctx )
9069{
9070 if( ssl->handshake != NULL )
9071 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009072}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009073#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009074
Paul Bakker5121ce52009-01-03 21:22:43 +00009075/*
9076 * SSL get accessors
9077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009079{
9080 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9081}
9082
Hanno Becker8b170a02017-10-10 11:51:19 +01009083int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9084{
9085 /*
9086 * Case A: We're currently holding back
9087 * a message for further processing.
9088 */
9089
9090 if( ssl->keep_current_message == 1 )
9091 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009093 return( 1 );
9094 }
9095
9096 /*
9097 * Case B: Further records are pending in the current datagram.
9098 */
9099
9100#if defined(MBEDTLS_SSL_PROTO_DTLS)
9101 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9102 ssl->in_left > ssl->next_record_offset )
9103 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009105 return( 1 );
9106 }
9107#endif /* MBEDTLS_SSL_PROTO_DTLS */
9108
9109 /*
9110 * Case C: A handshake message is being processed.
9111 */
9112
Hanno Becker8b170a02017-10-10 11:51:19 +01009113 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9114 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009116 return( 1 );
9117 }
9118
9119 /*
9120 * Case D: An application data message is being processed
9121 */
9122 if( ssl->in_offt != NULL )
9123 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009125 return( 1 );
9126 }
9127
9128 /*
9129 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009130 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009131 * we implement support for multiple alerts in single records.
9132 */
9133
9134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9135 return( 0 );
9136}
9137
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009138uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009139{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009140 if( ssl->session != NULL )
9141 return( ssl->session->verify_result );
9142
9143 if( ssl->session_negotiate != NULL )
9144 return( ssl->session_negotiate->verify_result );
9145
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009146 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009147}
9148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009150{
Paul Bakker926c8e42013-03-06 10:23:34 +01009151 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009152 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009155}
9156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009161 {
9162 switch( ssl->minor_ver )
9163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009165 return( "DTLSv1.0" );
9166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009168 return( "DTLSv1.2" );
9169
9170 default:
9171 return( "unknown (DTLS)" );
9172 }
9173 }
9174#endif
9175
Paul Bakker43ca69c2011-01-15 17:35:19 +00009176 switch( ssl->minor_ver )
9177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009179 return( "SSLv3.0" );
9180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009182 return( "TLSv1.0" );
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009185 return( "TLSv1.1" );
9186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009188 return( "TLSv1.2" );
9189
Paul Bakker43ca69c2011-01-15 17:35:19 +00009190 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009191 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009192 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009193}
9194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009196{
Hanno Becker3136ede2018-08-17 15:28:19 +01009197 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009199 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009200
Hanno Becker78640902018-08-13 16:35:15 +01009201 if( transform == NULL )
9202 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204#if defined(MBEDTLS_ZLIB_SUPPORT)
9205 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9206 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009207#endif
9208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211 case MBEDTLS_MODE_GCM:
9212 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009213 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009215 transform_expansion = transform->minlen;
9216 break;
9217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009219
9220 block_size = mbedtls_cipher_get_block_size(
9221 &transform->cipher_ctx_enc );
9222
Hanno Becker3136ede2018-08-17 15:28:19 +01009223 /* Expansion due to the addition of the MAC. */
9224 transform_expansion += transform->maclen;
9225
9226 /* Expansion due to the addition of CBC padding;
9227 * Theoretically up to 256 bytes, but we never use
9228 * more than the block size of the underlying cipher. */
9229 transform_expansion += block_size;
9230
9231 /* For TLS 1.1 or higher, an explicit IV is added
9232 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009233#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9234 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009235 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009236#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009237
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009238 break;
9239
9240 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009243 }
9244
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009245 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009246}
9247
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009248#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9249size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9250{
9251 size_t max_len;
9252
9253 /*
9254 * Assume mfl_code is correct since it was checked when set
9255 */
Angus Grattond8213d02016-05-25 20:56:48 +10009256 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009257
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009258 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009259 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009260 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009261 {
Angus Grattond8213d02016-05-25 20:56:48 +10009262 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009263 }
9264
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009265 /* During a handshake, use the value being negotiated */
9266 if( ssl->session_negotiate != NULL &&
9267 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9268 {
9269 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9270 }
9271
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009272 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009273}
9274#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9275
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009276#if defined(MBEDTLS_SSL_PROTO_DTLS)
9277static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9278{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009279 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9281 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9282 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9283 return ( 0 );
9284
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009285 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9286 return( ssl->mtu );
9287
9288 if( ssl->mtu == 0 )
9289 return( ssl->handshake->mtu );
9290
9291 return( ssl->mtu < ssl->handshake->mtu ?
9292 ssl->mtu : ssl->handshake->mtu );
9293}
9294#endif /* MBEDTLS_SSL_PROTO_DTLS */
9295
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009296int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9297{
9298 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9299
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009300#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9301 !defined(MBEDTLS_SSL_PROTO_DTLS)
9302 (void) ssl;
9303#endif
9304
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009305#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9306 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9307
9308 if( max_len > mfl )
9309 max_len = mfl;
9310#endif
9311
9312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009313 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009314 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009315 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009316 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9317 const size_t overhead = (size_t) ret;
9318
9319 if( ret < 0 )
9320 return( ret );
9321
9322 if( mtu <= overhead )
9323 {
9324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9325 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9326 }
9327
9328 if( max_len > mtu - overhead )
9329 max_len = mtu - overhead;
9330 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009331#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009332
Hanno Becker0defedb2018-08-10 12:35:02 +01009333#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9334 !defined(MBEDTLS_SSL_PROTO_DTLS)
9335 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009336#endif
9337
9338 return( (int) max_len );
9339}
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#if defined(MBEDTLS_X509_CRT_PARSE_C)
9342const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009343{
9344 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009345 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009346
Hanno Beckere6824572019-02-07 13:18:46 +00009347#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009348 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009349#else
9350 return( NULL );
9351#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009356int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9357 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009358{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009359 if( ssl == NULL ||
9360 dst == NULL ||
9361 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009362 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009365 }
9366
Hanno Becker52055ae2019-02-06 14:30:46 +00009367 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009370
Paul Bakker5121ce52009-01-03 21:22:43 +00009371/*
Paul Bakker1961b702013-01-25 14:49:24 +01009372 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009377
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009378 if( ssl == NULL || ssl->conf == NULL )
9379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009386 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009388#endif
9389
Paul Bakker1961b702013-01-25 14:49:24 +01009390 return( ret );
9391}
9392
9393/*
9394 * Perform the SSL handshake
9395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009397{
9398 int ret = 0;
9399
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009400 if( ssl == NULL || ssl->conf == NULL )
9401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009408
9409 if( ret != 0 )
9410 break;
9411 }
9412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009414
9415 return( ret );
9416}
9417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418#if defined(MBEDTLS_SSL_RENEGOTIATION)
9419#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009420/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009421 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009424{
9425 int ret;
9426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009428
9429 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9431 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009432
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009433 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009434 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009436 return( ret );
9437 }
9438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009440
9441 return( 0 );
9442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009444
9445/*
9446 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 * - any side: calling mbedtls_ssl_renegotiate(),
9448 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9449 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009450 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009451 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009455{
9456 int ret;
9457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009459
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009460 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9461 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009462
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009463 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9464 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009468 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009469 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009470 ssl->handshake->out_msg_seq = 1;
9471 else
9472 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009473 }
9474#endif
9475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9477 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009482 return( ret );
9483 }
9484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009486
9487 return( 0 );
9488}
9489
9490/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009491 * Renegotiate current connection on client,
9492 * or request renegotiation on server
9493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009497
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009498 if( ssl == NULL || ssl->conf == NULL )
9499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009502 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009503 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009509
9510 /* Did we already try/start sending HelloRequest? */
9511 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009513
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009514 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009515 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009519 /*
9520 * On client, either start the renegotiation process or,
9521 * if already in progress, continue the handshake
9522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009527
9528 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009531 return( ret );
9532 }
9533 }
9534 else
9535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009539 return( ret );
9540 }
9541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009543
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009544 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009545}
9546
9547/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009548 * Check record counters and renegotiate if they're above the limit.
9549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009551{
Andres AG2196c7f2016-12-15 17:01:16 +00009552 size_t ep_len = ssl_ep_len( ssl );
9553 int in_ctr_cmp;
9554 int out_ctr_cmp;
9555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9557 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009558 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009559 {
9560 return( 0 );
9561 }
9562
Andres AG2196c7f2016-12-15 17:01:16 +00009563 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9564 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009565 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009566 ssl->conf->renego_period + ep_len, 8 - ep_len );
9567
9568 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009569 {
9570 return( 0 );
9571 }
9572
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009577
9578/*
9579 * Receive application data decrypted from the SSL layer
9580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009582{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009583 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009584 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009585
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009586 if( ssl == NULL || ssl->conf == NULL )
9587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009595 return( ret );
9596
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009597 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009599 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009600 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009601 return( ret );
9602 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009603 }
9604#endif
9605
Hanno Becker4a810fb2017-05-24 16:27:30 +01009606 /*
9607 * Check if renegotiation is necessary and/or handshake is
9608 * in process. If yes, perform/continue, and fall through
9609 * if an unexpected packet is received while the client
9610 * is waiting for the ServerHello.
9611 *
9612 * (There is no equivalent to the last condition on
9613 * the server-side as it is not treated as within
9614 * a handshake while waiting for the ClientHello
9615 * after a renegotiation request.)
9616 */
9617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009619 ret = ssl_check_ctr_renegotiate( ssl );
9620 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9621 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009624 return( ret );
9625 }
9626#endif
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009631 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9632 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009635 return( ret );
9636 }
9637 }
9638
Hanno Beckere41158b2017-10-23 13:30:32 +01009639 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009640 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009641 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009642 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009643 if( ssl->f_get_timer != NULL &&
9644 ssl->f_get_timer( ssl->p_timer ) == -1 )
9645 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009646 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009647 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009648
Hanno Becker327c93b2018-08-15 13:56:18 +01009649 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009650 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009651 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9652 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009653
Hanno Becker4a810fb2017-05-24 16:27:30 +01009654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9655 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009656 }
9657
9658 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009660 {
9661 /*
9662 * OpenSSL sends empty messages to randomize the IV
9663 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009664 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009667 return( 0 );
9668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009670 return( ret );
9671 }
9672 }
9673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009677
Hanno Becker4a810fb2017-05-24 16:27:30 +01009678 /*
9679 * - For client-side, expect SERVER_HELLO_REQUEST.
9680 * - For server-side, expect CLIENT_HELLO.
9681 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9682 */
9683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009687 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009690
9691 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009693 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009694 {
9695 continue;
9696 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009699 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009700#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009701
Hanno Becker4a810fb2017-05-24 16:27:30 +01009702#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009703 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009707
9708 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009711 {
9712 continue;
9713 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009716 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009717#endif /* MBEDTLS_SSL_SRV_C */
9718
Hanno Becker21df7f92017-10-17 11:03:26 +01009719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009720 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009721 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9722 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9723 ssl->conf->allow_legacy_renegotiation ==
9724 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9725 {
9726 /*
9727 * Accept renegotiation request
9728 */
Paul Bakker48916f92012-09-16 19:57:18 +00009729
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009730 /* DTLS clients need to know renego is server-initiated */
9731#if defined(MBEDTLS_SSL_PROTO_DTLS)
9732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9733 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9734 {
9735 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9736 }
9737#endif
9738 ret = ssl_start_renegotiation( ssl );
9739 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9740 ret != 0 )
9741 {
9742 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9743 return( ret );
9744 }
9745 }
9746 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009747#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009748 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009749 /*
9750 * Refuse renegotiation
9751 */
9752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755#if defined(MBEDTLS_SSL_PROTO_SSL3)
9756 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009757 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009758 /* SSLv3 does not have a "no_renegotiation" warning, so
9759 we send a fatal alert and abort the connection. */
9760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9761 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9762 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009763 }
9764 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9766#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9767 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9768 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9771 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9772 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009773 {
9774 return( ret );
9775 }
Paul Bakker48916f92012-09-16 19:57:18 +00009776 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009777 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9779 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9782 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009783 }
Paul Bakker48916f92012-09-16 19:57:18 +00009784 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009785
Hanno Becker90333da2017-10-10 11:27:13 +01009786 /* At this point, we don't know whether the renegotiation has been
9787 * completed or not. The cases to consider are the following:
9788 * 1) The renegotiation is complete. In this case, no new record
9789 * has been read yet.
9790 * 2) The renegotiation is incomplete because the client received
9791 * an application data record while awaiting the ServerHello.
9792 * 3) The renegotiation is incomplete because the client received
9793 * a non-handshake, non-application data message while awaiting
9794 * the ServerHello.
9795 * In each of these case, looping will be the proper action:
9796 * - For 1), the next iteration will read a new record and check
9797 * if it's application data.
9798 * - For 2), the loop condition isn't satisfied as application data
9799 * is present, hence continue is the same as break
9800 * - For 3), the loop condition is satisfied and read_record
9801 * will re-deliver the message that was held back by the client
9802 * when expecting the ServerHello.
9803 */
9804 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009805 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009806#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009808 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009809 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009810 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009811 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009814 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009816 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009817 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9822 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009825 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009826 }
9827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9831 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009832 }
9833
9834 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009835
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009836 /* We're going to return something now, cancel timer,
9837 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009839 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009840
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009842 /* If we requested renego but received AppData, resend HelloRequest.
9843 * Do it now, after setting in_offt, to avoid taking this branch
9844 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009848 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009849 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009852 return( ret );
9853 }
9854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009856#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009857 }
9858
9859 n = ( len < ssl->in_msglen )
9860 ? len : ssl->in_msglen;
9861
9862 memcpy( buf, ssl->in_offt, n );
9863 ssl->in_msglen -= n;
9864
9865 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009866 {
9867 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009868 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009869 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009871 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009872 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009873 /* more data available */
9874 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009878
Paul Bakker23986e52011-04-24 08:57:21 +00009879 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009880}
9881
9882/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009883 * Send application data to be encrypted by the SSL layer, taking care of max
9884 * fragment length and buffer size.
9885 *
9886 * According to RFC 5246 Section 6.2.1:
9887 *
9888 * Zero-length fragments of Application data MAY be sent as they are
9889 * potentially useful as a traffic analysis countermeasure.
9890 *
9891 * Therefore, it is possible that the input message length is 0 and the
9892 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009893 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009894static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009895 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009896{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009897 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9898 const size_t max_len = (size_t) ret;
9899
9900 if( ret < 0 )
9901 {
9902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9903 return( ret );
9904 }
9905
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009906 if( len > max_len )
9907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009912 "maximum fragment length: %d > %d",
9913 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009915 }
9916 else
9917#endif
9918 len = max_len;
9919 }
Paul Bakker887bd502011-06-08 13:10:54 +00009920
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 if( ssl->out_left != 0 )
9922 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009923 /*
9924 * The user has previously tried to send the data and
9925 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9926 * written. In this case, we expect the high-level write function
9927 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009932 return( ret );
9933 }
9934 }
Paul Bakker887bd502011-06-08 13:10:54 +00009935 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009936 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009937 /*
9938 * The user is trying to send a message the first time, so we need to
9939 * copy the data into the internal buffers and setup the data structure
9940 * to keep track of partial writes
9941 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009942 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009944 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009945
Hanno Becker67bc7c32018-08-06 11:33:50 +01009946 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009949 return( ret );
9950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009951 }
9952
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009953 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009954}
9955
9956/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009957 * Write application data, doing 1/n-1 splitting if necessary.
9958 *
9959 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009960 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009961 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009964static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009965 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009966{
9967 int ret;
9968
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009969 if( ssl->conf->cbc_record_splitting ==
9970 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009971 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9973 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9974 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009975 {
9976 return( ssl_write_real( ssl, buf, len ) );
9977 }
9978
9979 if( ssl->split_done == 0 )
9980 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009981 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009982 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009983 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009984 }
9985
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009986 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9987 return( ret );
9988 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009989
9990 return( ret + 1 );
9991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009993
9994/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009995 * Write application data (public-facing wrapper)
9996 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009997int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009998{
9999 int ret;
10000
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010002
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010003 if( ssl == NULL || ssl->conf == NULL )
10004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10005
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010006#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010007 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10008 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010009 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010010 return( ret );
10011 }
10012#endif
10013
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010014 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010015 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010016 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010017 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010019 return( ret );
10020 }
10021 }
10022
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010023#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010024 ret = ssl_write_split( ssl, buf, len );
10025#else
10026 ret = ssl_write_real( ssl, buf, len );
10027#endif
10028
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010030
10031 return( ret );
10032}
10033
10034/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010035 * Notify the peer that the connection is being closed
10036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010038{
10039 int ret;
10040
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010041 if( ssl == NULL || ssl->conf == NULL )
10042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010045
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010046 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10052 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10053 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010056 return( ret );
10057 }
10058 }
10059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010061
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010062 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010063}
10064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010066{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010067 if( transform == NULL )
10068 return;
10069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010071 deflateEnd( &transform->ctx_deflate );
10072 inflateEnd( &transform->ctx_inflate );
10073#endif
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10076 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010077
Hanno Beckerd56ed242018-01-03 15:32:51 +000010078#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 mbedtls_md_free( &transform->md_ctx_enc );
10080 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010081#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010082
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010083 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010084}
10085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086#if defined(MBEDTLS_X509_CRT_PARSE_C)
10087static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010090
10091 while( cur != NULL )
10092 {
10093 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010095 cur = next;
10096 }
10097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010099
Hanno Becker0271f962018-08-16 13:23:47 +010010100#if defined(MBEDTLS_SSL_PROTO_DTLS)
10101
10102static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10103{
10104 unsigned offset;
10105 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10106
10107 if( hs == NULL )
10108 return;
10109
Hanno Becker283f5ef2018-08-24 09:34:47 +010010110 ssl_free_buffered_record( ssl );
10111
Hanno Becker0271f962018-08-16 13:23:47 +010010112 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010113 ssl_buffering_free_slot( ssl, offset );
10114}
10115
10116static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10117 uint8_t slot )
10118{
10119 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10120 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010121
10122 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10123 return;
10124
Hanno Beckere605b192018-08-21 15:59:07 +010010125 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010126 {
Hanno Beckere605b192018-08-21 15:59:07 +010010127 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010128 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010129 mbedtls_free( hs_buf->data );
10130 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010131 }
10132}
10133
10134#endif /* MBEDTLS_SSL_PROTO_DTLS */
10135
Gilles Peskine9b562d52018-04-25 20:32:43 +020010136void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010137{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010138 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10139
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010140 if( handshake == NULL )
10141 return;
10142
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010143#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10144 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10145 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010146 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010147 handshake->async_in_progress = 0;
10148 }
10149#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10150
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010151#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10152 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10153 mbedtls_md5_free( &handshake->fin_md5 );
10154 mbedtls_sha1_free( &handshake->fin_sha1 );
10155#endif
10156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10157#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010158#if defined(MBEDTLS_USE_PSA_CRYPTO)
10159 psa_hash_abort( &handshake->fin_sha256_psa );
10160#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010161 mbedtls_sha256_free( &handshake->fin_sha256 );
10162#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010163#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010164#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010165#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010166 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010167#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010168 mbedtls_sha512_free( &handshake->fin_sha512 );
10169#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010170#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173#if defined(MBEDTLS_DHM_C)
10174 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176#if defined(MBEDTLS_ECDH_C)
10177 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010178#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010179#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010180 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010181#if defined(MBEDTLS_SSL_CLI_C)
10182 mbedtls_free( handshake->ecjpake_cache );
10183 handshake->ecjpake_cache = NULL;
10184 handshake->ecjpake_cache_len = 0;
10185#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010186#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010187
Janos Follath4ae5c292016-02-10 11:27:43 +000010188#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10189 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010190 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010192#endif
10193
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010194#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10195 if( handshake->psk != NULL )
10196 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010197 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010198 mbedtls_free( handshake->psk );
10199 }
10200#endif
10201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10203 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010204 /*
10205 * Free only the linked list wrapper, not the keys themselves
10206 * since the belong to the SNI callback
10207 */
10208 if( handshake->sni_key_cert != NULL )
10209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010211
10212 while( cur != NULL )
10213 {
10214 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010216 cur = next;
10217 }
10218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010220
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010221#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010222 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010223 if( handshake->ecrs_peer_cert != NULL )
10224 {
10225 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10226 mbedtls_free( handshake->ecrs_peer_cert );
10227 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010228#endif
10229
Hanno Becker75173122019-02-06 16:18:31 +000010230#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10231 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10232 mbedtls_pk_free( &handshake->peer_pubkey );
10233#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235#if defined(MBEDTLS_SSL_PROTO_DTLS)
10236 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010237 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010238 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010239#endif
10240
Hanno Becker4a63ed42019-01-08 11:39:35 +000010241#if defined(MBEDTLS_ECDH_C) && \
10242 defined(MBEDTLS_USE_PSA_CRYPTO)
10243 psa_destroy_key( handshake->ecdh_psa_privkey );
10244#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10245
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010246 mbedtls_platform_zeroize( handshake,
10247 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010248}
10249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010251{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010252 if( session == NULL )
10253 return;
10254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010256 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010257#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010258
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010259#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010261#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010262
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010263 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010264}
10265
Paul Bakker5121ce52009-01-03 21:22:43 +000010266/*
10267 * Free an SSL context
10268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010270{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010271 if( ssl == NULL )
10272 return;
10273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010275
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010276 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010277 {
Angus Grattond8213d02016-05-25 20:56:48 +100010278 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010280 }
10281
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010282 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010283 {
Angus Grattond8213d02016-05-25 20:56:48 +100010284 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010286 }
10287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010289 if( ssl->compress_buf != NULL )
10290 {
Angus Grattond8213d02016-05-25 20:56:48 +100010291 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010292 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010293 }
10294#endif
10295
Paul Bakker48916f92012-09-16 19:57:18 +000010296 if( ssl->transform )
10297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 mbedtls_ssl_transform_free( ssl->transform );
10299 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010300 }
10301
10302 if( ssl->handshake )
10303 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010304 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10306 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308 mbedtls_free( ssl->handshake );
10309 mbedtls_free( ssl->transform_negotiate );
10310 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010311 }
10312
Paul Bakkerc0463502013-02-14 11:19:38 +010010313 if( ssl->session )
10314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 mbedtls_ssl_session_free( ssl->session );
10316 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010317 }
10318
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010319#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010320 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010321 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010322 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010324 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10328 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10331 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010332 }
10333#endif
10334
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010335#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010337#endif
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010340
Paul Bakker86f04f42013-02-14 11:20:09 +010010341 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010342 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010343}
10344
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010345/*
10346 * Initialze mbedtls_ssl_config
10347 */
10348void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10349{
10350 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10351}
10352
Simon Butcherc97b6972015-12-27 23:48:17 +000010353#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010354static int ssl_preset_default_hashes[] = {
10355#if defined(MBEDTLS_SHA512_C)
10356 MBEDTLS_MD_SHA512,
10357 MBEDTLS_MD_SHA384,
10358#endif
10359#if defined(MBEDTLS_SHA256_C)
10360 MBEDTLS_MD_SHA256,
10361 MBEDTLS_MD_SHA224,
10362#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010363#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010364 MBEDTLS_MD_SHA1,
10365#endif
10366 MBEDTLS_MD_NONE
10367};
Simon Butcherc97b6972015-12-27 23:48:17 +000010368#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010369
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010370static int ssl_preset_suiteb_ciphersuites[] = {
10371 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10372 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10373 0
10374};
10375
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010376#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010377static int ssl_preset_suiteb_hashes[] = {
10378 MBEDTLS_MD_SHA256,
10379 MBEDTLS_MD_SHA384,
10380 MBEDTLS_MD_NONE
10381};
10382#endif
10383
10384#if defined(MBEDTLS_ECP_C)
10385static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10386 MBEDTLS_ECP_DP_SECP256R1,
10387 MBEDTLS_ECP_DP_SECP384R1,
10388 MBEDTLS_ECP_DP_NONE
10389};
10390#endif
10391
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010392/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010393 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010394 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010395int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010396 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010397{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010398#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010399 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010400#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010401
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010402 /* Use the functions here so that they are covered in tests,
10403 * but otherwise access member directly for efficiency */
10404 mbedtls_ssl_conf_endpoint( conf, endpoint );
10405 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010406
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010407 /*
10408 * Things that are common to all presets
10409 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010410#if defined(MBEDTLS_SSL_CLI_C)
10411 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10412 {
10413 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10414#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10415 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10416#endif
10417 }
10418#endif
10419
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010420#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010421 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010422#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010423
10424#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10425 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10426#endif
10427
10428#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10429 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10430#endif
10431
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010432#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10433 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10434#endif
10435
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010436#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010437 conf->f_cookie_write = ssl_cookie_write_dummy;
10438 conf->f_cookie_check = ssl_cookie_check_dummy;
10439#endif
10440
10441#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10442 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10443#endif
10444
Janos Follath088ce432017-04-10 12:42:31 +010010445#if defined(MBEDTLS_SSL_SRV_C)
10446 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10447#endif
10448
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010449#if defined(MBEDTLS_SSL_PROTO_DTLS)
10450 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10451 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10452#endif
10453
10454#if defined(MBEDTLS_SSL_RENEGOTIATION)
10455 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010456 memset( conf->renego_period, 0x00, 2 );
10457 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010458#endif
10459
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010460#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10461 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10462 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010463 const unsigned char dhm_p[] =
10464 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10465 const unsigned char dhm_g[] =
10466 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10467
Hanno Beckera90658f2017-10-04 15:29:08 +010010468 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10469 dhm_p, sizeof( dhm_p ),
10470 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010471 {
10472 return( ret );
10473 }
10474 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010475#endif
10476
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010477 /*
10478 * Preset-specific defaults
10479 */
10480 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010481 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010482 /*
10483 * NSA Suite B
10484 */
10485 case MBEDTLS_SSL_PRESET_SUITEB:
10486 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10487 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10488 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10489 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10490
10491 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10492 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10493 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10494 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10495 ssl_preset_suiteb_ciphersuites;
10496
10497#if defined(MBEDTLS_X509_CRT_PARSE_C)
10498 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010499#endif
10500
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010501#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010502 conf->sig_hashes = ssl_preset_suiteb_hashes;
10503#endif
10504
10505#if defined(MBEDTLS_ECP_C)
10506 conf->curve_list = ssl_preset_suiteb_curves;
10507#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010508 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010509
10510 /*
10511 * Default
10512 */
10513 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010514 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10515 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10516 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10517 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10518 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10519 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10520 MBEDTLS_SSL_MIN_MINOR_VERSION :
10521 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010522 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10523 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10524
10525#if defined(MBEDTLS_SSL_PROTO_DTLS)
10526 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10527 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10528#endif
10529
10530 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10531 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10532 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10533 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10534 mbedtls_ssl_list_ciphersuites();
10535
10536#if defined(MBEDTLS_X509_CRT_PARSE_C)
10537 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10538#endif
10539
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010540#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010541 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010542#endif
10543
10544#if defined(MBEDTLS_ECP_C)
10545 conf->curve_list = mbedtls_ecp_grp_id_list();
10546#endif
10547
10548#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10549 conf->dhm_min_bitlen = 1024;
10550#endif
10551 }
10552
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010553 return( 0 );
10554}
10555
10556/*
10557 * Free mbedtls_ssl_config
10558 */
10559void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10560{
10561#if defined(MBEDTLS_DHM_C)
10562 mbedtls_mpi_free( &conf->dhm_P );
10563 mbedtls_mpi_free( &conf->dhm_G );
10564#endif
10565
10566#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10567 if( conf->psk != NULL )
10568 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010569 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010570 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010571 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010572 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010573 }
10574
10575 if( conf->psk_identity != NULL )
10576 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010577 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010578 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010579 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010580 conf->psk_identity_len = 0;
10581 }
10582#endif
10583
10584#if defined(MBEDTLS_X509_CRT_PARSE_C)
10585 ssl_key_cert_free( conf->key_cert );
10586#endif
10587
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010588 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010589}
10590
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010591#if defined(MBEDTLS_PK_C) && \
10592 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010593/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#if defined(MBEDTLS_RSA_C)
10599 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10600 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602#if defined(MBEDTLS_ECDSA_C)
10603 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10604 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010607}
10608
Hanno Becker7e5437a2017-04-28 17:15:26 +010010609unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10610{
10611 switch( type ) {
10612 case MBEDTLS_PK_RSA:
10613 return( MBEDTLS_SSL_SIG_RSA );
10614 case MBEDTLS_PK_ECDSA:
10615 case MBEDTLS_PK_ECKEY:
10616 return( MBEDTLS_SSL_SIG_ECDSA );
10617 default:
10618 return( MBEDTLS_SSL_SIG_ANON );
10619 }
10620}
10621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010623{
10624 switch( sig )
10625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626#if defined(MBEDTLS_RSA_C)
10627 case MBEDTLS_SSL_SIG_RSA:
10628 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630#if defined(MBEDTLS_ECDSA_C)
10631 case MBEDTLS_SSL_SIG_ECDSA:
10632 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010633#endif
10634 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010636 }
10637}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010638#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010639
Hanno Becker7e5437a2017-04-28 17:15:26 +010010640#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10641 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10642
10643/* Find an entry in a signature-hash set matching a given hash algorithm. */
10644mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10645 mbedtls_pk_type_t sig_alg )
10646{
10647 switch( sig_alg )
10648 {
10649 case MBEDTLS_PK_RSA:
10650 return( set->rsa );
10651 case MBEDTLS_PK_ECDSA:
10652 return( set->ecdsa );
10653 default:
10654 return( MBEDTLS_MD_NONE );
10655 }
10656}
10657
10658/* Add a signature-hash-pair to a signature-hash set */
10659void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10660 mbedtls_pk_type_t sig_alg,
10661 mbedtls_md_type_t md_alg )
10662{
10663 switch( sig_alg )
10664 {
10665 case MBEDTLS_PK_RSA:
10666 if( set->rsa == MBEDTLS_MD_NONE )
10667 set->rsa = md_alg;
10668 break;
10669
10670 case MBEDTLS_PK_ECDSA:
10671 if( set->ecdsa == MBEDTLS_MD_NONE )
10672 set->ecdsa = md_alg;
10673 break;
10674
10675 default:
10676 break;
10677 }
10678}
10679
10680/* Allow exactly one hash algorithm for each signature. */
10681void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10682 mbedtls_md_type_t md_alg )
10683{
10684 set->rsa = md_alg;
10685 set->ecdsa = md_alg;
10686}
10687
10688#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10689 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10690
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010691/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010692 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010695{
10696 switch( hash )
10697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698#if defined(MBEDTLS_MD5_C)
10699 case MBEDTLS_SSL_HASH_MD5:
10700 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#if defined(MBEDTLS_SHA1_C)
10703 case MBEDTLS_SSL_HASH_SHA1:
10704 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706#if defined(MBEDTLS_SHA256_C)
10707 case MBEDTLS_SSL_HASH_SHA224:
10708 return( MBEDTLS_MD_SHA224 );
10709 case MBEDTLS_SSL_HASH_SHA256:
10710 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712#if defined(MBEDTLS_SHA512_C)
10713 case MBEDTLS_SSL_HASH_SHA384:
10714 return( MBEDTLS_MD_SHA384 );
10715 case MBEDTLS_SSL_HASH_SHA512:
10716 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010717#endif
10718 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010720 }
10721}
10722
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010723/*
10724 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10725 */
10726unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10727{
10728 switch( md )
10729 {
10730#if defined(MBEDTLS_MD5_C)
10731 case MBEDTLS_MD_MD5:
10732 return( MBEDTLS_SSL_HASH_MD5 );
10733#endif
10734#if defined(MBEDTLS_SHA1_C)
10735 case MBEDTLS_MD_SHA1:
10736 return( MBEDTLS_SSL_HASH_SHA1 );
10737#endif
10738#if defined(MBEDTLS_SHA256_C)
10739 case MBEDTLS_MD_SHA224:
10740 return( MBEDTLS_SSL_HASH_SHA224 );
10741 case MBEDTLS_MD_SHA256:
10742 return( MBEDTLS_SSL_HASH_SHA256 );
10743#endif
10744#if defined(MBEDTLS_SHA512_C)
10745 case MBEDTLS_MD_SHA384:
10746 return( MBEDTLS_SSL_HASH_SHA384 );
10747 case MBEDTLS_MD_SHA512:
10748 return( MBEDTLS_SSL_HASH_SHA512 );
10749#endif
10750 default:
10751 return( MBEDTLS_SSL_HASH_NONE );
10752 }
10753}
10754
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010755#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010756/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010757 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010758 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010759 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010760int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010763
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010764 if( ssl->conf->curve_list == NULL )
10765 return( -1 );
10766
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010767 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010768 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010769 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010770
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010771 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010772}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010773#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010774
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010775#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010776/*
10777 * Check if a hash proposed by the peer is in our list.
10778 * Return 0 if we're willing to use it, -1 otherwise.
10779 */
10780int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10781 mbedtls_md_type_t md )
10782{
10783 const int *cur;
10784
10785 if( ssl->conf->sig_hashes == NULL )
10786 return( -1 );
10787
10788 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10789 if( *cur == (int) md )
10790 return( 0 );
10791
10792 return( -1 );
10793}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010794#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796#if defined(MBEDTLS_X509_CRT_PARSE_C)
10797int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10798 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010799 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010800 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010801{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010802 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010804 int usage = 0;
10805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010807 const char *ext_oid;
10808 size_t ext_len;
10809#endif
10810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10812 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010813 ((void) cert);
10814 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010815 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010816#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010818#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10819 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010820 {
10821 /* Server part of the key exchange */
10822 switch( ciphersuite->key_exchange )
10823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824 case MBEDTLS_KEY_EXCHANGE_RSA:
10825 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010826 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010827 break;
10828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010829 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10830 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10831 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10832 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010833 break;
10834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10836 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010837 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010838 break;
10839
10840 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841 case MBEDTLS_KEY_EXCHANGE_NONE:
10842 case MBEDTLS_KEY_EXCHANGE_PSK:
10843 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10844 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010845 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010846 usage = 0;
10847 }
10848 }
10849 else
10850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10852 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010853 }
10854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010856 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010857 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010858 ret = -1;
10859 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010860#else
10861 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10865 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10868 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010869 }
10870 else
10871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10873 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010874 }
10875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010877 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010878 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010879 ret = -1;
10880 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010882
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010883 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010886
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010887/*
10888 * Convert version numbers to/from wire format
10889 * and, for DTLS, to/from TLS equivalent.
10890 *
10891 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010892 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010893 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10894 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010897 unsigned char ver[2] )
10898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899#if defined(MBEDTLS_SSL_PROTO_DTLS)
10900 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010903 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10904
10905 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10906 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10907 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010908 else
10909#else
10910 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010911#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010912 {
10913 ver[0] = (unsigned char) major;
10914 ver[1] = (unsigned char) minor;
10915 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010916}
10917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010919 const unsigned char ver[2] )
10920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_SSL_PROTO_DTLS)
10922 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010923 {
10924 *major = 255 - ver[0] + 2;
10925 *minor = 255 - ver[1] + 1;
10926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010928 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10929 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010930 else
10931#else
10932 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010933#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010934 {
10935 *major = ver[0];
10936 *minor = ver[1];
10937 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010938}
10939
Simon Butcher99000142016-10-13 17:21:01 +010010940int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10941{
10942#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10943 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10944 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10945
10946 switch( md )
10947 {
10948#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10949#if defined(MBEDTLS_MD5_C)
10950 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010951 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010952#endif
10953#if defined(MBEDTLS_SHA1_C)
10954 case MBEDTLS_SSL_HASH_SHA1:
10955 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10956 break;
10957#endif
10958#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10959#if defined(MBEDTLS_SHA512_C)
10960 case MBEDTLS_SSL_HASH_SHA384:
10961 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10962 break;
10963#endif
10964#if defined(MBEDTLS_SHA256_C)
10965 case MBEDTLS_SSL_HASH_SHA256:
10966 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10967 break;
10968#endif
10969 default:
10970 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10971 }
10972
10973 return 0;
10974#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10975 (void) ssl;
10976 (void) md;
10977
10978 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10979#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10980}
10981
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010982#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10983 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10984int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10985 unsigned char *output,
10986 unsigned char *data, size_t data_len )
10987{
10988 int ret = 0;
10989 mbedtls_md5_context mbedtls_md5;
10990 mbedtls_sha1_context mbedtls_sha1;
10991
10992 mbedtls_md5_init( &mbedtls_md5 );
10993 mbedtls_sha1_init( &mbedtls_sha1 );
10994
10995 /*
10996 * digitally-signed struct {
10997 * opaque md5_hash[16];
10998 * opaque sha_hash[20];
10999 * };
11000 *
11001 * md5_hash
11002 * MD5(ClientHello.random + ServerHello.random
11003 * + ServerParams);
11004 * sha_hash
11005 * SHA(ClientHello.random + ServerHello.random
11006 * + ServerParams);
11007 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011008 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011009 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011011 goto exit;
11012 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011013 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011014 ssl->handshake->randbytes, 64 ) ) != 0 )
11015 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011017 goto exit;
11018 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011019 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011022 goto exit;
11023 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011024 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011025 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011027 goto exit;
11028 }
11029
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011030 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011031 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011033 goto exit;
11034 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011035 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011036 ssl->handshake->randbytes, 64 ) ) != 0 )
11037 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011039 goto exit;
11040 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011041 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011042 data_len ) ) != 0 )
11043 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011045 goto exit;
11046 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011047 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011048 output + 16 ) ) != 0 )
11049 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011051 goto exit;
11052 }
11053
11054exit:
11055 mbedtls_md5_free( &mbedtls_md5 );
11056 mbedtls_sha1_free( &mbedtls_sha1 );
11057
11058 if( ret != 0 )
11059 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11060 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11061
11062 return( ret );
11063
11064}
11065#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11066 MBEDTLS_SSL_PROTO_TLS1_1 */
11067
11068#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11069 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011070
11071#if defined(MBEDTLS_USE_PSA_CRYPTO)
11072int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11073 unsigned char *hash, size_t *hashlen,
11074 unsigned char *data, size_t data_len,
11075 mbedtls_md_type_t md_alg )
11076{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011077 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011078 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011079 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11080
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011082
11083 if( ( status = psa_hash_setup( &hash_operation,
11084 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011085 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011086 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011087 goto exit;
11088 }
11089
Andrzej Kurek814feff2019-01-14 04:35:19 -050011090 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11091 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011092 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011093 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011094 goto exit;
11095 }
11096
Andrzej Kurek814feff2019-01-14 04:35:19 -050011097 if( ( status = psa_hash_update( &hash_operation,
11098 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011099 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011100 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011101 goto exit;
11102 }
11103
Andrzej Kurek814feff2019-01-14 04:35:19 -050011104 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11105 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011106 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011107 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011108 goto exit;
11109 }
11110
11111exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011112 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011113 {
11114 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11115 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011116 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011117 {
11118 case PSA_ERROR_NOT_SUPPORTED:
11119 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011120 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011121 case PSA_ERROR_BUFFER_TOO_SMALL:
11122 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11123 case PSA_ERROR_INSUFFICIENT_MEMORY:
11124 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11125 default:
11126 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11127 }
11128 }
11129 return( 0 );
11130}
11131
11132#else
11133
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011134int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011135 unsigned char *hash, size_t *hashlen,
11136 unsigned char *data, size_t data_len,
11137 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011138{
11139 int ret = 0;
11140 mbedtls_md_context_t ctx;
11141 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011142 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011143
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011145
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011146 mbedtls_md_init( &ctx );
11147
11148 /*
11149 * digitally-signed struct {
11150 * opaque client_random[32];
11151 * opaque server_random[32];
11152 * ServerDHParams params;
11153 * };
11154 */
11155 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11156 {
11157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11158 goto exit;
11159 }
11160 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11161 {
11162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11163 goto exit;
11164 }
11165 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11166 {
11167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11168 goto exit;
11169 }
11170 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11171 {
11172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11173 goto exit;
11174 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011175 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011176 {
11177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11178 goto exit;
11179 }
11180
11181exit:
11182 mbedtls_md_free( &ctx );
11183
11184 if( ret != 0 )
11185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11186 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11187
11188 return( ret );
11189}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011190#endif /* MBEDTLS_USE_PSA_CRYPTO */
11191
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011192#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11193 MBEDTLS_SSL_PROTO_TLS1_2 */
11194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011195#endif /* MBEDTLS_SSL_TLS_C */