blob: bc4b621d0926eb42f999e86bdb60acddbba0f5e5 [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" ) );
959 transform->in_cid_len = ssl->own_cid_len;
960 transform->out_cid_len = ssl->handshake->peer_cid_len;
961 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
962 memcpy( transform->out_cid, ssl->handshake->peer_cid,
963 ssl->handshake->peer_cid_len );
964
965 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
966 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100967 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100968 transform->in_cid_len );
969 }
970#endif /* MBEDTLS_SSL_CID */
971
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000973 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SSL_PROTO_SSL3)
976 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000977 {
Paul Bakker48916f92012-09-16 19:57:18 +0000978 handshake->tls_prf = ssl3_prf;
979 handshake->calc_verify = ssl_calc_verify_ssl;
980 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000981 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200982 else
983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
985 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000986 {
Paul Bakker48916f92012-09-16 19:57:18 +0000987 handshake->tls_prf = tls1_prf;
988 handshake->calc_verify = ssl_calc_verify_tls;
989 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000990 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200991 else
992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
994#if defined(MBEDTLS_SHA512_C)
995 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000996 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000997 {
Paul Bakker48916f92012-09-16 19:57:18 +0000998 handshake->tls_prf = tls_prf_sha384;
999 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1000 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001001 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001002 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_SHA256_C)
1005 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001006 {
Paul Bakker48916f92012-09-16 19:57:18 +00001007 handshake->tls_prf = tls_prf_sha256;
1008 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1009 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001010 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001011 else
1012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001017 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001018
1019 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 * SSLv3:
1021 * master =
1022 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1023 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1024 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001025 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001026 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 * master = PRF( premaster, "master secret", randbytes )[0..47]
1028 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001029 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1032 }
1033 else
1034 {
1035 /* The label for the KDF used for key expansion.
1036 * This is either "master secret" or "extended master secret"
1037 * depending on whether the Extended Master Secret extension
1038 * is used. */
1039 char const *lbl = "master secret";
1040
1041 /* The salt for the KDF used for key expansion.
1042 * - If the Extended Master Secret extension is not used,
1043 * this is ClientHello.Random + ServerHello.Random
1044 * (see Sect. 8.1 in RFC 5246).
1045 * - If the Extended Master Secret extension is used,
1046 * this is the transcript of the handshake so far.
1047 * (see Sect. 4 in RFC 7627). */
1048 unsigned char const *salt = handshake->randbytes;
1049 size_t salt_len = 64;
1050
1051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001055
Hanno Becker35b23c72018-10-23 12:10:41 +01001056 lbl = "extended master secret";
1057 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001058 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1060 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001063 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1064 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001065 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001066 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001067 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001068#endif /* MBEDTLS_SHA512_C */
1069 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001070 }
1071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001073 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001074
Hanno Becker35b23c72018-10-23 12:10:41 +01001075 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001076 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001077#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1078
Hanno Becker7d0a5692018-10-23 15:26:22 +01001079#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1080 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1081 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1082 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1083 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001084 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001085 /* Perform PSK-to-MS expansion in a single step. */
1086 psa_status_t status;
1087 psa_algorithm_t alg;
1088 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001089 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001090
1091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1092
1093 psk = ssl->conf->psk_opaque;
1094 if( ssl->handshake->psk_opaque != 0 )
1095 psk = ssl->handshake->psk_opaque;
1096
Hanno Becker22bf1452019-04-05 11:21:08 +01001097 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001098 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1099 else
1100 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1101
1102 status = psa_key_derivation( &generator, psk, alg,
1103 salt, salt_len,
1104 (unsigned char const *) lbl,
1105 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001106 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001107 if( status != PSA_SUCCESS )
1108 {
1109 psa_generator_abort( &generator );
1110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1111 }
1112
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001113 status = psa_generator_read( &generator, session->master,
1114 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001115 if( status != PSA_SUCCESS )
1116 {
1117 psa_generator_abort( &generator );
1118 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1119 }
1120
1121 status = psa_generator_abort( &generator );
1122 if( status != PSA_SUCCESS )
1123 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001124 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001125 else
1126#endif
1127 {
1128 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1129 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001130 session->master,
1131 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001132 if( ret != 0 )
1133 {
1134 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1135 return( ret );
1136 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001137
Hanno Becker7d0a5692018-10-23 15:26:22 +01001138 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1139 handshake->premaster,
1140 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001141
Hanno Becker7d0a5692018-10-23 15:26:22 +01001142 mbedtls_platform_zeroize( handshake->premaster,
1143 sizeof(handshake->premaster) );
1144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
1147 /*
1148 * Swap the client and server random values.
1149 */
Paul Bakker48916f92012-09-16 19:57:18 +00001150 memcpy( tmp, handshake->randbytes, 64 );
1151 memcpy( handshake->randbytes, tmp + 32, 32 );
1152 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155 /*
1156 * SSLv3:
1157 * key block =
1158 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1159 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1160 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1161 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1162 * ...
1163 *
1164 * TLSv1:
1165 * key block = PRF( master, "key expansion", randbytes )
1166 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001167 ret = handshake->tls_prf( session->master, 48, "key expansion",
1168 handshake->randbytes, 64, keyblk, 256 );
1169 if( ret != 0 )
1170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001172 return( ret );
1173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1176 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1177 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1178 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1179 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 /*
1182 * Determine the appropriate key, IV and MAC length.
1183 */
Paul Bakker68884e32013-01-07 18:20:04 +01001184
Hanno Becker88aaf652017-12-27 08:17:40 +00001185 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001186
Hanno Becker8031d062018-01-03 15:32:31 +00001187#if defined(MBEDTLS_GCM_C) || \
1188 defined(MBEDTLS_CCM_C) || \
1189 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001191 cipher_info->mode == MBEDTLS_MODE_CCM ||
1192 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001194 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001195
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001196 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001197 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001198 transform->taglen =
1199 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001200
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001201 /* All modes haves 96-bit IVs;
1202 * GCM and CCM has 4 implicit and 8 explicit bytes
1203 * ChachaPoly has all 12 bytes implicit
1204 */
Paul Bakker68884e32013-01-07 18:20:04 +01001205 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001206 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1207 transform->fixed_ivlen = 12;
1208 else
1209 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001210
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001211 /* Minimum length of encrypted record */
1212 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001213 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001214 }
1215 else
Hanno Becker8031d062018-01-03 15:32:31 +00001216#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1217#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1218 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1219 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001220 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001221 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1223 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001226 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001228
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001229 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001230 mac_key_len = mbedtls_md_get_size( md_info );
1231 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001234 /*
1235 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1236 * (rfc 6066 page 13 or rfc 2104 section 4),
1237 * so we only need to adjust the length here.
1238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001242
1243#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1244 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001245 * HMAC implementation which also truncates the key
1246 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001247 mac_key_len = transform->maclen;
1248#endif
1249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001251
1252 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001253 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001255 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001257 transform->minlen = transform->maclen;
1258 else
Paul Bakker68884e32013-01-07 18:20:04 +01001259 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001260 /*
1261 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001262 * 1. if EtM is in use: one block plus MAC
1263 * otherwise: * first multiple of blocklen greater than maclen
1264 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1267 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001268 {
1269 transform->minlen = transform->maclen
1270 + cipher_info->block_size;
1271 }
1272 else
1273#endif
1274 {
1275 transform->minlen = transform->maclen
1276 + cipher_info->block_size
1277 - transform->maclen % cipher_info->block_size;
1278 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1281 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1282 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001283 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001284 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1287 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1288 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001289 {
1290 transform->minlen += transform->ivlen;
1291 }
1292 else
1293#endif
1294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001298 }
Paul Bakker68884e32013-01-07 18:20:04 +01001299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
Hanno Becker8031d062018-01-03 15:32:31 +00001301 else
1302#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1303 {
1304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Hanno Becker88aaf652017-12-27 08:17:40 +00001308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1309 (unsigned) keylen,
1310 (unsigned) transform->minlen,
1311 (unsigned) transform->ivlen,
1312 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
1314 /*
1315 * Finally setup the cipher contexts, IVs and MAC secrets.
1316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001318 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001320 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001321 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Paul Bakker68884e32013-01-07 18:20:04 +01001323 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001324 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326 /*
1327 * This is not used in TLS v1.1.
1328 */
Paul Bakker48916f92012-09-16 19:57:18 +00001329 iv_copy_len = ( transform->fixed_ivlen ) ?
1330 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001331 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1332 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001333 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 }
1335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_SSL_CLI_C */
1337#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001340 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001341 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Hanno Becker81c7b182017-11-09 18:39:33 +00001343 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001344 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001346 /*
1347 * This is not used in TLS v1.1.
1348 */
Paul Bakker48916f92012-09-16 19:57:18 +00001349 iv_copy_len = ( transform->fixed_ivlen ) ?
1350 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001351 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1352 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001359 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1360 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Hanno Beckerd56ed242018-01-03 15:32:51 +00001363#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_SSL_PROTO_SSL3)
1365 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001366 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001367 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001370 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1371 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001372 }
1373
Hanno Becker81c7b182017-11-09 18:39:33 +00001374 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1375 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001376 }
1377 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1379#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1380 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1381 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001382 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001383 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1384 For AEAD-based ciphersuites, there is nothing to do here. */
1385 if( mac_key_len != 0 )
1386 {
1387 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1388 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1389 }
Paul Bakker68884e32013-01-07 18:20:04 +01001390 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001391 else
1392#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001395 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1396 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001397 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001398#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1401 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001402 {
1403 int ret = 0;
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001406
Hanno Becker88aaf652017-12-27 08:17:40 +00001407 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001408 transform->iv_enc, transform->iv_dec,
1409 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001410 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001411 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001414 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1415 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001416 }
1417 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001418#else
1419 ((void) mac_dec);
1420 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001422
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001423#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1424 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001425 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001426 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1427 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001428 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001429 iv_copy_len );
1430 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001431
1432 if( ssl->conf->f_export_keys_ext != NULL )
1433 {
1434 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1435 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001436 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001437 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001438 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001439 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001440 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001441 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001442#endif
1443
Hanno Beckerf704bef2018-11-16 15:21:18 +00001444#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001445
1446 /* Only use PSA-based ciphers for TLS-1.2.
1447 * That's relevant at least for TLS-1.0, where
1448 * we assume that mbedtls_cipher_crypt() updates
1449 * the structure field for the IV, which the PSA-based
1450 * implementation currently doesn't. */
1451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1452 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001453 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001454 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001455 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001456 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1457 {
1458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001459 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001460 }
1461
1462 if( ret == 0 )
1463 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001465 psa_fallthrough = 0;
1466 }
1467 else
1468 {
1469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1470 psa_fallthrough = 1;
1471 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001472 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001473 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 psa_fallthrough = 1;
1475#else
1476 psa_fallthrough = 1;
1477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001478
Hanno Beckercb1cc802018-11-17 22:27:38 +00001479 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001480#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001481 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001482 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001486 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001487
Hanno Beckerf704bef2018-11-16 15:21:18 +00001488#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001489 /* Only use PSA-based ciphers for TLS-1.2.
1490 * That's relevant at least for TLS-1.0, where
1491 * we assume that mbedtls_cipher_crypt() updates
1492 * the structure field for the IV, which the PSA-based
1493 * implementation currently doesn't. */
1494#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1495 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001496 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001497 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001498 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1500 {
1501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001502 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001503 }
1504
1505 if( ret == 0 )
1506 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001508 psa_fallthrough = 0;
1509 }
1510 else
1511 {
1512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1513 psa_fallthrough = 1;
1514 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001515 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001516 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001517 psa_fallthrough = 1;
1518#else
1519 psa_fallthrough = 1;
1520#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001521
Hanno Beckercb1cc802018-11-17 22:27:38 +00001522 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001523#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001524 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001525 cipher_info ) ) != 0 )
1526 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001528 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001529 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001532 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001536 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001537 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001540 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001544 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_CIPHER_MODE_CBC)
1548 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1551 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001554 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001555 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1558 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001561 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001568 // Initialize compression
1569 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001571 {
Paul Bakker16770332013-10-11 09:59:44 +02001572 if( ssl->compress_buf == NULL )
1573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001575 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001576 if( ssl->compress_buf == NULL )
1577 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001579 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001580 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1581 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001582 }
1583 }
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586
Paul Bakker48916f92012-09-16 19:57:18 +00001587 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1588 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001589
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001590 if( deflateInit( &transform->ctx_deflate,
1591 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001592 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001595 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1596 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597 }
1598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001602end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001603 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1604 mbedtls_platform_zeroize( handshake->randbytes,
1605 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001606 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001607}
1608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_SSL_PROTO_SSL3)
1610void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001612 mbedtls_md5_context md5;
1613 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 unsigned char pad_1[48];
1615 unsigned char pad_2[48];
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001619 mbedtls_md5_init( &md5 );
1620 mbedtls_sha1_init( &sha1 );
1621
1622 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1623 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Paul Bakker380da532012-04-18 16:10:25 +00001625 memset( pad_1, 0x36, 48 );
1626 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001628 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1629 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1630 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001632 mbedtls_md5_starts_ret( &md5 );
1633 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1634 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1635 mbedtls_md5_update_ret( &md5, hash, 16 );
1636 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001638 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1639 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1640 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001641
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001642 mbedtls_sha1_starts_ret( &sha1 );
1643 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1644 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1645 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1646 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001650
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001651 mbedtls_md5_free( &md5 );
1652 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001653
Paul Bakker380da532012-04-18 16:10:25 +00001654 return;
1655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1659void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001660{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001661 mbedtls_md5_context md5;
1662 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001665
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001666 mbedtls_md5_init( &md5 );
1667 mbedtls_sha1_init( &sha1 );
1668
1669 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1670 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001671
Andrzej Kurekeb342242019-01-29 09:14:33 -05001672 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001673 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001678 mbedtls_md5_free( &md5 );
1679 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001680
Paul Bakker380da532012-04-18 16:10:25 +00001681 return;
1682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1686#if defined(MBEDTLS_SHA256_C)
1687void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001688{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001689#if defined(MBEDTLS_USE_PSA_CRYPTO)
1690 size_t hash_size;
1691 psa_status_t status;
1692 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1693
1694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1695 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1696 if( status != PSA_SUCCESS )
1697 {
1698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1699 return;
1700 }
1701
1702 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1703 if( status != PSA_SUCCESS )
1704 {
1705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1706 return;
1707 }
1708 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1710#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001711 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001712
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001713 mbedtls_sha256_init( &sha256 );
1714
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001716
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001717 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001718 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001722
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001723 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001724#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001725 return;
1726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_SHA512_C)
1730void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001731{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001732#if defined(MBEDTLS_USE_PSA_CRYPTO)
1733 size_t hash_size;
1734 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001735 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001736
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001738 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001739 if( status != PSA_SUCCESS )
1740 {
1741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1742 return;
1743 }
1744
Andrzej Kurek972fba52019-01-30 03:29:12 -05001745 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001746 if( status != PSA_SUCCESS )
1747 {
1748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1749 return;
1750 }
1751 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1753#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001755
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001756 mbedtls_sha512_init( &sha512 );
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001759
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001760 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001761 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001766 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001767#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 return;
1769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#endif /* MBEDTLS_SHA512_C */
1771#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1774int 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 +02001775{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001776 unsigned char *p = ssl->handshake->premaster;
1777 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001778 const unsigned char *psk = ssl->conf->psk;
1779 size_t psk_len = ssl->conf->psk_len;
1780
1781 /* If the psk callback was called, use its result */
1782 if( ssl->handshake->psk != NULL )
1783 {
1784 psk = ssl->handshake->psk;
1785 psk_len = ssl->handshake->psk_len;
1786 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001787
1788 /*
1789 * PMS = struct {
1790 * opaque other_secret<0..2^16-1>;
1791 * opaque psk<0..2^16-1>;
1792 * };
1793 * with "other_secret" depending on the particular key exchange
1794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1796 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001797 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001798 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001800
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001801 *(p++) = (unsigned char)( psk_len >> 8 );
1802 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001803
1804 if( end < p || (size_t)( end - p ) < psk_len )
1805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1806
1807 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001808 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001809 }
1810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1812#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1813 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001814 {
1815 /*
1816 * other_secret already set by the ClientKeyExchange message,
1817 * and is 48 bytes long
1818 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001819 if( end - p < 2 )
1820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1821
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001822 *p++ = 0;
1823 *p++ = 48;
1824 p += 48;
1825 }
1826 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1828#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1829 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001830 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001831 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001832 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001833
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001834 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001836 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001837 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001840 return( ret );
1841 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001842 *(p++) = (unsigned char)( len >> 8 );
1843 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001844 p += len;
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001847 }
1848 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1850#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1851 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001852 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001853 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001854 size_t zlen;
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001857 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001858 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001861 return( ret );
1862 }
1863
1864 *(p++) = (unsigned char)( zlen >> 8 );
1865 *(p++) = (unsigned char)( zlen );
1866 p += zlen;
1867
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001868 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1869 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 }
1871 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001876 }
1877
1878 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001879 if( end - p < 2 )
1880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001881
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001882 *(p++) = (unsigned char)( psk_len >> 8 );
1883 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001884
1885 if( end < p || (size_t)( end - p ) < psk_len )
1886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1887
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001888 memcpy( p, psk, psk_len );
1889 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001890
1891 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1892
1893 return( 0 );
1894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001898/*
1899 * SSLv3.0 MAC functions
1900 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001901#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001902static void ssl_mac( mbedtls_md_context_t *md_ctx,
1903 const unsigned char *secret,
1904 const unsigned char *buf, size_t len,
1905 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001906 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001907{
1908 unsigned char header[11];
1909 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001910 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1912 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001913
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001914 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001916 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001917 else
Paul Bakker68884e32013-01-07 18:20:04 +01001918 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
1920 memcpy( header, ctr, 8 );
1921 header[ 8] = (unsigned char) type;
1922 header[ 9] = (unsigned char)( len >> 8 );
1923 header[10] = (unsigned char)( len );
1924
Paul Bakker68884e32013-01-07 18:20:04 +01001925 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_md_starts( md_ctx );
1927 mbedtls_md_update( md_ctx, secret, md_size );
1928 mbedtls_md_update( md_ctx, padding, padlen );
1929 mbedtls_md_update( md_ctx, header, 11 );
1930 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001931 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Paul Bakker68884e32013-01-07 18:20:04 +01001933 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_md_starts( md_ctx );
1935 mbedtls_md_update( md_ctx, secret, md_size );
1936 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001937 mbedtls_md_update( md_ctx, out, md_size );
1938 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001941
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001942/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001943 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001944#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001945 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1946 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1947 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1948/* This function makes sure every byte in the memory region is accessed
1949 * (in ascending addresses order) */
1950static void ssl_read_memory( unsigned char *p, size_t len )
1951{
1952 unsigned char acc = 0;
1953 volatile unsigned char force;
1954
1955 for( ; len != 0; p++, len-- )
1956 acc ^= *p;
1957
1958 force = acc;
1959 (void) force;
1960}
1961#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1962
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001963/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001965 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001966
Hanno Beckercab87e62019-04-29 13:52:53 +01001967/* add_data must have size ( 13 + SSL_CID_LEN_MAX ) Bytes */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001968static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001969 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001970 mbedtls_record *rec )
1971{
Hanno Beckercab87e62019-04-29 13:52:53 +01001972 /* Quoting RFC 5246:
1973 *
1974 * additional_data = seq_num + TLSCompressed.type +
1975 * TLSCompressed.version + TLSCompressed.length;
1976 *
1977 * For the CID extension, this is extended as follows:
1978 *
1979 * additional_data = seq_num + DTLSPlaintext.type +
1980 * DTLSPlaintext.version +
1981 * cid + // New input
1982 * cid_length + // New input
1983 * length_of_DTLSInnerPlaintext;
1984 */
1985
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001986 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1987 add_data[8] = rec->type;
Hanno Beckercab87e62019-04-29 13:52:53 +01001988 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1989
1990#if defined(MBEDTLS_SSL_CID)
1991 memcpy( add_data + 11, rec->cid, rec->cid_len );
1992#endif /* MBEDTLS_SSL_CID */
1993
1994 add_data[11 + rec->cid_len + 0] = ( rec->data_len >> 8 ) & 0xFF;
1995 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 0 ) & 0xFF;
1996
1997#if defined(MBEDTLS_SSL_CID)
1998 *add_data_len = 13 + rec->cid_len;
1999#else
2000 *add_data_len = 13;
2001#endif /* MBEDTLS_SSL_CID */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002002}
2003
Hanno Beckera18d1322018-01-03 14:27:32 +00002004int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2005 mbedtls_ssl_transform *transform,
2006 mbedtls_record *rec,
2007 int (*f_rng)(void *, unsigned char *, size_t),
2008 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002011 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002012 unsigned char * data;
Hanno Beckercab87e62019-04-29 13:52:53 +01002013 unsigned char add_data[13 + SSL_CID_LEN_MAX ];
2014 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002015 size_t post_avail;
2016
2017 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002018#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002019 ((void) ssl);
2020#endif
2021
2022 /* The PRNG is used for dynamic IV generation that's used
2023 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2024#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2025 ( defined(MBEDTLS_AES_C) || \
2026 defined(MBEDTLS_ARIA_C) || \
2027 defined(MBEDTLS_CAMELLIA_C) ) && \
2028 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2029 ((void) f_rng);
2030 ((void) p_rng);
2031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002035 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002036 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2039 }
2040 if( rec == NULL ||
2041 rec->buf == NULL ||
2042 rec->buf_len < rec->data_offset ||
Hanno Beckercab87e62019-04-29 13:52:53 +01002043 rec->buf_len - rec->data_offset < rec->data_len ||
2044 rec->cid_len != 0 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002045 {
2046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002048 }
2049
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002050 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2051 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002053 data, rec->data_len );
2054
2055 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2056
2057 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2058 {
2059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2060 (unsigned) rec->data_len,
2061 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2063 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002064
Hanno Beckercab87e62019-04-29 13:52:53 +01002065#if defined(MBEDTLS_SSL_CID)
2066 /*
2067 * Add CID information
2068 */
2069 rec->cid_len = transform->out_cid_len;
2070 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2071 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2072#endif /* MBEDTLS_SSL_CID */
2073
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002075 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 */
Hanno Becker52344c22018-01-03 15:24:20 +00002077#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 if( mode == MBEDTLS_MODE_STREAM ||
2079 ( mode == MBEDTLS_MODE_CBC
2080#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002081 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002082#endif
2083 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085 if( post_avail < transform->maclen )
2086 {
2087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2088 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2089 }
2090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002093 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002094 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002095 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2096 data, rec->data_len, rec->ctr, rec->type, mac );
2097 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002098 }
2099 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2102 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002103 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002104 {
Hanno Becker992b6872017-11-09 18:57:39 +00002105 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2106
Hanno Beckercab87e62019-04-29 13:52:53 +01002107 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002108
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002110 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002111 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2112 data, rec->data_len );
2113 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2114 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2115
2116 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002117 }
2118 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002119#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002123 }
2124
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2126 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002127
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002128 rec->data_len += transform->maclen;
2129 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002130 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002131 }
Hanno Becker52344c22018-01-03 15:24:20 +00002132#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002134 /*
2135 * Encrypt
2136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2138 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002140 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002141 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143 "including %d bytes of padding",
2144 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002146 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2147 transform->iv_enc, transform->ivlen,
2148 data, rec->data_len,
2149 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002152 return( ret );
2153 }
2154
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002155 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
Paul Bakker68884e32013-01-07 18:20:04 +01002161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002163
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002164#if defined(MBEDTLS_GCM_C) || \
2165 defined(MBEDTLS_CCM_C) || \
2166 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002168 mode == MBEDTLS_MODE_CCM ||
2169 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002170 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002171 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002172 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002173 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002174
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002175 /* Check that there's space for both the authentication tag
2176 * and the explicit IV before and after the record content. */
2177 if( post_avail < transform->taglen ||
2178 rec->data_offset < explicit_iv_len )
2179 {
2180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2181 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2182 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002183
Paul Bakker68884e32013-01-07 18:20:04 +01002184 /*
2185 * Generate IV
2186 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002187 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2188 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002189 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002190 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002191 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2192 explicit_iv_len );
2193 /* Prefix record content with explicit IV. */
2194 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002195 }
2196 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2197 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002198 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002199 unsigned char i;
2200
2201 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2202
2203 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002204 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002205 }
2206 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002207 {
2208 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2210 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002211 }
2212
Hanno Beckercab87e62019-04-29 13:52:53 +01002213 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002214
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002215 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2216 iv, transform->ivlen );
2217 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002218 data - explicit_iv_len, explicit_iv_len );
2219 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002220 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002222 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002223 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002224
Paul Bakker68884e32013-01-07 18:20:04 +01002225 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002226 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002227 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002228
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002229 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002231 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 data, rec->data_len, /* source */
2233 data, &rec->data_len, /* destination */
2234 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002237 return( ret );
2238 }
2239
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2241 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002242
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 rec->data_len += transform->taglen + explicit_iv_len;
2244 rec->data_offset -= explicit_iv_len;
2245 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002246 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2250#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002251 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002254 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002255 size_t padlen, i;
2256 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002257
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002258 /* Currently we're always using minimal padding
2259 * (up to 255 bytes would be allowed). */
2260 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2261 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 padlen = 0;
2263
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264 /* Check there's enough space in the buffer for the padding. */
2265 if( post_avail < padlen + 1 )
2266 {
2267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2268 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2269 }
2270
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002272 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002274 rec->data_len += padlen + 1;
2275 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002278 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002279 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2280 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002281 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002283 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002284 if( f_rng == NULL )
2285 {
2286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2288 }
2289
2290 if( rec->data_offset < transform->ivlen )
2291 {
2292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2293 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2294 }
2295
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002296 /*
2297 * Generate IV
2298 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002299 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002300 if( ret != 0 )
2301 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002302
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 memcpy( data - transform->ivlen, transform->iv_enc,
2304 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002305
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002306 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002310 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002311 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002312 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002314 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2315 transform->iv_enc,
2316 transform->ivlen,
2317 data, rec->data_len,
2318 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002321 return( ret );
2322 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002323
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002324 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002328 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002332 {
2333 /*
2334 * Save IV in SSL3 and TLS1
2335 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002336 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2337 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002340#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002341 {
2342 data -= transform->ivlen;
2343 rec->data_offset -= transform->ivlen;
2344 rec->data_len += transform->ivlen;
2345 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002348 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002349 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002350 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2351
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002352 /*
2353 * MAC(MAC_write_key, seq_num +
2354 * TLSCipherText.type +
2355 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002356 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002357 * IV + // except for TLS 1.0
2358 * ENC(content + padding + padding_length));
2359 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002360
2361 if( post_avail < transform->maclen)
2362 {
2363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2364 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2365 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002366
Hanno Beckercab87e62019-04-29 13:52:53 +01002367 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002371 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002372
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002373 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002374 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2376 data, rec->data_len );
2377 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2378 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002379
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002381
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002382 rec->data_len += transform->maclen;
2383 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002384 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002388 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002390 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002394 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002396 /* Make extra sure authentication was performed, exactly once */
2397 if( auth_done != 1 )
2398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002401 }
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
2405 return( 0 );
2406}
2407
Hanno Beckera18d1322018-01-03 14:27:32 +00002408int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2409 mbedtls_ssl_transform *transform,
2410 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002412 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002414 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002415#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002416 size_t padlen = 0, correct = 1;
2417#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002418 unsigned char* data;
Hanno Beckercab87e62019-04-29 13:52:53 +01002419 unsigned char add_data[13 + SSL_CID_LEN_MAX ];
2420 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002421
Hanno Beckera18d1322018-01-03 14:27:32 +00002422#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002423 ((void) ssl);
2424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002427 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002428 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2430 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2431 }
2432 if( rec == NULL ||
2433 rec->buf == NULL ||
2434 rec->buf_len < rec->data_offset ||
2435 rec->buf_len - rec->data_offset < rec->data_len )
2436 {
2437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002439 }
2440
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002441 data = rec->buf + rec->data_offset;
2442 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Hanno Beckercab87e62019-04-29 13:52:53 +01002444#if defined(MBEDTLS_SSL_CID)
2445 /*
2446 * Match record's CID with incoming CID.
2447 */
2448
2449 /* TODO - Uncomment this once CID parsing is in place */
2450 /* if( rec->cid_len != transform->in_cid_len || */
2451 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2452 /* { */
2453 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2454 /* } */
2455 /* TODO - Remove this once CID parsing is in place */
2456 rec->cid_len = transform->in_cid_len;
2457 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2458 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2459#endif /* MBEDTLS_SSL_CID */
2460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2462 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002463 {
2464 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002465 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2466 transform->iv_dec,
2467 transform->ivlen,
2468 data, rec->data_len,
2469 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002472 return( ret );
2473 }
2474
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002475 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2478 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 }
Paul Bakker68884e32013-01-07 18:20:04 +01002481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002483#if defined(MBEDTLS_GCM_C) || \
2484 defined(MBEDTLS_CCM_C) || \
2485 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002487 mode == MBEDTLS_MODE_CCM ||
2488 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002489 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002490 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002491 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002492
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002493 /*
2494 * Compute and update sizes
2495 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002496 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002499 "+ taglen (%d)", rec->data_len,
2500 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002502 }
Paul Bakker68884e32013-01-07 18:20:04 +01002503
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002504 /*
2505 * Prepare IV
2506 */
2507 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2508 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002509 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002510 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002511 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002512
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002513 }
2514 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2515 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002516 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002517 unsigned char i;
2518
2519 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2520
2521 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002522 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002523 }
2524 else
2525 {
2526 /* Reminder if we ever add an AEAD mode with a different size */
2527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2529 }
2530
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002531 data += explicit_iv_len;
2532 rec->data_offset += explicit_iv_len;
2533 rec->data_len -= explicit_iv_len + transform->taglen;
2534
Hanno Beckercab87e62019-04-29 13:52:53 +01002535 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002537 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002538
2539 memcpy( transform->iv_dec + transform->fixed_ivlen,
2540 data - explicit_iv_len, explicit_iv_len );
2541
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002542 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002544 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002547 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002548 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002549 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2551 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002552 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553 data, rec->data_len,
2554 data, &olen,
2555 data + rec->data_len,
2556 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2561 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002562
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002563 return( ret );
2564 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002565 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002566
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002567 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002571 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2575#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002576 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002579 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002580
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 /*
Paul Bakker45829992013-01-03 14:52:21 +01002582 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002585 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2586 {
2587 /* The ciphertext is prefixed with the CBC IV. */
2588 minlen += transform->ivlen;
2589 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002590#endif
Paul Bakker45829992013-01-03 14:52:21 +01002591
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002592 /* Size considerations:
2593 *
2594 * - The CBC cipher text must not be empty and hence
2595 * at least of size transform->ivlen.
2596 *
2597 * Together with the potential IV-prefix, this explains
2598 * the first of the two checks below.
2599 *
2600 * - The record must contain a MAC, either in plain or
2601 * encrypted, depending on whether Encrypt-then-MAC
2602 * is used or not.
2603 * - If it is, the message contains the IV-prefix,
2604 * the CBC ciphertext, and the MAC.
2605 * - If it is not, the padded plaintext, and hence
2606 * the CBC ciphertext, has at least length maclen + 1
2607 * because there is at least the padding length byte.
2608 *
2609 * As the CBC ciphertext is not empty, both cases give the
2610 * lower bound minlen + maclen + 1 on the record size, which
2611 * we test for in the second check below.
2612 */
2613 if( rec->data_len < minlen + transform->ivlen ||
2614 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 "+ 1 ) ( + expl IV )", rec->data_len,
2618 transform->ivlen,
2619 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002621 }
2622
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002623 /*
2624 * Authenticate before decrypt if enabled
2625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002628 {
Hanno Becker992b6872017-11-09 18:57:39 +00002629 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002632
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2634 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002635
Hanno Beckercab87e62019-04-29 13:52:53 +01002636 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002637
Hanno Beckercab87e62019-04-29 13:52:53 +01002638 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2639 add_data_len );
2640 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2641 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002642 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2643 data, rec->data_len );
2644 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2645 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002646
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002647 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2648 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002649 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002650 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002651
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2653 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002657 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002658 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002661
2662 /*
2663 * Check length sanity
2664 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 }
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002673 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002674 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002675 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002677 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 /* This is safe because data_len >= minlen + maclen + 1 initially,
2679 * and at this point we have at most subtracted maclen (note that
2680 * minlen == transform->ivlen here). */
2681 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 data += transform->ivlen;
2684 rec->data_offset += transform->ivlen;
2685 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002688
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2690 transform->iv_dec, transform->ivlen,
2691 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002694 return( ret );
2695 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002696
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002697 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2700 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002701 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002704 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002705 {
2706 /*
2707 * Save IV in SSL3 and TLS1
2708 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2710 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 /* Safe since data_len >= minlen + maclen + 1, so after having
2715 * subtracted at most minlen and maclen up to this point,
2716 * data_len > 0. */
2717 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002718
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002719 if( auth_done == 1 )
2720 {
2721 correct *= ( rec->data_len >= padlen + 1 );
2722 padlen *= ( rec->data_len >= padlen + 1 );
2723 }
2724 else
Paul Bakker45829992013-01-03 14:52:21 +01002725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727 if( rec->data_len < transform->maclen + padlen + 1 )
2728 {
2729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2730 rec->data_len,
2731 transform->maclen,
2732 padlen + 1 ) );
2733 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002734#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002735
2736 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2737 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002740 padlen++;
2741
2742 /* Regardless of the validity of the padding,
2743 * we have data_len >= padlen here. */
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002746 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002748 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#if defined(MBEDTLS_SSL_DEBUG_ALL)
2751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002752 "should be no more than %d",
2753 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002754#endif
Paul Bakker45829992013-01-03 14:52:21 +01002755 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757 }
2758 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2760#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2761 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002762 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 /* The padding check involves a series of up to 256
2765 * consecutive memory reads at the end of the record
2766 * plaintext buffer. In order to hide the length and
2767 * validity of the padding, always perform exactly
2768 * `min(256,plaintext_len)` reads (but take into account
2769 * only the last `padlen` bytes for the padding check). */
2770 size_t pad_count = 0;
2771 size_t real_count = 0;
2772 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002773
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774 /* Index of first padding byte; it has been ensured above
2775 * that the subtraction is safe. */
2776 size_t const padding_idx = rec->data_len - padlen;
2777 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2778 size_t const start_idx = rec->data_len - num_checks;
2779 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002780
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002781 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002782 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002783 real_count |= ( idx >= padding_idx );
2784 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002785 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002789 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002791#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002792 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2796 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002800 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 /* If the padding was found to be invalid, padlen == 0
2803 * and the subtraction is safe. If the padding was found valid,
2804 * padlen hasn't been changed and the previous assertion
2805 * data_len >= padlen still holds. */
2806 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002808 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002810 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2813 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002816#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
2821 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002822 * Authenticate if not done yet.
2823 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 */
Hanno Becker52344c22018-01-03 15:24:20 +00002825#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002826 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002827 {
Hanno Becker992b6872017-11-09 18:57:39 +00002828 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002829
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 /* If the initial value of padlen was such that
2831 * data_len < maclen + padlen + 1, then padlen
2832 * got reset to 1, and the initial check
2833 * data_len >= minlen + maclen + 1
2834 * guarantees that at this point we still
2835 * have at least data_len >= maclen.
2836 *
2837 * If the initial value of padlen was such that
2838 * data_len >= maclen + padlen + 1, then we have
2839 * subtracted either padlen + 1 (if the padding was correct)
2840 * or 0 (if the padding was incorrect) since then,
2841 * hence data_len >= maclen in any case.
2842 */
2843 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Hanno Beckercab87e62019-04-29 13:52:53 +01002845 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002849 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002850 ssl_mac( &transform->md_ctx_dec,
2851 transform->mac_dec,
2852 data, rec->data_len,
2853 rec->ctr, rec->type,
2854 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002855 }
2856 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2858#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2859 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002860 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002861 {
2862 /*
2863 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002864 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002865 *
2866 * Known timing attacks:
2867 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2868 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002869 * To compensate for different timings for the MAC calculation
2870 * depending on how much padding was removed (which is determined
2871 * by padlen), process extra_run more blocks through the hash
2872 * function.
2873 *
2874 * The formula in the paper is
2875 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2876 * where L1 is the size of the header plus the decrypted message
2877 * plus CBC padding and L2 is the size of the header plus the
2878 * decrypted message. This is for an underlying hash function
2879 * with 64-byte blocks.
2880 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2881 * correctly. We round down instead of up, so -56 is the correct
2882 * value for our calculations instead of -55.
2883 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002884 * Repeat the formula rather than defining a block_size variable.
2885 * This avoids requiring division by a variable at runtime
2886 * (which would be marginally less efficient and would require
2887 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002888 */
2889 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002891
2892 /*
2893 * The next two sizes are the minimum and maximum values of
2894 * in_msglen over all padlen values.
2895 *
2896 * They're independent of padlen, since we previously did
2897 * in_msglen -= padlen.
2898 *
2899 * Note that max_len + maclen is never more than the buffer
2900 * length, as we previously did in_msglen -= maclen too.
2901 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002903 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2904
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905 memset( tmp, 0, sizeof( tmp ) );
2906
2907 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002908 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002909#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2910 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002911 case MBEDTLS_MD_MD5:
2912 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002913 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002914 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01002915 extra_run =
2916 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2917 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002918 break;
2919#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002920#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002921 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002922 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01002923 extra_run =
2924 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2925 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002926 break;
2927#endif
2928 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2931 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002932
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002933 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002934
Hanno Beckercab87e62019-04-29 13:52:53 +01002935 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2936 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2938 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002939 /* Make sure we access everything even when padlen > 0. This
2940 * makes the synchronisation requirements for just-in-time
2941 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002942 ssl_read_memory( data + rec->data_len, padlen );
2943 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002944
2945 /* Call mbedtls_md_process at least once due to cache attacks
2946 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002947 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002949
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002950 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002951
2952 /* Make sure we access all the memory that could contain the MAC,
2953 * before we check it in the next code block. This makes the
2954 * synchronisation requirements for just-in-time Prime+Probe
2955 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002956 ssl_read_memory( data + min_len,
2957 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002958 }
2959 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2961 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002967#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002968 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2969 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002970#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002972 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2973 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_DEBUG_ALL)
2976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002977#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002978 correct = 0;
2979 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002980 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002981 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002982
2983 /*
2984 * Finally check the correct flag
2985 */
2986 if( correct == 0 )
2987 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002988#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002989
2990 /* Make extra sure authentication was performed, exactly once */
2991 if( auth_done != 1 )
2992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2994 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
2999 return( 0 );
3000}
3001
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003002#undef MAC_NONE
3003#undef MAC_PLAINTEXT
3004#undef MAC_CIPHERTEXT
3005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003007/*
3008 * Compression/decompression functions
3009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003011{
3012 int ret;
3013 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003014 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003015 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003016 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003019
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003020 if( len_pre == 0 )
3021 return( 0 );
3022
Paul Bakker2770fbd2012-07-03 13:30:23 +00003023 memcpy( msg_pre, ssl->out_msg, len_pre );
3024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003026 ssl->out_msglen ) );
3027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003029 ssl->out_msg, ssl->out_msglen );
3030
Paul Bakker48916f92012-09-16 19:57:18 +00003031 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3032 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3033 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003034 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003035
Paul Bakker48916f92012-09-16 19:57:18 +00003036 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003037 if( ret != Z_OK )
3038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3040 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003041 }
3042
Angus Grattond8213d02016-05-25 20:56:48 +10003043 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003044 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003047 ssl->out_msglen ) );
3048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003050 ssl->out_msg, ssl->out_msglen );
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003053
3054 return( 0 );
3055}
3056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003058{
3059 int ret;
3060 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003061 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003062 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003063 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003066
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003067 if( len_pre == 0 )
3068 return( 0 );
3069
Paul Bakker2770fbd2012-07-03 13:30:23 +00003070 memcpy( msg_pre, ssl->in_msg, len_pre );
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003073 ssl->in_msglen ) );
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003076 ssl->in_msg, ssl->in_msglen );
3077
Paul Bakker48916f92012-09-16 19:57:18 +00003078 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3079 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3080 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003081 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003082 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003083
Paul Bakker48916f92012-09-16 19:57:18 +00003084 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003085 if( ret != Z_OK )
3086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3088 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003089 }
3090
Angus Grattond8213d02016-05-25 20:56:48 +10003091 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003092 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003095 ssl->in_msglen ) );
3096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003098 ssl->in_msg, ssl->in_msglen );
3099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003101
3102 return( 0 );
3103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3107static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_PROTO_DTLS)
3110static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003111{
3112 /* If renegotiation is not enforced, retransmit until we would reach max
3113 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003114 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003115 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003116 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003117 unsigned char doublings = 1;
3118
3119 while( ratio != 0 )
3120 {
3121 ++doublings;
3122 ratio >>= 1;
3123 }
3124
3125 if( ++ssl->renego_records_seen > doublings )
3126 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003128 return( 0 );
3129 }
3130 }
3131
3132 return( ssl_write_hello_request( ssl ) );
3133}
3134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003136
Paul Bakker5121ce52009-01-03 21:22:43 +00003137/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003138 * Fill the input message buffer by appending data to it.
3139 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003140 *
3141 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3142 * available (from this read and/or a previous one). Otherwise, an error code
3143 * is returned (possibly EOF or WANT_READ).
3144 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003145 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3146 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3147 * since we always read a whole datagram at once.
3148 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003149 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003150 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003153{
Paul Bakker23986e52011-04-24 08:57:21 +00003154 int ret;
3155 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003159 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003162 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003164 }
3165
Angus Grattond8213d02016-05-25 20:56:48 +10003166 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003170 }
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003175 uint32_t timeout;
3176
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003177 /* Just to be sure */
3178 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3179 {
3180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3181 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3183 }
3184
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003185 /*
3186 * The point is, we need to always read a full datagram at once, so we
3187 * sometimes read more then requested, and handle the additional data.
3188 * It could be the rest of the current record (while fetching the
3189 * header) and/or some other records in the same datagram.
3190 */
3191
3192 /*
3193 * Move to the next record in the already read datagram if applicable
3194 */
3195 if( ssl->next_record_offset != 0 )
3196 {
3197 if( ssl->in_left < ssl->next_record_offset )
3198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003201 }
3202
3203 ssl->in_left -= ssl->next_record_offset;
3204
3205 if( ssl->in_left != 0 )
3206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003208 ssl->next_record_offset ) );
3209 memmove( ssl->in_hdr,
3210 ssl->in_hdr + ssl->next_record_offset,
3211 ssl->in_left );
3212 }
3213
3214 ssl->next_record_offset = 0;
3215 }
3216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003219
3220 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003221 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003222 */
3223 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003226 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003227 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003228
3229 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003230 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003231 * are not at the beginning of a new record, the caller did something
3232 * wrong.
3233 */
3234 if( ssl->in_left != 0 )
3235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003238 }
3239
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003240 /*
3241 * Don't even try to read if time's out already.
3242 * This avoids by-passing the timer when repeatedly receiving messages
3243 * that will end up being dropped.
3244 */
3245 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003246 {
3247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003248 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003249 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003250 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003251 {
Angus Grattond8213d02016-05-25 20:56:48 +10003252 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003255 timeout = ssl->handshake->retransmit_timeout;
3256 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003257 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003260
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003261 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003262 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3263 timeout );
3264 else
3265 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003268
3269 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003271 }
3272
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003273 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003276 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003279 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003280 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003283 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003284 }
3285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003289 return( ret );
3290 }
3291
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003292 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003293 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003295 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003297 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003298 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003301 return( ret );
3302 }
3303
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003304 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003307 }
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 if( ret < 0 )
3310 return( ret );
3311
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003312 ssl->in_left = ret;
3313 }
3314 else
3315#endif
3316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003318 ssl->in_left, nb_want ) );
3319
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003320 while( ssl->in_left < nb_want )
3321 {
3322 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003323
3324 if( ssl_check_timer( ssl ) != 0 )
3325 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3326 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003327 {
3328 if( ssl->f_recv_timeout != NULL )
3329 {
3330 ret = ssl->f_recv_timeout( ssl->p_bio,
3331 ssl->in_hdr + ssl->in_left, len,
3332 ssl->conf->read_timeout );
3333 }
3334 else
3335 {
3336 ret = ssl->f_recv( ssl->p_bio,
3337 ssl->in_hdr + ssl->in_left, len );
3338 }
3339 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003342 ssl->in_left, nb_want ) );
3343 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003344
3345 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003347
3348 if( ret < 0 )
3349 return( ret );
3350
mohammad160352aecb92018-03-28 23:41:40 -07003351 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003352 {
Darryl Green11999bb2018-03-13 15:22:58 +00003353 MBEDTLS_SSL_DEBUG_MSG( 1,
3354 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003355 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003356 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3357 }
3358
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003359 ssl->in_left += ret;
3360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
3365 return( 0 );
3366}
3367
3368/*
3369 * Flush any data not yet written
3370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003372{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003373 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003374 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003378 if( ssl->f_send == NULL )
3379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003381 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003383 }
3384
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003385 /* Avoid incrementing counter if data is flushed */
3386 if( ssl->out_left == 0 )
3387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003389 return( 0 );
3390 }
3391
Paul Bakker5121ce52009-01-03 21:22:43 +00003392 while( ssl->out_left > 0 )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3395 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396
Hanno Becker2b1e3542018-08-06 11:19:13 +01003397 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003398 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
3402 if( ret <= 0 )
3403 return( ret );
3404
mohammad160352aecb92018-03-28 23:41:40 -07003405 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003406 {
Darryl Green11999bb2018-03-13 15:22:58 +00003407 MBEDTLS_SSL_DEBUG_MSG( 1,
3408 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003409 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003410 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3411 }
3412
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 ssl->out_left -= ret;
3414 }
3415
Hanno Becker2b1e3542018-08-06 11:19:13 +01003416#if defined(MBEDTLS_SSL_PROTO_DTLS)
3417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003418 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003419 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003420 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003421 else
3422#endif
3423 {
3424 ssl->out_hdr = ssl->out_buf + 8;
3425 }
3426 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
3430 return( 0 );
3431}
3432
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433/*
3434 * Functions to handle the DTLS retransmission state machine
3435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003437/*
3438 * Append current handshake message to current outgoing flight
3439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3444 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3445 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446
3447 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003448 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003449 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003452 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003453 }
3454
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003455 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003456 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003459 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460 }
3461
3462 /* Copy current handshake message with headers */
3463 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3464 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003465 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003466 msg->next = NULL;
3467
3468 /* Append to the current flight */
3469 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003470 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003471 else
3472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003474 while( cur->next != NULL )
3475 cur = cur->next;
3476 cur->next = msg;
3477 }
3478
Hanno Becker3b235902018-08-06 09:54:53 +01003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003480 return( 0 );
3481}
3482
3483/*
3484 * Free the current flight of handshake messages
3485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 mbedtls_ssl_flight_item *cur = flight;
3489 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003490
3491 while( cur != NULL )
3492 {
3493 next = cur->next;
3494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 mbedtls_free( cur->p );
3496 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003497
3498 cur = next;
3499 }
3500}
3501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3503static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003504#endif
3505
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003506/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003507 * Swap transform_out and out_ctr with the alternative ones
3508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003512 unsigned char tmp_out_ctr[8];
3513
3514 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003517 return;
3518 }
3519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003521
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003522 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003523 tmp_transform = ssl->transform_out;
3524 ssl->transform_out = ssl->handshake->alt_transform_out;
3525 ssl->handshake->alt_transform_out = tmp_transform;
3526
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003527 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003528 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3529 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003530 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003531
3532 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003533 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3536 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3541 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003542 }
3543 }
3544#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003545}
3546
3547/*
3548 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003549 */
3550int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3551{
3552 int ret = 0;
3553
3554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3555
3556 ret = mbedtls_ssl_flight_transmit( ssl );
3557
3558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3559
3560 return( ret );
3561}
3562
3563/*
3564 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565 *
3566 * Need to remember the current message in case flush_output returns
3567 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003568 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003569 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003570int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003571{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003572 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003576 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003578
3579 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003580 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003581 ssl_swap_epochs( ssl );
3582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003584 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585
3586 while( ssl->handshake->cur_msg != NULL )
3587 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003588 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003589 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003590
Hanno Beckere1dcb032018-08-17 16:47:58 +01003591 int const is_finished =
3592 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3593 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3594
Hanno Becker04da1892018-08-14 13:22:10 +01003595 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3596 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3597
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003598 /* Swap epochs before sending Finished: we can't do it after
3599 * sending ChangeCipherSpec, in case write returns WANT_READ.
3600 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003601 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003602 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003604 ssl_swap_epochs( ssl );
3605 }
3606
Hanno Becker67bc7c32018-08-06 11:33:50 +01003607 ret = ssl_get_remaining_payload_in_datagram( ssl );
3608 if( ret < 0 )
3609 return( ret );
3610 max_frag_len = (size_t) ret;
3611
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003612 /* CCS is copied as is, while HS messages may need fragmentation */
3613 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3614 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003615 if( max_frag_len == 0 )
3616 {
3617 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3618 return( ret );
3619
3620 continue;
3621 }
3622
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003623 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003624 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003625 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003627 /* Update position inside current message */
3628 ssl->handshake->cur_msg_p += cur->len;
3629 }
3630 else
3631 {
3632 const unsigned char * const p = ssl->handshake->cur_msg_p;
3633 const size_t hs_len = cur->len - 12;
3634 const size_t frag_off = p - ( cur->p + 12 );
3635 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003636 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003637
Hanno Beckere1dcb032018-08-17 16:47:58 +01003638 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003639 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003640 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003641 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642
Hanno Becker67bc7c32018-08-06 11:33:50 +01003643 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3644 return( ret );
3645
3646 continue;
3647 }
3648 max_hs_frag_len = max_frag_len - 12;
3649
3650 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3651 max_hs_frag_len : rem_len;
3652
3653 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003654 {
3655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003656 (unsigned) cur_hs_frag_len,
3657 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003658 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003659
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003660 /* Messages are stored with handshake headers as if not fragmented,
3661 * copy beginning of headers then fill fragmentation fields.
3662 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3663 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003664
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003665 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3666 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3667 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3668
Hanno Becker67bc7c32018-08-06 11:33:50 +01003669 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3670 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3671 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003672
3673 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3674
Hanno Becker3f7b9732018-08-28 09:53:25 +01003675 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003676 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3677 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003678 ssl->out_msgtype = cur->type;
3679
3680 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003681 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003682 }
3683
3684 /* If done with the current message move to the next one if any */
3685 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3686 {
3687 if( cur->next != NULL )
3688 {
3689 ssl->handshake->cur_msg = cur->next;
3690 ssl->handshake->cur_msg_p = cur->next->p + 12;
3691 }
3692 else
3693 {
3694 ssl->handshake->cur_msg = NULL;
3695 ssl->handshake->cur_msg_p = NULL;
3696 }
3697 }
3698
3699 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003700 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 return( ret );
3704 }
3705 }
3706
Hanno Becker67bc7c32018-08-06 11:33:50 +01003707 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3708 return( ret );
3709
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003710 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3712 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003713 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003716 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3717 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003718
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720
3721 return( 0 );
3722}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003723
3724/*
3725 * To be called when the last message of an incoming flight is received.
3726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003728{
3729 /* We won't need to resend that one any more */
3730 ssl_flight_free( ssl->handshake->flight );
3731 ssl->handshake->flight = NULL;
3732 ssl->handshake->cur_msg = NULL;
3733
3734 /* The next incoming flight will start with this msg_seq */
3735 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3736
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003737 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003738 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003739
Hanno Becker0271f962018-08-16 13:23:47 +01003740 /* Clear future message buffering structure. */
3741 ssl_buffering_free( ssl );
3742
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003743 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003744 ssl_set_timer( ssl, 0 );
3745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3747 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003750 }
3751 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003753}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003754
3755/*
3756 * To be called when the last message of an outgoing flight is send.
3757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003759{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003760 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003761 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3764 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003767 }
3768 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003772
Paul Bakker5121ce52009-01-03 21:22:43 +00003773/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003774 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003776
3777/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003778 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003779 *
3780 * - fill in handshake headers
3781 * - update handshake checksum
3782 * - DTLS: save message for resending
3783 * - then pass to the record layer
3784 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003785 * DTLS: except for HelloRequest, messages are only queued, and will only be
3786 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003787 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003788 * Inputs:
3789 * - ssl->out_msglen: 4 + actual handshake message len
3790 * (4 is the size of handshake headers for TLS)
3791 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3792 * - ssl->out_msg + 4: the handshake message body
3793 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003794 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003795 * - ssl->out_msglen: the length of the record contents
3796 * (including handshake headers but excluding record headers)
3797 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003798 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003799int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003800{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003801 int ret;
3802 const size_t hs_len = ssl->out_msglen - 4;
3803 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003804
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3806
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003807 /*
3808 * Sanity checks
3809 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003810 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003811 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3812 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003813 /* In SSLv3, the client might send a NoCertificate alert. */
3814#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3815 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3816 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3817 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3818#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3819 {
3820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3822 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003824
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003825 /* Whenever we send anything different from a
3826 * HelloRequest we should be in a handshake - double check. */
3827 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3828 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003829 ssl->handshake == NULL )
3830 {
3831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003837 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003839 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003844
Hanno Beckerb50a2532018-08-06 11:52:54 +01003845 /* Double-check that we did not exceed the bounds
3846 * of the outgoing record buffer.
3847 * This should never fail as the various message
3848 * writing functions must obey the bounds of the
3849 * outgoing record buffer, but better be safe.
3850 *
3851 * Note: We deliberately do not check for the MTU or MFL here.
3852 */
3853 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3854 {
3855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3856 "size %u, maximum %u",
3857 (unsigned) ssl->out_msglen,
3858 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3860 }
3861
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003862 /*
3863 * Fill handshake headers
3864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003867 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3868 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3869 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003871 /*
3872 * DTLS has additional fields in the Handshake layer,
3873 * between the length field and the actual payload:
3874 * uint16 message_seq;
3875 * uint24 fragment_offset;
3876 * uint24 fragment_length;
3877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003880 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003881 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003882 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003883 {
3884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3885 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003886 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003887 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3889 }
3890
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003891 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003892 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003893
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003894 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003895 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003896 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003897 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3898 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3899 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003900 }
3901 else
3902 {
3903 ssl->out_msg[4] = 0;
3904 ssl->out_msg[5] = 0;
3905 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003906
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003907 /* Handshake hashes are computed without fragmentation,
3908 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003909 memset( ssl->out_msg + 6, 0x00, 3 );
3910 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003911 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003913
Hanno Becker0207e532018-08-28 10:28:28 +01003914 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003915 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3916 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 }
3918
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003919 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003922 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3923 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924 {
3925 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 return( ret );
3929 }
3930 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003931 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003932#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003933 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003934 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003935 {
3936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3937 return( ret );
3938 }
3939 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003940
3941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3942
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003943 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003944}
3945
3946/*
3947 * Record layer functions
3948 */
3949
3950/*
3951 * Write current record.
3952 *
3953 * Uses:
3954 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3955 * - ssl->out_msglen: length of the record content (excl headers)
3956 * - ssl->out_msg: record content
3957 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003959{
3960 int ret, done = 0;
3961 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003963
3964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003967 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003969 {
3970 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003973 return( ret );
3974 }
3975
3976 len = ssl->out_msglen;
3977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3981 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 ret = mbedtls_ssl_hw_record_write( ssl );
3986 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3989 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003990 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003991
3992 if( ret == 0 )
3993 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003994 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003996 if( !done )
3997 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003998 unsigned i;
3999 size_t protected_record_size;
4000
Paul Bakker05ef8352012-05-08 09:17:57 +00004001 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004003 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004004
Hanno Becker19859472018-08-06 09:40:20 +01004005 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004006 ssl->out_len[0] = (unsigned char)( len >> 8 );
4007 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004008
Paul Bakker48916f92012-09-16 19:57:18 +00004009 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004010 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004011 mbedtls_record rec;
4012
4013 rec.buf = ssl->out_iv;
4014 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4015 ( ssl->out_iv - ssl->out_buf );
4016 rec.data_len = ssl->out_msglen;
4017 rec.data_offset = ssl->out_msg - rec.buf;
4018
4019 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4020 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4021 ssl->conf->transport, rec.ver );
4022 rec.type = ssl->out_msgtype;
4023
Hanno Beckercab87e62019-04-29 13:52:53 +01004024 rec.cid_len = 0;
4025
Hanno Beckera18d1322018-01-03 14:27:32 +00004026 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004027 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004030 return( ret );
4031 }
4032
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004033 if( rec.data_offset != 0 )
4034 {
4035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4037 }
4038
Hanno Becker78f839d2019-03-14 12:56:23 +00004039 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004040 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4041 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004042 }
4043
Hanno Becker2b1e3542018-08-06 11:19:13 +01004044 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4045
4046#if defined(MBEDTLS_SSL_PROTO_DTLS)
4047 /* In case of DTLS, double-check that we don't exceed
4048 * the remaining space in the datagram. */
4049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4050 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004051 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004052 if( ret < 0 )
4053 return( ret );
4054
4055 if( protected_record_size > (size_t) ret )
4056 {
4057 /* Should never happen */
4058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4059 }
4060 }
4061#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004064 "version = [%d:%d], msglen = %d",
4065 ssl->out_hdr[0], ssl->out_hdr[1],
4066 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004069 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004070
4071 ssl->out_left += protected_record_size;
4072 ssl->out_hdr += protected_record_size;
4073 ssl_update_out_pointers( ssl, ssl->transform_out );
4074
Hanno Becker04484622018-08-06 09:49:38 +01004075 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4076 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4077 break;
4078
4079 /* The loop goes to its end iff the counter is wrapping */
4080 if( i == ssl_ep_len( ssl ) )
4081 {
4082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4083 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004085 }
4086
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4089 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004090 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004091 size_t remaining;
4092 ret = ssl_get_remaining_payload_in_datagram( ssl );
4093 if( ret < 0 )
4094 {
4095 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4096 ret );
4097 return( ret );
4098 }
4099
4100 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004101 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004102 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004103 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004104 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004105 else
4106 {
Hanno Becker513815a2018-08-20 11:56:09 +01004107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004108 }
4109 }
4110#endif /* MBEDTLS_SSL_PROTO_DTLS */
4111
4112 if( ( flush == SSL_FORCE_FLUSH ) &&
4113 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 return( ret );
4117 }
4118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004120
4121 return( 0 );
4122}
4123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004125
4126static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4127{
4128 if( ssl->in_msglen < ssl->in_hslen ||
4129 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4130 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4131 {
4132 return( 1 );
4133 }
4134 return( 0 );
4135}
Hanno Becker44650b72018-08-16 12:51:11 +01004136
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004137static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004138{
4139 return( ( ssl->in_msg[9] << 16 ) |
4140 ( ssl->in_msg[10] << 8 ) |
4141 ssl->in_msg[11] );
4142}
4143
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004144static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004145{
4146 return( ( ssl->in_msg[6] << 16 ) |
4147 ( ssl->in_msg[7] << 8 ) |
4148 ssl->in_msg[8] );
4149}
4150
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004151static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004152{
4153 uint32_t msg_len, frag_off, frag_len;
4154
4155 msg_len = ssl_get_hs_total_len( ssl );
4156 frag_off = ssl_get_hs_frag_off( ssl );
4157 frag_len = ssl_get_hs_frag_len( ssl );
4158
4159 if( frag_off > msg_len )
4160 return( -1 );
4161
4162 if( frag_len > msg_len - frag_off )
4163 return( -1 );
4164
4165 if( frag_len + 12 > ssl->in_msglen )
4166 return( -1 );
4167
4168 return( 0 );
4169}
4170
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004171/*
4172 * Mark bits in bitmask (used for DTLS HS reassembly)
4173 */
4174static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4175{
4176 unsigned int start_bits, end_bits;
4177
4178 start_bits = 8 - ( offset % 8 );
4179 if( start_bits != 8 )
4180 {
4181 size_t first_byte_idx = offset / 8;
4182
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004183 /* Special case */
4184 if( len <= start_bits )
4185 {
4186 for( ; len != 0; len-- )
4187 mask[first_byte_idx] |= 1 << ( start_bits - len );
4188
4189 /* Avoid potential issues with offset or len becoming invalid */
4190 return;
4191 }
4192
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004193 offset += start_bits; /* Now offset % 8 == 0 */
4194 len -= start_bits;
4195
4196 for( ; start_bits != 0; start_bits-- )
4197 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4198 }
4199
4200 end_bits = len % 8;
4201 if( end_bits != 0 )
4202 {
4203 size_t last_byte_idx = ( offset + len ) / 8;
4204
4205 len -= end_bits; /* Now len % 8 == 0 */
4206
4207 for( ; end_bits != 0; end_bits-- )
4208 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4209 }
4210
4211 memset( mask + offset / 8, 0xFF, len / 8 );
4212}
4213
4214/*
4215 * Check that bitmask is full
4216 */
4217static int ssl_bitmask_check( unsigned char *mask, size_t len )
4218{
4219 size_t i;
4220
4221 for( i = 0; i < len / 8; i++ )
4222 if( mask[i] != 0xFF )
4223 return( -1 );
4224
4225 for( i = 0; i < len % 8; i++ )
4226 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4227 return( -1 );
4228
4229 return( 0 );
4230}
4231
Hanno Becker56e205e2018-08-16 09:06:12 +01004232/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004233static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004234 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004235{
Hanno Becker56e205e2018-08-16 09:06:12 +01004236 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004237
Hanno Becker56e205e2018-08-16 09:06:12 +01004238 alloc_len = 12; /* Handshake header */
4239 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004240
Hanno Beckerd07df862018-08-16 09:14:58 +01004241 if( add_bitmap )
4242 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004243
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004244 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004245}
Hanno Becker56e205e2018-08-16 09:06:12 +01004246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004248
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004249static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004250{
4251 return( ( ssl->in_msg[1] << 16 ) |
4252 ( ssl->in_msg[2] << 8 ) |
4253 ssl->in_msg[3] );
4254}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004255
Simon Butcher99000142016-10-13 17:21:01 +01004256int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004261 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004263 }
4264
Hanno Becker12555c62018-08-16 12:47:53 +01004265 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004268 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004269 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004273 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004274 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004275 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004276
Hanno Becker44650b72018-08-16 12:51:11 +01004277 if( ssl_check_hs_header( ssl ) != 0 )
4278 {
4279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4280 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4281 }
4282
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004283 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004284 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4285 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4286 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4287 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004288 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004289 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4290 {
4291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4292 recv_msg_seq,
4293 ssl->handshake->in_msg_seq ) );
4294 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4295 }
4296
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004297 /* Retransmit only on last message from previous flight, to avoid
4298 * too many retransmissions.
4299 * Besides, No sane server ever retransmits HelloVerifyRequest */
4300 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004304 "message_seq = %d, start_of_flight = %d",
4305 recv_msg_seq,
4306 ssl->handshake->in_flight_start_seq ) );
4307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004311 return( ret );
4312 }
4313 }
4314 else
4315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004317 "message_seq = %d, expected = %d",
4318 recv_msg_seq,
4319 ssl->handshake->in_msg_seq ) );
4320 }
4321
Hanno Becker90333da2017-10-10 11:27:13 +01004322 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004323 }
4324 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004325
Hanno Becker6d97ef52018-08-16 13:09:04 +01004326 /* Message reassembly is handled alongside buffering of future
4327 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004328 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004329 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004330 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004333 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004334 }
4335 }
4336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004338 /* With TLS we don't handle fragmentation (for now) */
4339 if( ssl->in_msglen < ssl->in_hslen )
4340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4342 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004343 }
4344
Simon Butcher99000142016-10-13 17:21:01 +01004345 return( 0 );
4346}
4347
4348void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4349{
Hanno Becker0271f962018-08-16 13:23:47 +01004350 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004351
Hanno Becker0271f962018-08-16 13:23:47 +01004352 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004353 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004354 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004355 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004356
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004357 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004360 ssl->handshake != NULL )
4361 {
Hanno Becker0271f962018-08-16 13:23:47 +01004362 unsigned offset;
4363 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004364
Hanno Becker0271f962018-08-16 13:23:47 +01004365 /* Increment handshake sequence number */
4366 hs->in_msg_seq++;
4367
4368 /*
4369 * Clear up handshake buffering and reassembly structure.
4370 */
4371
4372 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004373 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004374
4375 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004376 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4377 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004378 offset++, hs_buf++ )
4379 {
4380 *hs_buf = *(hs_buf + 1);
4381 }
4382
4383 /* Create a fresh last entry */
4384 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004385 }
4386#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004387}
4388
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004389/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004390 * DTLS anti-replay: RFC 6347 4.1.2.6
4391 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004392 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4393 * Bit n is set iff record number in_window_top - n has been seen.
4394 *
4395 * Usually, in_window_top is the last record number seen and the lsb of
4396 * in_window is set. The only exception is the initial state (record number 0
4397 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4400static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004401{
4402 ssl->in_window_top = 0;
4403 ssl->in_window = 0;
4404}
4405
4406static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4407{
4408 return( ( (uint64_t) buf[0] << 40 ) |
4409 ( (uint64_t) buf[1] << 32 ) |
4410 ( (uint64_t) buf[2] << 24 ) |
4411 ( (uint64_t) buf[3] << 16 ) |
4412 ( (uint64_t) buf[4] << 8 ) |
4413 ( (uint64_t) buf[5] ) );
4414}
4415
4416/*
4417 * Return 0 if sequence number is acceptable, -1 otherwise
4418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004420{
4421 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4422 uint64_t bit;
4423
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004424 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004425 return( 0 );
4426
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004427 if( rec_seqnum > ssl->in_window_top )
4428 return( 0 );
4429
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004430 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004431
4432 if( bit >= 64 )
4433 return( -1 );
4434
4435 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4436 return( -1 );
4437
4438 return( 0 );
4439}
4440
4441/*
4442 * Update replay window on new validated record
4443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004445{
4446 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4447
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004448 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004449 return;
4450
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004451 if( rec_seqnum > ssl->in_window_top )
4452 {
4453 /* Update window_top and the contents of the window */
4454 uint64_t shift = rec_seqnum - ssl->in_window_top;
4455
4456 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004457 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004458 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004459 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004460 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004461 ssl->in_window |= 1;
4462 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004463
4464 ssl->in_window_top = rec_seqnum;
4465 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004466 else
4467 {
4468 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004469 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004470
4471 if( bit < 64 ) /* Always true, but be extra sure */
4472 ssl->in_window |= (uint64_t) 1 << bit;
4473 }
4474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004476
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004477#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004478/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004479static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4480
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004481/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004482 * Without any SSL context, check if a datagram looks like a ClientHello with
4483 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004484 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004485 *
4486 * - if cookie is valid, return 0
4487 * - if ClientHello looks superficially valid but cookie is not,
4488 * fill obuf and set olen, then
4489 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4490 * - otherwise return a specific error code
4491 */
4492static int ssl_check_dtls_clihlo_cookie(
4493 mbedtls_ssl_cookie_write_t *f_cookie_write,
4494 mbedtls_ssl_cookie_check_t *f_cookie_check,
4495 void *p_cookie,
4496 const unsigned char *cli_id, size_t cli_id_len,
4497 const unsigned char *in, size_t in_len,
4498 unsigned char *obuf, size_t buf_len, size_t *olen )
4499{
4500 size_t sid_len, cookie_len;
4501 unsigned char *p;
4502
4503 if( f_cookie_write == NULL || f_cookie_check == NULL )
4504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4505
4506 /*
4507 * Structure of ClientHello with record and handshake headers,
4508 * and expected values. We don't need to check a lot, more checks will be
4509 * done when actually parsing the ClientHello - skipping those checks
4510 * avoids code duplication and does not make cookie forging any easier.
4511 *
4512 * 0-0 ContentType type; copied, must be handshake
4513 * 1-2 ProtocolVersion version; copied
4514 * 3-4 uint16 epoch; copied, must be 0
4515 * 5-10 uint48 sequence_number; copied
4516 * 11-12 uint16 length; (ignored)
4517 *
4518 * 13-13 HandshakeType msg_type; (ignored)
4519 * 14-16 uint24 length; (ignored)
4520 * 17-18 uint16 message_seq; copied
4521 * 19-21 uint24 fragment_offset; copied, must be 0
4522 * 22-24 uint24 fragment_length; (ignored)
4523 *
4524 * 25-26 ProtocolVersion client_version; (ignored)
4525 * 27-58 Random random; (ignored)
4526 * 59-xx SessionID session_id; 1 byte len + sid_len content
4527 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4528 * ...
4529 *
4530 * Minimum length is 61 bytes.
4531 */
4532 if( in_len < 61 ||
4533 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4534 in[3] != 0 || in[4] != 0 ||
4535 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4536 {
4537 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4538 }
4539
4540 sid_len = in[59];
4541 if( sid_len > in_len - 61 )
4542 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4543
4544 cookie_len = in[60 + sid_len];
4545 if( cookie_len > in_len - 60 )
4546 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4547
4548 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4549 cli_id, cli_id_len ) == 0 )
4550 {
4551 /* Valid cookie */
4552 return( 0 );
4553 }
4554
4555 /*
4556 * If we get here, we've got an invalid cookie, let's prepare HVR.
4557 *
4558 * 0-0 ContentType type; copied
4559 * 1-2 ProtocolVersion version; copied
4560 * 3-4 uint16 epoch; copied
4561 * 5-10 uint48 sequence_number; copied
4562 * 11-12 uint16 length; olen - 13
4563 *
4564 * 13-13 HandshakeType msg_type; hello_verify_request
4565 * 14-16 uint24 length; olen - 25
4566 * 17-18 uint16 message_seq; copied
4567 * 19-21 uint24 fragment_offset; copied
4568 * 22-24 uint24 fragment_length; olen - 25
4569 *
4570 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4571 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4572 *
4573 * Minimum length is 28.
4574 */
4575 if( buf_len < 28 )
4576 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4577
4578 /* Copy most fields and adapt others */
4579 memcpy( obuf, in, 25 );
4580 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4581 obuf[25] = 0xfe;
4582 obuf[26] = 0xff;
4583
4584 /* Generate and write actual cookie */
4585 p = obuf + 28;
4586 if( f_cookie_write( p_cookie,
4587 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4588 {
4589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4590 }
4591
4592 *olen = p - obuf;
4593
4594 /* Go back and fill length fields */
4595 obuf[27] = (unsigned char)( *olen - 28 );
4596
4597 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4598 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4599 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4600
4601 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4602 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4603
4604 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4605}
4606
4607/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004608 * Handle possible client reconnect with the same UDP quadruplet
4609 * (RFC 6347 Section 4.2.8).
4610 *
4611 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4612 * that looks like a ClientHello.
4613 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004614 * - if the input looks like a ClientHello without cookies,
4615 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004616 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004617 * - if the input looks like a ClientHello with a valid cookie,
4618 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004619 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004620 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004621 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004622 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004623 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4624 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004625 */
4626static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4627{
4628 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004629 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004630
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004631 ret = ssl_check_dtls_clihlo_cookie(
4632 ssl->conf->f_cookie_write,
4633 ssl->conf->f_cookie_check,
4634 ssl->conf->p_cookie,
4635 ssl->cli_id, ssl->cli_id_len,
4636 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004637 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004638
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004639 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4640
4641 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004642 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004643 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004644 * If the error is permanent we'll catch it later,
4645 * if it's not, then hopefully it'll work next time. */
4646 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4647
4648 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004649 }
4650
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004651 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004652 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004653 /* Got a valid cookie, partially reset context */
4654 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4655 {
4656 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4657 return( ret );
4658 }
4659
4660 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004661 }
4662
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004663 return( ret );
4664}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004665#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004666
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004667/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004668 * ContentType type;
4669 * ProtocolVersion version;
4670 * uint16 epoch; // DTLS only
4671 * uint48 sequence_number; // DTLS only
4672 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004673 *
4674 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004675 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004676 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4677 *
4678 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004679 * 1. proceed with the record if this function returns 0
4680 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4681 * 3. return CLIENT_RECONNECT if this function return that value
4682 * 4. drop the whole datagram if this function returns anything else.
4683 * Point 2 is needed when the peer is resending, and we have already received
4684 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004687{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004688 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 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 +02004691
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004693 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004694 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004697 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004698 ssl->in_msgtype,
4699 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004700
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004701 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4703 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4704 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4705 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004708
4709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004710 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4711 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004712 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4713#endif /* MBEDTLS_SSL_PROTO_DTLS */
4714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4715 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004718 }
4719
4720 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004721 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4724 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725 }
4726
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004727 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4730 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004731 }
4732
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004733 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004734 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004735 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4738 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004739 }
4740
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004741 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004742 * DTLS-related tests.
4743 * Check epoch before checking length constraint because
4744 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4745 * message gets duplicated before the corresponding Finished message,
4746 * the second ChangeCipherSpec should be discarded because it belongs
4747 * to an old epoch, but not because its length is shorter than
4748 * the minimum record length for packets using the new record transform.
4749 * Note that these two kinds of failures are handled differently,
4750 * as an unexpected record is silently skipped but an invalid
4751 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004752 */
4753#if defined(MBEDTLS_SSL_PROTO_DTLS)
4754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4755 {
4756 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4757
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004758 /* Check epoch (and sequence number) with DTLS */
4759 if( rec_epoch != ssl->in_epoch )
4760 {
4761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4762 "expected %d, received %d",
4763 ssl->in_epoch, rec_epoch ) );
4764
4765#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4766 /*
4767 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4768 * access the first byte of record content (handshake type), as we
4769 * have an active transform (possibly iv_len != 0), so use the
4770 * fact that the record header len is 13 instead.
4771 */
4772 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4773 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4774 rec_epoch == 0 &&
4775 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4776 ssl->in_left > 13 &&
4777 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4778 {
4779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4780 "from the same port" ) );
4781 return( ssl_handle_possible_reconnect( ssl ) );
4782 }
4783 else
4784#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004785 {
4786 /* Consider buffering the record. */
4787 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4788 {
4789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4790 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4791 }
4792
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004793 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004794 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004795 }
4796
4797#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4798 /* Replay detection only works for the current epoch */
4799 if( rec_epoch == ssl->in_epoch &&
4800 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4801 {
4802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4803 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4804 }
4805#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004806
Hanno Becker52c6dc62017-05-26 16:07:36 +01004807 /* Drop unexpected ApplicationData records,
4808 * except at the beginning of renegotiations */
4809 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4810 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4811#if defined(MBEDTLS_SSL_RENEGOTIATION)
4812 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4813 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4814#endif
4815 )
4816 {
4817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4818 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4819 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004820 }
4821#endif /* MBEDTLS_SSL_PROTO_DTLS */
4822
Hanno Becker52c6dc62017-05-26 16:07:36 +01004823
4824 /* Check length against bounds of the current transform and version */
4825 if( ssl->transform_in == NULL )
4826 {
4827 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004828 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004829 {
4830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4831 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4832 }
4833 }
4834 else
4835 {
4836 if( ssl->in_msglen < ssl->transform_in->minlen )
4837 {
4838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4840 }
4841
4842#if defined(MBEDTLS_SSL_PROTO_SSL3)
4843 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004844 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004845 {
4846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4847 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4848 }
4849#endif
4850#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4851 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4852 /*
4853 * TLS encrypted messages can have up to 256 bytes of padding
4854 */
4855 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4856 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004857 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004858 {
4859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4860 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4861 }
4862#endif
4863 }
4864
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004865 return( 0 );
4866}
Paul Bakker5121ce52009-01-03 21:22:43 +00004867
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004868/*
4869 * If applicable, decrypt (and decompress) record content
4870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004872{
4873 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4876 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4879 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 ret = mbedtls_ssl_hw_record_read( ssl );
4884 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4887 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004888 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004889
4890 if( ret == 0 )
4891 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004892 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004894 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004896 mbedtls_record rec;
4897
4898 rec.buf = ssl->in_iv;
4899 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4900 - ( ssl->in_iv - ssl->in_buf );
4901 rec.data_len = ssl->in_msglen;
4902 rec.data_offset = 0;
4903
4904 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4905 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4906 ssl->conf->transport, rec.ver );
4907 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004908 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4909 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 return( ret );
4913 }
4914
Hanno Becker29800d22018-08-07 14:30:18 +01004915 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4916 {
4917 /* Should never happen */
4918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004920
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004921 ssl->in_msglen = rec.data_len;
4922 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4923 ssl->in_len[1] = (unsigned char)( rec.data_len );
4924
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004925 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4926 ssl->in_msg, ssl->in_msglen );
4927
Angus Grattond8213d02016-05-25 20:56:48 +10004928 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4931 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004933 else if( ssl->in_msglen == 0 )
4934 {
4935#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4936 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4937 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4938 {
4939 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4941 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4942 }
4943#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4944
4945 ssl->nb_zero++;
4946
4947 /*
4948 * Three or more empty messages may be a DoS attack
4949 * (excessive CPU consumption).
4950 */
4951 if( ssl->nb_zero > 3 )
4952 {
4953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4954 "messages, possible DoS attack" ) );
4955 /* Q: Is that the right error code? */
4956 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4957 }
4958 }
4959 else
4960 ssl->nb_zero = 0;
4961
4962#if defined(MBEDTLS_SSL_PROTO_DTLS)
4963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4964 {
4965 ; /* in_ctr read from peer, not maintained internally */
4966 }
4967 else
4968#endif
4969 {
4970 unsigned i;
4971 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4972 if( ++ssl->in_ctr[i - 1] != 0 )
4973 break;
4974
4975 /* The loop goes to its end iff the counter is wrapping */
4976 if( i == ssl_ep_len( ssl ) )
4977 {
4978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4979 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4980 }
4981 }
4982
Paul Bakker5121ce52009-01-03 21:22:43 +00004983 }
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004986 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004988 {
4989 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004992 return( ret );
4993 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004994 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004998 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005001 }
5002#endif
5003
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005004 return( 0 );
5005}
5006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005008
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005009/*
5010 * Read a record.
5011 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005012 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5013 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5014 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005015 */
Hanno Becker1097b342018-08-15 14:09:41 +01005016
5017/* Helper functions for mbedtls_ssl_read_record(). */
5018static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005019static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5020static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005021
Hanno Becker327c93b2018-08-15 13:56:18 +01005022int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005023 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005024{
5025 int ret;
5026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005028
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005029 if( ssl->keep_current_message == 0 )
5030 {
5031 do {
Simon Butcher99000142016-10-13 17:21:01 +01005032
Hanno Becker26994592018-08-15 14:14:59 +01005033 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005034 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005035 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005036
Hanno Beckere74d5562018-08-15 14:26:08 +01005037 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005038 {
Hanno Becker40f50842018-08-15 14:48:01 +01005039#if defined(MBEDTLS_SSL_PROTO_DTLS)
5040 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005041
Hanno Becker40f50842018-08-15 14:48:01 +01005042 /* We only check for buffered messages if the
5043 * current datagram is fully consumed. */
5044 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005045 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005046 {
Hanno Becker40f50842018-08-15 14:48:01 +01005047 if( ssl_load_buffered_message( ssl ) == 0 )
5048 have_buffered = 1;
5049 }
5050
5051 if( have_buffered == 0 )
5052#endif /* MBEDTLS_SSL_PROTO_DTLS */
5053 {
5054 ret = ssl_get_next_record( ssl );
5055 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5056 continue;
5057
5058 if( ret != 0 )
5059 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005060 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005061 return( ret );
5062 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005063 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005064 }
5065
5066 ret = mbedtls_ssl_handle_message_type( ssl );
5067
Hanno Becker40f50842018-08-15 14:48:01 +01005068#if defined(MBEDTLS_SSL_PROTO_DTLS)
5069 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5070 {
5071 /* Buffer future message */
5072 ret = ssl_buffer_message( ssl );
5073 if( ret != 0 )
5074 return( ret );
5075
5076 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5077 }
5078#endif /* MBEDTLS_SSL_PROTO_DTLS */
5079
Hanno Becker90333da2017-10-10 11:27:13 +01005080 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5081 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005082
5083 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005084 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005085 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005086 return( ret );
5087 }
5088
Hanno Becker327c93b2018-08-15 13:56:18 +01005089 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005090 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005091 {
5092 mbedtls_ssl_update_handshake_status( ssl );
5093 }
Simon Butcher99000142016-10-13 17:21:01 +01005094 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005095 else
Simon Butcher99000142016-10-13 17:21:01 +01005096 {
Hanno Becker02f59072018-08-15 14:00:24 +01005097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005098 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005099 }
5100
5101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5102
5103 return( 0 );
5104}
5105
Hanno Becker40f50842018-08-15 14:48:01 +01005106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005107static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005108{
Hanno Becker40f50842018-08-15 14:48:01 +01005109 if( ssl->in_left > ssl->next_record_offset )
5110 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005111
Hanno Becker40f50842018-08-15 14:48:01 +01005112 return( 0 );
5113}
5114
5115static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5116{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005118 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005119 int ret = 0;
5120
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 if( hs == NULL )
5122 return( -1 );
5123
Hanno Beckere00ae372018-08-20 09:39:42 +01005124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5125
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005126 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5127 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5128 {
5129 /* Check if we have seen a ChangeCipherSpec before.
5130 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005131 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005132 {
5133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5134 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005135 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005136 }
5137
Hanno Becker39b8bc92018-08-28 17:17:13 +01005138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005139 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5140 ssl->in_msglen = 1;
5141 ssl->in_msg[0] = 1;
5142
5143 /* As long as they are equal, the exact value doesn't matter. */
5144 ssl->in_left = 0;
5145 ssl->next_record_offset = 0;
5146
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005147 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005148 goto exit;
5149 }
Hanno Becker37f95322018-08-16 13:55:32 +01005150
Hanno Beckerb8f50142018-08-28 10:01:34 +01005151#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005152 /* Debug only */
5153 {
5154 unsigned offset;
5155 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5156 {
5157 hs_buf = &hs->buffering.hs[offset];
5158 if( hs_buf->is_valid == 1 )
5159 {
5160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5161 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005162 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005163 }
5164 }
5165 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005166#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005167
5168 /* Check if we have buffered and/or fully reassembled the
5169 * next handshake message. */
5170 hs_buf = &hs->buffering.hs[0];
5171 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5172 {
5173 /* Synthesize a record containing the buffered HS message. */
5174 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5175 ( hs_buf->data[2] << 8 ) |
5176 hs_buf->data[3];
5177
5178 /* Double-check that we haven't accidentally buffered
5179 * a message that doesn't fit into the input buffer. */
5180 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5181 {
5182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5184 }
5185
5186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5187 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5188 hs_buf->data, msg_len + 12 );
5189
5190 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5191 ssl->in_hslen = msg_len + 12;
5192 ssl->in_msglen = msg_len + 12;
5193 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5194
5195 ret = 0;
5196 goto exit;
5197 }
5198 else
5199 {
5200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5201 hs->in_msg_seq ) );
5202 }
5203
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005204 ret = -1;
5205
5206exit:
5207
5208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5209 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005210}
5211
Hanno Beckera02b0b42018-08-21 17:20:27 +01005212static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5213 size_t desired )
5214{
5215 int offset;
5216 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5218 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219
Hanno Becker01315ea2018-08-21 17:22:17 +01005220 /* Get rid of future records epoch first, if such exist. */
5221 ssl_free_buffered_record( ssl );
5222
5223 /* Check if we have enough space available now. */
5224 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5225 hs->buffering.total_bytes_buffered ) )
5226 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005228 return( 0 );
5229 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005230
Hanno Becker4f432ad2018-08-28 10:02:32 +01005231 /* We don't have enough space to buffer the next expected handshake
5232 * message. Remove buffers used for future messages to gain space,
5233 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005234 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5235 offset >= 0; offset-- )
5236 {
5237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5238 offset ) );
5239
Hanno Beckerb309b922018-08-23 13:18:05 +01005240 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005241
5242 /* Check if we have enough space available now. */
5243 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5244 hs->buffering.total_bytes_buffered ) )
5245 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005247 return( 0 );
5248 }
5249 }
5250
5251 return( -1 );
5252}
5253
Hanno Becker40f50842018-08-15 14:48:01 +01005254static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5255{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005256 int ret = 0;
5257 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5258
5259 if( hs == NULL )
5260 return( 0 );
5261
5262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5263
5264 switch( ssl->in_msgtype )
5265 {
5266 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005268
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005269 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005270 break;
5271
5272 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005273 {
5274 unsigned recv_msg_seq_offset;
5275 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5276 mbedtls_ssl_hs_buffer *hs_buf;
5277 size_t msg_len = ssl->in_hslen - 12;
5278
5279 /* We should never receive an old handshake
5280 * message - double-check nonetheless. */
5281 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5282 {
5283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5285 }
5286
5287 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5288 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5289 {
5290 /* Silently ignore -- message too far in the future */
5291 MBEDTLS_SSL_DEBUG_MSG( 2,
5292 ( "Ignore future HS message with sequence number %u, "
5293 "buffering window %u - %u",
5294 recv_msg_seq, ssl->handshake->in_msg_seq,
5295 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5296
5297 goto exit;
5298 }
5299
5300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5301 recv_msg_seq, recv_msg_seq_offset ) );
5302
5303 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5304
5305 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005306 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005307 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005308 size_t reassembly_buf_sz;
5309
Hanno Becker37f95322018-08-16 13:55:32 +01005310 hs_buf->is_fragmented =
5311 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5312
5313 /* We copy the message back into the input buffer
5314 * after reassembly, so check that it's not too large.
5315 * This is an implementation-specific limitation
5316 * and not one from the standard, hence it is not
5317 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005318 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005319 {
5320 /* Ignore message */
5321 goto exit;
5322 }
5323
Hanno Beckere0b150f2018-08-21 15:51:03 +01005324 /* Check if we have enough space to buffer the message. */
5325 if( hs->buffering.total_bytes_buffered >
5326 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5327 {
5328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5330 }
5331
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005332 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5333 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005334
5335 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5336 hs->buffering.total_bytes_buffered ) )
5337 {
5338 if( recv_msg_seq_offset > 0 )
5339 {
5340 /* If we can't buffer a future message because
5341 * of space limitations -- ignore. */
5342 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",
5343 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5344 (unsigned) hs->buffering.total_bytes_buffered ) );
5345 goto exit;
5346 }
Hanno Beckere1801392018-08-21 16:51:05 +01005347 else
5348 {
5349 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",
5350 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5351 (unsigned) hs->buffering.total_bytes_buffered ) );
5352 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005353
Hanno Beckera02b0b42018-08-21 17:20:27 +01005354 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005355 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005356 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",
5357 (unsigned) msg_len,
5358 (unsigned) reassembly_buf_sz,
5359 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005360 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005361 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5362 goto exit;
5363 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005364 }
5365
5366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5367 msg_len ) );
5368
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005369 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5370 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005371 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005372 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005373 goto exit;
5374 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005375 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005376
5377 /* Prepare final header: copy msg_type, length and message_seq,
5378 * then add standardised fragment_offset and fragment_length */
5379 memcpy( hs_buf->data, ssl->in_msg, 6 );
5380 memset( hs_buf->data + 6, 0, 3 );
5381 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5382
5383 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005384
5385 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005386 }
5387 else
5388 {
5389 /* Make sure msg_type and length are consistent */
5390 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5391 {
5392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5393 /* Ignore */
5394 goto exit;
5395 }
5396 }
5397
Hanno Becker4422bbb2018-08-20 09:40:19 +01005398 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005399 {
5400 size_t frag_len, frag_off;
5401 unsigned char * const msg = hs_buf->data + 12;
5402
5403 /*
5404 * Check and copy current fragment
5405 */
5406
5407 /* Validation of header fields already done in
5408 * mbedtls_ssl_prepare_handshake_record(). */
5409 frag_off = ssl_get_hs_frag_off( ssl );
5410 frag_len = ssl_get_hs_frag_len( ssl );
5411
5412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5413 frag_off, frag_len ) );
5414 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5415
5416 if( hs_buf->is_fragmented )
5417 {
5418 unsigned char * const bitmask = msg + msg_len;
5419 ssl_bitmask_set( bitmask, frag_off, frag_len );
5420 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5421 msg_len ) == 0 );
5422 }
5423 else
5424 {
5425 hs_buf->is_complete = 1;
5426 }
5427
5428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5429 hs_buf->is_complete ? "" : "not yet " ) );
5430 }
5431
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005432 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005433 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005434
5435 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005436 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005437 break;
5438 }
5439
5440exit:
5441
5442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5443 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005444}
5445#endif /* MBEDTLS_SSL_PROTO_DTLS */
5446
Hanno Becker1097b342018-08-15 14:09:41 +01005447static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005448{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005449 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005450 * Consume last content-layer message and potentially
5451 * update in_msglen which keeps track of the contents'
5452 * consumption state.
5453 *
5454 * (1) Handshake messages:
5455 * Remove last handshake message, move content
5456 * and adapt in_msglen.
5457 *
5458 * (2) Alert messages:
5459 * Consume whole record content, in_msglen = 0.
5460 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005461 * (3) Change cipher spec:
5462 * Consume whole record content, in_msglen = 0.
5463 *
5464 * (4) Application data:
5465 * Don't do anything - the record layer provides
5466 * the application data as a stream transport
5467 * and consumes through mbedtls_ssl_read only.
5468 *
5469 */
5470
5471 /* Case (1): Handshake messages */
5472 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005473 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005474 /* Hard assertion to be sure that no application data
5475 * is in flight, as corrupting ssl->in_msglen during
5476 * ssl->in_offt != NULL is fatal. */
5477 if( ssl->in_offt != NULL )
5478 {
5479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5481 }
5482
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005483 /*
5484 * Get next Handshake message in the current record
5485 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005486
Hanno Becker4a810fb2017-05-24 16:27:30 +01005487 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005488 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005489 * current handshake content: If DTLS handshake
5490 * fragmentation is used, that's the fragment
5491 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005492 * size here is faulty and should be changed at
5493 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005494 * (2) While it doesn't seem to cause problems, one
5495 * has to be very careful not to assume that in_hslen
5496 * is always <= in_msglen in a sensible communication.
5497 * Again, it's wrong for DTLS handshake fragmentation.
5498 * The following check is therefore mandatory, and
5499 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005500 * Additionally, ssl->in_hslen might be arbitrarily out of
5501 * bounds after handling a DTLS message with an unexpected
5502 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005503 */
5504 if( ssl->in_hslen < ssl->in_msglen )
5505 {
5506 ssl->in_msglen -= ssl->in_hslen;
5507 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5508 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005509
Hanno Becker4a810fb2017-05-24 16:27:30 +01005510 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5511 ssl->in_msg, ssl->in_msglen );
5512 }
5513 else
5514 {
5515 ssl->in_msglen = 0;
5516 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005517
Hanno Becker4a810fb2017-05-24 16:27:30 +01005518 ssl->in_hslen = 0;
5519 }
5520 /* Case (4): Application data */
5521 else if( ssl->in_offt != NULL )
5522 {
5523 return( 0 );
5524 }
5525 /* Everything else (CCS & Alerts) */
5526 else
5527 {
5528 ssl->in_msglen = 0;
5529 }
5530
Hanno Becker1097b342018-08-15 14:09:41 +01005531 return( 0 );
5532}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005533
Hanno Beckere74d5562018-08-15 14:26:08 +01005534static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5535{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005536 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005537 return( 1 );
5538
5539 return( 0 );
5540}
5541
Hanno Becker5f066e72018-08-16 14:56:31 +01005542#if defined(MBEDTLS_SSL_PROTO_DTLS)
5543
5544static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5545{
5546 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5547 if( hs == NULL )
5548 return;
5549
Hanno Becker01315ea2018-08-21 17:22:17 +01005550 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005551 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005552 hs->buffering.total_bytes_buffered -=
5553 hs->buffering.future_record.len;
5554
5555 mbedtls_free( hs->buffering.future_record.data );
5556 hs->buffering.future_record.data = NULL;
5557 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005558}
5559
5560static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5561{
5562 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5563 unsigned char * rec;
5564 size_t rec_len;
5565 unsigned rec_epoch;
5566
5567 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5568 return( 0 );
5569
5570 if( hs == NULL )
5571 return( 0 );
5572
Hanno Becker5f066e72018-08-16 14:56:31 +01005573 rec = hs->buffering.future_record.data;
5574 rec_len = hs->buffering.future_record.len;
5575 rec_epoch = hs->buffering.future_record.epoch;
5576
5577 if( rec == NULL )
5578 return( 0 );
5579
Hanno Becker4cb782d2018-08-20 11:19:05 +01005580 /* Only consider loading future records if the
5581 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005582 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005583 return( 0 );
5584
Hanno Becker5f066e72018-08-16 14:56:31 +01005585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5586
5587 if( rec_epoch != ssl->in_epoch )
5588 {
5589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5590 goto exit;
5591 }
5592
5593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5594
5595 /* Double-check that the record is not too large */
5596 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5597 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5598 {
5599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5601 }
5602
5603 memcpy( ssl->in_hdr, rec, rec_len );
5604 ssl->in_left = rec_len;
5605 ssl->next_record_offset = 0;
5606
5607 ssl_free_buffered_record( ssl );
5608
5609exit:
5610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5611 return( 0 );
5612}
5613
5614static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5615{
5616 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5617 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005618 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005619
5620 /* Don't buffer future records outside handshakes. */
5621 if( hs == NULL )
5622 return( 0 );
5623
5624 /* Only buffer handshake records (we are only interested
5625 * in Finished messages). */
5626 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5627 return( 0 );
5628
5629 /* Don't buffer more than one future epoch record. */
5630 if( hs->buffering.future_record.data != NULL )
5631 return( 0 );
5632
Hanno Becker01315ea2018-08-21 17:22:17 +01005633 /* Don't buffer record if there's not enough buffering space remaining. */
5634 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5635 hs->buffering.total_bytes_buffered ) )
5636 {
5637 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",
5638 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5639 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005640 return( 0 );
5641 }
5642
Hanno Becker5f066e72018-08-16 14:56:31 +01005643 /* Buffer record */
5644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5645 ssl->in_epoch + 1 ) );
5646 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5647 rec_hdr_len + ssl->in_msglen );
5648
5649 /* ssl_parse_record_header() only considers records
5650 * of the next epoch as candidates for buffering. */
5651 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005652 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005653
5654 hs->buffering.future_record.data =
5655 mbedtls_calloc( 1, hs->buffering.future_record.len );
5656 if( hs->buffering.future_record.data == NULL )
5657 {
5658 /* If we run out of RAM trying to buffer a
5659 * record from the next epoch, just ignore. */
5660 return( 0 );
5661 }
5662
Hanno Becker01315ea2018-08-21 17:22:17 +01005663 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005664
Hanno Becker01315ea2018-08-21 17:22:17 +01005665 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005666 return( 0 );
5667}
5668
5669#endif /* MBEDTLS_SSL_PROTO_DTLS */
5670
Hanno Beckere74d5562018-08-15 14:26:08 +01005671static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005672{
5673 int ret;
5674
Hanno Becker5f066e72018-08-16 14:56:31 +01005675#if defined(MBEDTLS_SSL_PROTO_DTLS)
5676 /* We might have buffered a future record; if so,
5677 * and if the epoch matches now, load it.
5678 * On success, this call will set ssl->in_left to
5679 * the length of the buffered record, so that
5680 * the calls to ssl_fetch_input() below will
5681 * essentially be no-ops. */
5682 ret = ssl_load_buffered_record( ssl );
5683 if( ret != 0 )
5684 return( ret );
5685#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005690 return( ret );
5691 }
5692
5693 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5697 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005698 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005699 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5700 {
5701 ret = ssl_buffer_future_record( ssl );
5702 if( ret != 0 )
5703 return( ret );
5704
5705 /* Fall through to handling of unexpected records */
5706 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5707 }
5708
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005709 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5710 {
5711 /* Skip unexpected record (but not whole datagram) */
5712 ssl->next_record_offset = ssl->in_msglen
5713 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5716 "(header)" ) );
5717 }
5718 else
5719 {
5720 /* Skip invalid record and the rest of the datagram */
5721 ssl->next_record_offset = 0;
5722 ssl->in_left = 0;
5723
5724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5725 "(header)" ) );
5726 }
5727
5728 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005729 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005730 }
5731#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005732 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005733 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005734
5735 /*
5736 * Read and optionally decrypt the message contents
5737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5739 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005742 return( ret );
5743 }
5744
5745 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005747 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005750 if( ssl->next_record_offset < ssl->in_left )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5753 }
5754 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005755 else
5756#endif
5757 ssl->in_left = 0;
5758
5759 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005763 {
5764 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5766 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005767 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005768 /* Except when waiting for Finished as a bad mac here
5769 * probably means something went wrong in the handshake
5770 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5771 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5772 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5773 {
5774#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5775 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5776 {
5777 mbedtls_ssl_send_alert_message( ssl,
5778 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5779 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5780 }
5781#endif
5782 return( ret );
5783 }
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005786 if( ssl->conf->badmac_limit != 0 &&
5787 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5790 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005791 }
5792#endif
5793
Hanno Becker4a810fb2017-05-24 16:27:30 +01005794 /* As above, invalid records cause
5795 * dismissal of the whole datagram. */
5796
5797 ssl->next_record_offset = 0;
5798 ssl->in_left = 0;
5799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005801 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005802 }
5803
5804 return( ret );
5805 }
5806 else
5807#endif
5808 {
5809 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5811 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 mbedtls_ssl_send_alert_message( ssl,
5814 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5815 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005816 }
5817#endif
5818 return( ret );
5819 }
5820 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005821
Simon Butcher99000142016-10-13 17:21:01 +01005822 return( 0 );
5823}
5824
5825int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5826{
5827 int ret;
5828
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005829 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005830 * Handle particular types of records
5831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 {
Simon Butcher99000142016-10-13 17:21:01 +01005834 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5835 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005836 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 }
5839
Hanno Beckere678eaa2018-08-21 14:57:46 +01005840 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005841 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005842 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005843 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5845 ssl->in_msglen ) );
5846 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005847 }
5848
Hanno Beckere678eaa2018-08-21 14:57:46 +01005849 if( ssl->in_msg[0] != 1 )
5850 {
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5852 ssl->in_msg[0] ) );
5853 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5854 }
5855
5856#if defined(MBEDTLS_SSL_PROTO_DTLS)
5857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5858 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5859 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5860 {
5861 if( ssl->handshake == NULL )
5862 {
5863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5864 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5865 }
5866
5867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5868 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5869 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005870#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005871 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005875 if( ssl->in_msglen != 2 )
5876 {
5877 /* Note: Standard allows for more than one 2 byte alert
5878 to be packed in a single message, but Mbed TLS doesn't
5879 currently support this. */
5880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5881 ssl->in_msglen ) );
5882 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5883 }
5884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 ssl->in_msg[0], ssl->in_msg[1] ) );
5887
5888 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005889 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005894 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 }
5897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5899 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5902 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005904
5905#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5906 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5907 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5908 {
Hanno Becker90333da2017-10-10 11:27:13 +01005909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005910 /* Will be handled when trying to parse ServerHello */
5911 return( 0 );
5912 }
5913#endif
5914
5915#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5916 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5917 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5918 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5919 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5920 {
5921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5922 /* Will be handled in mbedtls_ssl_parse_certificate() */
5923 return( 0 );
5924 }
5925#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5926
5927 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005928 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005929 }
5930
Hanno Beckerc76c6192017-06-06 10:03:17 +01005931#if defined(MBEDTLS_SSL_PROTO_DTLS)
5932 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5933 ssl->handshake != NULL &&
5934 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5935 {
5936 ssl_handshake_wrapup_free_hs_transform( ssl );
5937 }
5938#endif
5939
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 return( 0 );
5941}
5942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005944{
5945 int ret;
5946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5948 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5949 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005950 {
5951 return( ret );
5952 }
5953
5954 return( 0 );
5955}
5956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005958 unsigned char level,
5959 unsigned char message )
5960{
5961 int ret;
5962
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005963 if( ssl == NULL || ssl->conf == NULL )
5964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005970 ssl->out_msglen = 2;
5971 ssl->out_msg[0] = level;
5972 ssl->out_msg[1] = message;
5973
Hanno Becker67bc7c32018-08-06 11:33:50 +01005974 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005977 return( ret );
5978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005980
5981 return( 0 );
5982}
5983
Hanno Beckerb9d44792019-02-08 07:19:04 +00005984#if defined(MBEDTLS_X509_CRT_PARSE_C)
5985static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5986{
5987#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5988 if( session->peer_cert != NULL )
5989 {
5990 mbedtls_x509_crt_free( session->peer_cert );
5991 mbedtls_free( session->peer_cert );
5992 session->peer_cert = NULL;
5993 }
5994#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5995 if( session->peer_cert_digest != NULL )
5996 {
5997 /* Zeroization is not necessary. */
5998 mbedtls_free( session->peer_cert_digest );
5999 session->peer_cert_digest = NULL;
6000 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6001 session->peer_cert_digest_len = 0;
6002 }
6003#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6004}
6005#endif /* MBEDTLS_X509_CRT_PARSE_C */
6006
Paul Bakker5121ce52009-01-03 21:22:43 +00006007/*
6008 * Handshake functions
6009 */
Hanno Becker21489932019-02-05 13:20:55 +00006010#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006011/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006013{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006014 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6015 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006018
Hanno Becker7177a882019-02-05 13:36:46 +00006019 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006022 ssl->state++;
6023 return( 0 );
6024 }
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006028}
6029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006032 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6033 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006036
Hanno Becker7177a882019-02-05 13:36:46 +00006037 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040 ssl->state++;
6041 return( 0 );
6042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006046}
Gilles Peskinef9828522017-05-03 12:28:43 +02006047
Hanno Becker21489932019-02-05 13:20:55 +00006048#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006049/* Some certificate support -> implement write and parse */
6050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006054 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006056 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6057 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006060
Hanno Becker7177a882019-02-05 13:36:46 +00006061 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006064 ssl->state++;
6065 return( 0 );
6066 }
6067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006069 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 {
6071 if( ssl->client_auth == 0 )
6072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 ssl->state++;
6075 return( 0 );
6076 }
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 /*
6080 * If using SSLv3 and got no cert, send an Alert message
6081 * (otherwise an empty Certificate message will be sent).
6082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6084 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 {
6086 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6088 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6089 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 goto write_msg;
6093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096#endif /* MBEDTLS_SSL_CLI_C */
6097#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006098 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6103 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006104 }
6105 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
6110 /*
6111 * 0 . 0 handshake type
6112 * 1 . 3 handshake length
6113 * 4 . 6 length of all certs
6114 * 7 . 9 length of cert. 1
6115 * 10 . n-1 peer certificate
6116 * n . n+2 length of cert. 2
6117 * n+3 . ... upper level cert, etc.
6118 */
6119 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Paul Bakker29087132010-03-21 21:03:34 +00006122 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 {
6124 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006125 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006128 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 }
6131
6132 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6133 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6134 ssl->out_msg[i + 2] = (unsigned char)( n );
6135
6136 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6137 i += n; crt = crt->next;
6138 }
6139
6140 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6141 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6142 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6143
6144 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6146 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006148#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006149write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006151
6152 ssl->state++;
6153
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006154 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 return( ret );
6158 }
6159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
Paul Bakkered27a042013-04-18 22:46:23 +02006162 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006163}
6164
Hanno Becker84879e32019-01-31 07:44:03 +00006165#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006166
6167#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006168static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6169 unsigned char *crt_buf,
6170 size_t crt_buf_len )
6171{
6172 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6173
6174 if( peer_crt == NULL )
6175 return( -1 );
6176
6177 if( peer_crt->raw.len != crt_buf_len )
6178 return( -1 );
6179
Hanno Becker46f34d02019-02-08 14:00:04 +00006180 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006181}
Hanno Becker177475a2019-02-05 17:02:46 +00006182#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6183static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6184 unsigned char *crt_buf,
6185 size_t crt_buf_len )
6186{
6187 int ret;
6188 unsigned char const * const peer_cert_digest =
6189 ssl->session->peer_cert_digest;
6190 mbedtls_md_type_t const peer_cert_digest_type =
6191 ssl->session->peer_cert_digest_type;
6192 mbedtls_md_info_t const * const digest_info =
6193 mbedtls_md_info_from_type( peer_cert_digest_type );
6194 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6195 size_t digest_len;
6196
6197 if( peer_cert_digest == NULL || digest_info == NULL )
6198 return( -1 );
6199
6200 digest_len = mbedtls_md_get_size( digest_info );
6201 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6202 return( -1 );
6203
6204 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6205 if( ret != 0 )
6206 return( -1 );
6207
6208 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6209}
6210#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006211#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006212
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006213/*
6214 * Once the certificate message is read, parse it into a cert chain and
6215 * perform basic checks, but leave actual verification to the caller
6216 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006217static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6218 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006219{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006220 int ret;
6221#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6222 int crt_cnt=0;
6223#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006224 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006225 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6231 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233 }
6234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6236 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006239 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6240 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 }
6243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006245
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006249 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006250
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006251 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006255 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6256 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006258 }
6259
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006260 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6261 i += 3;
6262
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006263 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006264 while( i < ssl->in_hslen )
6265 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006266 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006267 if ( i + 3 > ssl->in_hslen ) {
6268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006269 mbedtls_ssl_send_alert_message( ssl,
6270 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6271 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006272 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6273 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006274 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6275 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006276 if( ssl->in_msg[i] != 0 )
6277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006279 mbedtls_ssl_send_alert_message( ssl,
6280 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 }
6284
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006285 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6287 | (unsigned int) ssl->in_msg[i + 2];
6288 i += 3;
6289
6290 if( n < 128 || i + n > ssl->in_hslen )
6291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006293 mbedtls_ssl_send_alert_message( ssl,
6294 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6295 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297 }
6298
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006299 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006300#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6301 if( crt_cnt++ == 0 &&
6302 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6303 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006304 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006305 /* During client-side renegotiation, check that the server's
6306 * end-CRTs hasn't changed compared to the initial handshake,
6307 * mitigating the triple handshake attack. On success, reuse
6308 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6310 if( ssl_check_peer_crt_unchanged( ssl,
6311 &ssl->in_msg[i],
6312 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006313 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6315 mbedtls_ssl_send_alert_message( ssl,
6316 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6317 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6318 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006319 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006320
6321 /* Now we can safely free the original chain. */
6322 ssl_clear_peer_cert( ssl->session );
6323 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006324#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6325
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006326 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006327#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006328 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006329#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006330 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006331 * it in-place from the input buffer instead of making a copy. */
6332 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6333#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006334 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006336 case 0: /*ok*/
6337 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6338 /* Ignore certificate with an unknown algorithm: maybe a
6339 prior certificate was already trusted. */
6340 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006341
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006342 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6343 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6344 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006345
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006346 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6347 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6348 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006349
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006350 default:
6351 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6352 crt_parse_der_failed:
6353 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6354 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6355 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 }
6357
6358 i += n;
6359 }
6360
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006361 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006362 return( 0 );
6363}
6364
Hanno Becker4a55f632019-02-05 12:49:06 +00006365#if defined(MBEDTLS_SSL_SRV_C)
6366static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6367{
6368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6369 return( -1 );
6370
6371#if defined(MBEDTLS_SSL_PROTO_SSL3)
6372 /*
6373 * Check if the client sent an empty certificate
6374 */
6375 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6376 {
6377 if( ssl->in_msglen == 2 &&
6378 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6379 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6380 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6381 {
6382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6383 return( 0 );
6384 }
6385
6386 return( -1 );
6387 }
6388#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6389
6390#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6391 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6392 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6393 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6394 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6395 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6396 {
6397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6398 return( 0 );
6399 }
6400
6401 return( -1 );
6402#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6403 MBEDTLS_SSL_PROTO_TLS1_2 */
6404}
6405#endif /* MBEDTLS_SSL_SRV_C */
6406
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006407/* Check if a certificate message is expected.
6408 * Return either
6409 * - SSL_CERTIFICATE_EXPECTED, or
6410 * - SSL_CERTIFICATE_SKIP
6411 * indicating whether a Certificate message is expected or not.
6412 */
6413#define SSL_CERTIFICATE_EXPECTED 0
6414#define SSL_CERTIFICATE_SKIP 1
6415static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6416 int authmode )
6417{
6418 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006419 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006420
6421 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6422 return( SSL_CERTIFICATE_SKIP );
6423
6424#if defined(MBEDTLS_SSL_SRV_C)
6425 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6426 {
6427 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6428 return( SSL_CERTIFICATE_SKIP );
6429
6430 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6431 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006432 ssl->session_negotiate->verify_result =
6433 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6434 return( SSL_CERTIFICATE_SKIP );
6435 }
6436 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006437#else
6438 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006439#endif /* MBEDTLS_SSL_SRV_C */
6440
6441 return( SSL_CERTIFICATE_EXPECTED );
6442}
6443
Hanno Becker68636192019-02-05 14:36:34 +00006444static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6445 int authmode,
6446 mbedtls_x509_crt *chain,
6447 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006448{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006449 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006450 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006451 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006452 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006453
Hanno Becker8927c832019-04-03 12:52:50 +01006454 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6455 void *p_vrfy;
6456
Hanno Becker68636192019-02-05 14:36:34 +00006457 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6458 return( 0 );
6459
Hanno Becker8927c832019-04-03 12:52:50 +01006460 if( ssl->f_vrfy != NULL )
6461 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006463 f_vrfy = ssl->f_vrfy;
6464 p_vrfy = ssl->p_vrfy;
6465 }
6466 else
6467 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006468 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006469 f_vrfy = ssl->conf->f_vrfy;
6470 p_vrfy = ssl->conf->p_vrfy;
6471 }
6472
Hanno Becker68636192019-02-05 14:36:34 +00006473 /*
6474 * Main check: verify certificate
6475 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006476#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6477 if( ssl->conf->f_ca_cb != NULL )
6478 {
6479 ((void) rs_ctx);
6480 have_ca_chain = 1;
6481
6482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006483 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006484 chain,
6485 ssl->conf->f_ca_cb,
6486 ssl->conf->p_ca_cb,
6487 ssl->conf->cert_profile,
6488 ssl->hostname,
6489 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006490 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006491 }
6492 else
6493#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6494 {
6495 mbedtls_x509_crt *ca_chain;
6496 mbedtls_x509_crl *ca_crl;
6497
6498#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6499 if( ssl->handshake->sni_ca_chain != NULL )
6500 {
6501 ca_chain = ssl->handshake->sni_ca_chain;
6502 ca_crl = ssl->handshake->sni_ca_crl;
6503 }
6504 else
6505#endif
6506 {
6507 ca_chain = ssl->conf->ca_chain;
6508 ca_crl = ssl->conf->ca_crl;
6509 }
6510
6511 if( ca_chain != NULL )
6512 have_ca_chain = 1;
6513
6514 ret = mbedtls_x509_crt_verify_restartable(
6515 chain,
6516 ca_chain, ca_crl,
6517 ssl->conf->cert_profile,
6518 ssl->hostname,
6519 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006520 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006521 }
Hanno Becker68636192019-02-05 14:36:34 +00006522
6523 if( ret != 0 )
6524 {
6525 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6526 }
6527
6528#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6529 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6530 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6531#endif
6532
6533 /*
6534 * Secondary checks: always done, but change 'ret' only if it was 0
6535 */
6536
6537#if defined(MBEDTLS_ECP_C)
6538 {
6539 const mbedtls_pk_context *pk = &chain->pk;
6540
6541 /* If certificate uses an EC key, make sure the curve is OK */
6542 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6543 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6544 {
6545 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6546
6547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6548 if( ret == 0 )
6549 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6550 }
6551 }
6552#endif /* MBEDTLS_ECP_C */
6553
6554 if( mbedtls_ssl_check_cert_usage( chain,
6555 ciphersuite_info,
6556 ! ssl->conf->endpoint,
6557 &ssl->session_negotiate->verify_result ) != 0 )
6558 {
6559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6560 if( ret == 0 )
6561 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6562 }
6563
6564 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6565 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6566 * with details encoded in the verification flags. All other kinds
6567 * of error codes, including those from the user provided f_vrfy
6568 * functions, are treated as fatal and lead to a failure of
6569 * ssl_parse_certificate even if verification was optional. */
6570 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6571 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6572 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6573 {
6574 ret = 0;
6575 }
6576
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006577 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006578 {
6579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6580 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6581 }
6582
6583 if( ret != 0 )
6584 {
6585 uint8_t alert;
6586
6587 /* The certificate may have been rejected for several reasons.
6588 Pick one and send the corresponding alert. Which alert to send
6589 may be a subject of debate in some cases. */
6590 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6591 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6592 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6593 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6594 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6595 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6596 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6597 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6598 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6599 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6600 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6601 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6602 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6603 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6604 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6605 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6606 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6607 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6608 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6609 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6610 else
6611 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6613 alert );
6614 }
6615
6616#if defined(MBEDTLS_DEBUG_C)
6617 if( ssl->session_negotiate->verify_result != 0 )
6618 {
6619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6620 ssl->session_negotiate->verify_result ) );
6621 }
6622 else
6623 {
6624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6625 }
6626#endif /* MBEDTLS_DEBUG_C */
6627
6628 return( ret );
6629}
6630
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006631#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6632static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6633 unsigned char *start, size_t len )
6634{
6635 int ret;
6636 /* Remember digest of the peer's end-CRT. */
6637 ssl->session_negotiate->peer_cert_digest =
6638 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6639 if( ssl->session_negotiate->peer_cert_digest == NULL )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6642 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6643 mbedtls_ssl_send_alert_message( ssl,
6644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6645 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6646
6647 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6648 }
6649
6650 ret = mbedtls_md( mbedtls_md_info_from_type(
6651 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6652 start, len,
6653 ssl->session_negotiate->peer_cert_digest );
6654
6655 ssl->session_negotiate->peer_cert_digest_type =
6656 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6657 ssl->session_negotiate->peer_cert_digest_len =
6658 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6659
6660 return( ret );
6661}
6662
6663static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6664 unsigned char *start, size_t len )
6665{
6666 unsigned char *end = start + len;
6667 int ret;
6668
6669 /* Make a copy of the peer's raw public key. */
6670 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6671 ret = mbedtls_pk_parse_subpubkey( &start, end,
6672 &ssl->handshake->peer_pubkey );
6673 if( ret != 0 )
6674 {
6675 /* We should have parsed the public key before. */
6676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6677 }
6678
6679 return( 0 );
6680}
6681#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6682
Hanno Becker68636192019-02-05 14:36:34 +00006683int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6684{
6685 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006686 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006687#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6688 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6689 ? ssl->handshake->sni_authmode
6690 : ssl->conf->authmode;
6691#else
6692 const int authmode = ssl->conf->authmode;
6693#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006694 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006695 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006696
6697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6698
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006699 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6700 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006701 {
6702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006703 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006704 }
6705
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006706#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6707 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006708 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006709 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006710 chain = ssl->handshake->ecrs_peer_cert;
6711 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006712 goto crt_verify;
6713 }
6714#endif
6715
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006716 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006717 {
6718 /* mbedtls_ssl_read_record may have sent an alert already. We
6719 let it decide whether to alert. */
6720 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006721 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006722 }
6723
Hanno Becker4a55f632019-02-05 12:49:06 +00006724#if defined(MBEDTLS_SSL_SRV_C)
6725 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6726 {
6727 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006728
6729 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006730 ret = 0;
6731 else
6732 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006733
Hanno Becker6bdfab22019-02-05 13:11:17 +00006734 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006735 }
6736#endif /* MBEDTLS_SSL_SRV_C */
6737
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006738 /* Clear existing peer CRT structure in case we tried to
6739 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006740 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006741
6742 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6743 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006744 {
6745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6746 sizeof( mbedtls_x509_crt ) ) );
6747 mbedtls_ssl_send_alert_message( ssl,
6748 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6749 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006750
Hanno Becker3dad3112019-02-05 17:19:52 +00006751 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6752 goto exit;
6753 }
6754 mbedtls_x509_crt_init( chain );
6755
6756 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006757 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006758 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006759
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006760#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6761 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006762 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006763
6764crt_verify:
6765 if( ssl->handshake->ecrs_enabled)
6766 rs_ctx = &ssl->handshake->ecrs_ctx;
6767#endif
6768
Hanno Becker68636192019-02-05 14:36:34 +00006769 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006770 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006771 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006772 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006773
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006774#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006775 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006776 unsigned char *crt_start, *pk_start;
6777 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006778
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006779 /* We parse the CRT chain without copying, so
6780 * these pointers point into the input buffer,
6781 * and are hence still valid after freeing the
6782 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006783
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006784 crt_start = chain->raw.p;
6785 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006786
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006787 pk_start = chain->pk_raw.p;
6788 pk_len = chain->pk_raw.len;
6789
6790 /* Free the CRT structures before computing
6791 * digest and copying the peer's public key. */
6792 mbedtls_x509_crt_free( chain );
6793 mbedtls_free( chain );
6794 chain = NULL;
6795
6796 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006797 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006798 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006799
6800 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6801 if( ret != 0 )
6802 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006803 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006804#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6805 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006806 ssl->session_negotiate->peer_cert = chain;
6807 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006808#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811
Hanno Becker6bdfab22019-02-05 13:11:17 +00006812exit:
6813
Hanno Becker3dad3112019-02-05 17:19:52 +00006814 if( ret == 0 )
6815 ssl->state++;
6816
6817#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6818 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6819 {
6820 ssl->handshake->ecrs_peer_cert = chain;
6821 chain = NULL;
6822 }
6823#endif
6824
6825 if( chain != NULL )
6826 {
6827 mbedtls_x509_crt_free( chain );
6828 mbedtls_free( chain );
6829 }
6830
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 return( ret );
6832}
Hanno Becker21489932019-02-05 13:20:55 +00006833#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006836{
6837 int ret;
6838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006842 ssl->out_msglen = 1;
6843 ssl->out_msg[0] = 1;
6844
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 ssl->state++;
6846
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006847 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006848 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 return( ret );
6851 }
6852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
6855 return( 0 );
6856}
6857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006859{
6860 int ret;
6861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006863
Hanno Becker327c93b2018-08-15 13:56:18 +01006864 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867 return( ret );
6868 }
6869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006873 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6874 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006876 }
6877
Hanno Beckere678eaa2018-08-21 14:57:46 +01006878 /* CCS records are only accepted if they have length 1 and content '1',
6879 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006880
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006881 /*
6882 * Switch to our negotiated transform and session parameters for inbound
6883 * data.
6884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006886 ssl->transform_in = ssl->transform_negotiate;
6887 ssl->session_in = ssl->session_negotiate;
6888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006890 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006893 ssl_dtls_replay_reset( ssl );
6894#endif
6895
6896 /* Increment epoch */
6897 if( ++ssl->in_epoch == 0 )
6898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006900 /* This is highly unlikely to happen for legitimate reasons, so
6901 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006903 }
6904 }
6905 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006907 memset( ssl->in_ctr, 0, 8 );
6908
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006909 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6912 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006917 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6918 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006920 }
6921 }
6922#endif
6923
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 ssl->state++;
6925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006927
6928 return( 0 );
6929}
6930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6932 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006933{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006934 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6937 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6938 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006939 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006940 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6943#if defined(MBEDTLS_SHA512_C)
6944 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006945 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6946 else
6947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SHA256_C)
6949 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006950 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006951 else
6952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006956 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006957 }
Paul Bakker380da532012-04-18 16:10:25 +00006958}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6963 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006964 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6965 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6968#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006969#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006970 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006971 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6972#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006973 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006974#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006977#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006978 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006979 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006980#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006981 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006982#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006985}
6986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006988 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6991 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006992 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6993 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6996#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006997#if defined(MBEDTLS_USE_PSA_CRYPTO)
6998 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6999#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007000 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007001#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007004#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007005 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007006#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007007 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007008#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007011}
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7014 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7015static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007016 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007017{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007018 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7019 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007020}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007021#endif
Paul Bakker380da532012-04-18 16:10:25 +00007022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7024#if defined(MBEDTLS_SHA256_C)
7025static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007026 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007027{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007028#if defined(MBEDTLS_USE_PSA_CRYPTO)
7029 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7030#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007031 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007032#endif
Paul Bakker380da532012-04-18 16:10:25 +00007033}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007034#endif
Paul Bakker380da532012-04-18 16:10:25 +00007035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_SHA512_C)
7037static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007038 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007039{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007040#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007041 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007042#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007043 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007044#endif
Paul Bakker380da532012-04-18 16:10:25 +00007045}
Paul Bakker769075d2012-11-24 11:26:46 +01007046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007050static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007052{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007053 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007054 mbedtls_md5_context md5;
7055 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007056
Paul Bakker5121ce52009-01-03 21:22:43 +00007057 unsigned char padbuf[48];
7058 unsigned char md5sum[16];
7059 unsigned char sha1sum[20];
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007062 if( !session )
7063 session = ssl->session;
7064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007066
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007067 mbedtls_md5_init( &md5 );
7068 mbedtls_sha1_init( &sha1 );
7069
7070 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7071 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
7073 /*
7074 * SSLv3:
7075 * hash =
7076 * MD5( master + pad2 +
7077 * MD5( handshake + sender + master + pad1 ) )
7078 * + SHA1( master + pad2 +
7079 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 */
7081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007083 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7084 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007088 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7089 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007093 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Paul Bakker1ef83d62012-04-11 12:09:53 +00007095 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007097 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7098 mbedtls_md5_update_ret( &md5, session->master, 48 );
7099 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7100 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007101
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007102 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7103 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7104 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7105 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106
Paul Bakker1ef83d62012-04-11 12:09:53 +00007107 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007108
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007109 mbedtls_md5_starts_ret( &md5 );
7110 mbedtls_md5_update_ret( &md5, session->master, 48 );
7111 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7112 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7113 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007114
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007115 mbedtls_sha1_starts_ret( &sha1 );
7116 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7117 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7118 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7119 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007123 mbedtls_md5_free( &md5 );
7124 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007125
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007126 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7127 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7128 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007135static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007137{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007138 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007139 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007140 mbedtls_md5_context md5;
7141 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007145 if( !session )
7146 session = ssl->session;
7147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007149
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007150 mbedtls_md5_init( &md5 );
7151 mbedtls_sha1_init( &sha1 );
7152
7153 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7154 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007155
Paul Bakker1ef83d62012-04-11 12:09:53 +00007156 /*
7157 * TLSv1:
7158 * hash = PRF( master, finished_label,
7159 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7160 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007163 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7164 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007165#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007168 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7169 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007170#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007173 ? "client finished"
7174 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007175
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007176 mbedtls_md5_finish_ret( &md5, padbuf );
7177 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007178
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007179 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007180 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007183
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007184 mbedtls_md5_free( &md5 );
7185 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007186
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007187 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7194#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007195static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007197{
7198 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007199 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007200 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007201#if defined(MBEDTLS_USE_PSA_CRYPTO)
7202 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007203 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007204 psa_status_t status;
7205#else
7206 mbedtls_sha256_context sha256;
7207#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007210 if( !session )
7211 session = ssl->session;
7212
Andrzej Kurekeb342242019-01-29 09:14:33 -05007213 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7214 ? "client finished"
7215 : "server finished";
7216
7217#if defined(MBEDTLS_USE_PSA_CRYPTO)
7218 sha256_psa = psa_hash_operation_init();
7219
7220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7221
7222 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7223 if( status != PSA_SUCCESS )
7224 {
7225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7226 return;
7227 }
7228
7229 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7230 if( status != PSA_SUCCESS )
7231 {
7232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7233 return;
7234 }
7235 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7236#else
7237
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007238 mbedtls_sha256_init( &sha256 );
7239
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007241
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007242 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007243
7244 /*
7245 * TLSv1.2:
7246 * hash = PRF( master, finished_label,
7247 * Hash( handshake ) )[0.11]
7248 */
7249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#if !defined(MBEDTLS_SHA256_ALT)
7251 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007252 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007253#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007254
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007255 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007256 mbedtls_sha256_free( &sha256 );
7257#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007258
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007259 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007260 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007263
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007264 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007271static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007273{
7274 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007275 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007276 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007277#if defined(MBEDTLS_USE_PSA_CRYPTO)
7278 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007279 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007280 psa_status_t status;
7281#else
7282 mbedtls_sha512_context sha512;
7283#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007286 if( !session )
7287 session = ssl->session;
7288
Andrzej Kurekeb342242019-01-29 09:14:33 -05007289 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7290 ? "client finished"
7291 : "server finished";
7292
7293#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007294 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007295
7296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7297
Andrzej Kurek972fba52019-01-30 03:29:12 -05007298 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007299 if( status != PSA_SUCCESS )
7300 {
7301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7302 return;
7303 }
7304
Andrzej Kurek972fba52019-01-30 03:29:12 -05007305 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007306 if( status != PSA_SUCCESS )
7307 {
7308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7309 return;
7310 }
7311 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7312#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007313 mbedtls_sha512_init( &sha512 );
7314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007316
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007317 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007318
7319 /*
7320 * TLSv1.2:
7321 * hash = PRF( master, finished_label,
7322 * Hash( handshake ) )[0.11]
7323 */
7324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007326 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7327 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007328#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007329
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007330 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007331 mbedtls_sha512_free( &sha512 );
7332#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007333
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007334 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007335 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007338
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007339 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#endif /* MBEDTLS_SHA512_C */
7344#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007349
7350 /*
7351 * Free our handshake params
7352 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007353 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007355 ssl->handshake = NULL;
7356
7357 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007358 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007359 */
7360 if( ssl->transform )
7361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 mbedtls_ssl_transform_free( ssl->transform );
7363 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007364 }
7365 ssl->transform = ssl->transform_negotiate;
7366 ssl->transform_negotiate = NULL;
7367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007369}
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007372{
7373 int resume = ssl->handshake->resume;
7374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if defined(MBEDTLS_SSL_RENEGOTIATION)
7378 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007381 ssl->renego_records_seen = 0;
7382 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007383#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007384
7385 /*
7386 * Free the previous session and switch in the current one
7387 */
Paul Bakker0a597072012-09-25 21:55:46 +00007388 if( ssl->session )
7389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007391 /* RFC 7366 3.1: keep the EtM state */
7392 ssl->session_negotiate->encrypt_then_mac =
7393 ssl->session->encrypt_then_mac;
7394#endif
7395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 mbedtls_ssl_session_free( ssl->session );
7397 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007398 }
7399 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007400 ssl->session_negotiate = NULL;
7401
Paul Bakker0a597072012-09-25 21:55:46 +00007402 /*
7403 * Add cache entry
7404 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007405 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007406 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007407 resume == 0 )
7408 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007409 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007411 }
Paul Bakker0a597072012-09-25 21:55:46 +00007412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007415 ssl->handshake->flight != NULL )
7416 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007417 /* Cancel handshake timer */
7418 ssl_set_timer( ssl, 0 );
7419
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007420 /* Keep last flight around in case we need to resend it:
7421 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007423 }
7424 else
7425#endif
7426 ssl_handshake_wrapup_free_hs_transform( ssl );
7427
Paul Bakker48916f92012-09-16 19:57:18 +00007428 ssl->state++;
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007431}
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007435 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007438
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007439 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007440
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007441 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007443 /*
7444 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7445 * may define some other value. Currently (early 2016), no defined
7446 * ciphersuite does this (and this is unlikely to change as activity has
7447 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007452 ssl->verify_data_len = hash_len;
7453 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007454#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007455
Paul Bakker5121ce52009-01-03 21:22:43 +00007456 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7458 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
7460 /*
7461 * In case of session resuming, invert the client and server
7462 * ChangeCipherSpec messages order.
7463 */
Paul Bakker0a597072012-09-25 21:55:46 +00007464 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007467 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007471 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007474 }
7475 else
7476 ssl->state++;
7477
Paul Bakker48916f92012-09-16 19:57:18 +00007478 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007479 * Switch to our negotiated transform and session parameters for outbound
7480 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007486 {
7487 unsigned char i;
7488
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007489 /* Remember current epoch settings for resending */
7490 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007491 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007492
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007493 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007494 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007495
7496 /* Increment epoch */
7497 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007498 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007499 break;
7500
7501 /* The loop goes to its end iff the counter is wrapping */
7502 if( i == 0 )
7503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7505 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007506 }
7507 }
7508 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007510 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007511
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007512 ssl->transform_out = ssl->transform_negotiate;
7513 ssl->session_out = ssl->session_negotiate;
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7516 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7521 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007522 }
7523 }
7524#endif
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007527 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007529#endif
7530
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007531 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007532 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007534 return( ret );
7535 }
7536
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007537#if defined(MBEDTLS_SSL_PROTO_DTLS)
7538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7539 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7540 {
7541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7542 return( ret );
7543 }
7544#endif
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007547
7548 return( 0 );
7549}
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007552#define SSL_MAX_HASH_LEN 36
7553#else
7554#define SSL_MAX_HASH_LEN 12
7555#endif
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007558{
Paul Bakker23986e52011-04-24 08:57:21 +00007559 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007560 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007561 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007565 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007566
Hanno Becker327c93b2018-08-15 13:56:18 +01007567 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007570 return( ret );
7571 }
7572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7577 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007579 }
7580
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007581 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_SSL_PROTO_SSL3)
7583 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007584 hash_len = 36;
7585 else
7586#endif
7587 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7590 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7594 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007596 }
7597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007599 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007605 }
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007608 ssl->verify_data_len = hash_len;
7609 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007610#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007611
Paul Bakker0a597072012-09-25 21:55:46 +00007612 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007619 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007621#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007622 }
7623 else
7624 ssl->state++;
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007627 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007629#endif
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007632
7633 return( 0 );
7634}
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7641 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7642 mbedtls_md5_init( &handshake->fin_md5 );
7643 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007644 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7645 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007646#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7648#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007649#if defined(MBEDTLS_USE_PSA_CRYPTO)
7650 handshake->fin_sha256_psa = psa_hash_operation_init();
7651 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7652#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007654 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007655#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007656#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007658#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007659 handshake->fin_sha384_psa = psa_hash_operation_init();
7660 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007661#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007663 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007664#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007667
7668 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007669
7670#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7671 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7672 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7673#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_DHM_C)
7676 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_ECDH_C)
7679 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007680#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007681#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007682 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007683#if defined(MBEDTLS_SSL_CLI_C)
7684 handshake->ecjpake_cache = NULL;
7685 handshake->ecjpake_cache_len = 0;
7686#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007687#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007688
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007689#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007690 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007691#endif
7692
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007693#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7694 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7695#endif
Hanno Becker75173122019-02-06 16:18:31 +00007696
7697#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7698 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7699 mbedtls_pk_init( &handshake->peer_pubkey );
7700#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007701}
7702
Hanno Beckera18d1322018-01-03 14:27:32 +00007703void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7708 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007709
Hanno Beckerd56ed242018-01-03 15:32:51 +00007710#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 mbedtls_md_init( &transform->md_ctx_enc );
7712 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007713#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007714}
7715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007719}
7720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007722{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007723 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007724 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007726 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007728 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007729 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007730
7731 /*
7732 * Either the pointers are now NULL or cleared properly and can be freed.
7733 * Now allocate missing structures.
7734 */
7735 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007737 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007738 }
Paul Bakker48916f92012-09-16 19:57:18 +00007739
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007740 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007742 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007743 }
Paul Bakker48916f92012-09-16 19:57:18 +00007744
Paul Bakker82788fb2014-10-20 13:59:19 +02007745 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007747 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007748 }
Paul Bakker48916f92012-09-16 19:57:18 +00007749
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007750 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007751 if( ssl->handshake == NULL ||
7752 ssl->transform_negotiate == NULL ||
7753 ssl->session_negotiate == NULL )
7754 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 mbedtls_free( ssl->handshake );
7758 mbedtls_free( ssl->transform_negotiate );
7759 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007760
7761 ssl->handshake = NULL;
7762 ssl->transform_negotiate = NULL;
7763 ssl->session_negotiate = NULL;
7764
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007765 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007766 }
7767
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007768 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007770 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007771 ssl_handshake_params_init( ssl->handshake );
7772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7775 {
7776 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007777
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007778 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7779 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7780 else
7781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007782
7783 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007784 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007785#endif
7786
Paul Bakker48916f92012-09-16 19:57:18 +00007787 return( 0 );
7788}
7789
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007790#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007791/* Dummy cookie callbacks for defaults */
7792static int ssl_cookie_write_dummy( void *ctx,
7793 unsigned char **p, unsigned char *end,
7794 const unsigned char *cli_id, size_t cli_id_len )
7795{
7796 ((void) ctx);
7797 ((void) p);
7798 ((void) end);
7799 ((void) cli_id);
7800 ((void) cli_id_len);
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007803}
7804
7805static int ssl_cookie_check_dummy( void *ctx,
7806 const unsigned char *cookie, size_t cookie_len,
7807 const unsigned char *cli_id, size_t cli_id_len )
7808{
7809 ((void) ctx);
7810 ((void) cookie);
7811 ((void) cookie_len);
7812 ((void) cli_id);
7813 ((void) cli_id_len);
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007816}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007817#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007818
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007819/* Once ssl->out_hdr as the address of the beginning of the
7820 * next outgoing record is set, deduce the other pointers.
7821 *
7822 * Note: For TLS, we save the implicit record sequence number
7823 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7824 * and the caller has to make sure there's space for this.
7825 */
7826
7827static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7828 mbedtls_ssl_transform *transform )
7829{
7830#if defined(MBEDTLS_SSL_PROTO_DTLS)
7831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7832 {
7833 ssl->out_ctr = ssl->out_hdr + 3;
7834 ssl->out_len = ssl->out_hdr + 11;
7835 ssl->out_iv = ssl->out_hdr + 13;
7836 }
7837 else
7838#endif
7839 {
7840 ssl->out_ctr = ssl->out_hdr - 8;
7841 ssl->out_len = ssl->out_hdr + 3;
7842 ssl->out_iv = ssl->out_hdr + 5;
7843 }
7844
7845 /* Adjust out_msg to make space for explicit IV, if used. */
7846 if( transform != NULL &&
7847 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7848 {
7849 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7850 }
7851 else
7852 ssl->out_msg = ssl->out_iv;
7853}
7854
7855/* Once ssl->in_hdr as the address of the beginning of the
7856 * next incoming record is set, deduce the other pointers.
7857 *
7858 * Note: For TLS, we save the implicit record sequence number
7859 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7860 * and the caller has to make sure there's space for this.
7861 */
7862
7863static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7864 mbedtls_ssl_transform *transform )
7865{
7866#if defined(MBEDTLS_SSL_PROTO_DTLS)
7867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7868 {
7869 ssl->in_ctr = ssl->in_hdr + 3;
7870 ssl->in_len = ssl->in_hdr + 11;
7871 ssl->in_iv = ssl->in_hdr + 13;
7872 }
7873 else
7874#endif
7875 {
7876 ssl->in_ctr = ssl->in_hdr - 8;
7877 ssl->in_len = ssl->in_hdr + 3;
7878 ssl->in_iv = ssl->in_hdr + 5;
7879 }
7880
7881 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7882 if( transform != NULL &&
7883 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7884 {
7885 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7886 }
7887 else
7888 ssl->in_msg = ssl->in_iv;
7889}
7890
Paul Bakker5121ce52009-01-03 21:22:43 +00007891/*
7892 * Initialize an SSL context
7893 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007894void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7895{
7896 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7897}
7898
7899/*
7900 * Setup an SSL context
7901 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007902
7903static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7904{
7905 /* Set the incoming and outgoing record pointers. */
7906#if defined(MBEDTLS_SSL_PROTO_DTLS)
7907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7908 {
7909 ssl->out_hdr = ssl->out_buf;
7910 ssl->in_hdr = ssl->in_buf;
7911 }
7912 else
7913#endif /* MBEDTLS_SSL_PROTO_DTLS */
7914 {
7915 ssl->out_hdr = ssl->out_buf + 8;
7916 ssl->in_hdr = ssl->in_buf + 8;
7917 }
7918
7919 /* Derive other internal pointers. */
7920 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7921 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7922}
7923
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007924int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007925 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007926{
Paul Bakker48916f92012-09-16 19:57:18 +00007927 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007928
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007929 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007930
7931 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007932 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007933 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007934
7935 /* Set to NULL in case of an error condition */
7936 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007937
Angus Grattond8213d02016-05-25 20:56:48 +10007938 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7939 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007940 {
Angus Grattond8213d02016-05-25 20:56:48 +10007941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007942 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007943 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007944 }
7945
7946 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7947 if( ssl->out_buf == NULL )
7948 {
7949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007950 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007951 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007952 }
7953
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007954 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007955
Paul Bakker48916f92012-09-16 19:57:18 +00007956 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007957 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007958
7959 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007960
7961error:
7962 mbedtls_free( ssl->in_buf );
7963 mbedtls_free( ssl->out_buf );
7964
7965 ssl->conf = NULL;
7966
7967 ssl->in_buf = NULL;
7968 ssl->out_buf = NULL;
7969
7970 ssl->in_hdr = NULL;
7971 ssl->in_ctr = NULL;
7972 ssl->in_len = NULL;
7973 ssl->in_iv = NULL;
7974 ssl->in_msg = NULL;
7975
7976 ssl->out_hdr = NULL;
7977 ssl->out_ctr = NULL;
7978 ssl->out_len = NULL;
7979 ssl->out_iv = NULL;
7980 ssl->out_msg = NULL;
7981
k-stachowiak9f7798e2018-07-31 16:52:32 +02007982 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007983}
7984
7985/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007986 * Reset an initialized and used SSL context for re-use while retaining
7987 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007988 *
7989 * If partial is non-zero, keep data in the input buffer and client ID.
7990 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007991 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007992static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007993{
Paul Bakker48916f92012-09-16 19:57:18 +00007994 int ret;
7995
Hanno Becker7e772132018-08-10 12:38:21 +01007996#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7997 !defined(MBEDTLS_SSL_SRV_C)
7998 ((void) partial);
7999#endif
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008002
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008003 /* Cancel any possibly running timer */
8004 ssl_set_timer( ssl, 0 );
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006#if defined(MBEDTLS_SSL_RENEGOTIATION)
8007 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008008 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008009
8010 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8012 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008015
Paul Bakker7eb013f2011-10-06 12:37:39 +00008016 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008017 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008018
8019 ssl->in_msgtype = 0;
8020 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008022 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008023 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008026 ssl_dtls_replay_reset( ssl );
8027#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008028
8029 ssl->in_hslen = 0;
8030 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008031
8032 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008033
8034 ssl->out_msgtype = 0;
8035 ssl->out_msglen = 0;
8036 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8038 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008039 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008040#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008041
Hanno Becker19859472018-08-06 09:40:20 +01008042 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8043
Paul Bakker48916f92012-09-16 19:57:18 +00008044 ssl->transform_in = NULL;
8045 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008046
Hanno Becker78640902018-08-13 16:35:15 +01008047 ssl->session_in = NULL;
8048 ssl->session_out = NULL;
8049
Angus Grattond8213d02016-05-25 20:56:48 +10008050 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008051
8052#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008053 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008054#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8055 {
8056 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008057 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008058 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8061 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8064 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8067 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008068 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008069 }
8070#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008071
Paul Bakker48916f92012-09-16 19:57:18 +00008072 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 mbedtls_ssl_transform_free( ssl->transform );
8075 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008076 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008077 }
Paul Bakker48916f92012-09-16 19:57:18 +00008078
Paul Bakkerc0463502013-02-14 11:19:38 +01008079 if( ssl->session )
8080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 mbedtls_ssl_session_free( ssl->session );
8082 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008083 ssl->session = NULL;
8084 }
8085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008087 ssl->alpn_chosen = NULL;
8088#endif
8089
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008090#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008091#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008092 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008093#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008094 {
8095 mbedtls_free( ssl->cli_id );
8096 ssl->cli_id = NULL;
8097 ssl->cli_id_len = 0;
8098 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008099#endif
8100
Paul Bakker48916f92012-09-16 19:57:18 +00008101 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8102 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008103
8104 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008105}
8106
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008107/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008108 * Reset an initialized and used SSL context for re-use while retaining
8109 * all application-set variables, function pointers and data.
8110 */
8111int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8112{
8113 return( ssl_session_reset_int( ssl, 0 ) );
8114}
8115
8116/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008117 * SSL set accessors
8118 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008119void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008121 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008122}
8123
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008124void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008125{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008126 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008127}
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008130void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008132 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008133}
8134#endif
8135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008137void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008138{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008139 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008140}
8141#endif
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008144
Hanno Becker1841b0a2018-08-24 11:13:57 +01008145void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8146 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008147{
8148 ssl->disable_datagram_packing = !allow_packing;
8149}
8150
8151void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8152 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008153{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008154 conf->hs_timeout_min = min;
8155 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008156}
8157#endif
8158
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008159void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008160{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008161 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008162}
8163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008165void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008166 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008167 void *p_vrfy )
8168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008169 conf->f_vrfy = f_vrfy;
8170 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008173
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008174void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008175 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008176 void *p_rng )
8177{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008178 conf->f_rng = f_rng;
8179 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008180}
8181
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008182void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008183 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008184 void *p_dbg )
8185{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008186 conf->f_dbg = f_dbg;
8187 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008188}
8189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008191 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008192 mbedtls_ssl_send_t *f_send,
8193 mbedtls_ssl_recv_t *f_recv,
8194 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008195{
8196 ssl->p_bio = p_bio;
8197 ssl->f_send = f_send;
8198 ssl->f_recv = f_recv;
8199 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008200}
8201
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008202#if defined(MBEDTLS_SSL_PROTO_DTLS)
8203void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8204{
8205 ssl->mtu = mtu;
8206}
8207#endif
8208
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008209void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008210{
8211 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008212}
8213
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008214void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8215 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008216 mbedtls_ssl_set_timer_t *f_set_timer,
8217 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008218{
8219 ssl->p_timer = p_timer;
8220 ssl->f_set_timer = f_set_timer;
8221 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008222
8223 /* Make sure we start with no timer running */
8224 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008225}
8226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008228void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008229 void *p_cache,
8230 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8231 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008232{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008233 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008234 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008235 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239#if defined(MBEDTLS_SSL_CLI_C)
8240int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008241{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008242 int ret;
8243
8244 if( ssl == NULL ||
8245 session == NULL ||
8246 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008247 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008250 }
8251
Hanno Becker52055ae2019-02-06 14:30:46 +00008252 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8253 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008254 return( ret );
8255
Paul Bakker0a597072012-09-25 21:55:46 +00008256 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008257
8258 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008259}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008261
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008262void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008263 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008265 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8266 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8267 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8268 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008269}
8270
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008271void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008272 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008273 int major, int minor )
8274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008276 return;
8277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008279 return;
8280
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008281 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008282}
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008285void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008286 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008287{
8288 conf->cert_profile = profile;
8289}
8290
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008291/* Append a new keycert entry to a (possibly empty) list */
8292static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8293 mbedtls_x509_crt *cert,
8294 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008295{
niisato8ee24222018-06-25 19:05:48 +09008296 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008297
niisato8ee24222018-06-25 19:05:48 +09008298 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8299 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008300 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008301
niisato8ee24222018-06-25 19:05:48 +09008302 new_cert->cert = cert;
8303 new_cert->key = key;
8304 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008305
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008306 /* Update head is the list was null, else add to the end */
8307 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008308 {
niisato8ee24222018-06-25 19:05:48 +09008309 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008310 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008311 else
8312 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008313 mbedtls_ssl_key_cert *cur = *head;
8314 while( cur->next != NULL )
8315 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008316 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008317 }
8318
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008319 return( 0 );
8320}
8321
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008322int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008323 mbedtls_x509_crt *own_cert,
8324 mbedtls_pk_context *pk_key )
8325{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008326 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008327}
8328
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008329void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008330 mbedtls_x509_crt *ca_chain,
8331 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008332{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008333 conf->ca_chain = ca_chain;
8334 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008335
8336#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8337 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8338 * cannot be used together. */
8339 conf->f_ca_cb = NULL;
8340 conf->p_ca_cb = NULL;
8341#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008342}
Hanno Becker5adaad92019-03-27 16:54:37 +00008343
8344#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8345void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008346 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008347 void *p_ca_cb )
8348{
8349 conf->f_ca_cb = f_ca_cb;
8350 conf->p_ca_cb = p_ca_cb;
8351
8352 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8353 * cannot be used together. */
8354 conf->ca_chain = NULL;
8355 conf->ca_crl = NULL;
8356}
8357#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008359
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008360#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8361int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8362 mbedtls_x509_crt *own_cert,
8363 mbedtls_pk_context *pk_key )
8364{
8365 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8366 own_cert, pk_key ) );
8367}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008368
8369void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8370 mbedtls_x509_crt *ca_chain,
8371 mbedtls_x509_crl *ca_crl )
8372{
8373 ssl->handshake->sni_ca_chain = ca_chain;
8374 ssl->handshake->sni_ca_crl = ca_crl;
8375}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008376
8377void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8378 int authmode )
8379{
8380 ssl->handshake->sni_authmode = authmode;
8381}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008382#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8383
Hanno Becker8927c832019-04-03 12:52:50 +01008384#if defined(MBEDTLS_X509_CRT_PARSE_C)
8385void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8386 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8387 void *p_vrfy )
8388{
8389 ssl->f_vrfy = f_vrfy;
8390 ssl->p_vrfy = p_vrfy;
8391}
8392#endif
8393
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008394#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008395/*
8396 * Set EC J-PAKE password for current handshake
8397 */
8398int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8399 const unsigned char *pw,
8400 size_t pw_len )
8401{
8402 mbedtls_ecjpake_role role;
8403
Janos Follath8eb64132016-06-03 15:40:57 +01008404 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8406
8407 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8408 role = MBEDTLS_ECJPAKE_SERVER;
8409 else
8410 role = MBEDTLS_ECJPAKE_CLIENT;
8411
8412 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8413 role,
8414 MBEDTLS_MD_SHA256,
8415 MBEDTLS_ECP_DP_SECP256R1,
8416 pw, pw_len ) );
8417}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008418#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008421
8422static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8423{
8424 /* Remove reference to existing PSK, if any. */
8425#if defined(MBEDTLS_USE_PSA_CRYPTO)
8426 if( conf->psk_opaque != 0 )
8427 {
8428 /* The maintenance of the PSK key slot is the
8429 * user's responsibility. */
8430 conf->psk_opaque = 0;
8431 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008432 /* This and the following branch should never
8433 * be taken simultaenously as we maintain the
8434 * invariant that raw and opaque PSKs are never
8435 * configured simultaneously. As a safeguard,
8436 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008437#endif /* MBEDTLS_USE_PSA_CRYPTO */
8438 if( conf->psk != NULL )
8439 {
8440 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8441
8442 mbedtls_free( conf->psk );
8443 conf->psk = NULL;
8444 conf->psk_len = 0;
8445 }
8446
8447 /* Remove reference to PSK identity, if any. */
8448 if( conf->psk_identity != NULL )
8449 {
8450 mbedtls_free( conf->psk_identity );
8451 conf->psk_identity = NULL;
8452 conf->psk_identity_len = 0;
8453 }
8454}
8455
Hanno Becker7390c712018-11-15 13:33:04 +00008456/* This function assumes that PSK identity in the SSL config is unset.
8457 * It checks that the provided identity is well-formed and attempts
8458 * to make a copy of it in the SSL config.
8459 * On failure, the PSK identity in the config remains unset. */
8460static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8461 unsigned char const *psk_identity,
8462 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008463{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008464 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008465 if( psk_identity == NULL ||
8466 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008467 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008468 {
8469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8470 }
8471
Hanno Becker7390c712018-11-15 13:33:04 +00008472 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8473 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008474 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008475
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008476 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008477 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008478
8479 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008480}
8481
Hanno Becker7390c712018-11-15 13:33:04 +00008482int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8483 const unsigned char *psk, size_t psk_len,
8484 const unsigned char *psk_identity, size_t psk_identity_len )
8485{
8486 int ret;
8487 /* Remove opaque/raw PSK + PSK Identity */
8488 ssl_conf_remove_psk( conf );
8489
8490 /* Check and set raw PSK */
8491 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8493 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8494 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8495 conf->psk_len = psk_len;
8496 memcpy( conf->psk, psk, conf->psk_len );
8497
8498 /* Check and set PSK Identity */
8499 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8500 if( ret != 0 )
8501 ssl_conf_remove_psk( conf );
8502
8503 return( ret );
8504}
8505
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008506static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8507{
8508#if defined(MBEDTLS_USE_PSA_CRYPTO)
8509 if( ssl->handshake->psk_opaque != 0 )
8510 {
8511 ssl->handshake->psk_opaque = 0;
8512 }
8513 else
8514#endif /* MBEDTLS_USE_PSA_CRYPTO */
8515 if( ssl->handshake->psk != NULL )
8516 {
8517 mbedtls_platform_zeroize( ssl->handshake->psk,
8518 ssl->handshake->psk_len );
8519 mbedtls_free( ssl->handshake->psk );
8520 ssl->handshake->psk_len = 0;
8521 }
8522}
8523
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008524int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8525 const unsigned char *psk, size_t psk_len )
8526{
8527 if( psk == NULL || ssl->handshake == NULL )
8528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8529
8530 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8532
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008533 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008534
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008535 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008536 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008537
8538 ssl->handshake->psk_len = psk_len;
8539 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8540
8541 return( 0 );
8542}
8543
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008544#if defined(MBEDTLS_USE_PSA_CRYPTO)
8545int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008546 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008547 const unsigned char *psk_identity,
8548 size_t psk_identity_len )
8549{
Hanno Becker7390c712018-11-15 13:33:04 +00008550 int ret;
8551 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008552 ssl_conf_remove_psk( conf );
8553
Hanno Becker7390c712018-11-15 13:33:04 +00008554 /* Check and set opaque PSK */
8555 if( psk_slot == 0 )
8556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008557 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008558
8559 /* Check and set PSK Identity */
8560 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8561 psk_identity_len );
8562 if( ret != 0 )
8563 ssl_conf_remove_psk( conf );
8564
8565 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008566}
8567
8568int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008569 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008570{
8571 if( psk_slot == 0 || ssl->handshake == NULL )
8572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8573
8574 ssl_remove_psk( ssl );
8575 ssl->handshake->psk_opaque = psk_slot;
8576 return( 0 );
8577}
8578#endif /* MBEDTLS_USE_PSA_CRYPTO */
8579
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008580void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008582 size_t),
8583 void *p_psk )
8584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008585 conf->f_psk = f_psk;
8586 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008589
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008590#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008591
8592#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008593int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008594{
8595 int ret;
8596
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008597 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8598 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8599 {
8600 mbedtls_mpi_free( &conf->dhm_P );
8601 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008602 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008603 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008604
8605 return( 0 );
8606}
Hanno Becker470a8c42017-10-04 15:28:46 +01008607#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008608
Hanno Beckera90658f2017-10-04 15:29:08 +01008609int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8610 const unsigned char *dhm_P, size_t P_len,
8611 const unsigned char *dhm_G, size_t G_len )
8612{
8613 int ret;
8614
8615 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8616 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8617 {
8618 mbedtls_mpi_free( &conf->dhm_P );
8619 mbedtls_mpi_free( &conf->dhm_G );
8620 return( ret );
8621 }
8622
8623 return( 0 );
8624}
Paul Bakker5121ce52009-01-03 21:22:43 +00008625
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008626int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008627{
8628 int ret;
8629
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008630 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8631 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8632 {
8633 mbedtls_mpi_free( &conf->dhm_P );
8634 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008635 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008636 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008637
8638 return( 0 );
8639}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008640#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008641
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008642#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8643/*
8644 * Set the minimum length for Diffie-Hellman parameters
8645 */
8646void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8647 unsigned int bitlen )
8648{
8649 conf->dhm_min_bitlen = bitlen;
8650}
8651#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8652
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008653#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008654/*
8655 * Set allowed/preferred hashes for handshake signatures
8656 */
8657void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8658 const int *hashes )
8659{
8660 conf->sig_hashes = hashes;
8661}
Hanno Becker947194e2017-04-07 13:25:49 +01008662#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008663
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008664#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008665/*
8666 * Set the allowed elliptic curves
8667 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008669 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008671 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008672}
Hanno Becker947194e2017-04-07 13:25:49 +01008673#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008674
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008675#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008677{
Hanno Becker947194e2017-04-07 13:25:49 +01008678 /* Initialize to suppress unnecessary compiler warning */
8679 size_t hostname_len = 0;
8680
8681 /* Check if new hostname is valid before
8682 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008683 if( hostname != NULL )
8684 {
8685 hostname_len = strlen( hostname );
8686
8687 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8689 }
8690
8691 /* Now it's clear that we will overwrite the old hostname,
8692 * so we can free it safely */
8693
8694 if( ssl->hostname != NULL )
8695 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008696 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008697 mbedtls_free( ssl->hostname );
8698 }
8699
8700 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008701
Paul Bakker5121ce52009-01-03 21:22:43 +00008702 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008703 {
8704 ssl->hostname = NULL;
8705 }
8706 else
8707 {
8708 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008709 if( ssl->hostname == NULL )
8710 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008711
Hanno Becker947194e2017-04-07 13:25:49 +01008712 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008713
Hanno Becker947194e2017-04-07 13:25:49 +01008714 ssl->hostname[hostname_len] = '\0';
8715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008716
8717 return( 0 );
8718}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008719#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008720
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008721#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008722void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008724 const unsigned char *, size_t),
8725 void *p_sni )
8726{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008727 conf->f_sni = f_sni;
8728 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008733int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008734{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008735 size_t cur_len, tot_len;
8736 const char **p;
8737
8738 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008739 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8740 * MUST NOT be truncated."
8741 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008742 */
8743 tot_len = 0;
8744 for( p = protos; *p != NULL; p++ )
8745 {
8746 cur_len = strlen( *p );
8747 tot_len += cur_len;
8748
8749 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008751 }
8752
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008753 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008754
8755 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008756}
8757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008759{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008760 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008763
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008764void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008765{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008766 conf->max_major_ver = major;
8767 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008768}
8769
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008770void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008771{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008772 conf->min_major_ver = major;
8773 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008774}
8775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008777void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008778{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008779 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008780}
8781#endif
8782
Janos Follath088ce432017-04-10 12:42:31 +01008783#if defined(MBEDTLS_SSL_SRV_C)
8784void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8785 char cert_req_ca_list )
8786{
8787 conf->cert_req_ca_list = cert_req_ca_list;
8788}
8789#endif
8790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008792void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008793{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008794 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008795}
8796#endif
8797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008799void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008800{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008801 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008802}
8803#endif
8804
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008805#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008806void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008807{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008808 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008809}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008810#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008813int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008816 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008819 }
8820
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008821 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008822
8823 return( 0 );
8824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008828void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008829{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008830 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008835void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008836{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008837 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008838}
8839#endif
8840
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008841void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008842{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008843 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008844}
8845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008847void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008849 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008850}
8851
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008852void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008854 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008855}
8856
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008857void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008858 const unsigned char period[8] )
8859{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008860 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008861}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008865#if defined(MBEDTLS_SSL_CLI_C)
8866void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008867{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008868 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008869}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008870#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008871
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008872#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008873void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8874 mbedtls_ssl_ticket_write_t *f_ticket_write,
8875 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8876 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008877{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008878 conf->f_ticket_write = f_ticket_write;
8879 conf->f_ticket_parse = f_ticket_parse;
8880 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008881}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008884
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008885#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8886void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8887 mbedtls_ssl_export_keys_t *f_export_keys,
8888 void *p_export_keys )
8889{
8890 conf->f_export_keys = f_export_keys;
8891 conf->p_export_keys = p_export_keys;
8892}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008893
8894void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8895 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8896 void *p_export_keys )
8897{
8898 conf->f_export_keys_ext = f_export_keys_ext;
8899 conf->p_export_keys = p_export_keys;
8900}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008901#endif
8902
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008903#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008904void mbedtls_ssl_conf_async_private_cb(
8905 mbedtls_ssl_config *conf,
8906 mbedtls_ssl_async_sign_t *f_async_sign,
8907 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8908 mbedtls_ssl_async_resume_t *f_async_resume,
8909 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008910 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008911{
8912 conf->f_async_sign_start = f_async_sign;
8913 conf->f_async_decrypt_start = f_async_decrypt;
8914 conf->f_async_resume = f_async_resume;
8915 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008916 conf->p_async_config_data = async_config_data;
8917}
8918
Gilles Peskine8f97af72018-04-26 11:46:10 +02008919void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8920{
8921 return( conf->p_async_config_data );
8922}
8923
Gilles Peskine1febfef2018-04-30 11:54:39 +02008924void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008925{
8926 if( ssl->handshake == NULL )
8927 return( NULL );
8928 else
8929 return( ssl->handshake->user_async_ctx );
8930}
8931
Gilles Peskine1febfef2018-04-30 11:54:39 +02008932void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008933 void *ctx )
8934{
8935 if( ssl->handshake != NULL )
8936 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008937}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008938#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008939
Paul Bakker5121ce52009-01-03 21:22:43 +00008940/*
8941 * SSL get accessors
8942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008944{
8945 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8946}
8947
Hanno Becker8b170a02017-10-10 11:51:19 +01008948int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8949{
8950 /*
8951 * Case A: We're currently holding back
8952 * a message for further processing.
8953 */
8954
8955 if( ssl->keep_current_message == 1 )
8956 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008958 return( 1 );
8959 }
8960
8961 /*
8962 * Case B: Further records are pending in the current datagram.
8963 */
8964
8965#if defined(MBEDTLS_SSL_PROTO_DTLS)
8966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8967 ssl->in_left > ssl->next_record_offset )
8968 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008970 return( 1 );
8971 }
8972#endif /* MBEDTLS_SSL_PROTO_DTLS */
8973
8974 /*
8975 * Case C: A handshake message is being processed.
8976 */
8977
Hanno Becker8b170a02017-10-10 11:51:19 +01008978 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8979 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008981 return( 1 );
8982 }
8983
8984 /*
8985 * Case D: An application data message is being processed
8986 */
8987 if( ssl->in_offt != NULL )
8988 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008990 return( 1 );
8991 }
8992
8993 /*
8994 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008995 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008996 * we implement support for multiple alerts in single records.
8997 */
8998
8999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9000 return( 0 );
9001}
9002
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009003uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009004{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009005 if( ssl->session != NULL )
9006 return( ssl->session->verify_result );
9007
9008 if( ssl->session_negotiate != NULL )
9009 return( ssl->session_negotiate->verify_result );
9010
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009011 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009012}
9013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009015{
Paul Bakker926c8e42013-03-06 10:23:34 +01009016 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009017 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009020}
9021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009026 {
9027 switch( ssl->minor_ver )
9028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009030 return( "DTLSv1.0" );
9031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009033 return( "DTLSv1.2" );
9034
9035 default:
9036 return( "unknown (DTLS)" );
9037 }
9038 }
9039#endif
9040
Paul Bakker43ca69c2011-01-15 17:35:19 +00009041 switch( ssl->minor_ver )
9042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009044 return( "SSLv3.0" );
9045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009047 return( "TLSv1.0" );
9048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009050 return( "TLSv1.1" );
9051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009053 return( "TLSv1.2" );
9054
Paul Bakker43ca69c2011-01-15 17:35:19 +00009055 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009056 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009057 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009058}
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009061{
Hanno Becker3136ede2018-08-17 15:28:19 +01009062 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009064 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009065
Hanno Becker78640902018-08-13 16:35:15 +01009066 if( transform == NULL )
9067 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069#if defined(MBEDTLS_ZLIB_SUPPORT)
9070 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9071 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009072#endif
9073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 case MBEDTLS_MODE_GCM:
9077 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009078 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009080 transform_expansion = transform->minlen;
9081 break;
9082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009084
9085 block_size = mbedtls_cipher_get_block_size(
9086 &transform->cipher_ctx_enc );
9087
Hanno Becker3136ede2018-08-17 15:28:19 +01009088 /* Expansion due to the addition of the MAC. */
9089 transform_expansion += transform->maclen;
9090
9091 /* Expansion due to the addition of CBC padding;
9092 * Theoretically up to 256 bytes, but we never use
9093 * more than the block size of the underlying cipher. */
9094 transform_expansion += block_size;
9095
9096 /* For TLS 1.1 or higher, an explicit IV is added
9097 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009098#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9099 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009100 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009101#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009102
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009103 break;
9104
9105 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009108 }
9109
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009110 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009111}
9112
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009113#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9114size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9115{
9116 size_t max_len;
9117
9118 /*
9119 * Assume mfl_code is correct since it was checked when set
9120 */
Angus Grattond8213d02016-05-25 20:56:48 +10009121 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009122
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009123 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009124 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009125 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009126 {
Angus Grattond8213d02016-05-25 20:56:48 +10009127 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009128 }
9129
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009130 /* During a handshake, use the value being negotiated */
9131 if( ssl->session_negotiate != NULL &&
9132 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9133 {
9134 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9135 }
9136
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009137 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009138}
9139#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9140
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009141#if defined(MBEDTLS_SSL_PROTO_DTLS)
9142static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9143{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009144 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9145 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9146 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9147 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9148 return ( 0 );
9149
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009150 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9151 return( ssl->mtu );
9152
9153 if( ssl->mtu == 0 )
9154 return( ssl->handshake->mtu );
9155
9156 return( ssl->mtu < ssl->handshake->mtu ?
9157 ssl->mtu : ssl->handshake->mtu );
9158}
9159#endif /* MBEDTLS_SSL_PROTO_DTLS */
9160
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009161int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9162{
9163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9164
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009165#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9166 !defined(MBEDTLS_SSL_PROTO_DTLS)
9167 (void) ssl;
9168#endif
9169
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009170#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9171 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9172
9173 if( max_len > mfl )
9174 max_len = mfl;
9175#endif
9176
9177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009178 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009179 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009180 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009181 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9182 const size_t overhead = (size_t) ret;
9183
9184 if( ret < 0 )
9185 return( ret );
9186
9187 if( mtu <= overhead )
9188 {
9189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9190 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9191 }
9192
9193 if( max_len > mtu - overhead )
9194 max_len = mtu - overhead;
9195 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009196#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009197
Hanno Becker0defedb2018-08-10 12:35:02 +01009198#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9199 !defined(MBEDTLS_SSL_PROTO_DTLS)
9200 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009201#endif
9202
9203 return( (int) max_len );
9204}
9205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206#if defined(MBEDTLS_X509_CRT_PARSE_C)
9207const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009208{
9209 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009210 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009211
Hanno Beckere6824572019-02-07 13:18:46 +00009212#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009213 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009214#else
9215 return( NULL );
9216#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009221int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9222 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009223{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009224 if( ssl == NULL ||
9225 dst == NULL ||
9226 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009227 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009230 }
9231
Hanno Becker52055ae2019-02-06 14:30:46 +00009232 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009233}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009235
Paul Bakker5121ce52009-01-03 21:22:43 +00009236/*
Paul Bakker1961b702013-01-25 14:49:24 +01009237 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009242
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009243 if( ssl == NULL || ssl->conf == NULL )
9244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009247 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009251 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009253#endif
9254
Paul Bakker1961b702013-01-25 14:49:24 +01009255 return( ret );
9256}
9257
9258/*
9259 * Perform the SSL handshake
9260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009262{
9263 int ret = 0;
9264
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009265 if( ssl == NULL || ssl->conf == NULL )
9266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009273
9274 if( ret != 0 )
9275 break;
9276 }
9277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009279
9280 return( ret );
9281}
9282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#if defined(MBEDTLS_SSL_RENEGOTIATION)
9284#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009285/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009286 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009289{
9290 int ret;
9291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009293
9294 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9296 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009297
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009298 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009299 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009301 return( ret );
9302 }
9303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009305
9306 return( 0 );
9307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009309
9310/*
9311 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 * - any side: calling mbedtls_ssl_renegotiate(),
9313 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9314 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009315 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009316 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009320{
9321 int ret;
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009324
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009325 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9326 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009327
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009328 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9329 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009333 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009335 ssl->handshake->out_msg_seq = 1;
9336 else
9337 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009338 }
9339#endif
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9342 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009347 return( ret );
9348 }
9349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009351
9352 return( 0 );
9353}
9354
9355/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009356 * Renegotiate current connection on client,
9357 * or request renegotiation on server
9358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009362
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009363 if( ssl == NULL || ssl->conf == NULL )
9364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009367 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009374
9375 /* Did we already try/start sending HelloRequest? */
9376 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009378
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009379 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009380 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009384 /*
9385 * On client, either start the renegotiation process or,
9386 * if already in progress, continue the handshake
9387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009392
9393 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009396 return( ret );
9397 }
9398 }
9399 else
9400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009404 return( ret );
9405 }
9406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009408
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009409 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009410}
9411
9412/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009413 * Check record counters and renegotiate if they're above the limit.
9414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009416{
Andres AG2196c7f2016-12-15 17:01:16 +00009417 size_t ep_len = ssl_ep_len( ssl );
9418 int in_ctr_cmp;
9419 int out_ctr_cmp;
9420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9422 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009423 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009424 {
9425 return( 0 );
9426 }
9427
Andres AG2196c7f2016-12-15 17:01:16 +00009428 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9429 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009430 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009431 ssl->conf->renego_period + ep_len, 8 - ep_len );
9432
9433 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009434 {
9435 return( 0 );
9436 }
9437
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009442
9443/*
9444 * Receive application data decrypted from the SSL layer
9445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009447{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009448 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009449 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009450
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009451 if( ssl == NULL || ssl->conf == NULL )
9452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009457 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009460 return( ret );
9461
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009462 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009464 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009465 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009466 return( ret );
9467 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009468 }
9469#endif
9470
Hanno Becker4a810fb2017-05-24 16:27:30 +01009471 /*
9472 * Check if renegotiation is necessary and/or handshake is
9473 * in process. If yes, perform/continue, and fall through
9474 * if an unexpected packet is received while the client
9475 * is waiting for the ServerHello.
9476 *
9477 * (There is no equivalent to the last condition on
9478 * the server-side as it is not treated as within
9479 * a handshake while waiting for the ClientHello
9480 * after a renegotiation request.)
9481 */
9482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009484 ret = ssl_check_ctr_renegotiate( ssl );
9485 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9486 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009489 return( ret );
9490 }
9491#endif
9492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009496 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9497 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009500 return( ret );
9501 }
9502 }
9503
Hanno Beckere41158b2017-10-23 13:30:32 +01009504 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009505 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009506 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009507 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009508 if( ssl->f_get_timer != NULL &&
9509 ssl->f_get_timer( ssl->p_timer ) == -1 )
9510 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009511 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009512 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009513
Hanno Becker327c93b2018-08-15 13:56:18 +01009514 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009515 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009516 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9517 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009518
Hanno Becker4a810fb2017-05-24 16:27:30 +01009519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9520 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009521 }
9522
9523 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009525 {
9526 /*
9527 * OpenSSL sends empty messages to randomize the IV
9528 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009529 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009532 return( 0 );
9533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009535 return( ret );
9536 }
9537 }
9538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009542
Hanno Becker4a810fb2017-05-24 16:27:30 +01009543 /*
9544 * - For client-side, expect SERVER_HELLO_REQUEST.
9545 * - For server-side, expect CLIENT_HELLO.
9546 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9547 */
9548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009552 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009555
9556 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009558 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009559 {
9560 continue;
9561 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009564 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009565#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009566
Hanno Becker4a810fb2017-05-24 16:27:30 +01009567#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009568 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009572
9573 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009576 {
9577 continue;
9578 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009581 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009582#endif /* MBEDTLS_SSL_SRV_C */
9583
Hanno Becker21df7f92017-10-17 11:03:26 +01009584#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009585 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009586 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9587 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9588 ssl->conf->allow_legacy_renegotiation ==
9589 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9590 {
9591 /*
9592 * Accept renegotiation request
9593 */
Paul Bakker48916f92012-09-16 19:57:18 +00009594
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009595 /* DTLS clients need to know renego is server-initiated */
9596#if defined(MBEDTLS_SSL_PROTO_DTLS)
9597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9598 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9599 {
9600 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9601 }
9602#endif
9603 ret = ssl_start_renegotiation( ssl );
9604 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9605 ret != 0 )
9606 {
9607 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9608 return( ret );
9609 }
9610 }
9611 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009612#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009613 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009614 /*
9615 * Refuse renegotiation
9616 */
9617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620#if defined(MBEDTLS_SSL_PROTO_SSL3)
9621 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009622 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009623 /* SSLv3 does not have a "no_renegotiation" warning, so
9624 we send a fatal alert and abort the connection. */
9625 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9626 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9627 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009628 }
9629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9631#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9632 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9633 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9636 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9637 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009638 {
9639 return( ret );
9640 }
Paul Bakker48916f92012-09-16 19:57:18 +00009641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9644 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009648 }
Paul Bakker48916f92012-09-16 19:57:18 +00009649 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009650
Hanno Becker90333da2017-10-10 11:27:13 +01009651 /* At this point, we don't know whether the renegotiation has been
9652 * completed or not. The cases to consider are the following:
9653 * 1) The renegotiation is complete. In this case, no new record
9654 * has been read yet.
9655 * 2) The renegotiation is incomplete because the client received
9656 * an application data record while awaiting the ServerHello.
9657 * 3) The renegotiation is incomplete because the client received
9658 * a non-handshake, non-application data message while awaiting
9659 * the ServerHello.
9660 * In each of these case, looping will be the proper action:
9661 * - For 1), the next iteration will read a new record and check
9662 * if it's application data.
9663 * - For 2), the loop condition isn't satisfied as application data
9664 * is present, hence continue is the same as break
9665 * - For 3), the loop condition is satisfied and read_record
9666 * will re-deliver the message that was held back by the client
9667 * when expecting the ServerHello.
9668 */
9669 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009670 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009671#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009673 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009674 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009675 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009676 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009679 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009681 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009682 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9687 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009690 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009691 }
9692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9696 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009697 }
9698
9699 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009700
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009701 /* We're going to return something now, cancel timer,
9702 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009704 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009705
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009707 /* If we requested renego but received AppData, resend HelloRequest.
9708 * Do it now, after setting in_offt, to avoid taking this branch
9709 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009711 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009713 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009714 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009717 return( ret );
9718 }
9719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009721#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009722 }
9723
9724 n = ( len < ssl->in_msglen )
9725 ? len : ssl->in_msglen;
9726
9727 memcpy( buf, ssl->in_offt, n );
9728 ssl->in_msglen -= n;
9729
9730 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009731 {
9732 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009733 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009734 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009736 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009737 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009738 /* more data available */
9739 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009743
Paul Bakker23986e52011-04-24 08:57:21 +00009744 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009745}
9746
9747/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009748 * Send application data to be encrypted by the SSL layer, taking care of max
9749 * fragment length and buffer size.
9750 *
9751 * According to RFC 5246 Section 6.2.1:
9752 *
9753 * Zero-length fragments of Application data MAY be sent as they are
9754 * potentially useful as a traffic analysis countermeasure.
9755 *
9756 * Therefore, it is possible that the input message length is 0 and the
9757 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009758 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009759static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009760 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009761{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009762 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9763 const size_t max_len = (size_t) ret;
9764
9765 if( ret < 0 )
9766 {
9767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9768 return( ret );
9769 }
9770
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009771 if( len > max_len )
9772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009777 "maximum fragment length: %d > %d",
9778 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009780 }
9781 else
9782#endif
9783 len = max_len;
9784 }
Paul Bakker887bd502011-06-08 13:10:54 +00009785
Paul Bakker5121ce52009-01-03 21:22:43 +00009786 if( ssl->out_left != 0 )
9787 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009788 /*
9789 * The user has previously tried to send the data and
9790 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9791 * written. In this case, we expect the high-level write function
9792 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009797 return( ret );
9798 }
9799 }
Paul Bakker887bd502011-06-08 13:10:54 +00009800 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009801 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009802 /*
9803 * The user is trying to send a message the first time, so we need to
9804 * copy the data into the internal buffers and setup the data structure
9805 * to keep track of partial writes
9806 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009807 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009809 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009810
Hanno Becker67bc7c32018-08-06 11:33:50 +01009811 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009814 return( ret );
9815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009816 }
9817
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009818 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009819}
9820
9821/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009822 * Write application data, doing 1/n-1 splitting if necessary.
9823 *
9824 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009825 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009826 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009829static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009830 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009831{
9832 int ret;
9833
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009834 if( ssl->conf->cbc_record_splitting ==
9835 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009836 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9838 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9839 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009840 {
9841 return( ssl_write_real( ssl, buf, len ) );
9842 }
9843
9844 if( ssl->split_done == 0 )
9845 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009846 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009847 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009848 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009849 }
9850
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009851 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9852 return( ret );
9853 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009854
9855 return( ret + 1 );
9856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009858
9859/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009860 * Write application data (public-facing wrapper)
9861 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009862int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009863{
9864 int ret;
9865
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009867
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009868 if( ssl == NULL || ssl->conf == NULL )
9869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9870
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009871#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009872 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9873 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009875 return( ret );
9876 }
9877#endif
9878
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009879 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009880 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009881 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009882 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009884 return( ret );
9885 }
9886 }
9887
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009888#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009889 ret = ssl_write_split( ssl, buf, len );
9890#else
9891 ret = ssl_write_real( ssl, buf, len );
9892#endif
9893
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009895
9896 return( ret );
9897}
9898
9899/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009900 * Notify the peer that the connection is being closed
9901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009903{
9904 int ret;
9905
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009906 if( ssl == NULL || ssl->conf == NULL )
9907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009910
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009911 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9917 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9918 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 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_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 return( ret );
9922 }
9923 }
9924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009926
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009927 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009928}
9929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009931{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009932 if( transform == NULL )
9933 return;
9934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009936 deflateEnd( &transform->ctx_deflate );
9937 inflateEnd( &transform->ctx_inflate );
9938#endif
9939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9941 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009942
Hanno Beckerd56ed242018-01-03 15:32:51 +00009943#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 mbedtls_md_free( &transform->md_ctx_enc );
9945 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009946#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009947
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009948 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009949}
9950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951#if defined(MBEDTLS_X509_CRT_PARSE_C)
9952static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009955
9956 while( cur != NULL )
9957 {
9958 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009960 cur = next;
9961 }
9962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009964
Hanno Becker0271f962018-08-16 13:23:47 +01009965#if defined(MBEDTLS_SSL_PROTO_DTLS)
9966
9967static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9968{
9969 unsigned offset;
9970 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9971
9972 if( hs == NULL )
9973 return;
9974
Hanno Becker283f5ef2018-08-24 09:34:47 +01009975 ssl_free_buffered_record( ssl );
9976
Hanno Becker0271f962018-08-16 13:23:47 +01009977 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009978 ssl_buffering_free_slot( ssl, offset );
9979}
9980
9981static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9982 uint8_t slot )
9983{
9984 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9985 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009986
9987 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9988 return;
9989
Hanno Beckere605b192018-08-21 15:59:07 +01009990 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009991 {
Hanno Beckere605b192018-08-21 15:59:07 +01009992 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009993 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009994 mbedtls_free( hs_buf->data );
9995 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009996 }
9997}
9998
9999#endif /* MBEDTLS_SSL_PROTO_DTLS */
10000
Gilles Peskine9b562d52018-04-25 20:32:43 +020010001void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010002{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010003 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10004
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010005 if( handshake == NULL )
10006 return;
10007
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010008#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10009 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10010 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010011 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010012 handshake->async_in_progress = 0;
10013 }
10014#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10015
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010016#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10017 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10018 mbedtls_md5_free( &handshake->fin_md5 );
10019 mbedtls_sha1_free( &handshake->fin_sha1 );
10020#endif
10021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10022#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010023#if defined(MBEDTLS_USE_PSA_CRYPTO)
10024 psa_hash_abort( &handshake->fin_sha256_psa );
10025#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010026 mbedtls_sha256_free( &handshake->fin_sha256 );
10027#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010028#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010029#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010031 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010032#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010033 mbedtls_sha512_free( &handshake->fin_sha512 );
10034#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010035#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038#if defined(MBEDTLS_DHM_C)
10039 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041#if defined(MBEDTLS_ECDH_C)
10042 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010043#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010044#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010045 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010046#if defined(MBEDTLS_SSL_CLI_C)
10047 mbedtls_free( handshake->ecjpake_cache );
10048 handshake->ecjpake_cache = NULL;
10049 handshake->ecjpake_cache_len = 0;
10050#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010051#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010052
Janos Follath4ae5c292016-02-10 11:27:43 +000010053#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10054 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010055 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010057#endif
10058
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010059#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10060 if( handshake->psk != NULL )
10061 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010062 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010063 mbedtls_free( handshake->psk );
10064 }
10065#endif
10066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10068 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010069 /*
10070 * Free only the linked list wrapper, not the keys themselves
10071 * since the belong to the SNI callback
10072 */
10073 if( handshake->sni_key_cert != NULL )
10074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010076
10077 while( cur != NULL )
10078 {
10079 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010081 cur = next;
10082 }
10083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010085
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010086#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010087 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010088 if( handshake->ecrs_peer_cert != NULL )
10089 {
10090 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10091 mbedtls_free( handshake->ecrs_peer_cert );
10092 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010093#endif
10094
Hanno Becker75173122019-02-06 16:18:31 +000010095#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10096 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10097 mbedtls_pk_free( &handshake->peer_pubkey );
10098#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100#if defined(MBEDTLS_SSL_PROTO_DTLS)
10101 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010102 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010103 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010104#endif
10105
Hanno Becker4a63ed42019-01-08 11:39:35 +000010106#if defined(MBEDTLS_ECDH_C) && \
10107 defined(MBEDTLS_USE_PSA_CRYPTO)
10108 psa_destroy_key( handshake->ecdh_psa_privkey );
10109#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10110
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010111 mbedtls_platform_zeroize( handshake,
10112 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010113}
10114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010116{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010117 if( session == NULL )
10118 return;
10119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010121 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010122#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010123
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010124#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010126#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010127
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010128 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010129}
10130
Paul Bakker5121ce52009-01-03 21:22:43 +000010131/*
10132 * Free an SSL context
10133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010135{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010136 if( ssl == NULL )
10137 return;
10138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010140
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010141 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010142 {
Angus Grattond8213d02016-05-25 20:56:48 +100010143 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010145 }
10146
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010147 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010148 {
Angus Grattond8213d02016-05-25 20:56:48 +100010149 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010151 }
10152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010154 if( ssl->compress_buf != NULL )
10155 {
Angus Grattond8213d02016-05-25 20:56:48 +100010156 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010157 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010158 }
10159#endif
10160
Paul Bakker48916f92012-09-16 19:57:18 +000010161 if( ssl->transform )
10162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163 mbedtls_ssl_transform_free( ssl->transform );
10164 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010165 }
10166
10167 if( ssl->handshake )
10168 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010169 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10171 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173 mbedtls_free( ssl->handshake );
10174 mbedtls_free( ssl->transform_negotiate );
10175 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010176 }
10177
Paul Bakkerc0463502013-02-14 11:19:38 +010010178 if( ssl->session )
10179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 mbedtls_ssl_session_free( ssl->session );
10181 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010182 }
10183
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010184#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010185 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010186 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010187 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010189 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010190#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10193 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10196 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010197 }
10198#endif
10199
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010200#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010202#endif
10203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010205
Paul Bakker86f04f42013-02-14 11:20:09 +010010206 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010207 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010208}
10209
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010210/*
10211 * Initialze mbedtls_ssl_config
10212 */
10213void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10214{
10215 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10216}
10217
Simon Butcherc97b6972015-12-27 23:48:17 +000010218#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010219static int ssl_preset_default_hashes[] = {
10220#if defined(MBEDTLS_SHA512_C)
10221 MBEDTLS_MD_SHA512,
10222 MBEDTLS_MD_SHA384,
10223#endif
10224#if defined(MBEDTLS_SHA256_C)
10225 MBEDTLS_MD_SHA256,
10226 MBEDTLS_MD_SHA224,
10227#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010228#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010229 MBEDTLS_MD_SHA1,
10230#endif
10231 MBEDTLS_MD_NONE
10232};
Simon Butcherc97b6972015-12-27 23:48:17 +000010233#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010234
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010235static int ssl_preset_suiteb_ciphersuites[] = {
10236 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10237 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10238 0
10239};
10240
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010241#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010242static int ssl_preset_suiteb_hashes[] = {
10243 MBEDTLS_MD_SHA256,
10244 MBEDTLS_MD_SHA384,
10245 MBEDTLS_MD_NONE
10246};
10247#endif
10248
10249#if defined(MBEDTLS_ECP_C)
10250static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10251 MBEDTLS_ECP_DP_SECP256R1,
10252 MBEDTLS_ECP_DP_SECP384R1,
10253 MBEDTLS_ECP_DP_NONE
10254};
10255#endif
10256
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010257/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010258 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010259 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010260int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010261 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010262{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010263#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010264 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010265#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010266
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010267 /* Use the functions here so that they are covered in tests,
10268 * but otherwise access member directly for efficiency */
10269 mbedtls_ssl_conf_endpoint( conf, endpoint );
10270 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010271
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010272 /*
10273 * Things that are common to all presets
10274 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010275#if defined(MBEDTLS_SSL_CLI_C)
10276 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10277 {
10278 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10279#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10280 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10281#endif
10282 }
10283#endif
10284
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010285#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010286 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010287#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010288
10289#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10290 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10291#endif
10292
10293#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10294 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10295#endif
10296
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010297#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10298 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10299#endif
10300
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010301#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010302 conf->f_cookie_write = ssl_cookie_write_dummy;
10303 conf->f_cookie_check = ssl_cookie_check_dummy;
10304#endif
10305
10306#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10307 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10308#endif
10309
Janos Follath088ce432017-04-10 12:42:31 +010010310#if defined(MBEDTLS_SSL_SRV_C)
10311 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10312#endif
10313
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010314#if defined(MBEDTLS_SSL_PROTO_DTLS)
10315 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10316 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10317#endif
10318
10319#if defined(MBEDTLS_SSL_RENEGOTIATION)
10320 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010321 memset( conf->renego_period, 0x00, 2 );
10322 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010323#endif
10324
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010325#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10326 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10327 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010328 const unsigned char dhm_p[] =
10329 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10330 const unsigned char dhm_g[] =
10331 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10332
Hanno Beckera90658f2017-10-04 15:29:08 +010010333 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10334 dhm_p, sizeof( dhm_p ),
10335 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010336 {
10337 return( ret );
10338 }
10339 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010340#endif
10341
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010342 /*
10343 * Preset-specific defaults
10344 */
10345 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010346 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010347 /*
10348 * NSA Suite B
10349 */
10350 case MBEDTLS_SSL_PRESET_SUITEB:
10351 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10352 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10353 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10354 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10355
10356 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10357 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10358 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10359 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10360 ssl_preset_suiteb_ciphersuites;
10361
10362#if defined(MBEDTLS_X509_CRT_PARSE_C)
10363 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010364#endif
10365
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010366#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010367 conf->sig_hashes = ssl_preset_suiteb_hashes;
10368#endif
10369
10370#if defined(MBEDTLS_ECP_C)
10371 conf->curve_list = ssl_preset_suiteb_curves;
10372#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010373 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010374
10375 /*
10376 * Default
10377 */
10378 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010379 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10380 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10381 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10382 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10383 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10384 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10385 MBEDTLS_SSL_MIN_MINOR_VERSION :
10386 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010387 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10388 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10389
10390#if defined(MBEDTLS_SSL_PROTO_DTLS)
10391 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10392 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10393#endif
10394
10395 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10396 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10397 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10398 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10399 mbedtls_ssl_list_ciphersuites();
10400
10401#if defined(MBEDTLS_X509_CRT_PARSE_C)
10402 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10403#endif
10404
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010405#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010406 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010407#endif
10408
10409#if defined(MBEDTLS_ECP_C)
10410 conf->curve_list = mbedtls_ecp_grp_id_list();
10411#endif
10412
10413#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10414 conf->dhm_min_bitlen = 1024;
10415#endif
10416 }
10417
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010418 return( 0 );
10419}
10420
10421/*
10422 * Free mbedtls_ssl_config
10423 */
10424void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10425{
10426#if defined(MBEDTLS_DHM_C)
10427 mbedtls_mpi_free( &conf->dhm_P );
10428 mbedtls_mpi_free( &conf->dhm_G );
10429#endif
10430
10431#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10432 if( conf->psk != NULL )
10433 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010434 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010435 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010436 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010437 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010438 }
10439
10440 if( conf->psk_identity != NULL )
10441 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010442 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010443 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010444 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010445 conf->psk_identity_len = 0;
10446 }
10447#endif
10448
10449#if defined(MBEDTLS_X509_CRT_PARSE_C)
10450 ssl_key_cert_free( conf->key_cert );
10451#endif
10452
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010453 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010454}
10455
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010456#if defined(MBEDTLS_PK_C) && \
10457 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010458/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010462{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463#if defined(MBEDTLS_RSA_C)
10464 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10465 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467#if defined(MBEDTLS_ECDSA_C)
10468 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10469 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010472}
10473
Hanno Becker7e5437a2017-04-28 17:15:26 +010010474unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10475{
10476 switch( type ) {
10477 case MBEDTLS_PK_RSA:
10478 return( MBEDTLS_SSL_SIG_RSA );
10479 case MBEDTLS_PK_ECDSA:
10480 case MBEDTLS_PK_ECKEY:
10481 return( MBEDTLS_SSL_SIG_ECDSA );
10482 default:
10483 return( MBEDTLS_SSL_SIG_ANON );
10484 }
10485}
10486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010488{
10489 switch( sig )
10490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491#if defined(MBEDTLS_RSA_C)
10492 case MBEDTLS_SSL_SIG_RSA:
10493 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495#if defined(MBEDTLS_ECDSA_C)
10496 case MBEDTLS_SSL_SIG_ECDSA:
10497 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010498#endif
10499 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010501 }
10502}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010503#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010504
Hanno Becker7e5437a2017-04-28 17:15:26 +010010505#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10506 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10507
10508/* Find an entry in a signature-hash set matching a given hash algorithm. */
10509mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10510 mbedtls_pk_type_t sig_alg )
10511{
10512 switch( sig_alg )
10513 {
10514 case MBEDTLS_PK_RSA:
10515 return( set->rsa );
10516 case MBEDTLS_PK_ECDSA:
10517 return( set->ecdsa );
10518 default:
10519 return( MBEDTLS_MD_NONE );
10520 }
10521}
10522
10523/* Add a signature-hash-pair to a signature-hash set */
10524void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10525 mbedtls_pk_type_t sig_alg,
10526 mbedtls_md_type_t md_alg )
10527{
10528 switch( sig_alg )
10529 {
10530 case MBEDTLS_PK_RSA:
10531 if( set->rsa == MBEDTLS_MD_NONE )
10532 set->rsa = md_alg;
10533 break;
10534
10535 case MBEDTLS_PK_ECDSA:
10536 if( set->ecdsa == MBEDTLS_MD_NONE )
10537 set->ecdsa = md_alg;
10538 break;
10539
10540 default:
10541 break;
10542 }
10543}
10544
10545/* Allow exactly one hash algorithm for each signature. */
10546void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10547 mbedtls_md_type_t md_alg )
10548{
10549 set->rsa = md_alg;
10550 set->ecdsa = md_alg;
10551}
10552
10553#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10554 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10555
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010556/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010557 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010560{
10561 switch( hash )
10562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010563#if defined(MBEDTLS_MD5_C)
10564 case MBEDTLS_SSL_HASH_MD5:
10565 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010567#if defined(MBEDTLS_SHA1_C)
10568 case MBEDTLS_SSL_HASH_SHA1:
10569 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571#if defined(MBEDTLS_SHA256_C)
10572 case MBEDTLS_SSL_HASH_SHA224:
10573 return( MBEDTLS_MD_SHA224 );
10574 case MBEDTLS_SSL_HASH_SHA256:
10575 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if defined(MBEDTLS_SHA512_C)
10578 case MBEDTLS_SSL_HASH_SHA384:
10579 return( MBEDTLS_MD_SHA384 );
10580 case MBEDTLS_SSL_HASH_SHA512:
10581 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010582#endif
10583 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010584 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010585 }
10586}
10587
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010588/*
10589 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10590 */
10591unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10592{
10593 switch( md )
10594 {
10595#if defined(MBEDTLS_MD5_C)
10596 case MBEDTLS_MD_MD5:
10597 return( MBEDTLS_SSL_HASH_MD5 );
10598#endif
10599#if defined(MBEDTLS_SHA1_C)
10600 case MBEDTLS_MD_SHA1:
10601 return( MBEDTLS_SSL_HASH_SHA1 );
10602#endif
10603#if defined(MBEDTLS_SHA256_C)
10604 case MBEDTLS_MD_SHA224:
10605 return( MBEDTLS_SSL_HASH_SHA224 );
10606 case MBEDTLS_MD_SHA256:
10607 return( MBEDTLS_SSL_HASH_SHA256 );
10608#endif
10609#if defined(MBEDTLS_SHA512_C)
10610 case MBEDTLS_MD_SHA384:
10611 return( MBEDTLS_SSL_HASH_SHA384 );
10612 case MBEDTLS_MD_SHA512:
10613 return( MBEDTLS_SSL_HASH_SHA512 );
10614#endif
10615 default:
10616 return( MBEDTLS_SSL_HASH_NONE );
10617 }
10618}
10619
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010620#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010621/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010622 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010623 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010624 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010625int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010628
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010629 if( ssl->conf->curve_list == NULL )
10630 return( -1 );
10631
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010632 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010633 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010634 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010635
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010636 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010637}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010638#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010639
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010640#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010641/*
10642 * Check if a hash proposed by the peer is in our list.
10643 * Return 0 if we're willing to use it, -1 otherwise.
10644 */
10645int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10646 mbedtls_md_type_t md )
10647{
10648 const int *cur;
10649
10650 if( ssl->conf->sig_hashes == NULL )
10651 return( -1 );
10652
10653 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10654 if( *cur == (int) md )
10655 return( 0 );
10656
10657 return( -1 );
10658}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010659#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661#if defined(MBEDTLS_X509_CRT_PARSE_C)
10662int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10663 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010664 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010665 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010666{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010667 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010669 int usage = 0;
10670#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010672 const char *ext_oid;
10673 size_t ext_len;
10674#endif
10675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010676#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10677 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010678 ((void) cert);
10679 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010680 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010681#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10684 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010685 {
10686 /* Server part of the key exchange */
10687 switch( ciphersuite->key_exchange )
10688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689 case MBEDTLS_KEY_EXCHANGE_RSA:
10690 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010691 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010692 break;
10693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10695 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10696 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10697 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010698 break;
10699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10701 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010702 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010703 break;
10704
10705 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706 case MBEDTLS_KEY_EXCHANGE_NONE:
10707 case MBEDTLS_KEY_EXCHANGE_PSK:
10708 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10709 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010710 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010711 usage = 0;
10712 }
10713 }
10714 else
10715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10717 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010718 }
10719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010721 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010722 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010723 ret = -1;
10724 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010725#else
10726 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10730 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10733 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010734 }
10735 else
10736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10738 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010739 }
10740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010742 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010743 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010744 ret = -1;
10745 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010747
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010748 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010751
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010752/*
10753 * Convert version numbers to/from wire format
10754 * and, for DTLS, to/from TLS equivalent.
10755 *
10756 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010757 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010758 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10759 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010762 unsigned char ver[2] )
10763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764#if defined(MBEDTLS_SSL_PROTO_DTLS)
10765 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010768 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10769
10770 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10771 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10772 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010773 else
10774#else
10775 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010776#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010777 {
10778 ver[0] = (unsigned char) major;
10779 ver[1] = (unsigned char) minor;
10780 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010781}
10782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010784 const unsigned char ver[2] )
10785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786#if defined(MBEDTLS_SSL_PROTO_DTLS)
10787 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010788 {
10789 *major = 255 - ver[0] + 2;
10790 *minor = 255 - ver[1] + 1;
10791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010793 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10794 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010795 else
10796#else
10797 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010798#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010799 {
10800 *major = ver[0];
10801 *minor = ver[1];
10802 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010803}
10804
Simon Butcher99000142016-10-13 17:21:01 +010010805int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10806{
10807#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10808 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10809 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10810
10811 switch( md )
10812 {
10813#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10814#if defined(MBEDTLS_MD5_C)
10815 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010816 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010817#endif
10818#if defined(MBEDTLS_SHA1_C)
10819 case MBEDTLS_SSL_HASH_SHA1:
10820 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10821 break;
10822#endif
10823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10824#if defined(MBEDTLS_SHA512_C)
10825 case MBEDTLS_SSL_HASH_SHA384:
10826 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10827 break;
10828#endif
10829#if defined(MBEDTLS_SHA256_C)
10830 case MBEDTLS_SSL_HASH_SHA256:
10831 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10832 break;
10833#endif
10834 default:
10835 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10836 }
10837
10838 return 0;
10839#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10840 (void) ssl;
10841 (void) md;
10842
10843 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10844#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10845}
10846
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010847#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10848 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10849int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10850 unsigned char *output,
10851 unsigned char *data, size_t data_len )
10852{
10853 int ret = 0;
10854 mbedtls_md5_context mbedtls_md5;
10855 mbedtls_sha1_context mbedtls_sha1;
10856
10857 mbedtls_md5_init( &mbedtls_md5 );
10858 mbedtls_sha1_init( &mbedtls_sha1 );
10859
10860 /*
10861 * digitally-signed struct {
10862 * opaque md5_hash[16];
10863 * opaque sha_hash[20];
10864 * };
10865 *
10866 * md5_hash
10867 * MD5(ClientHello.random + ServerHello.random
10868 * + ServerParams);
10869 * sha_hash
10870 * SHA(ClientHello.random + ServerHello.random
10871 * + ServerParams);
10872 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010873 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010874 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010876 goto exit;
10877 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010878 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010879 ssl->handshake->randbytes, 64 ) ) != 0 )
10880 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010882 goto exit;
10883 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010884 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010885 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010887 goto exit;
10888 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010889 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010890 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010892 goto exit;
10893 }
10894
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010895 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010896 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010898 goto exit;
10899 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010900 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010901 ssl->handshake->randbytes, 64 ) ) != 0 )
10902 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010904 goto exit;
10905 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010906 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010907 data_len ) ) != 0 )
10908 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010910 goto exit;
10911 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010912 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010913 output + 16 ) ) != 0 )
10914 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010916 goto exit;
10917 }
10918
10919exit:
10920 mbedtls_md5_free( &mbedtls_md5 );
10921 mbedtls_sha1_free( &mbedtls_sha1 );
10922
10923 if( ret != 0 )
10924 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10925 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10926
10927 return( ret );
10928
10929}
10930#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10931 MBEDTLS_SSL_PROTO_TLS1_1 */
10932
10933#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10934 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010935
10936#if defined(MBEDTLS_USE_PSA_CRYPTO)
10937int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10938 unsigned char *hash, size_t *hashlen,
10939 unsigned char *data, size_t data_len,
10940 mbedtls_md_type_t md_alg )
10941{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010942 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010943 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010944 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10945
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010947
10948 if( ( status = psa_hash_setup( &hash_operation,
10949 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010950 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010951 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010952 goto exit;
10953 }
10954
Andrzej Kurek814feff2019-01-14 04:35:19 -050010955 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10956 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010957 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010958 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010959 goto exit;
10960 }
10961
Andrzej Kurek814feff2019-01-14 04:35:19 -050010962 if( ( status = psa_hash_update( &hash_operation,
10963 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010964 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010965 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010966 goto exit;
10967 }
10968
Andrzej Kurek814feff2019-01-14 04:35:19 -050010969 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10970 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010971 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010972 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010973 goto exit;
10974 }
10975
10976exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010977 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010978 {
10979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10980 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010981 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010982 {
10983 case PSA_ERROR_NOT_SUPPORTED:
10984 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010985 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010986 case PSA_ERROR_BUFFER_TOO_SMALL:
10987 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10988 case PSA_ERROR_INSUFFICIENT_MEMORY:
10989 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10990 default:
10991 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10992 }
10993 }
10994 return( 0 );
10995}
10996
10997#else
10998
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010999int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011000 unsigned char *hash, size_t *hashlen,
11001 unsigned char *data, size_t data_len,
11002 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011003{
11004 int ret = 0;
11005 mbedtls_md_context_t ctx;
11006 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011007 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011008
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011010
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011011 mbedtls_md_init( &ctx );
11012
11013 /*
11014 * digitally-signed struct {
11015 * opaque client_random[32];
11016 * opaque server_random[32];
11017 * ServerDHParams params;
11018 * };
11019 */
11020 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11021 {
11022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11023 goto exit;
11024 }
11025 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11026 {
11027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11028 goto exit;
11029 }
11030 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11031 {
11032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11033 goto exit;
11034 }
11035 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11036 {
11037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11038 goto exit;
11039 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011040 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011041 {
11042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11043 goto exit;
11044 }
11045
11046exit:
11047 mbedtls_md_free( &ctx );
11048
11049 if( ret != 0 )
11050 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11051 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11052
11053 return( ret );
11054}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011055#endif /* MBEDTLS_USE_PSA_CRYPTO */
11056
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011057#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11058 MBEDTLS_SSL_PROTO_TLS1_2 */
11059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060#endif /* MBEDTLS_SSL_TLS_C */