blob: d139489a1edeef8e56e681829a95a60f80af6283 [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)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001971/* This functions transforms a DTLS plaintext fragment and a record content
1972 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001973 *
1974 * struct {
1975 * opaque content[DTLSPlaintext.length];
1976 * ContentType real_type;
1977 * uint8 zeros[length_of_padding];
1978 * } DTLSInnerPlaintext;
1979 *
1980 * Input:
1981 * - `content`: The beginning of the buffer holding the
1982 * plaintext to be wrapped.
1983 * - `*content_size`: The length of the plaintext in Bytes.
1984 * - `max_len`: The number of Bytes available starting from
1985 * `content`. This must be `>= *content_size`.
1986 * - `rec_type`: The desired record content type.
1987 *
1988 * Output:
1989 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1990 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1991 *
1992 * Returns:
1993 * - `0` on success.
1994 * - A negative error code if `max_len` didn't offer enough space
1995 * for the expansion.
1996 */
1997static int ssl_cid_build_inner_plaintext( unsigned char *content,
1998 size_t *content_size,
1999 size_t remaining,
2000 uint8_t rec_type )
2001{
2002 size_t len = *content_size;
2003 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2004
2005 /* Write real content type */
2006 if( remaining == 0 )
2007 return( -1 );
2008 content[ len ] = rec_type;
2009 len++;
2010 remaining--;
2011
2012 if( remaining < pad )
2013 return( -1 );
2014 memset( content + len, 0, pad );
2015 len += pad;
2016 remaining -= pad;
2017
2018 *content_size = len;
2019 return( 0 );
2020}
2021
Hanno Becker07dc97d2019-05-20 15:08:01 +01002022/* This function parses a DTLSInnerPlaintext structure.
2023 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002024static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2025 size_t *content_size,
2026 uint8_t *rec_type )
2027{
2028 size_t remaining = *content_size;
2029
2030 /* Determine length of padding by skipping zeroes from the back. */
2031 do
2032 {
2033 if( remaining == 0 )
2034 return( -1 );
2035 remaining--;
2036 } while( content[ remaining ] == 0 );
2037
2038 *content_size = remaining;
2039 *rec_type = content[ remaining ];
2040
2041 return( 0 );
2042}
2043#endif /* MBEDTLS_SSL_CID */
2044
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002045/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002046 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002047static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002048 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002049 mbedtls_record *rec )
2050{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002051 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002052 *
2053 * additional_data = seq_num + TLSCompressed.type +
2054 * TLSCompressed.version + TLSCompressed.length;
2055 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002056 * For the CID extension, this is extended as follows
2057 * (quoting draft-ietf-tls-dtls-connection-id-05,
2058 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002059 *
2060 * additional_data = seq_num + DTLSPlaintext.type +
2061 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002062 * cid +
2063 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002064 * length_of_DTLSInnerPlaintext;
2065 */
2066
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002067 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2068 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002069 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002070
2071#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002072 if( rec->cid_len != 0 )
2073 {
2074 memcpy( add_data + 11, rec->cid, rec->cid_len );
2075 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2076 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2077 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2078 *add_data_len = 13 + 1 + rec->cid_len;
2079 }
2080 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002081#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002082 {
2083 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2084 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2085 *add_data_len = 13;
2086 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087}
2088
Hanno Beckera18d1322018-01-03 14:27:32 +00002089int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2090 mbedtls_ssl_transform *transform,
2091 mbedtls_record *rec,
2092 int (*f_rng)(void *, unsigned char *, size_t),
2093 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002096 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002097 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002098 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002099 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002100 size_t post_avail;
2101
2102 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002103#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104 ((void) ssl);
2105#endif
2106
2107 /* The PRNG is used for dynamic IV generation that's used
2108 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2109#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2110 ( defined(MBEDTLS_AES_C) || \
2111 defined(MBEDTLS_ARIA_C) || \
2112 defined(MBEDTLS_CAMELLIA_C) ) && \
2113 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2114 ((void) f_rng);
2115 ((void) p_rng);
2116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002120 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2124 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002125 if( rec == NULL
2126 || rec->buf == NULL
2127 || rec->buf_len < rec->data_offset
2128 || rec->buf_len - rec->data_offset < rec->data_len
2129#if defined(MBEDTLS_SSL_CID)
2130 || rec->cid_len != 0
2131#endif
2132 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002133 {
2134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002136 }
2137
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002139 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002141 data, rec->data_len );
2142
2143 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2144
2145 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2146 {
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2148 (unsigned) rec->data_len,
2149 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2151 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002152
Hanno Beckercab87e62019-04-29 13:52:53 +01002153#if defined(MBEDTLS_SSL_CID)
2154 /*
2155 * Add CID information
2156 */
2157 rec->cid_len = transform->out_cid_len;
2158 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2159 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002160
2161 if( rec->cid_len != 0 )
2162 {
2163 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002164 * Wrap plaintext into DTLSInnerPlaintext structure.
2165 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002166 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002167 * Note that this changes `rec->data_len`, and hence
2168 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002169 */
2170 if( ssl_cid_build_inner_plaintext( data,
2171 &rec->data_len,
2172 post_avail,
2173 rec->type ) != 0 )
2174 {
2175 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2176 }
2177
2178 rec->type = MBEDTLS_SSL_MSG_CID;
2179 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002180#endif /* MBEDTLS_SSL_CID */
2181
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002182 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2183
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002185 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 */
Hanno Becker52344c22018-01-03 15:24:20 +00002187#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 if( mode == MBEDTLS_MODE_STREAM ||
2189 ( mode == MBEDTLS_MODE_CBC
2190#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002191 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002192#endif
2193 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002195 if( post_avail < transform->maclen )
2196 {
2197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2198 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2199 }
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002202 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002203 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002204 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002205 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2206 data, rec->data_len, rec->ctr, rec->type, mac );
2207 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002208 }
2209 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2212 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002213 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002214 {
Hanno Becker992b6872017-11-09 18:57:39 +00002215 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2216
Hanno Beckercab87e62019-04-29 13:52:53 +01002217 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002218
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002220 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002221 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2222 data, rec->data_len );
2223 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2224 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2225
2226 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 }
2228 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002229#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2232 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 }
2234
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2236 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002237
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002238 rec->data_len += transform->maclen;
2239 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002241 }
Hanno Becker52344c22018-01-03 15:24:20 +00002242#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 /*
2245 * Encrypt
2246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2248 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002250 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002251 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253 "including %d bytes of padding",
2254 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002256 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2257 transform->iv_enc, transform->ivlen,
2258 data, rec->data_len,
2259 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002262 return( ret );
2263 }
2264
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002265 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002269 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 }
Paul Bakker68884e32013-01-07 18:20:04 +01002271 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002273
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002274#if defined(MBEDTLS_GCM_C) || \
2275 defined(MBEDTLS_CCM_C) || \
2276 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002278 mode == MBEDTLS_MODE_CCM ||
2279 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002280 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002281 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002282 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002284
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002285 /* Check that there's space for both the authentication tag
2286 * and the explicit IV before and after the record content. */
2287 if( post_avail < transform->taglen ||
2288 rec->data_offset < explicit_iv_len )
2289 {
2290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2291 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2292 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002293
Paul Bakker68884e32013-01-07 18:20:04 +01002294 /*
2295 * Generate IV
2296 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002297 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2298 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002299 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002300 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002301 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2302 explicit_iv_len );
2303 /* Prefix record content with explicit IV. */
2304 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002305 }
2306 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2307 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002308 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002309 unsigned char i;
2310
2311 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2312
2313 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002314 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002315 }
2316 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002317 {
2318 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002321 }
2322
Hanno Beckercab87e62019-04-29 13:52:53 +01002323 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002324
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2326 iv, transform->ivlen );
2327 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002328 data - explicit_iv_len, explicit_iv_len );
2329 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002330 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002332 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002334
Paul Bakker68884e32013-01-07 18:20:04 +01002335 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002336 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002337 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002338
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002340 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002341 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002342 data, rec->data_len, /* source */
2343 data, &rec->data_len, /* destination */
2344 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002347 return( ret );
2348 }
2349
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002350 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2351 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002352
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353 rec->data_len += transform->taglen + explicit_iv_len;
2354 rec->data_offset -= explicit_iv_len;
2355 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002356 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2360#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002361 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002364 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 size_t padlen, i;
2366 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002367
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 /* Currently we're always using minimal padding
2369 * (up to 255 bytes would be allowed). */
2370 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2371 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 padlen = 0;
2373
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002374 /* Check there's enough space in the buffer for the padding. */
2375 if( post_avail < padlen + 1 )
2376 {
2377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2378 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2379 }
2380
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002382 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002384 rec->data_len += padlen + 1;
2385 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002388 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002389 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2390 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002391 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002392 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002393 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002394 if( f_rng == NULL )
2395 {
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2398 }
2399
2400 if( rec->data_offset < transform->ivlen )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2403 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2404 }
2405
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 /*
2407 * Generate IV
2408 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002410 if( ret != 0 )
2411 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002412
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 memcpy( data - transform->ivlen, transform->iv_enc,
2414 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002415
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002421 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002422 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002424 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2425 transform->iv_enc,
2426 transform->ivlen,
2427 data, rec->data_len,
2428 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002431 return( ret );
2432 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002433
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002438 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002441 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002442 {
2443 /*
2444 * Save IV in SSL3 and TLS1
2445 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2447 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002450#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 {
2452 data -= transform->ivlen;
2453 rec->data_offset -= transform->ivlen;
2454 rec->data_len += transform->ivlen;
2455 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002458 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002459 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002460 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2461
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002462 /*
2463 * MAC(MAC_write_key, seq_num +
2464 * TLSCipherText.type +
2465 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002466 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002467 * IV + // except for TLS 1.0
2468 * ENC(content + padding + padding_length));
2469 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470
2471 if( post_avail < transform->maclen)
2472 {
2473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2474 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2475 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476
Hanno Beckercab87e62019-04-29 13:52:53 +01002477 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002481 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002482
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002483 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002484 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002485 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2486 data, rec->data_len );
2487 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2488 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002489
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002490 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002492 rec->data_len += transform->maclen;
2493 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002494 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002498 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002500 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 /* Make extra sure authentication was performed, exactly once */
2507 if( auth_done != 1 )
2508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002511 }
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
2515 return( 0 );
2516}
2517
Hanno Beckera18d1322018-01-03 14:27:32 +00002518int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2519 mbedtls_ssl_transform *transform,
2520 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002522 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002524 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002525#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002526 size_t padlen = 0, correct = 1;
2527#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002528 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002529 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002530 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002531
Hanno Beckera18d1322018-01-03 14:27:32 +00002532#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002533 ((void) ssl);
2534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002537 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002538 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2541 }
2542 if( rec == NULL ||
2543 rec->buf == NULL ||
2544 rec->buf_len < rec->data_offset ||
2545 rec->buf_len - rec->data_offset < rec->data_len )
2546 {
2547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002549 }
2550
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002551 data = rec->buf + rec->data_offset;
2552 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Hanno Beckercab87e62019-04-29 13:52:53 +01002554#if defined(MBEDTLS_SSL_CID)
2555 /*
2556 * Match record's CID with incoming CID.
2557 */
2558
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002559 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002560 /* if( rec->cid_len != transform->in_cid_len || */
2561 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2562 /* { */
2563 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2564 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002565
2566 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002567 rec->cid_len = transform->in_cid_len;
2568 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2569 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2570#endif /* MBEDTLS_SSL_CID */
2571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2573 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002574 {
2575 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002576 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2577 transform->iv_dec,
2578 transform->ivlen,
2579 data, rec->data_len,
2580 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002583 return( ret );
2584 }
2585
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002586 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 }
Paul Bakker68884e32013-01-07 18:20:04 +01002592 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002594#if defined(MBEDTLS_GCM_C) || \
2595 defined(MBEDTLS_CCM_C) || \
2596 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002598 mode == MBEDTLS_MODE_CCM ||
2599 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002600 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002601 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002602 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002603
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604 /*
2605 * Compute and update sizes
2606 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002607 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002610 "+ taglen (%d)", rec->data_len,
2611 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002613 }
Paul Bakker68884e32013-01-07 18:20:04 +01002614
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 /*
2616 * Prepare IV
2617 */
2618 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2619 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002620 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002622 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002623
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 }
2625 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2626 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002627 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002628 unsigned char i;
2629
2630 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2631
2632 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002634 }
2635 else
2636 {
2637 /* Reminder if we ever add an AEAD mode with a different size */
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2640 }
2641
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002642 data += explicit_iv_len;
2643 rec->data_offset += explicit_iv_len;
2644 rec->data_len -= explicit_iv_len + transform->taglen;
2645
Hanno Beckercab87e62019-04-29 13:52:53 +01002646 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002647 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002648 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649
2650 memcpy( transform->iv_dec + transform->fixed_ivlen,
2651 data - explicit_iv_len, explicit_iv_len );
2652
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002653 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002654 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002655 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002656
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002658 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002659 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002660 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002661 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2662 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002663 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 data, rec->data_len,
2665 data, &olen,
2666 data + rec->data_len,
2667 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2672 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002673
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002674 return( ret );
2675 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002676 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002677
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002682 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2686#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002687 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002690 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002691
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 /*
Paul Bakker45829992013-01-03 14:52:21 +01002693 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002696 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2697 {
2698 /* The ciphertext is prefixed with the CBC IV. */
2699 minlen += transform->ivlen;
2700 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002701#endif
Paul Bakker45829992013-01-03 14:52:21 +01002702
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 /* Size considerations:
2704 *
2705 * - The CBC cipher text must not be empty and hence
2706 * at least of size transform->ivlen.
2707 *
2708 * Together with the potential IV-prefix, this explains
2709 * the first of the two checks below.
2710 *
2711 * - The record must contain a MAC, either in plain or
2712 * encrypted, depending on whether Encrypt-then-MAC
2713 * is used or not.
2714 * - If it is, the message contains the IV-prefix,
2715 * the CBC ciphertext, and the MAC.
2716 * - If it is not, the padded plaintext, and hence
2717 * the CBC ciphertext, has at least length maclen + 1
2718 * because there is at least the padding length byte.
2719 *
2720 * As the CBC ciphertext is not empty, both cases give the
2721 * lower bound minlen + maclen + 1 on the record size, which
2722 * we test for in the second check below.
2723 */
2724 if( rec->data_len < minlen + transform->ivlen ||
2725 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002728 "+ 1 ) ( + expl IV )", rec->data_len,
2729 transform->ivlen,
2730 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002732 }
2733
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002734 /*
2735 * Authenticate before decrypt if enabled
2736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002738 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002739 {
Hanno Becker992b6872017-11-09 18:57:39 +00002740 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002743
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002744 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2745 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002746
Hanno Beckercab87e62019-04-29 13:52:53 +01002747 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002748
Hanno Beckercab87e62019-04-29 13:52:53 +01002749 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2750 add_data_len );
2751 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2752 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002753 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2754 data, rec->data_len );
2755 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2756 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002757
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002758 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2759 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002760 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002762
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002763 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2764 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002769 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002772
2773 /*
2774 * Check length sanity
2775 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002779 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781 }
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002784 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002785 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002786 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002788 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 /* This is safe because data_len >= minlen + maclen + 1 initially,
2790 * and at this point we have at most subtracted maclen (note that
2791 * minlen == transform->ivlen here). */
2792 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002793
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 data += transform->ivlen;
2795 rec->data_offset += transform->ivlen;
2796 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2801 transform->iv_dec, transform->ivlen,
2802 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002805 return( ret );
2806 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002807
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002808 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002812 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002815 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002816 {
2817 /*
2818 * Save IV in SSL3 and TLS1
2819 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002820 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2821 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 /* Safe since data_len >= minlen + maclen + 1, so after having
2826 * subtracted at most minlen and maclen up to this point,
2827 * data_len > 0. */
2828 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002829
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 if( auth_done == 1 )
2831 {
2832 correct *= ( rec->data_len >= padlen + 1 );
2833 padlen *= ( rec->data_len >= padlen + 1 );
2834 }
2835 else
Paul Bakker45829992013-01-03 14:52:21 +01002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 if( rec->data_len < transform->maclen + padlen + 1 )
2839 {
2840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2841 rec->data_len,
2842 transform->maclen,
2843 padlen + 1 ) );
2844 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002845#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846
2847 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2848 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 padlen++;
2852
2853 /* Regardless of the validity of the padding,
2854 * we have data_len >= padlen here. */
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002859 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#if defined(MBEDTLS_SSL_DEBUG_ALL)
2862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 "should be no more than %d",
2864 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002865#endif
Paul Bakker45829992013-01-03 14:52:21 +01002866 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 }
2868 }
2869 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2871#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2872 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002873 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 /* The padding check involves a series of up to 256
2876 * consecutive memory reads at the end of the record
2877 * plaintext buffer. In order to hide the length and
2878 * validity of the padding, always perform exactly
2879 * `min(256,plaintext_len)` reads (but take into account
2880 * only the last `padlen` bytes for the padding check). */
2881 size_t pad_count = 0;
2882 size_t real_count = 0;
2883 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002884
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002885 /* Index of first padding byte; it has been ensured above
2886 * that the subtraction is safe. */
2887 size_t const padding_idx = rec->data_len - padlen;
2888 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2889 size_t const start_idx = rec->data_len - num_checks;
2890 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002891
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002893 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 real_count |= ( idx >= padding_idx );
2895 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002896 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002900 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002902#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002903 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002905 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2907 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002911 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002912
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002913 /* If the padding was found to be invalid, padlen == 0
2914 * and the subtraction is safe. If the padding was found valid,
2915 * padlen hasn't been changed and the previous assertion
2916 * data_len >= padlen still holds. */
2917 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002918 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002919 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002921 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2924 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002925 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002927#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002929 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002930#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
2932 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002933 * Authenticate if not done yet.
2934 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 */
Hanno Becker52344c22018-01-03 15:24:20 +00002936#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002937 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002938 {
Hanno Becker992b6872017-11-09 18:57:39 +00002939 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002940
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002941 /* If the initial value of padlen was such that
2942 * data_len < maclen + padlen + 1, then padlen
2943 * got reset to 1, and the initial check
2944 * data_len >= minlen + maclen + 1
2945 * guarantees that at this point we still
2946 * have at least data_len >= maclen.
2947 *
2948 * If the initial value of padlen was such that
2949 * data_len >= maclen + padlen + 1, then we have
2950 * subtracted either padlen + 1 (if the padding was correct)
2951 * or 0 (if the padding was incorrect) since then,
2952 * hence data_len >= maclen in any case.
2953 */
2954 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Hanno Beckercab87e62019-04-29 13:52:53 +01002956 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002959 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002960 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002961 ssl_mac( &transform->md_ctx_dec,
2962 transform->mac_dec,
2963 data, rec->data_len,
2964 rec->ctr, rec->type,
2965 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002966 }
2967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2970 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002972 {
2973 /*
2974 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002975 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002976 *
2977 * Known timing attacks:
2978 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2979 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002980 * To compensate for different timings for the MAC calculation
2981 * depending on how much padding was removed (which is determined
2982 * by padlen), process extra_run more blocks through the hash
2983 * function.
2984 *
2985 * The formula in the paper is
2986 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2987 * where L1 is the size of the header plus the decrypted message
2988 * plus CBC padding and L2 is the size of the header plus the
2989 * decrypted message. This is for an underlying hash function
2990 * with 64-byte blocks.
2991 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2992 * correctly. We round down instead of up, so -56 is the correct
2993 * value for our calculations instead of -55.
2994 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002995 * Repeat the formula rather than defining a block_size variable.
2996 * This avoids requiring division by a variable at runtime
2997 * (which would be marginally less efficient and would require
2998 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002999 */
3000 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003002
3003 /*
3004 * The next two sizes are the minimum and maximum values of
3005 * in_msglen over all padlen values.
3006 *
3007 * They're independent of padlen, since we previously did
3008 * in_msglen -= padlen.
3009 *
3010 * Note that max_len + maclen is never more than the buffer
3011 * length, as we previously did in_msglen -= maclen too.
3012 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003013 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003014 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3015
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 memset( tmp, 0, sizeof( tmp ) );
3017
3018 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003019 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003020#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3021 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003022 case MBEDTLS_MD_MD5:
3023 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003024 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003025 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003026 extra_run =
3027 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3028 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003029 break;
3030#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003031#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003032 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003033 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003034 extra_run =
3035 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3036 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 break;
3038#endif
3039 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3042 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003043
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003044 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003045
Hanno Beckercab87e62019-04-29 13:52:53 +01003046 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3047 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003048 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3049 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003050 /* Make sure we access everything even when padlen > 0. This
3051 * makes the synchronisation requirements for just-in-time
3052 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003053 ssl_read_memory( data + rec->data_len, padlen );
3054 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003055
3056 /* Call mbedtls_md_process at least once due to cache attacks
3057 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003058 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003060
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003061 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003062
3063 /* Make sure we access all the memory that could contain the MAC,
3064 * before we check it in the next code block. This makes the
3065 * synchronisation requirements for just-in-time Prime+Probe
3066 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 ssl_read_memory( data + min_len,
3068 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003069 }
3070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3072 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003078#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3080 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003083 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3084 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#if defined(MBEDTLS_SSL_DEBUG_ALL)
3087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003088#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003089 correct = 0;
3090 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003091 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003092 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003093
3094 /*
3095 * Finally check the correct flag
3096 */
3097 if( correct == 0 )
3098 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003099#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003100
3101 /* Make extra sure authentication was performed, exactly once */
3102 if( auth_done != 1 )
3103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3105 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003106 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003108#if defined(MBEDTLS_SSL_CID)
3109 if( rec->cid_len != 0 )
3110 {
3111 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3112 &rec->type );
3113 if( ret != 0 )
3114 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3115 }
3116#endif /* MBEDTLS_SSL_CID */
3117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
3120 return( 0 );
3121}
3122
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003123#undef MAC_NONE
3124#undef MAC_PLAINTEXT
3125#undef MAC_CIPHERTEXT
3126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003128/*
3129 * Compression/decompression functions
3130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003132{
3133 int ret;
3134 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003135 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003136 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003137 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003140
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003141 if( len_pre == 0 )
3142 return( 0 );
3143
Paul Bakker2770fbd2012-07-03 13:30:23 +00003144 memcpy( msg_pre, ssl->out_msg, len_pre );
3145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003147 ssl->out_msglen ) );
3148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150 ssl->out_msg, ssl->out_msglen );
3151
Paul Bakker48916f92012-09-16 19:57:18 +00003152 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3153 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3154 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003155 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156
Paul Bakker48916f92012-09-16 19:57:18 +00003157 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158 if( ret != Z_OK )
3159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3161 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162 }
3163
Angus Grattond8213d02016-05-25 20:56:48 +10003164 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003165 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 ssl->out_msglen ) );
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003171 ssl->out_msg, ssl->out_msglen );
3172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174
3175 return( 0 );
3176}
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179{
3180 int ret;
3181 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003182 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003184 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003188 if( len_pre == 0 )
3189 return( 0 );
3190
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191 memcpy( msg_pre, ssl->in_msg, len_pre );
3192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194 ssl->in_msglen ) );
3195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197 ssl->in_msg, ssl->in_msglen );
3198
Paul Bakker48916f92012-09-16 19:57:18 +00003199 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3200 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3201 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003202 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003203 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204
Paul Bakker48916f92012-09-16 19:57:18 +00003205 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003206 if( ret != Z_OK )
3207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3209 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210 }
3211
Angus Grattond8213d02016-05-25 20:56:48 +10003212 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003213 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216 ssl->in_msglen ) );
3217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003219 ssl->in_msg, ssl->in_msglen );
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222
3223 return( 0 );
3224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3228static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#if defined(MBEDTLS_SSL_PROTO_DTLS)
3231static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003232{
3233 /* If renegotiation is not enforced, retransmit until we would reach max
3234 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003235 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003236 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003237 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003238 unsigned char doublings = 1;
3239
3240 while( ratio != 0 )
3241 {
3242 ++doublings;
3243 ratio >>= 1;
3244 }
3245
3246 if( ++ssl->renego_records_seen > doublings )
3247 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003249 return( 0 );
3250 }
3251 }
3252
3253 return( ssl_write_hello_request( ssl ) );
3254}
3255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003257
Paul Bakker5121ce52009-01-03 21:22:43 +00003258/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003259 * Fill the input message buffer by appending data to it.
3260 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003261 *
3262 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3263 * available (from this read and/or a previous one). Otherwise, an error code
3264 * is returned (possibly EOF or WANT_READ).
3265 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003266 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3267 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3268 * since we always read a whole datagram at once.
3269 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003270 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003271 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003274{
Paul Bakker23986e52011-04-24 08:57:21 +00003275 int ret;
3276 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003279
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003280 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003283 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003285 }
3286
Angus Grattond8213d02016-05-25 20:56:48 +10003287 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003291 }
3292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003296 uint32_t timeout;
3297
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003298 /* Just to be sure */
3299 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3300 {
3301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3302 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3304 }
3305
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003306 /*
3307 * The point is, we need to always read a full datagram at once, so we
3308 * sometimes read more then requested, and handle the additional data.
3309 * It could be the rest of the current record (while fetching the
3310 * header) and/or some other records in the same datagram.
3311 */
3312
3313 /*
3314 * Move to the next record in the already read datagram if applicable
3315 */
3316 if( ssl->next_record_offset != 0 )
3317 {
3318 if( ssl->in_left < ssl->next_record_offset )
3319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003322 }
3323
3324 ssl->in_left -= ssl->next_record_offset;
3325
3326 if( ssl->in_left != 0 )
3327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003329 ssl->next_record_offset ) );
3330 memmove( ssl->in_hdr,
3331 ssl->in_hdr + ssl->next_record_offset,
3332 ssl->in_left );
3333 }
3334
3335 ssl->next_record_offset = 0;
3336 }
3337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003340
3341 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003342 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003343 */
3344 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003347 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003348 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003349
3350 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003351 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003352 * are not at the beginning of a new record, the caller did something
3353 * wrong.
3354 */
3355 if( ssl->in_left != 0 )
3356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003359 }
3360
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003361 /*
3362 * Don't even try to read if time's out already.
3363 * This avoids by-passing the timer when repeatedly receiving messages
3364 * that will end up being dropped.
3365 */
3366 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003367 {
3368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003369 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003370 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003371 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003372 {
Angus Grattond8213d02016-05-25 20:56:48 +10003373 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003376 timeout = ssl->handshake->retransmit_timeout;
3377 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003378 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003381
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003382 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003383 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3384 timeout );
3385 else
3386 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003389
3390 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392 }
3393
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003394 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003400 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003401 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003404 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003405 }
3406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003410 return( ret );
3411 }
3412
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003413 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003414 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003416 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003418 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003419 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003422 return( ret );
3423 }
3424
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003425 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003428 }
3429
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 if( ret < 0 )
3431 return( ret );
3432
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003433 ssl->in_left = ret;
3434 }
3435 else
3436#endif
3437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003439 ssl->in_left, nb_want ) );
3440
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003441 while( ssl->in_left < nb_want )
3442 {
3443 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003444
3445 if( ssl_check_timer( ssl ) != 0 )
3446 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3447 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003448 {
3449 if( ssl->f_recv_timeout != NULL )
3450 {
3451 ret = ssl->f_recv_timeout( ssl->p_bio,
3452 ssl->in_hdr + ssl->in_left, len,
3453 ssl->conf->read_timeout );
3454 }
3455 else
3456 {
3457 ret = ssl->f_recv( ssl->p_bio,
3458 ssl->in_hdr + ssl->in_left, len );
3459 }
3460 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003463 ssl->in_left, nb_want ) );
3464 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003465
3466 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003468
3469 if( ret < 0 )
3470 return( ret );
3471
mohammad160352aecb92018-03-28 23:41:40 -07003472 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003473 {
Darryl Green11999bb2018-03-13 15:22:58 +00003474 MBEDTLS_SSL_DEBUG_MSG( 1,
3475 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003476 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3478 }
3479
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003480 ssl->in_left += ret;
3481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 }
3483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
3486 return( 0 );
3487}
3488
3489/*
3490 * Flush any data not yet written
3491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003493{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003494 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003495 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003498
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003499 if( ssl->f_send == NULL )
3500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003502 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003504 }
3505
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003506 /* Avoid incrementing counter if data is flushed */
3507 if( ssl->out_left == 0 )
3508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003510 return( 0 );
3511 }
3512
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 while( ssl->out_left > 0 )
3514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3516 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
Hanno Becker2b1e3542018-08-06 11:19:13 +01003518 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003519 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
3523 if( ret <= 0 )
3524 return( ret );
3525
mohammad160352aecb92018-03-28 23:41:40 -07003526 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003527 {
Darryl Green11999bb2018-03-13 15:22:58 +00003528 MBEDTLS_SSL_DEBUG_MSG( 1,
3529 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003530 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3532 }
3533
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 ssl->out_left -= ret;
3535 }
3536
Hanno Becker2b1e3542018-08-06 11:19:13 +01003537#if defined(MBEDTLS_SSL_PROTO_DTLS)
3538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003539 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003540 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003541 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003542 else
3543#endif
3544 {
3545 ssl->out_hdr = ssl->out_buf + 8;
3546 }
3547 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
3551 return( 0 );
3552}
3553
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003554/*
3555 * Functions to handle the DTLS retransmission state machine
3556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003558/*
3559 * Append current handshake message to current outgoing flight
3560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3565 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3566 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003567
3568 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003569 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003570 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003573 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003574 }
3575
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003576 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003580 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 }
3582
3583 /* Copy current handshake message with headers */
3584 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3585 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003586 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 msg->next = NULL;
3588
3589 /* Append to the current flight */
3590 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003591 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 else
3593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003595 while( cur->next != NULL )
3596 cur = cur->next;
3597 cur->next = msg;
3598 }
3599
Hanno Becker3b235902018-08-06 09:54:53 +01003600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601 return( 0 );
3602}
3603
3604/*
3605 * Free the current flight of handshake messages
3606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 mbedtls_ssl_flight_item *cur = flight;
3610 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611
3612 while( cur != NULL )
3613 {
3614 next = cur->next;
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 mbedtls_free( cur->p );
3617 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618
3619 cur = next;
3620 }
3621}
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3624static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003625#endif
3626
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003627/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003628 * Swap transform_out and out_ctr with the alternative ones
3629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003633 unsigned char tmp_out_ctr[8];
3634
3635 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003638 return;
3639 }
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003642
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003643 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003644 tmp_transform = ssl->transform_out;
3645 ssl->transform_out = ssl->handshake->alt_transform_out;
3646 ssl->handshake->alt_transform_out = tmp_transform;
3647
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003648 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003649 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3650 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003651 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003652
3653 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003654 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3657 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3662 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003663 }
3664 }
3665#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003666}
3667
3668/*
3669 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003670 */
3671int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3672{
3673 int ret = 0;
3674
3675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3676
3677 ret = mbedtls_ssl_flight_transmit( ssl );
3678
3679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3680
3681 return( ret );
3682}
3683
3684/*
3685 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003686 *
3687 * Need to remember the current message in case flush_output returns
3688 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003689 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003690 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003691int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003693 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003697 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003699
3700 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003701 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003702 ssl_swap_epochs( ssl );
3703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003705 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003706
3707 while( ssl->handshake->cur_msg != NULL )
3708 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003709 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003710 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003711
Hanno Beckere1dcb032018-08-17 16:47:58 +01003712 int const is_finished =
3713 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3714 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3715
Hanno Becker04da1892018-08-14 13:22:10 +01003716 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3717 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3718
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003719 /* Swap epochs before sending Finished: we can't do it after
3720 * sending ChangeCipherSpec, in case write returns WANT_READ.
3721 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003722 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003723 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003725 ssl_swap_epochs( ssl );
3726 }
3727
Hanno Becker67bc7c32018-08-06 11:33:50 +01003728 ret = ssl_get_remaining_payload_in_datagram( ssl );
3729 if( ret < 0 )
3730 return( ret );
3731 max_frag_len = (size_t) ret;
3732
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003733 /* CCS is copied as is, while HS messages may need fragmentation */
3734 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3735 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003736 if( max_frag_len == 0 )
3737 {
3738 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3739 return( ret );
3740
3741 continue;
3742 }
3743
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003744 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003745 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003746 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003747
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003748 /* Update position inside current message */
3749 ssl->handshake->cur_msg_p += cur->len;
3750 }
3751 else
3752 {
3753 const unsigned char * const p = ssl->handshake->cur_msg_p;
3754 const size_t hs_len = cur->len - 12;
3755 const size_t frag_off = p - ( cur->p + 12 );
3756 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003757 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003758
Hanno Beckere1dcb032018-08-17 16:47:58 +01003759 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003760 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003761 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003763
Hanno Becker67bc7c32018-08-06 11:33:50 +01003764 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3765 return( ret );
3766
3767 continue;
3768 }
3769 max_hs_frag_len = max_frag_len - 12;
3770
3771 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3772 max_hs_frag_len : rem_len;
3773
3774 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003775 {
3776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003777 (unsigned) cur_hs_frag_len,
3778 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003779 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003780
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003781 /* Messages are stored with handshake headers as if not fragmented,
3782 * copy beginning of headers then fill fragmentation fields.
3783 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3784 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003785
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003786 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3787 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3788 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3789
Hanno Becker67bc7c32018-08-06 11:33:50 +01003790 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3791 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3792 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003793
3794 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3795
Hanno Becker3f7b9732018-08-28 09:53:25 +01003796 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003797 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3798 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003799 ssl->out_msgtype = cur->type;
3800
3801 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003802 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003803 }
3804
3805 /* If done with the current message move to the next one if any */
3806 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3807 {
3808 if( cur->next != NULL )
3809 {
3810 ssl->handshake->cur_msg = cur->next;
3811 ssl->handshake->cur_msg_p = cur->next->p + 12;
3812 }
3813 else
3814 {
3815 ssl->handshake->cur_msg = NULL;
3816 ssl->handshake->cur_msg_p = NULL;
3817 }
3818 }
3819
3820 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003821 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003824 return( ret );
3825 }
3826 }
3827
Hanno Becker67bc7c32018-08-06 11:33:50 +01003828 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3829 return( ret );
3830
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003831 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3833 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003834 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003837 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3838 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003839
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003841
3842 return( 0 );
3843}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003844
3845/*
3846 * To be called when the last message of an incoming flight is received.
3847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003849{
3850 /* We won't need to resend that one any more */
3851 ssl_flight_free( ssl->handshake->flight );
3852 ssl->handshake->flight = NULL;
3853 ssl->handshake->cur_msg = NULL;
3854
3855 /* The next incoming flight will start with this msg_seq */
3856 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3857
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003858 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003859 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003860
Hanno Becker0271f962018-08-16 13:23:47 +01003861 /* Clear future message buffering structure. */
3862 ssl_buffering_free( ssl );
3863
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003864 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003865 ssl_set_timer( ssl, 0 );
3866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3868 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003871 }
3872 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003874}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003875
3876/*
3877 * To be called when the last message of an outgoing flight is send.
3878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003880{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003881 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003882 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3885 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003888 }
3889 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003893
Paul Bakker5121ce52009-01-03 21:22:43 +00003894/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003895 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003897
3898/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003899 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003900 *
3901 * - fill in handshake headers
3902 * - update handshake checksum
3903 * - DTLS: save message for resending
3904 * - then pass to the record layer
3905 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003906 * DTLS: except for HelloRequest, messages are only queued, and will only be
3907 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003908 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003909 * Inputs:
3910 * - ssl->out_msglen: 4 + actual handshake message len
3911 * (4 is the size of handshake headers for TLS)
3912 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3913 * - ssl->out_msg + 4: the handshake message body
3914 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003915 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003916 * - ssl->out_msglen: the length of the record contents
3917 * (including handshake headers but excluding record headers)
3918 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003919 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003920int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003921{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003922 int ret;
3923 const size_t hs_len = ssl->out_msglen - 4;
3924 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3927
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003928 /*
3929 * Sanity checks
3930 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003931 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003932 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3933 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003934 /* In SSLv3, the client might send a NoCertificate alert. */
3935#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3936 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3937 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3938 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3939#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3940 {
3941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3942 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3943 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003946 /* Whenever we send anything different from a
3947 * HelloRequest we should be in a handshake - double check. */
3948 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3949 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003950 ssl->handshake == NULL )
3951 {
3952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003958 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003960 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003963 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003964#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003965
Hanno Beckerb50a2532018-08-06 11:52:54 +01003966 /* Double-check that we did not exceed the bounds
3967 * of the outgoing record buffer.
3968 * This should never fail as the various message
3969 * writing functions must obey the bounds of the
3970 * outgoing record buffer, but better be safe.
3971 *
3972 * Note: We deliberately do not check for the MTU or MFL here.
3973 */
3974 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3975 {
3976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3977 "size %u, maximum %u",
3978 (unsigned) ssl->out_msglen,
3979 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3981 }
3982
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003983 /*
3984 * Fill handshake headers
3985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003988 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3989 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3990 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003992 /*
3993 * DTLS has additional fields in the Handshake layer,
3994 * between the length field and the actual payload:
3995 * uint16 message_seq;
3996 * uint24 fragment_offset;
3997 * uint24 fragment_length;
3998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004001 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004002 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004003 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004004 {
4005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4006 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004007 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004008 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4010 }
4011
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004012 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004013 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004014
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004015 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004016 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004017 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004018 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4019 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4020 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004021 }
4022 else
4023 {
4024 ssl->out_msg[4] = 0;
4025 ssl->out_msg[5] = 0;
4026 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004027
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004028 /* Handshake hashes are computed without fragmentation,
4029 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004030 memset( ssl->out_msg + 6, 0x00, 3 );
4031 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004034
Hanno Becker0207e532018-08-28 10:28:28 +01004035 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004036 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4037 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 }
4039
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004040 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004042 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004043 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4044 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004045 {
4046 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 return( ret );
4050 }
4051 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004052 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004054 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004055 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004056 {
4057 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4058 return( ret );
4059 }
4060 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004061
4062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4063
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004064 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004065}
4066
4067/*
4068 * Record layer functions
4069 */
4070
4071/*
4072 * Write current record.
4073 *
4074 * Uses:
4075 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4076 * - ssl->out_msglen: length of the record content (excl headers)
4077 * - ssl->out_msg: record content
4078 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004079int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004080{
4081 int ret, done = 0;
4082 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004083 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004084
4085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004088 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004090 {
4091 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004094 return( ret );
4095 }
4096
4097 len = ssl->out_msglen;
4098 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4102 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 ret = mbedtls_ssl_hw_record_write( ssl );
4107 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004111 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004112
4113 if( ret == 0 )
4114 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004117 if( !done )
4118 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004119 unsigned i;
4120 size_t protected_record_size;
4121
Paul Bakker05ef8352012-05-08 09:17:57 +00004122 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004124 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004125
Hanno Becker19859472018-08-06 09:40:20 +01004126 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004127 ssl->out_len[0] = (unsigned char)( len >> 8 );
4128 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004129
Paul Bakker48916f92012-09-16 19:57:18 +00004130 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004131 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004132 mbedtls_record rec;
4133
4134 rec.buf = ssl->out_iv;
4135 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4136 ( ssl->out_iv - ssl->out_buf );
4137 rec.data_len = ssl->out_msglen;
4138 rec.data_offset = ssl->out_msg - rec.buf;
4139
4140 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4141 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4142 ssl->conf->transport, rec.ver );
4143 rec.type = ssl->out_msgtype;
4144
Hanno Becker43c24b82019-05-01 09:45:57 +01004145#if defined(MBEDTLS_SSL_CID)
4146 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004147 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004148#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004149
Hanno Beckera18d1322018-01-03 14:27:32 +00004150 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004151 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004154 return( ret );
4155 }
4156
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004157 if( rec.data_offset != 0 )
4158 {
4159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4161 }
4162
Hanno Becker78f839d2019-03-14 12:56:23 +00004163 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004164 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4165 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004166 }
4167
Hanno Becker2b1e3542018-08-06 11:19:13 +01004168 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4169
4170#if defined(MBEDTLS_SSL_PROTO_DTLS)
4171 /* In case of DTLS, double-check that we don't exceed
4172 * the remaining space in the datagram. */
4173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4174 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004175 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004176 if( ret < 0 )
4177 return( ret );
4178
4179 if( protected_record_size > (size_t) ret )
4180 {
4181 /* Should never happen */
4182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4183 }
4184 }
4185#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004188 "version = [%d:%d], msglen = %d",
4189 ssl->out_hdr[0], ssl->out_hdr[1],
4190 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004193 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004194
4195 ssl->out_left += protected_record_size;
4196 ssl->out_hdr += protected_record_size;
4197 ssl_update_out_pointers( ssl, ssl->transform_out );
4198
Hanno Becker04484622018-08-06 09:49:38 +01004199 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4200 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4201 break;
4202
4203 /* The loop goes to its end iff the counter is wrapping */
4204 if( i == ssl_ep_len( ssl ) )
4205 {
4206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4207 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4208 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004209 }
4210
Hanno Becker67bc7c32018-08-06 11:33:50 +01004211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4213 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004214 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004215 size_t remaining;
4216 ret = ssl_get_remaining_payload_in_datagram( ssl );
4217 if( ret < 0 )
4218 {
4219 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4220 ret );
4221 return( ret );
4222 }
4223
4224 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004226 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004227 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004228 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004229 else
4230 {
Hanno Becker513815a2018-08-20 11:56:09 +01004231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004232 }
4233 }
4234#endif /* MBEDTLS_SSL_PROTO_DTLS */
4235
4236 if( ( flush == SSL_FORCE_FLUSH ) &&
4237 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240 return( ret );
4241 }
4242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004244
4245 return( 0 );
4246}
4247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004249
4250static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4251{
4252 if( ssl->in_msglen < ssl->in_hslen ||
4253 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4254 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4255 {
4256 return( 1 );
4257 }
4258 return( 0 );
4259}
Hanno Becker44650b72018-08-16 12:51:11 +01004260
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004261static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004262{
4263 return( ( ssl->in_msg[9] << 16 ) |
4264 ( ssl->in_msg[10] << 8 ) |
4265 ssl->in_msg[11] );
4266}
4267
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004268static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004269{
4270 return( ( ssl->in_msg[6] << 16 ) |
4271 ( ssl->in_msg[7] << 8 ) |
4272 ssl->in_msg[8] );
4273}
4274
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004275static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004276{
4277 uint32_t msg_len, frag_off, frag_len;
4278
4279 msg_len = ssl_get_hs_total_len( ssl );
4280 frag_off = ssl_get_hs_frag_off( ssl );
4281 frag_len = ssl_get_hs_frag_len( ssl );
4282
4283 if( frag_off > msg_len )
4284 return( -1 );
4285
4286 if( frag_len > msg_len - frag_off )
4287 return( -1 );
4288
4289 if( frag_len + 12 > ssl->in_msglen )
4290 return( -1 );
4291
4292 return( 0 );
4293}
4294
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004295/*
4296 * Mark bits in bitmask (used for DTLS HS reassembly)
4297 */
4298static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4299{
4300 unsigned int start_bits, end_bits;
4301
4302 start_bits = 8 - ( offset % 8 );
4303 if( start_bits != 8 )
4304 {
4305 size_t first_byte_idx = offset / 8;
4306
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004307 /* Special case */
4308 if( len <= start_bits )
4309 {
4310 for( ; len != 0; len-- )
4311 mask[first_byte_idx] |= 1 << ( start_bits - len );
4312
4313 /* Avoid potential issues with offset or len becoming invalid */
4314 return;
4315 }
4316
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004317 offset += start_bits; /* Now offset % 8 == 0 */
4318 len -= start_bits;
4319
4320 for( ; start_bits != 0; start_bits-- )
4321 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4322 }
4323
4324 end_bits = len % 8;
4325 if( end_bits != 0 )
4326 {
4327 size_t last_byte_idx = ( offset + len ) / 8;
4328
4329 len -= end_bits; /* Now len % 8 == 0 */
4330
4331 for( ; end_bits != 0; end_bits-- )
4332 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4333 }
4334
4335 memset( mask + offset / 8, 0xFF, len / 8 );
4336}
4337
4338/*
4339 * Check that bitmask is full
4340 */
4341static int ssl_bitmask_check( unsigned char *mask, size_t len )
4342{
4343 size_t i;
4344
4345 for( i = 0; i < len / 8; i++ )
4346 if( mask[i] != 0xFF )
4347 return( -1 );
4348
4349 for( i = 0; i < len % 8; i++ )
4350 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4351 return( -1 );
4352
4353 return( 0 );
4354}
4355
Hanno Becker56e205e2018-08-16 09:06:12 +01004356/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004357static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004358 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004359{
Hanno Becker56e205e2018-08-16 09:06:12 +01004360 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004361
Hanno Becker56e205e2018-08-16 09:06:12 +01004362 alloc_len = 12; /* Handshake header */
4363 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004364
Hanno Beckerd07df862018-08-16 09:14:58 +01004365 if( add_bitmap )
4366 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004367
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004368 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004369}
Hanno Becker56e205e2018-08-16 09:06:12 +01004370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004372
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004373static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004374{
4375 return( ( ssl->in_msg[1] << 16 ) |
4376 ( ssl->in_msg[2] << 8 ) |
4377 ssl->in_msg[3] );
4378}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004379
Simon Butcher99000142016-10-13 17:21:01 +01004380int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004385 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004387 }
4388
Hanno Becker12555c62018-08-16 12:47:53 +01004389 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004392 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004393 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004397 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004398 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004399 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004400
Hanno Becker44650b72018-08-16 12:51:11 +01004401 if( ssl_check_hs_header( ssl ) != 0 )
4402 {
4403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4405 }
4406
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004407 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004408 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4409 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4410 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4411 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004412 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004413 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4414 {
4415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4416 recv_msg_seq,
4417 ssl->handshake->in_msg_seq ) );
4418 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4419 }
4420
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004421 /* Retransmit only on last message from previous flight, to avoid
4422 * too many retransmissions.
4423 * Besides, No sane server ever retransmits HelloVerifyRequest */
4424 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004428 "message_seq = %d, start_of_flight = %d",
4429 recv_msg_seq,
4430 ssl->handshake->in_flight_start_seq ) );
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004435 return( ret );
4436 }
4437 }
4438 else
4439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004441 "message_seq = %d, expected = %d",
4442 recv_msg_seq,
4443 ssl->handshake->in_msg_seq ) );
4444 }
4445
Hanno Becker90333da2017-10-10 11:27:13 +01004446 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004447 }
4448 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004449
Hanno Becker6d97ef52018-08-16 13:09:04 +01004450 /* Message reassembly is handled alongside buffering of future
4451 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004452 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004453 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004454 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004457 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004458 }
4459 }
4460 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004462 /* With TLS we don't handle fragmentation (for now) */
4463 if( ssl->in_msglen < ssl->in_hslen )
4464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4466 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004467 }
4468
Simon Butcher99000142016-10-13 17:21:01 +01004469 return( 0 );
4470}
4471
4472void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4473{
Hanno Becker0271f962018-08-16 13:23:47 +01004474 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004475
Hanno Becker0271f962018-08-16 13:23:47 +01004476 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004477 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004478 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004479 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004480
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004481 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004484 ssl->handshake != NULL )
4485 {
Hanno Becker0271f962018-08-16 13:23:47 +01004486 unsigned offset;
4487 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004488
Hanno Becker0271f962018-08-16 13:23:47 +01004489 /* Increment handshake sequence number */
4490 hs->in_msg_seq++;
4491
4492 /*
4493 * Clear up handshake buffering and reassembly structure.
4494 */
4495
4496 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004497 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004498
4499 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004500 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4501 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004502 offset++, hs_buf++ )
4503 {
4504 *hs_buf = *(hs_buf + 1);
4505 }
4506
4507 /* Create a fresh last entry */
4508 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004509 }
4510#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004511}
4512
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004513/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004514 * DTLS anti-replay: RFC 6347 4.1.2.6
4515 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004516 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4517 * Bit n is set iff record number in_window_top - n has been seen.
4518 *
4519 * Usually, in_window_top is the last record number seen and the lsb of
4520 * in_window is set. The only exception is the initial state (record number 0
4521 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4524static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004525{
4526 ssl->in_window_top = 0;
4527 ssl->in_window = 0;
4528}
4529
4530static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4531{
4532 return( ( (uint64_t) buf[0] << 40 ) |
4533 ( (uint64_t) buf[1] << 32 ) |
4534 ( (uint64_t) buf[2] << 24 ) |
4535 ( (uint64_t) buf[3] << 16 ) |
4536 ( (uint64_t) buf[4] << 8 ) |
4537 ( (uint64_t) buf[5] ) );
4538}
4539
4540/*
4541 * Return 0 if sequence number is acceptable, -1 otherwise
4542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004544{
4545 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4546 uint64_t bit;
4547
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004548 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004549 return( 0 );
4550
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004551 if( rec_seqnum > ssl->in_window_top )
4552 return( 0 );
4553
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004554 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004555
4556 if( bit >= 64 )
4557 return( -1 );
4558
4559 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4560 return( -1 );
4561
4562 return( 0 );
4563}
4564
4565/*
4566 * Update replay window on new validated record
4567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004569{
4570 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4571
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004572 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004573 return;
4574
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575 if( rec_seqnum > ssl->in_window_top )
4576 {
4577 /* Update window_top and the contents of the window */
4578 uint64_t shift = rec_seqnum - ssl->in_window_top;
4579
4580 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004581 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004582 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004583 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004584 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004585 ssl->in_window |= 1;
4586 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587
4588 ssl->in_window_top = rec_seqnum;
4589 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004590 else
4591 {
4592 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004593 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004594
4595 if( bit < 64 ) /* Always true, but be extra sure */
4596 ssl->in_window |= (uint64_t) 1 << bit;
4597 }
4598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004600
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004601#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004602/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004603static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4604
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004605/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004606 * Without any SSL context, check if a datagram looks like a ClientHello with
4607 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004608 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004609 *
4610 * - if cookie is valid, return 0
4611 * - if ClientHello looks superficially valid but cookie is not,
4612 * fill obuf and set olen, then
4613 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4614 * - otherwise return a specific error code
4615 */
4616static int ssl_check_dtls_clihlo_cookie(
4617 mbedtls_ssl_cookie_write_t *f_cookie_write,
4618 mbedtls_ssl_cookie_check_t *f_cookie_check,
4619 void *p_cookie,
4620 const unsigned char *cli_id, size_t cli_id_len,
4621 const unsigned char *in, size_t in_len,
4622 unsigned char *obuf, size_t buf_len, size_t *olen )
4623{
4624 size_t sid_len, cookie_len;
4625 unsigned char *p;
4626
4627 if( f_cookie_write == NULL || f_cookie_check == NULL )
4628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4629
4630 /*
4631 * Structure of ClientHello with record and handshake headers,
4632 * and expected values. We don't need to check a lot, more checks will be
4633 * done when actually parsing the ClientHello - skipping those checks
4634 * avoids code duplication and does not make cookie forging any easier.
4635 *
4636 * 0-0 ContentType type; copied, must be handshake
4637 * 1-2 ProtocolVersion version; copied
4638 * 3-4 uint16 epoch; copied, must be 0
4639 * 5-10 uint48 sequence_number; copied
4640 * 11-12 uint16 length; (ignored)
4641 *
4642 * 13-13 HandshakeType msg_type; (ignored)
4643 * 14-16 uint24 length; (ignored)
4644 * 17-18 uint16 message_seq; copied
4645 * 19-21 uint24 fragment_offset; copied, must be 0
4646 * 22-24 uint24 fragment_length; (ignored)
4647 *
4648 * 25-26 ProtocolVersion client_version; (ignored)
4649 * 27-58 Random random; (ignored)
4650 * 59-xx SessionID session_id; 1 byte len + sid_len content
4651 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4652 * ...
4653 *
4654 * Minimum length is 61 bytes.
4655 */
4656 if( in_len < 61 ||
4657 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4658 in[3] != 0 || in[4] != 0 ||
4659 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4660 {
4661 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4662 }
4663
4664 sid_len = in[59];
4665 if( sid_len > in_len - 61 )
4666 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4667
4668 cookie_len = in[60 + sid_len];
4669 if( cookie_len > in_len - 60 )
4670 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4671
4672 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4673 cli_id, cli_id_len ) == 0 )
4674 {
4675 /* Valid cookie */
4676 return( 0 );
4677 }
4678
4679 /*
4680 * If we get here, we've got an invalid cookie, let's prepare HVR.
4681 *
4682 * 0-0 ContentType type; copied
4683 * 1-2 ProtocolVersion version; copied
4684 * 3-4 uint16 epoch; copied
4685 * 5-10 uint48 sequence_number; copied
4686 * 11-12 uint16 length; olen - 13
4687 *
4688 * 13-13 HandshakeType msg_type; hello_verify_request
4689 * 14-16 uint24 length; olen - 25
4690 * 17-18 uint16 message_seq; copied
4691 * 19-21 uint24 fragment_offset; copied
4692 * 22-24 uint24 fragment_length; olen - 25
4693 *
4694 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4695 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4696 *
4697 * Minimum length is 28.
4698 */
4699 if( buf_len < 28 )
4700 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4701
4702 /* Copy most fields and adapt others */
4703 memcpy( obuf, in, 25 );
4704 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4705 obuf[25] = 0xfe;
4706 obuf[26] = 0xff;
4707
4708 /* Generate and write actual cookie */
4709 p = obuf + 28;
4710 if( f_cookie_write( p_cookie,
4711 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4712 {
4713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4714 }
4715
4716 *olen = p - obuf;
4717
4718 /* Go back and fill length fields */
4719 obuf[27] = (unsigned char)( *olen - 28 );
4720
4721 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4722 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4723 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4724
4725 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4726 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4727
4728 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4729}
4730
4731/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004732 * Handle possible client reconnect with the same UDP quadruplet
4733 * (RFC 6347 Section 4.2.8).
4734 *
4735 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4736 * that looks like a ClientHello.
4737 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004738 * - if the input looks like a ClientHello without cookies,
4739 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004740 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004741 * - if the input looks like a ClientHello with a valid cookie,
4742 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004743 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004744 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004745 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004746 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004747 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4748 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004749 */
4750static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4751{
4752 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004753 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004754
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004755 ret = ssl_check_dtls_clihlo_cookie(
4756 ssl->conf->f_cookie_write,
4757 ssl->conf->f_cookie_check,
4758 ssl->conf->p_cookie,
4759 ssl->cli_id, ssl->cli_id_len,
4760 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004761 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004762
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004763 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4764
4765 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004767 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004768 * If the error is permanent we'll catch it later,
4769 * if it's not, then hopefully it'll work next time. */
4770 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4771
4772 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004773 }
4774
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004775 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004776 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004777 /* Got a valid cookie, partially reset context */
4778 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4779 {
4780 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4781 return( ret );
4782 }
4783
4784 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004785 }
4786
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004787 return( ret );
4788}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004789#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004791/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004792 * ContentType type;
4793 * ProtocolVersion version;
4794 * uint16 epoch; // DTLS only
4795 * uint48 sequence_number; // DTLS only
4796 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004797 *
4798 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004799 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004800 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4801 *
4802 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004803 * 1. proceed with the record if this function returns 0
4804 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4805 * 3. return CLIENT_RECONNECT if this function return that value
4806 * 4. drop the whole datagram if this function returns anything else.
4807 * Point 2 is needed when the peer is resending, and we have already received
4808 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004811{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004812 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 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 +02004815
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004817 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004818 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004822 ssl->in_msgtype,
4823 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004824
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004825 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4827 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4828 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4829 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004832
4833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004834 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4835 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004836 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4837#endif /* MBEDTLS_SSL_PROTO_DTLS */
4838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4839 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004842 }
4843
4844 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004845 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4848 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 }
4850
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004851 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4854 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 }
4856
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004857 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004858 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004859 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4862 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004863 }
4864
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004865 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004866 * DTLS-related tests.
4867 * Check epoch before checking length constraint because
4868 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4869 * message gets duplicated before the corresponding Finished message,
4870 * the second ChangeCipherSpec should be discarded because it belongs
4871 * to an old epoch, but not because its length is shorter than
4872 * the minimum record length for packets using the new record transform.
4873 * Note that these two kinds of failures are handled differently,
4874 * as an unexpected record is silently skipped but an invalid
4875 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004876 */
4877#if defined(MBEDTLS_SSL_PROTO_DTLS)
4878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4879 {
4880 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4881
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004882 /* Check epoch (and sequence number) with DTLS */
4883 if( rec_epoch != ssl->in_epoch )
4884 {
4885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4886 "expected %d, received %d",
4887 ssl->in_epoch, rec_epoch ) );
4888
4889#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4890 /*
4891 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4892 * access the first byte of record content (handshake type), as we
4893 * have an active transform (possibly iv_len != 0), so use the
4894 * fact that the record header len is 13 instead.
4895 */
4896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4897 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4898 rec_epoch == 0 &&
4899 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4900 ssl->in_left > 13 &&
4901 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4902 {
4903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4904 "from the same port" ) );
4905 return( ssl_handle_possible_reconnect( ssl ) );
4906 }
4907 else
4908#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004909 {
4910 /* Consider buffering the record. */
4911 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4912 {
4913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4914 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4915 }
4916
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004917 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004918 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004919 }
4920
4921#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4922 /* Replay detection only works for the current epoch */
4923 if( rec_epoch == ssl->in_epoch &&
4924 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4927 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4928 }
4929#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004930
Hanno Becker52c6dc62017-05-26 16:07:36 +01004931 /* Drop unexpected ApplicationData records,
4932 * except at the beginning of renegotiations */
4933 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4934 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4935#if defined(MBEDTLS_SSL_RENEGOTIATION)
4936 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4937 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4938#endif
4939 )
4940 {
4941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4942 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4943 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004944 }
4945#endif /* MBEDTLS_SSL_PROTO_DTLS */
4946
Hanno Becker52c6dc62017-05-26 16:07:36 +01004947
4948 /* Check length against bounds of the current transform and version */
4949 if( ssl->transform_in == NULL )
4950 {
4951 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004952 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004953 {
4954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4955 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4956 }
4957 }
4958 else
4959 {
4960 if( ssl->in_msglen < ssl->transform_in->minlen )
4961 {
4962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4963 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4964 }
4965
4966#if defined(MBEDTLS_SSL_PROTO_SSL3)
4967 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004968 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004969 {
4970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4971 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4972 }
4973#endif
4974#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4975 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4976 /*
4977 * TLS encrypted messages can have up to 256 bytes of padding
4978 */
4979 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4980 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004981 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004982 {
4983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4984 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4985 }
4986#endif
4987 }
4988
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004989 return( 0 );
4990}
Paul Bakker5121ce52009-01-03 21:22:43 +00004991
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004992/*
4993 * If applicable, decrypt (and decompress) record content
4994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004996{
4997 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
5000 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5003 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 ret = mbedtls_ssl_hw_record_read( ssl );
5008 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5011 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005012 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005013
5014 if( ret == 0 )
5015 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005016 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005018 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005019 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005020 mbedtls_record rec;
5021
5022 rec.buf = ssl->in_iv;
5023 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5024 - ( ssl->in_iv - ssl->in_buf );
5025 rec.data_len = ssl->in_msglen;
5026 rec.data_offset = 0;
5027
5028 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5029 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5030 ssl->conf->transport, rec.ver );
5031 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005032 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5033 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005036 return( ret );
5037 }
5038
Hanno Becker29800d22018-08-07 14:30:18 +01005039 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
5040 {
5041 /* Should never happen */
5042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005044
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005045 ssl->in_msglen = rec.data_len;
5046 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5047 ssl->in_len[1] = (unsigned char)( rec.data_len );
5048
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005049 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5050 ssl->in_msg, ssl->in_msglen );
5051
Angus Grattond8213d02016-05-25 20:56:48 +10005052 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5055 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005057 else if( ssl->in_msglen == 0 )
5058 {
5059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5060 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5061 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5062 {
5063 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5065 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5066 }
5067#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5068
5069 ssl->nb_zero++;
5070
5071 /*
5072 * Three or more empty messages may be a DoS attack
5073 * (excessive CPU consumption).
5074 */
5075 if( ssl->nb_zero > 3 )
5076 {
5077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
5078 "messages, possible DoS attack" ) );
5079 /* Q: Is that the right error code? */
5080 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5081 }
5082 }
5083 else
5084 ssl->nb_zero = 0;
5085
5086#if defined(MBEDTLS_SSL_PROTO_DTLS)
5087 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5088 {
5089 ; /* in_ctr read from peer, not maintained internally */
5090 }
5091 else
5092#endif
5093 {
5094 unsigned i;
5095 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5096 if( ++ssl->in_ctr[i - 1] != 0 )
5097 break;
5098
5099 /* The loop goes to its end iff the counter is wrapping */
5100 if( i == ssl_ep_len( ssl ) )
5101 {
5102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5103 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5104 }
5105 }
5106
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 }
5108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005110 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005112 {
5113 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005116 return( ret );
5117 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005118 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005125 }
5126#endif
5127
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005128 return( 0 );
5129}
5130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005132
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005133/*
5134 * Read a record.
5135 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005136 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5137 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5138 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005139 */
Hanno Becker1097b342018-08-15 14:09:41 +01005140
5141/* Helper functions for mbedtls_ssl_read_record(). */
5142static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005143static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5144static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005145
Hanno Becker327c93b2018-08-15 13:56:18 +01005146int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005147 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005148{
5149 int ret;
5150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005152
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005153 if( ssl->keep_current_message == 0 )
5154 {
5155 do {
Simon Butcher99000142016-10-13 17:21:01 +01005156
Hanno Becker26994592018-08-15 14:14:59 +01005157 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005158 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005159 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005160
Hanno Beckere74d5562018-08-15 14:26:08 +01005161 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005162 {
Hanno Becker40f50842018-08-15 14:48:01 +01005163#if defined(MBEDTLS_SSL_PROTO_DTLS)
5164 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005165
Hanno Becker40f50842018-08-15 14:48:01 +01005166 /* We only check for buffered messages if the
5167 * current datagram is fully consumed. */
5168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005169 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005170 {
Hanno Becker40f50842018-08-15 14:48:01 +01005171 if( ssl_load_buffered_message( ssl ) == 0 )
5172 have_buffered = 1;
5173 }
5174
5175 if( have_buffered == 0 )
5176#endif /* MBEDTLS_SSL_PROTO_DTLS */
5177 {
5178 ret = ssl_get_next_record( ssl );
5179 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5180 continue;
5181
5182 if( ret != 0 )
5183 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005184 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005185 return( ret );
5186 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005187 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005188 }
5189
5190 ret = mbedtls_ssl_handle_message_type( ssl );
5191
Hanno Becker40f50842018-08-15 14:48:01 +01005192#if defined(MBEDTLS_SSL_PROTO_DTLS)
5193 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5194 {
5195 /* Buffer future message */
5196 ret = ssl_buffer_message( ssl );
5197 if( ret != 0 )
5198 return( ret );
5199
5200 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5201 }
5202#endif /* MBEDTLS_SSL_PROTO_DTLS */
5203
Hanno Becker90333da2017-10-10 11:27:13 +01005204 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5205 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005206
5207 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005208 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005209 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005210 return( ret );
5211 }
5212
Hanno Becker327c93b2018-08-15 13:56:18 +01005213 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005214 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005215 {
5216 mbedtls_ssl_update_handshake_status( ssl );
5217 }
Simon Butcher99000142016-10-13 17:21:01 +01005218 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005219 else
Simon Butcher99000142016-10-13 17:21:01 +01005220 {
Hanno Becker02f59072018-08-15 14:00:24 +01005221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005222 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005223 }
5224
5225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5226
5227 return( 0 );
5228}
5229
Hanno Becker40f50842018-08-15 14:48:01 +01005230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005231static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005232{
Hanno Becker40f50842018-08-15 14:48:01 +01005233 if( ssl->in_left > ssl->next_record_offset )
5234 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005235
Hanno Becker40f50842018-08-15 14:48:01 +01005236 return( 0 );
5237}
5238
5239static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5240{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005241 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005242 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005243 int ret = 0;
5244
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005245 if( hs == NULL )
5246 return( -1 );
5247
Hanno Beckere00ae372018-08-20 09:39:42 +01005248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5249
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005250 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5251 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5252 {
5253 /* Check if we have seen a ChangeCipherSpec before.
5254 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005255 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005256 {
5257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5258 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005259 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005260 }
5261
Hanno Becker39b8bc92018-08-28 17:17:13 +01005262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005263 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5264 ssl->in_msglen = 1;
5265 ssl->in_msg[0] = 1;
5266
5267 /* As long as they are equal, the exact value doesn't matter. */
5268 ssl->in_left = 0;
5269 ssl->next_record_offset = 0;
5270
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005271 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005272 goto exit;
5273 }
Hanno Becker37f95322018-08-16 13:55:32 +01005274
Hanno Beckerb8f50142018-08-28 10:01:34 +01005275#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005276 /* Debug only */
5277 {
5278 unsigned offset;
5279 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5280 {
5281 hs_buf = &hs->buffering.hs[offset];
5282 if( hs_buf->is_valid == 1 )
5283 {
5284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5285 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005286 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005287 }
5288 }
5289 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005290#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005291
5292 /* Check if we have buffered and/or fully reassembled the
5293 * next handshake message. */
5294 hs_buf = &hs->buffering.hs[0];
5295 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5296 {
5297 /* Synthesize a record containing the buffered HS message. */
5298 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5299 ( hs_buf->data[2] << 8 ) |
5300 hs_buf->data[3];
5301
5302 /* Double-check that we haven't accidentally buffered
5303 * a message that doesn't fit into the input buffer. */
5304 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5305 {
5306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5308 }
5309
5310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5311 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5312 hs_buf->data, msg_len + 12 );
5313
5314 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5315 ssl->in_hslen = msg_len + 12;
5316 ssl->in_msglen = msg_len + 12;
5317 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5318
5319 ret = 0;
5320 goto exit;
5321 }
5322 else
5323 {
5324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5325 hs->in_msg_seq ) );
5326 }
5327
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005328 ret = -1;
5329
5330exit:
5331
5332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5333 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005334}
5335
Hanno Beckera02b0b42018-08-21 17:20:27 +01005336static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5337 size_t desired )
5338{
5339 int offset;
5340 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5342 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005343
Hanno Becker01315ea2018-08-21 17:22:17 +01005344 /* Get rid of future records epoch first, if such exist. */
5345 ssl_free_buffered_record( ssl );
5346
5347 /* Check if we have enough space available now. */
5348 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5349 hs->buffering.total_bytes_buffered ) )
5350 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005352 return( 0 );
5353 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005354
Hanno Becker4f432ad2018-08-28 10:02:32 +01005355 /* We don't have enough space to buffer the next expected handshake
5356 * message. Remove buffers used for future messages to gain space,
5357 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005358 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5359 offset >= 0; offset-- )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5362 offset ) );
5363
Hanno Beckerb309b922018-08-23 13:18:05 +01005364 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005365
5366 /* Check if we have enough space available now. */
5367 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5368 hs->buffering.total_bytes_buffered ) )
5369 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005371 return( 0 );
5372 }
5373 }
5374
5375 return( -1 );
5376}
5377
Hanno Becker40f50842018-08-15 14:48:01 +01005378static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5379{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005380 int ret = 0;
5381 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5382
5383 if( hs == NULL )
5384 return( 0 );
5385
5386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5387
5388 switch( ssl->in_msgtype )
5389 {
5390 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005392
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005393 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005394 break;
5395
5396 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005397 {
5398 unsigned recv_msg_seq_offset;
5399 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5400 mbedtls_ssl_hs_buffer *hs_buf;
5401 size_t msg_len = ssl->in_hslen - 12;
5402
5403 /* We should never receive an old handshake
5404 * message - double-check nonetheless. */
5405 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5406 {
5407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5409 }
5410
5411 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5412 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5413 {
5414 /* Silently ignore -- message too far in the future */
5415 MBEDTLS_SSL_DEBUG_MSG( 2,
5416 ( "Ignore future HS message with sequence number %u, "
5417 "buffering window %u - %u",
5418 recv_msg_seq, ssl->handshake->in_msg_seq,
5419 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5420
5421 goto exit;
5422 }
5423
5424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5425 recv_msg_seq, recv_msg_seq_offset ) );
5426
5427 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5428
5429 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005430 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005431 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005432 size_t reassembly_buf_sz;
5433
Hanno Becker37f95322018-08-16 13:55:32 +01005434 hs_buf->is_fragmented =
5435 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5436
5437 /* We copy the message back into the input buffer
5438 * after reassembly, so check that it's not too large.
5439 * This is an implementation-specific limitation
5440 * and not one from the standard, hence it is not
5441 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005442 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005443 {
5444 /* Ignore message */
5445 goto exit;
5446 }
5447
Hanno Beckere0b150f2018-08-21 15:51:03 +01005448 /* Check if we have enough space to buffer the message. */
5449 if( hs->buffering.total_bytes_buffered >
5450 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5451 {
5452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5454 }
5455
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005456 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5457 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005458
5459 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5460 hs->buffering.total_bytes_buffered ) )
5461 {
5462 if( recv_msg_seq_offset > 0 )
5463 {
5464 /* If we can't buffer a future message because
5465 * of space limitations -- ignore. */
5466 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",
5467 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5468 (unsigned) hs->buffering.total_bytes_buffered ) );
5469 goto exit;
5470 }
Hanno Beckere1801392018-08-21 16:51:05 +01005471 else
5472 {
5473 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",
5474 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5475 (unsigned) hs->buffering.total_bytes_buffered ) );
5476 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005477
Hanno Beckera02b0b42018-08-21 17:20:27 +01005478 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005479 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005480 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",
5481 (unsigned) msg_len,
5482 (unsigned) reassembly_buf_sz,
5483 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005484 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005485 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5486 goto exit;
5487 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005488 }
5489
5490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5491 msg_len ) );
5492
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005493 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5494 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005495 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005496 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005497 goto exit;
5498 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005499 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005500
5501 /* Prepare final header: copy msg_type, length and message_seq,
5502 * then add standardised fragment_offset and fragment_length */
5503 memcpy( hs_buf->data, ssl->in_msg, 6 );
5504 memset( hs_buf->data + 6, 0, 3 );
5505 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5506
5507 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005508
5509 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005510 }
5511 else
5512 {
5513 /* Make sure msg_type and length are consistent */
5514 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5515 {
5516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5517 /* Ignore */
5518 goto exit;
5519 }
5520 }
5521
Hanno Becker4422bbb2018-08-20 09:40:19 +01005522 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005523 {
5524 size_t frag_len, frag_off;
5525 unsigned char * const msg = hs_buf->data + 12;
5526
5527 /*
5528 * Check and copy current fragment
5529 */
5530
5531 /* Validation of header fields already done in
5532 * mbedtls_ssl_prepare_handshake_record(). */
5533 frag_off = ssl_get_hs_frag_off( ssl );
5534 frag_len = ssl_get_hs_frag_len( ssl );
5535
5536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5537 frag_off, frag_len ) );
5538 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5539
5540 if( hs_buf->is_fragmented )
5541 {
5542 unsigned char * const bitmask = msg + msg_len;
5543 ssl_bitmask_set( bitmask, frag_off, frag_len );
5544 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5545 msg_len ) == 0 );
5546 }
5547 else
5548 {
5549 hs_buf->is_complete = 1;
5550 }
5551
5552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5553 hs_buf->is_complete ? "" : "not yet " ) );
5554 }
5555
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005556 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005557 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005558
5559 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005560 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005561 break;
5562 }
5563
5564exit:
5565
5566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5567 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005568}
5569#endif /* MBEDTLS_SSL_PROTO_DTLS */
5570
Hanno Becker1097b342018-08-15 14:09:41 +01005571static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005572{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005573 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005574 * Consume last content-layer message and potentially
5575 * update in_msglen which keeps track of the contents'
5576 * consumption state.
5577 *
5578 * (1) Handshake messages:
5579 * Remove last handshake message, move content
5580 * and adapt in_msglen.
5581 *
5582 * (2) Alert messages:
5583 * Consume whole record content, in_msglen = 0.
5584 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005585 * (3) Change cipher spec:
5586 * Consume whole record content, in_msglen = 0.
5587 *
5588 * (4) Application data:
5589 * Don't do anything - the record layer provides
5590 * the application data as a stream transport
5591 * and consumes through mbedtls_ssl_read only.
5592 *
5593 */
5594
5595 /* Case (1): Handshake messages */
5596 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005597 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005598 /* Hard assertion to be sure that no application data
5599 * is in flight, as corrupting ssl->in_msglen during
5600 * ssl->in_offt != NULL is fatal. */
5601 if( ssl->in_offt != NULL )
5602 {
5603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5605 }
5606
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005607 /*
5608 * Get next Handshake message in the current record
5609 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610
Hanno Becker4a810fb2017-05-24 16:27:30 +01005611 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005612 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005613 * current handshake content: If DTLS handshake
5614 * fragmentation is used, that's the fragment
5615 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005616 * size here is faulty and should be changed at
5617 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005618 * (2) While it doesn't seem to cause problems, one
5619 * has to be very careful not to assume that in_hslen
5620 * is always <= in_msglen in a sensible communication.
5621 * Again, it's wrong for DTLS handshake fragmentation.
5622 * The following check is therefore mandatory, and
5623 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005624 * Additionally, ssl->in_hslen might be arbitrarily out of
5625 * bounds after handling a DTLS message with an unexpected
5626 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005627 */
5628 if( ssl->in_hslen < ssl->in_msglen )
5629 {
5630 ssl->in_msglen -= ssl->in_hslen;
5631 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5632 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005633
Hanno Becker4a810fb2017-05-24 16:27:30 +01005634 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5635 ssl->in_msg, ssl->in_msglen );
5636 }
5637 else
5638 {
5639 ssl->in_msglen = 0;
5640 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005641
Hanno Becker4a810fb2017-05-24 16:27:30 +01005642 ssl->in_hslen = 0;
5643 }
5644 /* Case (4): Application data */
5645 else if( ssl->in_offt != NULL )
5646 {
5647 return( 0 );
5648 }
5649 /* Everything else (CCS & Alerts) */
5650 else
5651 {
5652 ssl->in_msglen = 0;
5653 }
5654
Hanno Becker1097b342018-08-15 14:09:41 +01005655 return( 0 );
5656}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005657
Hanno Beckere74d5562018-08-15 14:26:08 +01005658static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5659{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005660 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005661 return( 1 );
5662
5663 return( 0 );
5664}
5665
Hanno Becker5f066e72018-08-16 14:56:31 +01005666#if defined(MBEDTLS_SSL_PROTO_DTLS)
5667
5668static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5669{
5670 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5671 if( hs == NULL )
5672 return;
5673
Hanno Becker01315ea2018-08-21 17:22:17 +01005674 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005675 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005676 hs->buffering.total_bytes_buffered -=
5677 hs->buffering.future_record.len;
5678
5679 mbedtls_free( hs->buffering.future_record.data );
5680 hs->buffering.future_record.data = NULL;
5681 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005682}
5683
5684static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5685{
5686 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5687 unsigned char * rec;
5688 size_t rec_len;
5689 unsigned rec_epoch;
5690
5691 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5692 return( 0 );
5693
5694 if( hs == NULL )
5695 return( 0 );
5696
Hanno Becker5f066e72018-08-16 14:56:31 +01005697 rec = hs->buffering.future_record.data;
5698 rec_len = hs->buffering.future_record.len;
5699 rec_epoch = hs->buffering.future_record.epoch;
5700
5701 if( rec == NULL )
5702 return( 0 );
5703
Hanno Becker4cb782d2018-08-20 11:19:05 +01005704 /* Only consider loading future records if the
5705 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005706 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005707 return( 0 );
5708
Hanno Becker5f066e72018-08-16 14:56:31 +01005709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5710
5711 if( rec_epoch != ssl->in_epoch )
5712 {
5713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5714 goto exit;
5715 }
5716
5717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5718
5719 /* Double-check that the record is not too large */
5720 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5721 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5722 {
5723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5725 }
5726
5727 memcpy( ssl->in_hdr, rec, rec_len );
5728 ssl->in_left = rec_len;
5729 ssl->next_record_offset = 0;
5730
5731 ssl_free_buffered_record( ssl );
5732
5733exit:
5734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5735 return( 0 );
5736}
5737
5738static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5739{
5740 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5741 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005742 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005743
5744 /* Don't buffer future records outside handshakes. */
5745 if( hs == NULL )
5746 return( 0 );
5747
5748 /* Only buffer handshake records (we are only interested
5749 * in Finished messages). */
5750 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5751 return( 0 );
5752
5753 /* Don't buffer more than one future epoch record. */
5754 if( hs->buffering.future_record.data != NULL )
5755 return( 0 );
5756
Hanno Becker01315ea2018-08-21 17:22:17 +01005757 /* Don't buffer record if there's not enough buffering space remaining. */
5758 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5759 hs->buffering.total_bytes_buffered ) )
5760 {
5761 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",
5762 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5763 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005764 return( 0 );
5765 }
5766
Hanno Becker5f066e72018-08-16 14:56:31 +01005767 /* Buffer record */
5768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5769 ssl->in_epoch + 1 ) );
5770 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5771 rec_hdr_len + ssl->in_msglen );
5772
5773 /* ssl_parse_record_header() only considers records
5774 * of the next epoch as candidates for buffering. */
5775 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005776 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005777
5778 hs->buffering.future_record.data =
5779 mbedtls_calloc( 1, hs->buffering.future_record.len );
5780 if( hs->buffering.future_record.data == NULL )
5781 {
5782 /* If we run out of RAM trying to buffer a
5783 * record from the next epoch, just ignore. */
5784 return( 0 );
5785 }
5786
Hanno Becker01315ea2018-08-21 17:22:17 +01005787 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005788
Hanno Becker01315ea2018-08-21 17:22:17 +01005789 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005790 return( 0 );
5791}
5792
5793#endif /* MBEDTLS_SSL_PROTO_DTLS */
5794
Hanno Beckere74d5562018-08-15 14:26:08 +01005795static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005796{
5797 int ret;
5798
Hanno Becker5f066e72018-08-16 14:56:31 +01005799#if defined(MBEDTLS_SSL_PROTO_DTLS)
5800 /* We might have buffered a future record; if so,
5801 * and if the epoch matches now, load it.
5802 * On success, this call will set ssl->in_left to
5803 * the length of the buffered record, so that
5804 * the calls to ssl_fetch_input() below will
5805 * essentially be no-ops. */
5806 ret = ssl_load_buffered_record( ssl );
5807 if( ret != 0 )
5808 return( ret );
5809#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005814 return( ret );
5815 }
5816
5817 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005820 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5821 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005822 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005823 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5824 {
5825 ret = ssl_buffer_future_record( ssl );
5826 if( ret != 0 )
5827 return( ret );
5828
5829 /* Fall through to handling of unexpected records */
5830 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5831 }
5832
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005833 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5834 {
5835 /* Skip unexpected record (but not whole datagram) */
5836 ssl->next_record_offset = ssl->in_msglen
5837 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005838
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5840 "(header)" ) );
5841 }
5842 else
5843 {
5844 /* Skip invalid record and the rest of the datagram */
5845 ssl->next_record_offset = 0;
5846 ssl->in_left = 0;
5847
5848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5849 "(header)" ) );
5850 }
5851
5852 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005853 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005854 }
5855#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005856 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005857 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005858
5859 /*
5860 * Read and optionally decrypt the message contents
5861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5863 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005866 return( ret );
5867 }
5868
5869 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005874 if( ssl->next_record_offset < ssl->in_left )
5875 {
5876 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5877 }
5878 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005879 else
5880#endif
5881 ssl->in_left = 0;
5882
5883 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005887 {
5888 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5890 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005891 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005892 /* Except when waiting for Finished as a bad mac here
5893 * probably means something went wrong in the handshake
5894 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5895 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5896 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5897 {
5898#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5899 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5900 {
5901 mbedtls_ssl_send_alert_message( ssl,
5902 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5903 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5904 }
5905#endif
5906 return( ret );
5907 }
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005910 if( ssl->conf->badmac_limit != 0 &&
5911 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5914 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005915 }
5916#endif
5917
Hanno Becker4a810fb2017-05-24 16:27:30 +01005918 /* As above, invalid records cause
5919 * dismissal of the whole datagram. */
5920
5921 ssl->next_record_offset = 0;
5922 ssl->in_left = 0;
5923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005925 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005926 }
5927
5928 return( ret );
5929 }
5930 else
5931#endif
5932 {
5933 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5935 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 mbedtls_ssl_send_alert_message( ssl,
5938 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5939 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005940 }
5941#endif
5942 return( ret );
5943 }
5944 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005945
Simon Butcher99000142016-10-13 17:21:01 +01005946 return( 0 );
5947}
5948
5949int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5950{
5951 int ret;
5952
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005953 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005954 * Handle particular types of records
5955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 {
Simon Butcher99000142016-10-13 17:21:01 +01005958 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5959 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005960 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 }
5963
Hanno Beckere678eaa2018-08-21 14:57:46 +01005964 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005965 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005966 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005967 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5969 ssl->in_msglen ) );
5970 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005971 }
5972
Hanno Beckere678eaa2018-08-21 14:57:46 +01005973 if( ssl->in_msg[0] != 1 )
5974 {
5975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5976 ssl->in_msg[0] ) );
5977 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5978 }
5979
5980#if defined(MBEDTLS_SSL_PROTO_DTLS)
5981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5982 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5983 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5984 {
5985 if( ssl->handshake == NULL )
5986 {
5987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5988 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5989 }
5990
5991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5992 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5993 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005994#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005995 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005998 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005999 if( ssl->in_msglen != 2 )
6000 {
6001 /* Note: Standard allows for more than one 2 byte alert
6002 to be packed in a single message, but Mbed TLS doesn't
6003 currently support this. */
6004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6005 ssl->in_msglen ) );
6006 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6007 }
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006010 ssl->in_msg[0], ssl->in_msg[1] ) );
6011
6012 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006013 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006018 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006020 }
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6023 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6026 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006028
6029#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6030 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6031 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6032 {
Hanno Becker90333da2017-10-10 11:27:13 +01006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006034 /* Will be handled when trying to parse ServerHello */
6035 return( 0 );
6036 }
6037#endif
6038
6039#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6040 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6041 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6042 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6043 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6044 {
6045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6046 /* Will be handled in mbedtls_ssl_parse_certificate() */
6047 return( 0 );
6048 }
6049#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6050
6051 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006052 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 }
6054
Hanno Beckerc76c6192017-06-06 10:03:17 +01006055#if defined(MBEDTLS_SSL_PROTO_DTLS)
6056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6057 ssl->handshake != NULL &&
6058 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6059 {
6060 ssl_handshake_wrapup_free_hs_transform( ssl );
6061 }
6062#endif
6063
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 return( 0 );
6065}
6066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006068{
6069 int ret;
6070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6072 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6073 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006074 {
6075 return( ret );
6076 }
6077
6078 return( 0 );
6079}
6080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006082 unsigned char level,
6083 unsigned char message )
6084{
6085 int ret;
6086
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006087 if( ssl == NULL || ssl->conf == NULL )
6088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006094 ssl->out_msglen = 2;
6095 ssl->out_msg[0] = level;
6096 ssl->out_msg[1] = message;
6097
Hanno Becker67bc7c32018-08-06 11:33:50 +01006098 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006101 return( ret );
6102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006104
6105 return( 0 );
6106}
6107
Hanno Beckerb9d44792019-02-08 07:19:04 +00006108#if defined(MBEDTLS_X509_CRT_PARSE_C)
6109static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6110{
6111#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6112 if( session->peer_cert != NULL )
6113 {
6114 mbedtls_x509_crt_free( session->peer_cert );
6115 mbedtls_free( session->peer_cert );
6116 session->peer_cert = NULL;
6117 }
6118#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6119 if( session->peer_cert_digest != NULL )
6120 {
6121 /* Zeroization is not necessary. */
6122 mbedtls_free( session->peer_cert_digest );
6123 session->peer_cert_digest = NULL;
6124 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6125 session->peer_cert_digest_len = 0;
6126 }
6127#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6128}
6129#endif /* MBEDTLS_X509_CRT_PARSE_C */
6130
Paul Bakker5121ce52009-01-03 21:22:43 +00006131/*
6132 * Handshake functions
6133 */
Hanno Becker21489932019-02-05 13:20:55 +00006134#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006135/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006137{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006138 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6139 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006142
Hanno Becker7177a882019-02-05 13:36:46 +00006143 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006146 ssl->state++;
6147 return( 0 );
6148 }
6149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6151 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006152}
6153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006155{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006156 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6157 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006160
Hanno Becker7177a882019-02-05 13:36:46 +00006161 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006164 ssl->state++;
6165 return( 0 );
6166 }
6167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006170}
Gilles Peskinef9828522017-05-03 12:28:43 +02006171
Hanno Becker21489932019-02-05 13:20:55 +00006172#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006173/* Some certificate support -> implement write and parse */
6174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006178 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006180 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6181 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006184
Hanno Becker7177a882019-02-05 13:36:46 +00006185 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006188 ssl->state++;
6189 return( 0 );
6190 }
6191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006193 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 {
6195 if( ssl->client_auth == 0 )
6196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 ssl->state++;
6199 return( 0 );
6200 }
6201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 /*
6204 * If using SSLv3 and got no cert, send an Alert message
6205 * (otherwise an empty Certificate message will be sent).
6206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6208 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006209 {
6210 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6212 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6213 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 goto write_msg;
6217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220#endif /* MBEDTLS_SSL_CLI_C */
6221#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006222 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6227 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 }
6229 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006230#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
6234 /*
6235 * 0 . 0 handshake type
6236 * 1 . 3 handshake length
6237 * 4 . 6 length of all certs
6238 * 7 . 9 length of cert. 1
6239 * 10 . n-1 peer certificate
6240 * n . n+2 length of cert. 2
6241 * n+3 . ... upper level cert, etc.
6242 */
6243 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006245
Paul Bakker29087132010-03-21 21:03:34 +00006246 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 {
6248 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006249 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006252 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 }
6255
6256 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6257 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6258 ssl->out_msg[i + 2] = (unsigned char)( n );
6259
6260 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6261 i += n; crt = crt->next;
6262 }
6263
6264 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6265 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6266 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6267
6268 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6270 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006271
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006272#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006273write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006275
6276 ssl->state++;
6277
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006278 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006279 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006281 return( ret );
6282 }
6283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006285
Paul Bakkered27a042013-04-18 22:46:23 +02006286 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006287}
6288
Hanno Becker84879e32019-01-31 07:44:03 +00006289#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006290
6291#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006292static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6293 unsigned char *crt_buf,
6294 size_t crt_buf_len )
6295{
6296 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6297
6298 if( peer_crt == NULL )
6299 return( -1 );
6300
6301 if( peer_crt->raw.len != crt_buf_len )
6302 return( -1 );
6303
Hanno Becker46f34d02019-02-08 14:00:04 +00006304 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006305}
Hanno Becker177475a2019-02-05 17:02:46 +00006306#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6307static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6308 unsigned char *crt_buf,
6309 size_t crt_buf_len )
6310{
6311 int ret;
6312 unsigned char const * const peer_cert_digest =
6313 ssl->session->peer_cert_digest;
6314 mbedtls_md_type_t const peer_cert_digest_type =
6315 ssl->session->peer_cert_digest_type;
6316 mbedtls_md_info_t const * const digest_info =
6317 mbedtls_md_info_from_type( peer_cert_digest_type );
6318 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6319 size_t digest_len;
6320
6321 if( peer_cert_digest == NULL || digest_info == NULL )
6322 return( -1 );
6323
6324 digest_len = mbedtls_md_get_size( digest_info );
6325 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6326 return( -1 );
6327
6328 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6329 if( ret != 0 )
6330 return( -1 );
6331
6332 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6333}
6334#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006335#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006336
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006337/*
6338 * Once the certificate message is read, parse it into a cert chain and
6339 * perform basic checks, but leave actual verification to the caller
6340 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006341static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6342 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006343{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006344 int ret;
6345#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6346 int crt_cnt=0;
6347#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006348 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006349 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006354 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6355 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 }
6358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6360 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006363 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6364 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 }
6367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006369
Paul Bakker5121ce52009-01-03 21:22:43 +00006370 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006373 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006374
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006375 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006379 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6380 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 }
6383
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006384 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6385 i += 3;
6386
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006387 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006388 while( i < ssl->in_hslen )
6389 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006390 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006391 if ( i + 3 > ssl->in_hslen ) {
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006393 mbedtls_ssl_send_alert_message( ssl,
6394 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006396 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6397 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006398 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6399 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006400 if( ssl->in_msg[i] != 0 )
6401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006403 mbedtls_ssl_send_alert_message( ssl,
6404 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6405 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006407 }
6408
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006409 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006410 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6411 | (unsigned int) ssl->in_msg[i + 2];
6412 i += 3;
6413
6414 if( n < 128 || i + n > ssl->in_hslen )
6415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006417 mbedtls_ssl_send_alert_message( ssl,
6418 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6419 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006421 }
6422
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006423 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006424#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6425 if( crt_cnt++ == 0 &&
6426 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6427 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006428 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006429 /* During client-side renegotiation, check that the server's
6430 * end-CRTs hasn't changed compared to the initial handshake,
6431 * mitigating the triple handshake attack. On success, reuse
6432 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6434 if( ssl_check_peer_crt_unchanged( ssl,
6435 &ssl->in_msg[i],
6436 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006437 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6439 mbedtls_ssl_send_alert_message( ssl,
6440 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6441 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6442 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006443 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006444
6445 /* Now we can safely free the original chain. */
6446 ssl_clear_peer_cert( ssl->session );
6447 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006448#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6449
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006450 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006451#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006452 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006453#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006454 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006455 * it in-place from the input buffer instead of making a copy. */
6456 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6457#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006458 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006459 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006460 case 0: /*ok*/
6461 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6462 /* Ignore certificate with an unknown algorithm: maybe a
6463 prior certificate was already trusted. */
6464 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006465
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006466 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6467 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6468 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006469
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006470 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6471 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6472 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006473
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006474 default:
6475 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6476 crt_parse_der_failed:
6477 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6478 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6479 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006480 }
6481
6482 i += n;
6483 }
6484
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006485 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006486 return( 0 );
6487}
6488
Hanno Becker4a55f632019-02-05 12:49:06 +00006489#if defined(MBEDTLS_SSL_SRV_C)
6490static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6491{
6492 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6493 return( -1 );
6494
6495#if defined(MBEDTLS_SSL_PROTO_SSL3)
6496 /*
6497 * Check if the client sent an empty certificate
6498 */
6499 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6500 {
6501 if( ssl->in_msglen == 2 &&
6502 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6503 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6504 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6505 {
6506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6507 return( 0 );
6508 }
6509
6510 return( -1 );
6511 }
6512#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6513
6514#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6515 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6516 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6517 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6518 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6519 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6520 {
6521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6522 return( 0 );
6523 }
6524
6525 return( -1 );
6526#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6527 MBEDTLS_SSL_PROTO_TLS1_2 */
6528}
6529#endif /* MBEDTLS_SSL_SRV_C */
6530
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006531/* Check if a certificate message is expected.
6532 * Return either
6533 * - SSL_CERTIFICATE_EXPECTED, or
6534 * - SSL_CERTIFICATE_SKIP
6535 * indicating whether a Certificate message is expected or not.
6536 */
6537#define SSL_CERTIFICATE_EXPECTED 0
6538#define SSL_CERTIFICATE_SKIP 1
6539static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6540 int authmode )
6541{
6542 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006543 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006544
6545 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6546 return( SSL_CERTIFICATE_SKIP );
6547
6548#if defined(MBEDTLS_SSL_SRV_C)
6549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6550 {
6551 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6552 return( SSL_CERTIFICATE_SKIP );
6553
6554 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6555 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006556 ssl->session_negotiate->verify_result =
6557 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6558 return( SSL_CERTIFICATE_SKIP );
6559 }
6560 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006561#else
6562 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006563#endif /* MBEDTLS_SSL_SRV_C */
6564
6565 return( SSL_CERTIFICATE_EXPECTED );
6566}
6567
Hanno Becker68636192019-02-05 14:36:34 +00006568static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6569 int authmode,
6570 mbedtls_x509_crt *chain,
6571 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006572{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006573 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006574 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006575 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006576 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006577
Hanno Becker8927c832019-04-03 12:52:50 +01006578 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6579 void *p_vrfy;
6580
Hanno Becker68636192019-02-05 14:36:34 +00006581 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6582 return( 0 );
6583
Hanno Becker8927c832019-04-03 12:52:50 +01006584 if( ssl->f_vrfy != NULL )
6585 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006587 f_vrfy = ssl->f_vrfy;
6588 p_vrfy = ssl->p_vrfy;
6589 }
6590 else
6591 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006593 f_vrfy = ssl->conf->f_vrfy;
6594 p_vrfy = ssl->conf->p_vrfy;
6595 }
6596
Hanno Becker68636192019-02-05 14:36:34 +00006597 /*
6598 * Main check: verify certificate
6599 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006600#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6601 if( ssl->conf->f_ca_cb != NULL )
6602 {
6603 ((void) rs_ctx);
6604 have_ca_chain = 1;
6605
6606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006607 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006608 chain,
6609 ssl->conf->f_ca_cb,
6610 ssl->conf->p_ca_cb,
6611 ssl->conf->cert_profile,
6612 ssl->hostname,
6613 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006614 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006615 }
6616 else
6617#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6618 {
6619 mbedtls_x509_crt *ca_chain;
6620 mbedtls_x509_crl *ca_crl;
6621
6622#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6623 if( ssl->handshake->sni_ca_chain != NULL )
6624 {
6625 ca_chain = ssl->handshake->sni_ca_chain;
6626 ca_crl = ssl->handshake->sni_ca_crl;
6627 }
6628 else
6629#endif
6630 {
6631 ca_chain = ssl->conf->ca_chain;
6632 ca_crl = ssl->conf->ca_crl;
6633 }
6634
6635 if( ca_chain != NULL )
6636 have_ca_chain = 1;
6637
6638 ret = mbedtls_x509_crt_verify_restartable(
6639 chain,
6640 ca_chain, ca_crl,
6641 ssl->conf->cert_profile,
6642 ssl->hostname,
6643 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006644 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006645 }
Hanno Becker68636192019-02-05 14:36:34 +00006646
6647 if( ret != 0 )
6648 {
6649 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6650 }
6651
6652#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6653 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6654 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6655#endif
6656
6657 /*
6658 * Secondary checks: always done, but change 'ret' only if it was 0
6659 */
6660
6661#if defined(MBEDTLS_ECP_C)
6662 {
6663 const mbedtls_pk_context *pk = &chain->pk;
6664
6665 /* If certificate uses an EC key, make sure the curve is OK */
6666 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6667 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6668 {
6669 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6670
6671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6672 if( ret == 0 )
6673 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6674 }
6675 }
6676#endif /* MBEDTLS_ECP_C */
6677
6678 if( mbedtls_ssl_check_cert_usage( chain,
6679 ciphersuite_info,
6680 ! ssl->conf->endpoint,
6681 &ssl->session_negotiate->verify_result ) != 0 )
6682 {
6683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6684 if( ret == 0 )
6685 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6686 }
6687
6688 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6689 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6690 * with details encoded in the verification flags. All other kinds
6691 * of error codes, including those from the user provided f_vrfy
6692 * functions, are treated as fatal and lead to a failure of
6693 * ssl_parse_certificate even if verification was optional. */
6694 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6695 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6696 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6697 {
6698 ret = 0;
6699 }
6700
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006701 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006702 {
6703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6704 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6705 }
6706
6707 if( ret != 0 )
6708 {
6709 uint8_t alert;
6710
6711 /* The certificate may have been rejected for several reasons.
6712 Pick one and send the corresponding alert. Which alert to send
6713 may be a subject of debate in some cases. */
6714 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6715 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6716 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6717 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6718 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6719 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6720 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6721 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6722 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6723 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6724 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6725 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6726 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6727 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6728 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6729 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6730 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6731 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6732 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6733 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6734 else
6735 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6737 alert );
6738 }
6739
6740#if defined(MBEDTLS_DEBUG_C)
6741 if( ssl->session_negotiate->verify_result != 0 )
6742 {
6743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6744 ssl->session_negotiate->verify_result ) );
6745 }
6746 else
6747 {
6748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6749 }
6750#endif /* MBEDTLS_DEBUG_C */
6751
6752 return( ret );
6753}
6754
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006755#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6756static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6757 unsigned char *start, size_t len )
6758{
6759 int ret;
6760 /* Remember digest of the peer's end-CRT. */
6761 ssl->session_negotiate->peer_cert_digest =
6762 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6763 if( ssl->session_negotiate->peer_cert_digest == NULL )
6764 {
6765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6766 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6767 mbedtls_ssl_send_alert_message( ssl,
6768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6769 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6770
6771 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6772 }
6773
6774 ret = mbedtls_md( mbedtls_md_info_from_type(
6775 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6776 start, len,
6777 ssl->session_negotiate->peer_cert_digest );
6778
6779 ssl->session_negotiate->peer_cert_digest_type =
6780 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6781 ssl->session_negotiate->peer_cert_digest_len =
6782 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6783
6784 return( ret );
6785}
6786
6787static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6788 unsigned char *start, size_t len )
6789{
6790 unsigned char *end = start + len;
6791 int ret;
6792
6793 /* Make a copy of the peer's raw public key. */
6794 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6795 ret = mbedtls_pk_parse_subpubkey( &start, end,
6796 &ssl->handshake->peer_pubkey );
6797 if( ret != 0 )
6798 {
6799 /* We should have parsed the public key before. */
6800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6801 }
6802
6803 return( 0 );
6804}
6805#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6806
Hanno Becker68636192019-02-05 14:36:34 +00006807int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6808{
6809 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006810 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006811#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6812 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6813 ? ssl->handshake->sni_authmode
6814 : ssl->conf->authmode;
6815#else
6816 const int authmode = ssl->conf->authmode;
6817#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006818 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006819 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006820
6821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6822
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006823 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6824 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006825 {
6826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006827 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006828 }
6829
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006830#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6831 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006832 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006833 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006834 chain = ssl->handshake->ecrs_peer_cert;
6835 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006836 goto crt_verify;
6837 }
6838#endif
6839
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006840 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006841 {
6842 /* mbedtls_ssl_read_record may have sent an alert already. We
6843 let it decide whether to alert. */
6844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006845 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006846 }
6847
Hanno Becker4a55f632019-02-05 12:49:06 +00006848#if defined(MBEDTLS_SSL_SRV_C)
6849 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6850 {
6851 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006852
6853 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006854 ret = 0;
6855 else
6856 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006857
Hanno Becker6bdfab22019-02-05 13:11:17 +00006858 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006859 }
6860#endif /* MBEDTLS_SSL_SRV_C */
6861
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006862 /* Clear existing peer CRT structure in case we tried to
6863 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006864 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006865
6866 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6867 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006868 {
6869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6870 sizeof( mbedtls_x509_crt ) ) );
6871 mbedtls_ssl_send_alert_message( ssl,
6872 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6873 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006874
Hanno Becker3dad3112019-02-05 17:19:52 +00006875 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6876 goto exit;
6877 }
6878 mbedtls_x509_crt_init( chain );
6879
6880 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006881 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006882 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006883
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006884#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6885 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006886 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006887
6888crt_verify:
6889 if( ssl->handshake->ecrs_enabled)
6890 rs_ctx = &ssl->handshake->ecrs_ctx;
6891#endif
6892
Hanno Becker68636192019-02-05 14:36:34 +00006893 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006894 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006895 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006896 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006897
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006898#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006899 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006900 unsigned char *crt_start, *pk_start;
6901 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006902
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006903 /* We parse the CRT chain without copying, so
6904 * these pointers point into the input buffer,
6905 * and are hence still valid after freeing the
6906 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006907
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006908 crt_start = chain->raw.p;
6909 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006910
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006911 pk_start = chain->pk_raw.p;
6912 pk_len = chain->pk_raw.len;
6913
6914 /* Free the CRT structures before computing
6915 * digest and copying the peer's public key. */
6916 mbedtls_x509_crt_free( chain );
6917 mbedtls_free( chain );
6918 chain = NULL;
6919
6920 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006921 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006922 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006923
6924 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6925 if( ret != 0 )
6926 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006927 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006928#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6929 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006930 ssl->session_negotiate->peer_cert = chain;
6931 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006932#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Hanno Becker6bdfab22019-02-05 13:11:17 +00006936exit:
6937
Hanno Becker3dad3112019-02-05 17:19:52 +00006938 if( ret == 0 )
6939 ssl->state++;
6940
6941#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6942 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6943 {
6944 ssl->handshake->ecrs_peer_cert = chain;
6945 chain = NULL;
6946 }
6947#endif
6948
6949 if( chain != NULL )
6950 {
6951 mbedtls_x509_crt_free( chain );
6952 mbedtls_free( chain );
6953 }
6954
Paul Bakker5121ce52009-01-03 21:22:43 +00006955 return( ret );
6956}
Hanno Becker21489932019-02-05 13:20:55 +00006957#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006960{
6961 int ret;
6962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 ssl->out_msglen = 1;
6967 ssl->out_msg[0] = 1;
6968
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 ssl->state++;
6970
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006971 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006974 return( ret );
6975 }
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978
6979 return( 0 );
6980}
6981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006983{
6984 int ret;
6985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006987
Hanno Becker327c93b2018-08-15 13:56:18 +01006988 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 return( ret );
6992 }
6993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006997 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6998 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007000 }
7001
Hanno Beckere678eaa2018-08-21 14:57:46 +01007002 /* CCS records are only accepted if they have length 1 and content '1',
7003 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007004
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007005 /*
7006 * Switch to our negotiated transform and session parameters for inbound
7007 * data.
7008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007010 ssl->transform_in = ssl->transform_negotiate;
7011 ssl->session_in = ssl->session_negotiate;
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007017 ssl_dtls_replay_reset( ssl );
7018#endif
7019
7020 /* Increment epoch */
7021 if( ++ssl->in_epoch == 0 )
7022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007024 /* This is highly unlikely to happen for legitimate reasons, so
7025 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007027 }
7028 }
7029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007031 memset( ssl->in_ctr, 0, 8 );
7032
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007033 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7036 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007041 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7042 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007044 }
7045 }
7046#endif
7047
Paul Bakker5121ce52009-01-03 21:22:43 +00007048 ssl->state++;
7049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
7052 return( 0 );
7053}
7054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7056 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007057{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007058 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7061 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7062 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007063 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007064 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7067#if defined(MBEDTLS_SHA512_C)
7068 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007069 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7070 else
7071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#if defined(MBEDTLS_SHA256_C)
7073 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007074 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007075 else
7076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007080 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007081 }
Paul Bakker380da532012-04-18 16:10:25 +00007082}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7087 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007088 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7089 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7092#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007093#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007094 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007095 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7096#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007097 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007098#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007101#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007102 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007103 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007105 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007106#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007109}
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007112 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7115 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007116 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7117 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7120#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007121#if defined(MBEDTLS_USE_PSA_CRYPTO)
7122 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7123#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007124 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007125#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007129 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007130#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007131 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007132#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007135}
7136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7138 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7139static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007140 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007141{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007142 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7143 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007144}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007145#endif
Paul Bakker380da532012-04-18 16:10:25 +00007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7148#if defined(MBEDTLS_SHA256_C)
7149static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007150 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007151{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007152#if defined(MBEDTLS_USE_PSA_CRYPTO)
7153 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7154#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007155 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007156#endif
Paul Bakker380da532012-04-18 16:10:25 +00007157}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007158#endif
Paul Bakker380da532012-04-18 16:10:25 +00007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SHA512_C)
7161static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007162 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007163{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007165 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007166#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007167 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007168#endif
Paul Bakker380da532012-04-18 16:10:25 +00007169}
Paul Bakker769075d2012-11-24 11:26:46 +01007170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007176{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007177 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007178 mbedtls_md5_context md5;
7179 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007180
Paul Bakker5121ce52009-01-03 21:22:43 +00007181 unsigned char padbuf[48];
7182 unsigned char md5sum[16];
7183 unsigned char sha1sum[20];
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007186 if( !session )
7187 session = ssl->session;
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007191 mbedtls_md5_init( &md5 );
7192 mbedtls_sha1_init( &sha1 );
7193
7194 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7195 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007196
7197 /*
7198 * SSLv3:
7199 * hash =
7200 * MD5( master + pad2 +
7201 * MD5( handshake + sender + master + pad1 ) )
7202 * + SHA1( master + pad2 +
7203 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007204 */
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007207 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7208 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007212 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7213 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007214#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007217 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007218
Paul Bakker1ef83d62012-04-11 12:09:53 +00007219 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007220
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007221 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7222 mbedtls_md5_update_ret( &md5, session->master, 48 );
7223 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7224 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007225
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007226 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7227 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7228 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7229 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007230
Paul Bakker1ef83d62012-04-11 12:09:53 +00007231 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007232
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007233 mbedtls_md5_starts_ret( &md5 );
7234 mbedtls_md5_update_ret( &md5, session->master, 48 );
7235 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7236 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7237 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007239 mbedtls_sha1_starts_ret( &sha1 );
7240 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7241 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7242 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7243 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007246
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007247 mbedtls_md5_free( &md5 );
7248 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007249
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007250 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7251 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7252 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007259static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007261{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007262 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007263 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007264 mbedtls_md5_context md5;
7265 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007266 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007269 if( !session )
7270 session = ssl->session;
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007273
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007274 mbedtls_md5_init( &md5 );
7275 mbedtls_sha1_init( &sha1 );
7276
7277 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7278 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007279
Paul Bakker1ef83d62012-04-11 12:09:53 +00007280 /*
7281 * TLSv1:
7282 * hash = PRF( master, finished_label,
7283 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7284 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007287 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7288 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007289#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007292 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7293 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007294#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007297 ? "client finished"
7298 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007299
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007300 mbedtls_md5_finish_ret( &md5, padbuf );
7301 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007303 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007304 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007307
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007308 mbedtls_md5_free( &md5 );
7309 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007310
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007311 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7318#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007319static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007321{
7322 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007323 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007324 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007325#if defined(MBEDTLS_USE_PSA_CRYPTO)
7326 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007327 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007328 psa_status_t status;
7329#else
7330 mbedtls_sha256_context sha256;
7331#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007334 if( !session )
7335 session = ssl->session;
7336
Andrzej Kurekeb342242019-01-29 09:14:33 -05007337 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7338 ? "client finished"
7339 : "server finished";
7340
7341#if defined(MBEDTLS_USE_PSA_CRYPTO)
7342 sha256_psa = psa_hash_operation_init();
7343
7344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7345
7346 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7347 if( status != PSA_SUCCESS )
7348 {
7349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7350 return;
7351 }
7352
7353 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7354 if( status != PSA_SUCCESS )
7355 {
7356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7357 return;
7358 }
7359 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7360#else
7361
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007362 mbedtls_sha256_init( &sha256 );
7363
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007365
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007366 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007367
7368 /*
7369 * TLSv1.2:
7370 * hash = PRF( master, finished_label,
7371 * Hash( handshake ) )[0.11]
7372 */
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if !defined(MBEDTLS_SHA256_ALT)
7375 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007376 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007377#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007379 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007380 mbedtls_sha256_free( &sha256 );
7381#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007382
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007383 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007384 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007387
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007388 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007395static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007397{
7398 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007399 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007400 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007401#if defined(MBEDTLS_USE_PSA_CRYPTO)
7402 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007403 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007404 psa_status_t status;
7405#else
7406 mbedtls_sha512_context sha512;
7407#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007410 if( !session )
7411 session = ssl->session;
7412
Andrzej Kurekeb342242019-01-29 09:14:33 -05007413 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7414 ? "client finished"
7415 : "server finished";
7416
7417#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007418 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007419
7420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7421
Andrzej Kurek972fba52019-01-30 03:29:12 -05007422 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007423 if( status != PSA_SUCCESS )
7424 {
7425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7426 return;
7427 }
7428
Andrzej Kurek972fba52019-01-30 03:29:12 -05007429 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007430 if( status != PSA_SUCCESS )
7431 {
7432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7433 return;
7434 }
7435 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7436#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007437 mbedtls_sha512_init( &sha512 );
7438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007440
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007441 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007442
7443 /*
7444 * TLSv1.2:
7445 * hash = PRF( master, finished_label,
7446 * Hash( handshake ) )[0.11]
7447 */
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007450 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7451 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007452#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007454 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007455 mbedtls_sha512_free( &sha512 );
7456#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007457
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007458 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007459 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007462
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007463 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467#endif /* MBEDTLS_SHA512_C */
7468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007473
7474 /*
7475 * Free our handshake params
7476 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007477 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007479 ssl->handshake = NULL;
7480
7481 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007482 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007483 */
7484 if( ssl->transform )
7485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 mbedtls_ssl_transform_free( ssl->transform );
7487 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007488 }
7489 ssl->transform = ssl->transform_negotiate;
7490 ssl->transform_negotiate = NULL;
7491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007493}
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007496{
7497 int resume = ssl->handshake->resume;
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501#if defined(MBEDTLS_SSL_RENEGOTIATION)
7502 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007505 ssl->renego_records_seen = 0;
7506 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007507#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007508
7509 /*
7510 * Free the previous session and switch in the current one
7511 */
Paul Bakker0a597072012-09-25 21:55:46 +00007512 if( ssl->session )
7513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007515 /* RFC 7366 3.1: keep the EtM state */
7516 ssl->session_negotiate->encrypt_then_mac =
7517 ssl->session->encrypt_then_mac;
7518#endif
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 mbedtls_ssl_session_free( ssl->session );
7521 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007522 }
7523 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007524 ssl->session_negotiate = NULL;
7525
Paul Bakker0a597072012-09-25 21:55:46 +00007526 /*
7527 * Add cache entry
7528 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007529 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007530 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007531 resume == 0 )
7532 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007533 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007535 }
Paul Bakker0a597072012-09-25 21:55:46 +00007536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007539 ssl->handshake->flight != NULL )
7540 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007541 /* Cancel handshake timer */
7542 ssl_set_timer( ssl, 0 );
7543
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007544 /* Keep last flight around in case we need to resend it:
7545 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007547 }
7548 else
7549#endif
7550 ssl_handshake_wrapup_free_hs_transform( ssl );
7551
Paul Bakker48916f92012-09-16 19:57:18 +00007552 ssl->state++;
7553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007555}
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007558{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007559 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007562
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007563 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007564
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007565 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007566
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007567 /*
7568 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7569 * may define some other value. Currently (early 2016), no defined
7570 * ciphersuite does this (and this is unlikely to change as activity has
7571 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007576 ssl->verify_data_len = hash_len;
7577 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007578#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007579
Paul Bakker5121ce52009-01-03 21:22:43 +00007580 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7582 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007583
7584 /*
7585 * In case of session resuming, invert the client and server
7586 * ChangeCipherSpec messages order.
7587 */
Paul Bakker0a597072012-09-25 21:55:46 +00007588 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007598 }
7599 else
7600 ssl->state++;
7601
Paul Bakker48916f92012-09-16 19:57:18 +00007602 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007603 * Switch to our negotiated transform and session parameters for outbound
7604 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007610 {
7611 unsigned char i;
7612
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007613 /* Remember current epoch settings for resending */
7614 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007615 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007616
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007617 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007618 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007619
7620 /* Increment epoch */
7621 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007622 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007623 break;
7624
7625 /* The loop goes to its end iff the counter is wrapping */
7626 if( i == 0 )
7627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7629 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007630 }
7631 }
7632 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007634 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007635
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007636 ssl->transform_out = ssl->transform_negotiate;
7637 ssl->session_out = ssl->session_negotiate;
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7640 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7645 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007646 }
7647 }
7648#endif
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007651 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007653#endif
7654
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007655 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007656 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007658 return( ret );
7659 }
7660
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007661#if defined(MBEDTLS_SSL_PROTO_DTLS)
7662 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7663 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7664 {
7665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7666 return( ret );
7667 }
7668#endif
7669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007671
7672 return( 0 );
7673}
7674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007676#define SSL_MAX_HASH_LEN 36
7677#else
7678#define SSL_MAX_HASH_LEN 12
7679#endif
7680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007682{
Paul Bakker23986e52011-04-24 08:57:21 +00007683 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007684 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007685 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007688
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007689 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Hanno Becker327c93b2018-08-15 13:56:18 +01007691 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007694 return( ret );
7695 }
7696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7701 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 }
7704
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007705 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_PROTO_SSL3)
7707 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007708 hash_len = 36;
7709 else
7710#endif
7711 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7714 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007717 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7718 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 }
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007723 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7727 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007729 }
7730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007732 ssl->verify_data_len = hash_len;
7733 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007734#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007735
Paul Bakker0a597072012-09-25 21:55:46 +00007736 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007743 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007746 }
7747 else
7748 ssl->state++;
7749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007753#endif
7754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007756
7757 return( 0 );
7758}
7759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7765 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7766 mbedtls_md5_init( &handshake->fin_md5 );
7767 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007768 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7769 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7772#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007773#if defined(MBEDTLS_USE_PSA_CRYPTO)
7774 handshake->fin_sha256_psa = psa_hash_operation_init();
7775 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7776#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007778 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007779#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007782#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007783 handshake->fin_sha384_psa = psa_hash_operation_init();
7784 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007785#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007787 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007788#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007791
7792 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007793
7794#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7795 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7796 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7797#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799#if defined(MBEDTLS_DHM_C)
7800 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#if defined(MBEDTLS_ECDH_C)
7803 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007804#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007805#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007806 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007807#if defined(MBEDTLS_SSL_CLI_C)
7808 handshake->ecjpake_cache = NULL;
7809 handshake->ecjpake_cache_len = 0;
7810#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007811#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007812
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007813#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007814 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007815#endif
7816
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007817#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7818 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7819#endif
Hanno Becker75173122019-02-06 16:18:31 +00007820
7821#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7822 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7823 mbedtls_pk_init( &handshake->peer_pubkey );
7824#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007825}
7826
Hanno Beckera18d1322018-01-03 14:27:32 +00007827void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7832 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007833
Hanno Beckerd56ed242018-01-03 15:32:51 +00007834#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 mbedtls_md_init( &transform->md_ctx_enc );
7836 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007837#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007838}
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007843}
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007846{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007847 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007848 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007850 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007852 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007853 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007854
7855 /*
7856 * Either the pointers are now NULL or cleared properly and can be freed.
7857 * Now allocate missing structures.
7858 */
7859 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007860 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007861 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007862 }
Paul Bakker48916f92012-09-16 19:57:18 +00007863
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007864 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007865 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007866 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007867 }
Paul Bakker48916f92012-09-16 19:57:18 +00007868
Paul Bakker82788fb2014-10-20 13:59:19 +02007869 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007870 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007871 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007872 }
Paul Bakker48916f92012-09-16 19:57:18 +00007873
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007874 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007875 if( ssl->handshake == NULL ||
7876 ssl->transform_negotiate == NULL ||
7877 ssl->session_negotiate == NULL )
7878 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 mbedtls_free( ssl->handshake );
7882 mbedtls_free( ssl->transform_negotiate );
7883 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007884
7885 ssl->handshake = NULL;
7886 ssl->transform_negotiate = NULL;
7887 ssl->session_negotiate = NULL;
7888
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007889 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007890 }
7891
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007892 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007894 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007895 ssl_handshake_params_init( ssl->handshake );
7896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7899 {
7900 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007901
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7903 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7904 else
7905 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007906
7907 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007908 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007909#endif
7910
Paul Bakker48916f92012-09-16 19:57:18 +00007911 return( 0 );
7912}
7913
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007914#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007915/* Dummy cookie callbacks for defaults */
7916static int ssl_cookie_write_dummy( void *ctx,
7917 unsigned char **p, unsigned char *end,
7918 const unsigned char *cli_id, size_t cli_id_len )
7919{
7920 ((void) ctx);
7921 ((void) p);
7922 ((void) end);
7923 ((void) cli_id);
7924 ((void) cli_id_len);
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007927}
7928
7929static int ssl_cookie_check_dummy( void *ctx,
7930 const unsigned char *cookie, size_t cookie_len,
7931 const unsigned char *cli_id, size_t cli_id_len )
7932{
7933 ((void) ctx);
7934 ((void) cookie);
7935 ((void) cookie_len);
7936 ((void) cli_id);
7937 ((void) cli_id_len);
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007940}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007941#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007942
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007943/* Once ssl->out_hdr as the address of the beginning of the
7944 * next outgoing record is set, deduce the other pointers.
7945 *
7946 * Note: For TLS, we save the implicit record sequence number
7947 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7948 * and the caller has to make sure there's space for this.
7949 */
7950
7951static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7952 mbedtls_ssl_transform *transform )
7953{
7954#if defined(MBEDTLS_SSL_PROTO_DTLS)
7955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7956 {
7957 ssl->out_ctr = ssl->out_hdr + 3;
7958 ssl->out_len = ssl->out_hdr + 11;
7959 ssl->out_iv = ssl->out_hdr + 13;
7960 }
7961 else
7962#endif
7963 {
7964 ssl->out_ctr = ssl->out_hdr - 8;
7965 ssl->out_len = ssl->out_hdr + 3;
7966 ssl->out_iv = ssl->out_hdr + 5;
7967 }
7968
7969 /* Adjust out_msg to make space for explicit IV, if used. */
7970 if( transform != NULL &&
7971 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7972 {
7973 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7974 }
7975 else
7976 ssl->out_msg = ssl->out_iv;
7977}
7978
7979/* Once ssl->in_hdr as the address of the beginning of the
7980 * next incoming record is set, deduce the other pointers.
7981 *
7982 * Note: For TLS, we save the implicit record sequence number
7983 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7984 * and the caller has to make sure there's space for this.
7985 */
7986
7987static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7988 mbedtls_ssl_transform *transform )
7989{
7990#if defined(MBEDTLS_SSL_PROTO_DTLS)
7991 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7992 {
7993 ssl->in_ctr = ssl->in_hdr + 3;
7994 ssl->in_len = ssl->in_hdr + 11;
7995 ssl->in_iv = ssl->in_hdr + 13;
7996 }
7997 else
7998#endif
7999 {
8000 ssl->in_ctr = ssl->in_hdr - 8;
8001 ssl->in_len = ssl->in_hdr + 3;
8002 ssl->in_iv = ssl->in_hdr + 5;
8003 }
8004
8005 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8006 if( transform != NULL &&
8007 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8008 {
8009 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8010 }
8011 else
8012 ssl->in_msg = ssl->in_iv;
8013}
8014
Paul Bakker5121ce52009-01-03 21:22:43 +00008015/*
8016 * Initialize an SSL context
8017 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008018void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8019{
8020 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8021}
8022
8023/*
8024 * Setup an SSL context
8025 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008026
8027static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8028{
8029 /* Set the incoming and outgoing record pointers. */
8030#if defined(MBEDTLS_SSL_PROTO_DTLS)
8031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8032 {
8033 ssl->out_hdr = ssl->out_buf;
8034 ssl->in_hdr = ssl->in_buf;
8035 }
8036 else
8037#endif /* MBEDTLS_SSL_PROTO_DTLS */
8038 {
8039 ssl->out_hdr = ssl->out_buf + 8;
8040 ssl->in_hdr = ssl->in_buf + 8;
8041 }
8042
8043 /* Derive other internal pointers. */
8044 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8045 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8046}
8047
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008048int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008049 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008050{
Paul Bakker48916f92012-09-16 19:57:18 +00008051 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008052
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008053 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008054
8055 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008056 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008057 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008058
8059 /* Set to NULL in case of an error condition */
8060 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008061
Angus Grattond8213d02016-05-25 20:56:48 +10008062 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8063 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008064 {
Angus Grattond8213d02016-05-25 20:56:48 +10008065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008066 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008067 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008068 }
8069
8070 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8071 if( ssl->out_buf == NULL )
8072 {
8073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008074 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008075 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008076 }
8077
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008078 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008079
Paul Bakker48916f92012-09-16 19:57:18 +00008080 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008081 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008082
8083 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008084
8085error:
8086 mbedtls_free( ssl->in_buf );
8087 mbedtls_free( ssl->out_buf );
8088
8089 ssl->conf = NULL;
8090
8091 ssl->in_buf = NULL;
8092 ssl->out_buf = NULL;
8093
8094 ssl->in_hdr = NULL;
8095 ssl->in_ctr = NULL;
8096 ssl->in_len = NULL;
8097 ssl->in_iv = NULL;
8098 ssl->in_msg = NULL;
8099
8100 ssl->out_hdr = NULL;
8101 ssl->out_ctr = NULL;
8102 ssl->out_len = NULL;
8103 ssl->out_iv = NULL;
8104 ssl->out_msg = NULL;
8105
k-stachowiak9f7798e2018-07-31 16:52:32 +02008106 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008107}
8108
8109/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008110 * Reset an initialized and used SSL context for re-use while retaining
8111 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008112 *
8113 * If partial is non-zero, keep data in the input buffer and client ID.
8114 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008115 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008116static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008117{
Paul Bakker48916f92012-09-16 19:57:18 +00008118 int ret;
8119
Hanno Becker7e772132018-08-10 12:38:21 +01008120#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8121 !defined(MBEDTLS_SSL_SRV_C)
8122 ((void) partial);
8123#endif
8124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008126
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008127 /* Cancel any possibly running timer */
8128 ssl_set_timer( ssl, 0 );
8129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130#if defined(MBEDTLS_SSL_RENEGOTIATION)
8131 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008132 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008133
8134 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8136 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008139
Paul Bakker7eb013f2011-10-06 12:37:39 +00008140 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008141 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008142
8143 ssl->in_msgtype = 0;
8144 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008146 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008147 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008150 ssl_dtls_replay_reset( ssl );
8151#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008152
8153 ssl->in_hslen = 0;
8154 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008155
8156 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008157
8158 ssl->out_msgtype = 0;
8159 ssl->out_msglen = 0;
8160 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8162 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008163 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008164#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008165
Hanno Becker19859472018-08-06 09:40:20 +01008166 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8167
Paul Bakker48916f92012-09-16 19:57:18 +00008168 ssl->transform_in = NULL;
8169 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008170
Hanno Becker78640902018-08-13 16:35:15 +01008171 ssl->session_in = NULL;
8172 ssl->session_out = NULL;
8173
Angus Grattond8213d02016-05-25 20:56:48 +10008174 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008175
8176#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008177 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008178#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8179 {
8180 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008181 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008182 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8185 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8188 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8191 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008192 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008193 }
8194#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008195
Paul Bakker48916f92012-09-16 19:57:18 +00008196 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 mbedtls_ssl_transform_free( ssl->transform );
8199 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008200 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008201 }
Paul Bakker48916f92012-09-16 19:57:18 +00008202
Paul Bakkerc0463502013-02-14 11:19:38 +01008203 if( ssl->session )
8204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 mbedtls_ssl_session_free( ssl->session );
8206 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008207 ssl->session = NULL;
8208 }
8209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008211 ssl->alpn_chosen = NULL;
8212#endif
8213
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008214#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008215#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008216 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008217#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008218 {
8219 mbedtls_free( ssl->cli_id );
8220 ssl->cli_id = NULL;
8221 ssl->cli_id_len = 0;
8222 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008223#endif
8224
Paul Bakker48916f92012-09-16 19:57:18 +00008225 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8226 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008227
8228 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008229}
8230
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008231/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008232 * Reset an initialized and used SSL context for re-use while retaining
8233 * all application-set variables, function pointers and data.
8234 */
8235int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8236{
8237 return( ssl_session_reset_int( ssl, 0 ) );
8238}
8239
8240/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008241 * SSL set accessors
8242 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008243void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008244{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008245 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008246}
8247
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008248void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008250 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008251}
8252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008254void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008256 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008257}
8258#endif
8259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008261void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008263 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008264}
8265#endif
8266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008268
Hanno Becker1841b0a2018-08-24 11:13:57 +01008269void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8270 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008271{
8272 ssl->disable_datagram_packing = !allow_packing;
8273}
8274
8275void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8276 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008277{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008278 conf->hs_timeout_min = min;
8279 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008280}
8281#endif
8282
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008283void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008285 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008286}
8287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008289void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008290 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008291 void *p_vrfy )
8292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008293 conf->f_vrfy = f_vrfy;
8294 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008297
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008298void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008299 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008300 void *p_rng )
8301{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008302 conf->f_rng = f_rng;
8303 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008304}
8305
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008306void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008307 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008308 void *p_dbg )
8309{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008310 conf->f_dbg = f_dbg;
8311 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008312}
8313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008315 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008316 mbedtls_ssl_send_t *f_send,
8317 mbedtls_ssl_recv_t *f_recv,
8318 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008319{
8320 ssl->p_bio = p_bio;
8321 ssl->f_send = f_send;
8322 ssl->f_recv = f_recv;
8323 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008324}
8325
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008326#if defined(MBEDTLS_SSL_PROTO_DTLS)
8327void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8328{
8329 ssl->mtu = mtu;
8330}
8331#endif
8332
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008333void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008334{
8335 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008336}
8337
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008338void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8339 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008340 mbedtls_ssl_set_timer_t *f_set_timer,
8341 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008342{
8343 ssl->p_timer = p_timer;
8344 ssl->f_set_timer = f_set_timer;
8345 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008346
8347 /* Make sure we start with no timer running */
8348 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008349}
8350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008352void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008353 void *p_cache,
8354 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8355 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008356{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008357 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008358 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008359 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#if defined(MBEDTLS_SSL_CLI_C)
8364int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008365{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008366 int ret;
8367
8368 if( ssl == NULL ||
8369 session == NULL ||
8370 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008371 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008374 }
8375
Hanno Becker52055ae2019-02-06 14:30:46 +00008376 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8377 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008378 return( ret );
8379
Paul Bakker0a597072012-09-25 21:55:46 +00008380 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008381
8382 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008385
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008386void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008387 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008388{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008389 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8390 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8391 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8392 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008393}
8394
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008395void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008396 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008397 int major, int minor )
8398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008400 return;
8401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008403 return;
8404
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008406}
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008409void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008410 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008411{
8412 conf->cert_profile = profile;
8413}
8414
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008415/* Append a new keycert entry to a (possibly empty) list */
8416static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8417 mbedtls_x509_crt *cert,
8418 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008419{
niisato8ee24222018-06-25 19:05:48 +09008420 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008421
niisato8ee24222018-06-25 19:05:48 +09008422 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8423 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008424 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008425
niisato8ee24222018-06-25 19:05:48 +09008426 new_cert->cert = cert;
8427 new_cert->key = key;
8428 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008429
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008430 /* Update head is the list was null, else add to the end */
8431 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008432 {
niisato8ee24222018-06-25 19:05:48 +09008433 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008434 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008435 else
8436 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008437 mbedtls_ssl_key_cert *cur = *head;
8438 while( cur->next != NULL )
8439 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008440 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008441 }
8442
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008443 return( 0 );
8444}
8445
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008446int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008447 mbedtls_x509_crt *own_cert,
8448 mbedtls_pk_context *pk_key )
8449{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008450 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008451}
8452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008453void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008454 mbedtls_x509_crt *ca_chain,
8455 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008456{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008457 conf->ca_chain = ca_chain;
8458 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008459
8460#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8461 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8462 * cannot be used together. */
8463 conf->f_ca_cb = NULL;
8464 conf->p_ca_cb = NULL;
8465#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008466}
Hanno Becker5adaad92019-03-27 16:54:37 +00008467
8468#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8469void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008470 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008471 void *p_ca_cb )
8472{
8473 conf->f_ca_cb = f_ca_cb;
8474 conf->p_ca_cb = p_ca_cb;
8475
8476 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8477 * cannot be used together. */
8478 conf->ca_chain = NULL;
8479 conf->ca_crl = NULL;
8480}
8481#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008483
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008484#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8485int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8486 mbedtls_x509_crt *own_cert,
8487 mbedtls_pk_context *pk_key )
8488{
8489 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8490 own_cert, pk_key ) );
8491}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008492
8493void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8494 mbedtls_x509_crt *ca_chain,
8495 mbedtls_x509_crl *ca_crl )
8496{
8497 ssl->handshake->sni_ca_chain = ca_chain;
8498 ssl->handshake->sni_ca_crl = ca_crl;
8499}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008500
8501void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8502 int authmode )
8503{
8504 ssl->handshake->sni_authmode = authmode;
8505}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008506#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8507
Hanno Becker8927c832019-04-03 12:52:50 +01008508#if defined(MBEDTLS_X509_CRT_PARSE_C)
8509void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8510 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8511 void *p_vrfy )
8512{
8513 ssl->f_vrfy = f_vrfy;
8514 ssl->p_vrfy = p_vrfy;
8515}
8516#endif
8517
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008518#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008519/*
8520 * Set EC J-PAKE password for current handshake
8521 */
8522int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8523 const unsigned char *pw,
8524 size_t pw_len )
8525{
8526 mbedtls_ecjpake_role role;
8527
Janos Follath8eb64132016-06-03 15:40:57 +01008528 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8530
8531 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8532 role = MBEDTLS_ECJPAKE_SERVER;
8533 else
8534 role = MBEDTLS_ECJPAKE_CLIENT;
8535
8536 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8537 role,
8538 MBEDTLS_MD_SHA256,
8539 MBEDTLS_ECP_DP_SECP256R1,
8540 pw, pw_len ) );
8541}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008542#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008545
8546static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8547{
8548 /* Remove reference to existing PSK, if any. */
8549#if defined(MBEDTLS_USE_PSA_CRYPTO)
8550 if( conf->psk_opaque != 0 )
8551 {
8552 /* The maintenance of the PSK key slot is the
8553 * user's responsibility. */
8554 conf->psk_opaque = 0;
8555 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008556 /* This and the following branch should never
8557 * be taken simultaenously as we maintain the
8558 * invariant that raw and opaque PSKs are never
8559 * configured simultaneously. As a safeguard,
8560 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008561#endif /* MBEDTLS_USE_PSA_CRYPTO */
8562 if( conf->psk != NULL )
8563 {
8564 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8565
8566 mbedtls_free( conf->psk );
8567 conf->psk = NULL;
8568 conf->psk_len = 0;
8569 }
8570
8571 /* Remove reference to PSK identity, if any. */
8572 if( conf->psk_identity != NULL )
8573 {
8574 mbedtls_free( conf->psk_identity );
8575 conf->psk_identity = NULL;
8576 conf->psk_identity_len = 0;
8577 }
8578}
8579
Hanno Becker7390c712018-11-15 13:33:04 +00008580/* This function assumes that PSK identity in the SSL config is unset.
8581 * It checks that the provided identity is well-formed and attempts
8582 * to make a copy of it in the SSL config.
8583 * On failure, the PSK identity in the config remains unset. */
8584static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8585 unsigned char const *psk_identity,
8586 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008587{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008588 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008589 if( psk_identity == NULL ||
8590 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008591 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008592 {
8593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8594 }
8595
Hanno Becker7390c712018-11-15 13:33:04 +00008596 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8597 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008598 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008599
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008600 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008601 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008602
8603 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008604}
8605
Hanno Becker7390c712018-11-15 13:33:04 +00008606int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8607 const unsigned char *psk, size_t psk_len,
8608 const unsigned char *psk_identity, size_t psk_identity_len )
8609{
8610 int ret;
8611 /* Remove opaque/raw PSK + PSK Identity */
8612 ssl_conf_remove_psk( conf );
8613
8614 /* Check and set raw PSK */
8615 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8617 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8618 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8619 conf->psk_len = psk_len;
8620 memcpy( conf->psk, psk, conf->psk_len );
8621
8622 /* Check and set PSK Identity */
8623 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8624 if( ret != 0 )
8625 ssl_conf_remove_psk( conf );
8626
8627 return( ret );
8628}
8629
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008630static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8631{
8632#if defined(MBEDTLS_USE_PSA_CRYPTO)
8633 if( ssl->handshake->psk_opaque != 0 )
8634 {
8635 ssl->handshake->psk_opaque = 0;
8636 }
8637 else
8638#endif /* MBEDTLS_USE_PSA_CRYPTO */
8639 if( ssl->handshake->psk != NULL )
8640 {
8641 mbedtls_platform_zeroize( ssl->handshake->psk,
8642 ssl->handshake->psk_len );
8643 mbedtls_free( ssl->handshake->psk );
8644 ssl->handshake->psk_len = 0;
8645 }
8646}
8647
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008648int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8649 const unsigned char *psk, size_t psk_len )
8650{
8651 if( psk == NULL || ssl->handshake == NULL )
8652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8653
8654 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8656
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008657 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008658
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008659 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008660 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008661
8662 ssl->handshake->psk_len = psk_len;
8663 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8664
8665 return( 0 );
8666}
8667
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008668#if defined(MBEDTLS_USE_PSA_CRYPTO)
8669int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008670 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008671 const unsigned char *psk_identity,
8672 size_t psk_identity_len )
8673{
Hanno Becker7390c712018-11-15 13:33:04 +00008674 int ret;
8675 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008676 ssl_conf_remove_psk( conf );
8677
Hanno Becker7390c712018-11-15 13:33:04 +00008678 /* Check and set opaque PSK */
8679 if( psk_slot == 0 )
8680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008681 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008682
8683 /* Check and set PSK Identity */
8684 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8685 psk_identity_len );
8686 if( ret != 0 )
8687 ssl_conf_remove_psk( conf );
8688
8689 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008690}
8691
8692int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008693 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008694{
8695 if( psk_slot == 0 || ssl->handshake == NULL )
8696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8697
8698 ssl_remove_psk( ssl );
8699 ssl->handshake->psk_opaque = psk_slot;
8700 return( 0 );
8701}
8702#endif /* MBEDTLS_USE_PSA_CRYPTO */
8703
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008704void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008706 size_t),
8707 void *p_psk )
8708{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008709 conf->f_psk = f_psk;
8710 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008713
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008714#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008715
8716#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008717int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008718{
8719 int ret;
8720
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008721 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8722 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8723 {
8724 mbedtls_mpi_free( &conf->dhm_P );
8725 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008726 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008727 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008728
8729 return( 0 );
8730}
Hanno Becker470a8c42017-10-04 15:28:46 +01008731#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008732
Hanno Beckera90658f2017-10-04 15:29:08 +01008733int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8734 const unsigned char *dhm_P, size_t P_len,
8735 const unsigned char *dhm_G, size_t G_len )
8736{
8737 int ret;
8738
8739 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8740 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8741 {
8742 mbedtls_mpi_free( &conf->dhm_P );
8743 mbedtls_mpi_free( &conf->dhm_G );
8744 return( ret );
8745 }
8746
8747 return( 0 );
8748}
Paul Bakker5121ce52009-01-03 21:22:43 +00008749
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008750int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008751{
8752 int ret;
8753
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008754 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8755 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8756 {
8757 mbedtls_mpi_free( &conf->dhm_P );
8758 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008759 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008760 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008761
8762 return( 0 );
8763}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008764#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008765
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008766#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8767/*
8768 * Set the minimum length for Diffie-Hellman parameters
8769 */
8770void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8771 unsigned int bitlen )
8772{
8773 conf->dhm_min_bitlen = bitlen;
8774}
8775#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8776
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008777#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008778/*
8779 * Set allowed/preferred hashes for handshake signatures
8780 */
8781void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8782 const int *hashes )
8783{
8784 conf->sig_hashes = hashes;
8785}
Hanno Becker947194e2017-04-07 13:25:49 +01008786#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008787
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008788#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008789/*
8790 * Set the allowed elliptic curves
8791 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008792void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008793 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008794{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008795 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008796}
Hanno Becker947194e2017-04-07 13:25:49 +01008797#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008798
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008799#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008801{
Hanno Becker947194e2017-04-07 13:25:49 +01008802 /* Initialize to suppress unnecessary compiler warning */
8803 size_t hostname_len = 0;
8804
8805 /* Check if new hostname is valid before
8806 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008807 if( hostname != NULL )
8808 {
8809 hostname_len = strlen( hostname );
8810
8811 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8813 }
8814
8815 /* Now it's clear that we will overwrite the old hostname,
8816 * so we can free it safely */
8817
8818 if( ssl->hostname != NULL )
8819 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008820 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008821 mbedtls_free( ssl->hostname );
8822 }
8823
8824 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008825
Paul Bakker5121ce52009-01-03 21:22:43 +00008826 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008827 {
8828 ssl->hostname = NULL;
8829 }
8830 else
8831 {
8832 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008833 if( ssl->hostname == NULL )
8834 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008835
Hanno Becker947194e2017-04-07 13:25:49 +01008836 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008837
Hanno Becker947194e2017-04-07 13:25:49 +01008838 ssl->hostname[hostname_len] = '\0';
8839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008840
8841 return( 0 );
8842}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008843#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008844
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008845#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008846void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008848 const unsigned char *, size_t),
8849 void *p_sni )
8850{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008851 conf->f_sni = f_sni;
8852 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008857int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008858{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008859 size_t cur_len, tot_len;
8860 const char **p;
8861
8862 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008863 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8864 * MUST NOT be truncated."
8865 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008866 */
8867 tot_len = 0;
8868 for( p = protos; *p != NULL; p++ )
8869 {
8870 cur_len = strlen( *p );
8871 tot_len += cur_len;
8872
8873 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008875 }
8876
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008877 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008878
8879 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008880}
8881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008883{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008884 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008887
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008888void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008890 conf->max_major_ver = major;
8891 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008892}
8893
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008894void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008895{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008896 conf->min_major_ver = major;
8897 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008898}
8899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008901void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008902{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008903 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008904}
8905#endif
8906
Janos Follath088ce432017-04-10 12:42:31 +01008907#if defined(MBEDTLS_SSL_SRV_C)
8908void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8909 char cert_req_ca_list )
8910{
8911 conf->cert_req_ca_list = cert_req_ca_list;
8912}
8913#endif
8914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008916void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008917{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008918 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008919}
8920#endif
8921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008923void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008925 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008926}
8927#endif
8928
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008929#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008930void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008932 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008933}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008934#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008937int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008940 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008943 }
8944
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008945 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008946
8947 return( 0 );
8948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008952void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008953{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008954 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008959void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008960{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008961 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008962}
8963#endif
8964
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008965void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008966{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008967 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008968}
8969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008971void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008973 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008974}
8975
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008976void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008978 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008979}
8980
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008981void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008982 const unsigned char period[8] )
8983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008984 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008989#if defined(MBEDTLS_SSL_CLI_C)
8990void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008991{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008992 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008993}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008994#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008995
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008996#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008997void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8998 mbedtls_ssl_ticket_write_t *f_ticket_write,
8999 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9000 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009001{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009002 conf->f_ticket_write = f_ticket_write;
9003 conf->f_ticket_parse = f_ticket_parse;
9004 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009005}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009008
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009009#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9010void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9011 mbedtls_ssl_export_keys_t *f_export_keys,
9012 void *p_export_keys )
9013{
9014 conf->f_export_keys = f_export_keys;
9015 conf->p_export_keys = p_export_keys;
9016}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009017
9018void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9019 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9020 void *p_export_keys )
9021{
9022 conf->f_export_keys_ext = f_export_keys_ext;
9023 conf->p_export_keys = p_export_keys;
9024}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009025#endif
9026
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009027#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009028void mbedtls_ssl_conf_async_private_cb(
9029 mbedtls_ssl_config *conf,
9030 mbedtls_ssl_async_sign_t *f_async_sign,
9031 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9032 mbedtls_ssl_async_resume_t *f_async_resume,
9033 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009034 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009035{
9036 conf->f_async_sign_start = f_async_sign;
9037 conf->f_async_decrypt_start = f_async_decrypt;
9038 conf->f_async_resume = f_async_resume;
9039 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009040 conf->p_async_config_data = async_config_data;
9041}
9042
Gilles Peskine8f97af72018-04-26 11:46:10 +02009043void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9044{
9045 return( conf->p_async_config_data );
9046}
9047
Gilles Peskine1febfef2018-04-30 11:54:39 +02009048void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009049{
9050 if( ssl->handshake == NULL )
9051 return( NULL );
9052 else
9053 return( ssl->handshake->user_async_ctx );
9054}
9055
Gilles Peskine1febfef2018-04-30 11:54:39 +02009056void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009057 void *ctx )
9058{
9059 if( ssl->handshake != NULL )
9060 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009061}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009062#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009063
Paul Bakker5121ce52009-01-03 21:22:43 +00009064/*
9065 * SSL get accessors
9066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009068{
9069 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9070}
9071
Hanno Becker8b170a02017-10-10 11:51:19 +01009072int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9073{
9074 /*
9075 * Case A: We're currently holding back
9076 * a message for further processing.
9077 */
9078
9079 if( ssl->keep_current_message == 1 )
9080 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009082 return( 1 );
9083 }
9084
9085 /*
9086 * Case B: Further records are pending in the current datagram.
9087 */
9088
9089#if defined(MBEDTLS_SSL_PROTO_DTLS)
9090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9091 ssl->in_left > ssl->next_record_offset )
9092 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009094 return( 1 );
9095 }
9096#endif /* MBEDTLS_SSL_PROTO_DTLS */
9097
9098 /*
9099 * Case C: A handshake message is being processed.
9100 */
9101
Hanno Becker8b170a02017-10-10 11:51:19 +01009102 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9103 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009105 return( 1 );
9106 }
9107
9108 /*
9109 * Case D: An application data message is being processed
9110 */
9111 if( ssl->in_offt != NULL )
9112 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009114 return( 1 );
9115 }
9116
9117 /*
9118 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009119 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009120 * we implement support for multiple alerts in single records.
9121 */
9122
9123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9124 return( 0 );
9125}
9126
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009127uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009128{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009129 if( ssl->session != NULL )
9130 return( ssl->session->verify_result );
9131
9132 if( ssl->session_negotiate != NULL )
9133 return( ssl->session_negotiate->verify_result );
9134
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009135 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009136}
9137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009139{
Paul Bakker926c8e42013-03-06 10:23:34 +01009140 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009141 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009144}
9145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009149 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009150 {
9151 switch( ssl->minor_ver )
9152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009154 return( "DTLSv1.0" );
9155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009157 return( "DTLSv1.2" );
9158
9159 default:
9160 return( "unknown (DTLS)" );
9161 }
9162 }
9163#endif
9164
Paul Bakker43ca69c2011-01-15 17:35:19 +00009165 switch( ssl->minor_ver )
9166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009168 return( "SSLv3.0" );
9169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009171 return( "TLSv1.0" );
9172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009174 return( "TLSv1.1" );
9175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009177 return( "TLSv1.2" );
9178
Paul Bakker43ca69c2011-01-15 17:35:19 +00009179 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009180 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009181 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009182}
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009185{
Hanno Becker3136ede2018-08-17 15:28:19 +01009186 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009188 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009189
Hanno Becker78640902018-08-13 16:35:15 +01009190 if( transform == NULL )
9191 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#if defined(MBEDTLS_ZLIB_SUPPORT)
9194 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9195 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009196#endif
9197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200 case MBEDTLS_MODE_GCM:
9201 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009202 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009204 transform_expansion = transform->minlen;
9205 break;
9206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009208
9209 block_size = mbedtls_cipher_get_block_size(
9210 &transform->cipher_ctx_enc );
9211
Hanno Becker3136ede2018-08-17 15:28:19 +01009212 /* Expansion due to the addition of the MAC. */
9213 transform_expansion += transform->maclen;
9214
9215 /* Expansion due to the addition of CBC padding;
9216 * Theoretically up to 256 bytes, but we never use
9217 * more than the block size of the underlying cipher. */
9218 transform_expansion += block_size;
9219
9220 /* For TLS 1.1 or higher, an explicit IV is added
9221 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009222#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9223 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009224 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009225#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009226
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009227 break;
9228
9229 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009232 }
9233
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009234 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009235}
9236
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9238size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9239{
9240 size_t max_len;
9241
9242 /*
9243 * Assume mfl_code is correct since it was checked when set
9244 */
Angus Grattond8213d02016-05-25 20:56:48 +10009245 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009246
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009247 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009248 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009249 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009250 {
Angus Grattond8213d02016-05-25 20:56:48 +10009251 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009252 }
9253
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009254 /* During a handshake, use the value being negotiated */
9255 if( ssl->session_negotiate != NULL &&
9256 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9257 {
9258 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9259 }
9260
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009261 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009262}
9263#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9264
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009265#if defined(MBEDTLS_SSL_PROTO_DTLS)
9266static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9267{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009268 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9269 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9270 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9271 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9272 return ( 0 );
9273
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009274 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9275 return( ssl->mtu );
9276
9277 if( ssl->mtu == 0 )
9278 return( ssl->handshake->mtu );
9279
9280 return( ssl->mtu < ssl->handshake->mtu ?
9281 ssl->mtu : ssl->handshake->mtu );
9282}
9283#endif /* MBEDTLS_SSL_PROTO_DTLS */
9284
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009285int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9286{
9287 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9288
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009289#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9290 !defined(MBEDTLS_SSL_PROTO_DTLS)
9291 (void) ssl;
9292#endif
9293
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009294#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9295 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9296
9297 if( max_len > mfl )
9298 max_len = mfl;
9299#endif
9300
9301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009302 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009303 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009304 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009305 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9306 const size_t overhead = (size_t) ret;
9307
9308 if( ret < 0 )
9309 return( ret );
9310
9311 if( mtu <= overhead )
9312 {
9313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9314 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9315 }
9316
9317 if( max_len > mtu - overhead )
9318 max_len = mtu - overhead;
9319 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009320#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009321
Hanno Becker0defedb2018-08-10 12:35:02 +01009322#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9323 !defined(MBEDTLS_SSL_PROTO_DTLS)
9324 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009325#endif
9326
9327 return( (int) max_len );
9328}
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_X509_CRT_PARSE_C)
9331const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009332{
9333 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009334 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009335
Hanno Beckere6824572019-02-07 13:18:46 +00009336#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009337 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009338#else
9339 return( NULL );
9340#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009345int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9346 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009347{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009348 if( ssl == NULL ||
9349 dst == NULL ||
9350 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009351 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009354 }
9355
Hanno Becker52055ae2019-02-06 14:30:46 +00009356 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009359
Paul Bakker5121ce52009-01-03 21:22:43 +00009360/*
Paul Bakker1961b702013-01-25 14:49:24 +01009361 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009366
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009367 if( ssl == NULL || ssl->conf == NULL )
9368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009377#endif
9378
Paul Bakker1961b702013-01-25 14:49:24 +01009379 return( ret );
9380}
9381
9382/*
9383 * Perform the SSL handshake
9384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009386{
9387 int ret = 0;
9388
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009389 if( ssl == NULL || ssl->conf == NULL )
9390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009397
9398 if( ret != 0 )
9399 break;
9400 }
9401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009403
9404 return( ret );
9405}
9406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_SSL_RENEGOTIATION)
9408#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009409/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009410 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009413{
9414 int ret;
9415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009417
9418 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9420 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009421
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009422 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009423 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009425 return( ret );
9426 }
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009429
9430 return( 0 );
9431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009433
9434/*
9435 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436 * - any side: calling mbedtls_ssl_renegotiate(),
9437 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9438 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009439 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009440 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009444{
9445 int ret;
9446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009448
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009449 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9450 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009451
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009452 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9453 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009457 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009458 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009459 ssl->handshake->out_msg_seq = 1;
9460 else
9461 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009462 }
9463#endif
9464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9466 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009471 return( ret );
9472 }
9473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009475
9476 return( 0 );
9477}
9478
9479/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009480 * Renegotiate current connection on client,
9481 * or request renegotiation on server
9482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009486
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009487 if( ssl == NULL || ssl->conf == NULL )
9488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009491 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009492 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009498
9499 /* Did we already try/start sending HelloRequest? */
9500 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009502
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009503 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009508 /*
9509 * On client, either start the renegotiation process or,
9510 * if already in progress, continue the handshake
9511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009516
9517 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009520 return( ret );
9521 }
9522 }
9523 else
9524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009528 return( ret );
9529 }
9530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009532
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009533 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009534}
9535
9536/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009537 * Check record counters and renegotiate if they're above the limit.
9538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009540{
Andres AG2196c7f2016-12-15 17:01:16 +00009541 size_t ep_len = ssl_ep_len( ssl );
9542 int in_ctr_cmp;
9543 int out_ctr_cmp;
9544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9546 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009547 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009548 {
9549 return( 0 );
9550 }
9551
Andres AG2196c7f2016-12-15 17:01:16 +00009552 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9553 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009554 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009555 ssl->conf->renego_period + ep_len, 8 - ep_len );
9556
9557 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009558 {
9559 return( 0 );
9560 }
9561
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009566
9567/*
9568 * Receive application data decrypted from the SSL layer
9569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009571{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009572 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009573 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009574
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009575 if( ssl == NULL || ssl->conf == NULL )
9576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009581 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009584 return( ret );
9585
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009586 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009588 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009589 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009590 return( ret );
9591 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009592 }
9593#endif
9594
Hanno Becker4a810fb2017-05-24 16:27:30 +01009595 /*
9596 * Check if renegotiation is necessary and/or handshake is
9597 * in process. If yes, perform/continue, and fall through
9598 * if an unexpected packet is received while the client
9599 * is waiting for the ServerHello.
9600 *
9601 * (There is no equivalent to the last condition on
9602 * the server-side as it is not treated as within
9603 * a handshake while waiting for the ClientHello
9604 * after a renegotiation request.)
9605 */
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009608 ret = ssl_check_ctr_renegotiate( ssl );
9609 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9610 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009613 return( ret );
9614 }
9615#endif
9616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009620 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9621 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009624 return( ret );
9625 }
9626 }
9627
Hanno Beckere41158b2017-10-23 13:30:32 +01009628 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009629 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009630 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009631 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009632 if( ssl->f_get_timer != NULL &&
9633 ssl->f_get_timer( ssl->p_timer ) == -1 )
9634 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009635 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009636 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009637
Hanno Becker327c93b2018-08-15 13:56:18 +01009638 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009639 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009640 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9641 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009642
Hanno Becker4a810fb2017-05-24 16:27:30 +01009643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9644 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009645 }
9646
9647 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009649 {
9650 /*
9651 * OpenSSL sends empty messages to randomize the IV
9652 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009653 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009656 return( 0 );
9657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009659 return( ret );
9660 }
9661 }
9662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009666
Hanno Becker4a810fb2017-05-24 16:27:30 +01009667 /*
9668 * - For client-side, expect SERVER_HELLO_REQUEST.
9669 * - For server-side, expect CLIENT_HELLO.
9670 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9671 */
9672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009674 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009676 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009679
9680 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009682 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009683 {
9684 continue;
9685 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009688 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009689#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009690
Hanno Becker4a810fb2017-05-24 16:27:30 +01009691#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009696
9697 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009700 {
9701 continue;
9702 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009705 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009706#endif /* MBEDTLS_SSL_SRV_C */
9707
Hanno Becker21df7f92017-10-17 11:03:26 +01009708#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009709 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009710 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9711 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9712 ssl->conf->allow_legacy_renegotiation ==
9713 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9714 {
9715 /*
9716 * Accept renegotiation request
9717 */
Paul Bakker48916f92012-09-16 19:57:18 +00009718
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009719 /* DTLS clients need to know renego is server-initiated */
9720#if defined(MBEDTLS_SSL_PROTO_DTLS)
9721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9722 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9723 {
9724 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9725 }
9726#endif
9727 ret = ssl_start_renegotiation( ssl );
9728 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9729 ret != 0 )
9730 {
9731 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9732 return( ret );
9733 }
9734 }
9735 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009736#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009737 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009738 /*
9739 * Refuse renegotiation
9740 */
9741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744#if defined(MBEDTLS_SSL_PROTO_SSL3)
9745 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009746 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009747 /* SSLv3 does not have a "no_renegotiation" warning, so
9748 we send a fatal alert and abort the connection. */
9749 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9750 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9751 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009752 }
9753 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9755#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9756 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9757 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9760 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9761 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009762 {
9763 return( ret );
9764 }
Paul Bakker48916f92012-09-16 19:57:18 +00009765 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009766 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9768 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009772 }
Paul Bakker48916f92012-09-16 19:57:18 +00009773 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009774
Hanno Becker90333da2017-10-10 11:27:13 +01009775 /* At this point, we don't know whether the renegotiation has been
9776 * completed or not. The cases to consider are the following:
9777 * 1) The renegotiation is complete. In this case, no new record
9778 * has been read yet.
9779 * 2) The renegotiation is incomplete because the client received
9780 * an application data record while awaiting the ServerHello.
9781 * 3) The renegotiation is incomplete because the client received
9782 * a non-handshake, non-application data message while awaiting
9783 * the ServerHello.
9784 * In each of these case, looping will be the proper action:
9785 * - For 1), the next iteration will read a new record and check
9786 * if it's application data.
9787 * - For 2), the loop condition isn't satisfied as application data
9788 * is present, hence continue is the same as break
9789 * - For 3), the loop condition is satisfied and read_record
9790 * will re-deliver the message that was held back by the client
9791 * when expecting the ServerHello.
9792 */
9793 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009794 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009795#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009797 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009798 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009799 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009800 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009803 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009805 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009806 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009807 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9811 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009814 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009815 }
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9820 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 }
9822
9823 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009824
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009825 /* We're going to return something now, cancel timer,
9826 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009828 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009829
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009831 /* If we requested renego but received AppData, resend HelloRequest.
9832 * Do it now, after setting in_offt, to avoid taking this branch
9833 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009837 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009838 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009841 return( ret );
9842 }
9843 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009845#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009846 }
9847
9848 n = ( len < ssl->in_msglen )
9849 ? len : ssl->in_msglen;
9850
9851 memcpy( buf, ssl->in_offt, n );
9852 ssl->in_msglen -= n;
9853
9854 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009855 {
9856 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009857 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009858 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009860 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009861 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009862 /* more data available */
9863 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009867
Paul Bakker23986e52011-04-24 08:57:21 +00009868 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009869}
9870
9871/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009872 * Send application data to be encrypted by the SSL layer, taking care of max
9873 * fragment length and buffer size.
9874 *
9875 * According to RFC 5246 Section 6.2.1:
9876 *
9877 * Zero-length fragments of Application data MAY be sent as they are
9878 * potentially useful as a traffic analysis countermeasure.
9879 *
9880 * Therefore, it is possible that the input message length is 0 and the
9881 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009882 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009883static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009884 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009885{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009886 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9887 const size_t max_len = (size_t) ret;
9888
9889 if( ret < 0 )
9890 {
9891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9892 return( ret );
9893 }
9894
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009895 if( len > max_len )
9896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009901 "maximum fragment length: %d > %d",
9902 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009904 }
9905 else
9906#endif
9907 len = max_len;
9908 }
Paul Bakker887bd502011-06-08 13:10:54 +00009909
Paul Bakker5121ce52009-01-03 21:22:43 +00009910 if( ssl->out_left != 0 )
9911 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009912 /*
9913 * The user has previously tried to send the data and
9914 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9915 * written. In this case, we expect the high-level write function
9916 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 return( ret );
9922 }
9923 }
Paul Bakker887bd502011-06-08 13:10:54 +00009924 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009925 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009926 /*
9927 * The user is trying to send a message the first time, so we need to
9928 * copy the data into the internal buffers and setup the data structure
9929 * to keep track of partial writes
9930 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009931 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009933 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009934
Hanno Becker67bc7c32018-08-06 11:33:50 +01009935 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009938 return( ret );
9939 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009940 }
9941
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009942 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009943}
9944
9945/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009946 * Write application data, doing 1/n-1 splitting if necessary.
9947 *
9948 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009949 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009950 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009953static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009954 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009955{
9956 int ret;
9957
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009958 if( ssl->conf->cbc_record_splitting ==
9959 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009960 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9962 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9963 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009964 {
9965 return( ssl_write_real( ssl, buf, len ) );
9966 }
9967
9968 if( ssl->split_done == 0 )
9969 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009970 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009971 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009972 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009973 }
9974
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009975 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9976 return( ret );
9977 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009978
9979 return( ret + 1 );
9980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009982
9983/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009984 * Write application data (public-facing wrapper)
9985 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009986int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009987{
9988 int ret;
9989
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009991
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009992 if( ssl == NULL || ssl->conf == NULL )
9993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9994
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009995#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009996 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9997 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009998 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009999 return( ret );
10000 }
10001#endif
10002
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010003 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010004 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010005 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010006 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010008 return( ret );
10009 }
10010 }
10011
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010012#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010013 ret = ssl_write_split( ssl, buf, len );
10014#else
10015 ret = ssl_write_real( ssl, buf, len );
10016#endif
10017
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010019
10020 return( ret );
10021}
10022
10023/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 * Notify the peer that the connection is being closed
10025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010027{
10028 int ret;
10029
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010030 if( ssl == NULL || ssl->conf == NULL )
10031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010034
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010035 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10041 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10042 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 return( ret );
10046 }
10047 }
10048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010050
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010051 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010052}
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010055{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010056 if( transform == NULL )
10057 return;
10058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010060 deflateEnd( &transform->ctx_deflate );
10061 inflateEnd( &transform->ctx_inflate );
10062#endif
10063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10065 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010066
Hanno Beckerd56ed242018-01-03 15:32:51 +000010067#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 mbedtls_md_free( &transform->md_ctx_enc );
10069 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010070#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010071
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010072 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010073}
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075#if defined(MBEDTLS_X509_CRT_PARSE_C)
10076static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010079
10080 while( cur != NULL )
10081 {
10082 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010084 cur = next;
10085 }
10086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010088
Hanno Becker0271f962018-08-16 13:23:47 +010010089#if defined(MBEDTLS_SSL_PROTO_DTLS)
10090
10091static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10092{
10093 unsigned offset;
10094 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10095
10096 if( hs == NULL )
10097 return;
10098
Hanno Becker283f5ef2018-08-24 09:34:47 +010010099 ssl_free_buffered_record( ssl );
10100
Hanno Becker0271f962018-08-16 13:23:47 +010010101 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010102 ssl_buffering_free_slot( ssl, offset );
10103}
10104
10105static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10106 uint8_t slot )
10107{
10108 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10109 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010110
10111 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10112 return;
10113
Hanno Beckere605b192018-08-21 15:59:07 +010010114 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010115 {
Hanno Beckere605b192018-08-21 15:59:07 +010010116 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010117 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010118 mbedtls_free( hs_buf->data );
10119 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010120 }
10121}
10122
10123#endif /* MBEDTLS_SSL_PROTO_DTLS */
10124
Gilles Peskine9b562d52018-04-25 20:32:43 +020010125void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010126{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010127 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10128
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010129 if( handshake == NULL )
10130 return;
10131
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010132#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10133 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10134 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010135 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010136 handshake->async_in_progress = 0;
10137 }
10138#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10139
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010140#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10141 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10142 mbedtls_md5_free( &handshake->fin_md5 );
10143 mbedtls_sha1_free( &handshake->fin_sha1 );
10144#endif
10145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10146#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010147#if defined(MBEDTLS_USE_PSA_CRYPTO)
10148 psa_hash_abort( &handshake->fin_sha256_psa );
10149#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010150 mbedtls_sha256_free( &handshake->fin_sha256 );
10151#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010152#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010153#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010154#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010155 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010156#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010157 mbedtls_sha512_free( &handshake->fin_sha512 );
10158#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010159#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010160#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162#if defined(MBEDTLS_DHM_C)
10163 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165#if defined(MBEDTLS_ECDH_C)
10166 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010167#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010168#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010169 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010170#if defined(MBEDTLS_SSL_CLI_C)
10171 mbedtls_free( handshake->ecjpake_cache );
10172 handshake->ecjpake_cache = NULL;
10173 handshake->ecjpake_cache_len = 0;
10174#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010175#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010176
Janos Follath4ae5c292016-02-10 11:27:43 +000010177#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10178 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010179 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010181#endif
10182
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010183#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10184 if( handshake->psk != NULL )
10185 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010186 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010187 mbedtls_free( handshake->psk );
10188 }
10189#endif
10190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10192 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010193 /*
10194 * Free only the linked list wrapper, not the keys themselves
10195 * since the belong to the SNI callback
10196 */
10197 if( handshake->sni_key_cert != NULL )
10198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010200
10201 while( cur != NULL )
10202 {
10203 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010205 cur = next;
10206 }
10207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010209
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010210#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010211 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010212 if( handshake->ecrs_peer_cert != NULL )
10213 {
10214 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10215 mbedtls_free( handshake->ecrs_peer_cert );
10216 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010217#endif
10218
Hanno Becker75173122019-02-06 16:18:31 +000010219#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10220 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10221 mbedtls_pk_free( &handshake->peer_pubkey );
10222#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224#if defined(MBEDTLS_SSL_PROTO_DTLS)
10225 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010226 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010227 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010228#endif
10229
Hanno Becker4a63ed42019-01-08 11:39:35 +000010230#if defined(MBEDTLS_ECDH_C) && \
10231 defined(MBEDTLS_USE_PSA_CRYPTO)
10232 psa_destroy_key( handshake->ecdh_psa_privkey );
10233#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10234
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010235 mbedtls_platform_zeroize( handshake,
10236 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010237}
10238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010240{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010241 if( session == NULL )
10242 return;
10243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010245 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010246#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010247
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010248#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010250#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010251
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010252 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010253}
10254
Paul Bakker5121ce52009-01-03 21:22:43 +000010255/*
10256 * Free an SSL context
10257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010258void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010259{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010260 if( ssl == NULL )
10261 return;
10262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010264
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010265 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010266 {
Angus Grattond8213d02016-05-25 20:56:48 +100010267 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010269 }
10270
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010271 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 {
Angus Grattond8213d02016-05-25 20:56:48 +100010273 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010275 }
10276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010278 if( ssl->compress_buf != NULL )
10279 {
Angus Grattond8213d02016-05-25 20:56:48 +100010280 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010282 }
10283#endif
10284
Paul Bakker48916f92012-09-16 19:57:18 +000010285 if( ssl->transform )
10286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 mbedtls_ssl_transform_free( ssl->transform );
10288 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010289 }
10290
10291 if( ssl->handshake )
10292 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010293 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10295 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 mbedtls_free( ssl->handshake );
10298 mbedtls_free( ssl->transform_negotiate );
10299 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010300 }
10301
Paul Bakkerc0463502013-02-14 11:19:38 +010010302 if( ssl->session )
10303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 mbedtls_ssl_session_free( ssl->session );
10305 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010306 }
10307
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010308#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010309 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010310 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010311 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010313 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10317 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10320 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010321 }
10322#endif
10323
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010324#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010326#endif
10327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010329
Paul Bakker86f04f42013-02-14 11:20:09 +010010330 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010331 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010332}
10333
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010334/*
10335 * Initialze mbedtls_ssl_config
10336 */
10337void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10338{
10339 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10340}
10341
Simon Butcherc97b6972015-12-27 23:48:17 +000010342#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010343static int ssl_preset_default_hashes[] = {
10344#if defined(MBEDTLS_SHA512_C)
10345 MBEDTLS_MD_SHA512,
10346 MBEDTLS_MD_SHA384,
10347#endif
10348#if defined(MBEDTLS_SHA256_C)
10349 MBEDTLS_MD_SHA256,
10350 MBEDTLS_MD_SHA224,
10351#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010352#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010353 MBEDTLS_MD_SHA1,
10354#endif
10355 MBEDTLS_MD_NONE
10356};
Simon Butcherc97b6972015-12-27 23:48:17 +000010357#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010358
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010359static int ssl_preset_suiteb_ciphersuites[] = {
10360 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10361 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10362 0
10363};
10364
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010365#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010366static int ssl_preset_suiteb_hashes[] = {
10367 MBEDTLS_MD_SHA256,
10368 MBEDTLS_MD_SHA384,
10369 MBEDTLS_MD_NONE
10370};
10371#endif
10372
10373#if defined(MBEDTLS_ECP_C)
10374static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10375 MBEDTLS_ECP_DP_SECP256R1,
10376 MBEDTLS_ECP_DP_SECP384R1,
10377 MBEDTLS_ECP_DP_NONE
10378};
10379#endif
10380
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010381/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010382 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010383 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010384int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010385 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010386{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010387#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010388 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010389#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010390
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010391 /* Use the functions here so that they are covered in tests,
10392 * but otherwise access member directly for efficiency */
10393 mbedtls_ssl_conf_endpoint( conf, endpoint );
10394 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010395
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010396 /*
10397 * Things that are common to all presets
10398 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010399#if defined(MBEDTLS_SSL_CLI_C)
10400 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10401 {
10402 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10403#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10404 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10405#endif
10406 }
10407#endif
10408
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010409#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010410 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010411#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010412
10413#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10414 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10415#endif
10416
10417#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10418 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10419#endif
10420
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010421#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10422 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10423#endif
10424
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010425#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010426 conf->f_cookie_write = ssl_cookie_write_dummy;
10427 conf->f_cookie_check = ssl_cookie_check_dummy;
10428#endif
10429
10430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10431 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10432#endif
10433
Janos Follath088ce432017-04-10 12:42:31 +010010434#if defined(MBEDTLS_SSL_SRV_C)
10435 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10436#endif
10437
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010438#if defined(MBEDTLS_SSL_PROTO_DTLS)
10439 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10440 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10441#endif
10442
10443#if defined(MBEDTLS_SSL_RENEGOTIATION)
10444 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010445 memset( conf->renego_period, 0x00, 2 );
10446 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010447#endif
10448
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010449#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10450 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10451 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010452 const unsigned char dhm_p[] =
10453 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10454 const unsigned char dhm_g[] =
10455 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10456
Hanno Beckera90658f2017-10-04 15:29:08 +010010457 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10458 dhm_p, sizeof( dhm_p ),
10459 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010460 {
10461 return( ret );
10462 }
10463 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010464#endif
10465
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010466 /*
10467 * Preset-specific defaults
10468 */
10469 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010470 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010471 /*
10472 * NSA Suite B
10473 */
10474 case MBEDTLS_SSL_PRESET_SUITEB:
10475 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10476 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10477 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10478 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10479
10480 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10481 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10482 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10483 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10484 ssl_preset_suiteb_ciphersuites;
10485
10486#if defined(MBEDTLS_X509_CRT_PARSE_C)
10487 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010488#endif
10489
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010490#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010491 conf->sig_hashes = ssl_preset_suiteb_hashes;
10492#endif
10493
10494#if defined(MBEDTLS_ECP_C)
10495 conf->curve_list = ssl_preset_suiteb_curves;
10496#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010497 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010498
10499 /*
10500 * Default
10501 */
10502 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010503 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10504 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10505 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10506 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10507 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10508 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10509 MBEDTLS_SSL_MIN_MINOR_VERSION :
10510 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010511 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10512 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10513
10514#if defined(MBEDTLS_SSL_PROTO_DTLS)
10515 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10516 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10517#endif
10518
10519 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10520 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10521 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10522 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10523 mbedtls_ssl_list_ciphersuites();
10524
10525#if defined(MBEDTLS_X509_CRT_PARSE_C)
10526 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10527#endif
10528
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010529#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010530 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010531#endif
10532
10533#if defined(MBEDTLS_ECP_C)
10534 conf->curve_list = mbedtls_ecp_grp_id_list();
10535#endif
10536
10537#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10538 conf->dhm_min_bitlen = 1024;
10539#endif
10540 }
10541
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010542 return( 0 );
10543}
10544
10545/*
10546 * Free mbedtls_ssl_config
10547 */
10548void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10549{
10550#if defined(MBEDTLS_DHM_C)
10551 mbedtls_mpi_free( &conf->dhm_P );
10552 mbedtls_mpi_free( &conf->dhm_G );
10553#endif
10554
10555#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10556 if( conf->psk != NULL )
10557 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010558 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010559 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010560 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010561 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010562 }
10563
10564 if( conf->psk_identity != NULL )
10565 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010566 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010567 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010568 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010569 conf->psk_identity_len = 0;
10570 }
10571#endif
10572
10573#if defined(MBEDTLS_X509_CRT_PARSE_C)
10574 ssl_key_cert_free( conf->key_cert );
10575#endif
10576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010577 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010578}
10579
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010580#if defined(MBEDTLS_PK_C) && \
10581 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010582/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587#if defined(MBEDTLS_RSA_C)
10588 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10589 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_ECDSA_C)
10592 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10593 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010596}
10597
Hanno Becker7e5437a2017-04-28 17:15:26 +010010598unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10599{
10600 switch( type ) {
10601 case MBEDTLS_PK_RSA:
10602 return( MBEDTLS_SSL_SIG_RSA );
10603 case MBEDTLS_PK_ECDSA:
10604 case MBEDTLS_PK_ECKEY:
10605 return( MBEDTLS_SSL_SIG_ECDSA );
10606 default:
10607 return( MBEDTLS_SSL_SIG_ANON );
10608 }
10609}
10610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010612{
10613 switch( sig )
10614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615#if defined(MBEDTLS_RSA_C)
10616 case MBEDTLS_SSL_SIG_RSA:
10617 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619#if defined(MBEDTLS_ECDSA_C)
10620 case MBEDTLS_SSL_SIG_ECDSA:
10621 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010622#endif
10623 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010625 }
10626}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010627#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010628
Hanno Becker7e5437a2017-04-28 17:15:26 +010010629#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10630 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10631
10632/* Find an entry in a signature-hash set matching a given hash algorithm. */
10633mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10634 mbedtls_pk_type_t sig_alg )
10635{
10636 switch( sig_alg )
10637 {
10638 case MBEDTLS_PK_RSA:
10639 return( set->rsa );
10640 case MBEDTLS_PK_ECDSA:
10641 return( set->ecdsa );
10642 default:
10643 return( MBEDTLS_MD_NONE );
10644 }
10645}
10646
10647/* Add a signature-hash-pair to a signature-hash set */
10648void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10649 mbedtls_pk_type_t sig_alg,
10650 mbedtls_md_type_t md_alg )
10651{
10652 switch( sig_alg )
10653 {
10654 case MBEDTLS_PK_RSA:
10655 if( set->rsa == MBEDTLS_MD_NONE )
10656 set->rsa = md_alg;
10657 break;
10658
10659 case MBEDTLS_PK_ECDSA:
10660 if( set->ecdsa == MBEDTLS_MD_NONE )
10661 set->ecdsa = md_alg;
10662 break;
10663
10664 default:
10665 break;
10666 }
10667}
10668
10669/* Allow exactly one hash algorithm for each signature. */
10670void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10671 mbedtls_md_type_t md_alg )
10672{
10673 set->rsa = md_alg;
10674 set->ecdsa = md_alg;
10675}
10676
10677#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10678 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10679
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010680/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010681 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010684{
10685 switch( hash )
10686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#if defined(MBEDTLS_MD5_C)
10688 case MBEDTLS_SSL_HASH_MD5:
10689 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_SHA1_C)
10692 case MBEDTLS_SSL_HASH_SHA1:
10693 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695#if defined(MBEDTLS_SHA256_C)
10696 case MBEDTLS_SSL_HASH_SHA224:
10697 return( MBEDTLS_MD_SHA224 );
10698 case MBEDTLS_SSL_HASH_SHA256:
10699 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701#if defined(MBEDTLS_SHA512_C)
10702 case MBEDTLS_SSL_HASH_SHA384:
10703 return( MBEDTLS_MD_SHA384 );
10704 case MBEDTLS_SSL_HASH_SHA512:
10705 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010706#endif
10707 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010709 }
10710}
10711
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010712/*
10713 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10714 */
10715unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10716{
10717 switch( md )
10718 {
10719#if defined(MBEDTLS_MD5_C)
10720 case MBEDTLS_MD_MD5:
10721 return( MBEDTLS_SSL_HASH_MD5 );
10722#endif
10723#if defined(MBEDTLS_SHA1_C)
10724 case MBEDTLS_MD_SHA1:
10725 return( MBEDTLS_SSL_HASH_SHA1 );
10726#endif
10727#if defined(MBEDTLS_SHA256_C)
10728 case MBEDTLS_MD_SHA224:
10729 return( MBEDTLS_SSL_HASH_SHA224 );
10730 case MBEDTLS_MD_SHA256:
10731 return( MBEDTLS_SSL_HASH_SHA256 );
10732#endif
10733#if defined(MBEDTLS_SHA512_C)
10734 case MBEDTLS_MD_SHA384:
10735 return( MBEDTLS_SSL_HASH_SHA384 );
10736 case MBEDTLS_MD_SHA512:
10737 return( MBEDTLS_SSL_HASH_SHA512 );
10738#endif
10739 default:
10740 return( MBEDTLS_SSL_HASH_NONE );
10741 }
10742}
10743
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010744#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010745/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010746 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010747 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010748 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010749int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010752
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010753 if( ssl->conf->curve_list == NULL )
10754 return( -1 );
10755
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010756 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010757 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010758 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010759
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010760 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010761}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010762#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010763
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010764#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010765/*
10766 * Check if a hash proposed by the peer is in our list.
10767 * Return 0 if we're willing to use it, -1 otherwise.
10768 */
10769int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10770 mbedtls_md_type_t md )
10771{
10772 const int *cur;
10773
10774 if( ssl->conf->sig_hashes == NULL )
10775 return( -1 );
10776
10777 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10778 if( *cur == (int) md )
10779 return( 0 );
10780
10781 return( -1 );
10782}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010783#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785#if defined(MBEDTLS_X509_CRT_PARSE_C)
10786int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10787 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010788 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010789 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010790{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010791 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010793 int usage = 0;
10794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010796 const char *ext_oid;
10797 size_t ext_len;
10798#endif
10799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10801 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010802 ((void) cert);
10803 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010804 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010805#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10808 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010809 {
10810 /* Server part of the key exchange */
10811 switch( ciphersuite->key_exchange )
10812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813 case MBEDTLS_KEY_EXCHANGE_RSA:
10814 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010815 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010816 break;
10817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010818 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10819 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10820 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10821 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010822 break;
10823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10825 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010826 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010827 break;
10828
10829 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830 case MBEDTLS_KEY_EXCHANGE_NONE:
10831 case MBEDTLS_KEY_EXCHANGE_PSK:
10832 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10833 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010834 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010835 usage = 0;
10836 }
10837 }
10838 else
10839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10841 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010842 }
10843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010845 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010846 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010847 ret = -1;
10848 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010849#else
10850 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10854 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10857 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010858 }
10859 else
10860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10862 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010863 }
10864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010866 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010867 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010868 ret = -1;
10869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010871
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010872 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010875
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010876/*
10877 * Convert version numbers to/from wire format
10878 * and, for DTLS, to/from TLS equivalent.
10879 *
10880 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010881 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010882 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10883 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010886 unsigned char ver[2] )
10887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888#if defined(MBEDTLS_SSL_PROTO_DTLS)
10889 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010892 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10893
10894 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10895 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10896 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010897 else
10898#else
10899 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010900#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010901 {
10902 ver[0] = (unsigned char) major;
10903 ver[1] = (unsigned char) minor;
10904 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010905}
10906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908 const unsigned char ver[2] )
10909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910#if defined(MBEDTLS_SSL_PROTO_DTLS)
10911 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010912 {
10913 *major = 255 - ver[0] + 2;
10914 *minor = 255 - ver[1] + 1;
10915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010917 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10918 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010919 else
10920#else
10921 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010922#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010923 {
10924 *major = ver[0];
10925 *minor = ver[1];
10926 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010927}
10928
Simon Butcher99000142016-10-13 17:21:01 +010010929int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10930{
10931#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10932 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10933 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10934
10935 switch( md )
10936 {
10937#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10938#if defined(MBEDTLS_MD5_C)
10939 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010940 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010941#endif
10942#if defined(MBEDTLS_SHA1_C)
10943 case MBEDTLS_SSL_HASH_SHA1:
10944 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10945 break;
10946#endif
10947#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10948#if defined(MBEDTLS_SHA512_C)
10949 case MBEDTLS_SSL_HASH_SHA384:
10950 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10951 break;
10952#endif
10953#if defined(MBEDTLS_SHA256_C)
10954 case MBEDTLS_SSL_HASH_SHA256:
10955 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10956 break;
10957#endif
10958 default:
10959 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10960 }
10961
10962 return 0;
10963#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10964 (void) ssl;
10965 (void) md;
10966
10967 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10968#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10969}
10970
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010971#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10972 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10973int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10974 unsigned char *output,
10975 unsigned char *data, size_t data_len )
10976{
10977 int ret = 0;
10978 mbedtls_md5_context mbedtls_md5;
10979 mbedtls_sha1_context mbedtls_sha1;
10980
10981 mbedtls_md5_init( &mbedtls_md5 );
10982 mbedtls_sha1_init( &mbedtls_sha1 );
10983
10984 /*
10985 * digitally-signed struct {
10986 * opaque md5_hash[16];
10987 * opaque sha_hash[20];
10988 * };
10989 *
10990 * md5_hash
10991 * MD5(ClientHello.random + ServerHello.random
10992 * + ServerParams);
10993 * sha_hash
10994 * SHA(ClientHello.random + ServerHello.random
10995 * + ServerParams);
10996 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010997 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010998 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011000 goto exit;
11001 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011002 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011003 ssl->handshake->randbytes, 64 ) ) != 0 )
11004 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011006 goto exit;
11007 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011008 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 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_update_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_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011014 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011016 goto exit;
11017 }
11018
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011019 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_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_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011025 ssl->handshake->randbytes, 64 ) ) != 0 )
11026 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011028 goto exit;
11029 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011030 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011031 data_len ) ) != 0 )
11032 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011034 goto exit;
11035 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011036 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011037 output + 16 ) ) != 0 )
11038 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011040 goto exit;
11041 }
11042
11043exit:
11044 mbedtls_md5_free( &mbedtls_md5 );
11045 mbedtls_sha1_free( &mbedtls_sha1 );
11046
11047 if( ret != 0 )
11048 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11049 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11050
11051 return( ret );
11052
11053}
11054#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11055 MBEDTLS_SSL_PROTO_TLS1_1 */
11056
11057#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11058 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011059
11060#if defined(MBEDTLS_USE_PSA_CRYPTO)
11061int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11062 unsigned char *hash, size_t *hashlen,
11063 unsigned char *data, size_t data_len,
11064 mbedtls_md_type_t md_alg )
11065{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011066 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011067 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011068 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11069
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011071
11072 if( ( status = psa_hash_setup( &hash_operation,
11073 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011074 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011075 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011076 goto exit;
11077 }
11078
Andrzej Kurek814feff2019-01-14 04:35:19 -050011079 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11080 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011081 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011082 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011083 goto exit;
11084 }
11085
Andrzej Kurek814feff2019-01-14 04:35:19 -050011086 if( ( status = psa_hash_update( &hash_operation,
11087 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011088 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011089 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011090 goto exit;
11091 }
11092
Andrzej Kurek814feff2019-01-14 04:35:19 -050011093 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11094 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011095 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011096 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011097 goto exit;
11098 }
11099
11100exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011101 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011102 {
11103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11104 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011105 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011106 {
11107 case PSA_ERROR_NOT_SUPPORTED:
11108 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011109 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011110 case PSA_ERROR_BUFFER_TOO_SMALL:
11111 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11112 case PSA_ERROR_INSUFFICIENT_MEMORY:
11113 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11114 default:
11115 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11116 }
11117 }
11118 return( 0 );
11119}
11120
11121#else
11122
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011123int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011124 unsigned char *hash, size_t *hashlen,
11125 unsigned char *data, size_t data_len,
11126 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011127{
11128 int ret = 0;
11129 mbedtls_md_context_t ctx;
11130 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011131 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011132
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011134
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135 mbedtls_md_init( &ctx );
11136
11137 /*
11138 * digitally-signed struct {
11139 * opaque client_random[32];
11140 * opaque server_random[32];
11141 * ServerDHParams params;
11142 * };
11143 */
11144 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11145 {
11146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11147 goto exit;
11148 }
11149 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11150 {
11151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11152 goto exit;
11153 }
11154 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11155 {
11156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11157 goto exit;
11158 }
11159 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11160 {
11161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11162 goto exit;
11163 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011164 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011165 {
11166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11167 goto exit;
11168 }
11169
11170exit:
11171 mbedtls_md_free( &ctx );
11172
11173 if( ret != 0 )
11174 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11175 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11176
11177 return( ret );
11178}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011179#endif /* MBEDTLS_USE_PSA_CRYPTO */
11180
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11182 MBEDTLS_SSL_PROTO_TLS1_2 */
11183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011184#endif /* MBEDTLS_SSL_TLS_C */