blob: 45cafffa285417248a1662b2fceebabe7846d028 [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 );
148 ssl->own_cid_len = own_cid_len;
149
150 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100151 return( 0 );
152}
153
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100154/* WARNING: The CID feature isn't fully implemented yet
155 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100156int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
157 int *enabled,
158 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
159 size_t *peer_cid_len )
160{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100161 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100162
163 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
165
166 /* What shall we report if we have exchanged if both client
167 * and server have used the CID extension, but negotiated
168 * empty CIDs? This is indistinguishable from not using the
169 * CID extension in the first place, and we're reporting
170 * MBEDTLS_SSL_CID_DISABLED in this case. */
171 if( ssl->transform_in->in_cid_len == 0 &&
172 ssl->transform_in->out_cid_len == 0 )
173 {
174 return( 0 );
175 }
176
177 *peer_cid_len = ssl->transform_in->out_cid_len;
178 memcpy( peer_cid, ssl->transform_in->out_cid,
179 ssl->transform_in->out_cid_len );
180
181 *enabled = MBEDTLS_SSL_CID_ENABLED;
182
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183 return( 0 );
184}
Hanno Becker35c36a62019-04-23 12:31:42 +0100185#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100186
Hanno Beckerd5847772018-08-28 10:09:23 +0100187/* Forward declarations for functions related to message buffering. */
188static void ssl_buffering_free( mbedtls_ssl_context *ssl );
189static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
190 uint8_t slot );
191static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
192static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
193static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
194static int ssl_buffer_message( mbedtls_ssl_context *ssl );
195static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100196static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100197
Hanno Beckera67dee22018-08-22 10:05:20 +0100198static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100199static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100200{
Hanno Becker11682cc2018-08-22 14:41:02 +0100201 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100202
203 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100204 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100205
206 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
207}
208
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
210{
Hanno Becker11682cc2018-08-22 14:41:02 +0100211 size_t const bytes_written = ssl->out_left;
212 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100213
214 /* Double-check that the write-index hasn't gone
215 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100216 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100217 {
218 /* Should never happen... */
219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
220 }
221
222 return( (int) ( mtu - bytes_written ) );
223}
224
225static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
226{
227 int ret;
228 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400229 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100230
231#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
232 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
233
234 if( max_len > mfl )
235 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100236
237 /* By the standard (RFC 6066 Sect. 4), the MFL extension
238 * only limits the maximum record payload size, so in theory
239 * we would be allowed to pack multiple records of payload size
240 * MFL into a single datagram. However, this would mean that there's
241 * no way to explicitly communicate MTU restrictions to the peer.
242 *
243 * The following reduction of max_len makes sure that we never
244 * write datagrams larger than MFL + Record Expansion Overhead.
245 */
246 if( max_len <= ssl->out_left )
247 return( 0 );
248
249 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100250#endif
251
252 ret = ssl_get_remaining_space_in_datagram( ssl );
253 if( ret < 0 )
254 return( ret );
255 remaining = (size_t) ret;
256
257 ret = mbedtls_ssl_get_record_expansion( ssl );
258 if( ret < 0 )
259 return( ret );
260 expansion = (size_t) ret;
261
262 if( remaining <= expansion )
263 return( 0 );
264
265 remaining -= expansion;
266 if( remaining >= max_len )
267 remaining = max_len;
268
269 return( (int) remaining );
270}
271
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200272/*
273 * Double the retransmit timeout value, within the allowed range,
274 * returning -1 if the maximum value has already been reached.
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200277{
278 uint32_t new_timeout;
279
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200280 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281 return( -1 );
282
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200283 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
284 * in the following way: after the initial transmission and a first
285 * retransmission, back off to a temporary estimated MTU of 508 bytes.
286 * This value is guaranteed to be deliverable (if not guaranteed to be
287 * delivered) of any compliant IPv4 (and IPv6) network, and should work
288 * on most non-IP stacks too. */
289 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400290 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200291 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
293 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295 new_timeout = 2 * ssl->handshake->retransmit_timeout;
296
297 /* Avoid arithmetic overflow and range overflow */
298 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200299 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200301 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200302 }
303
304 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200306 ssl->handshake->retransmit_timeout ) );
307
308 return( 0 );
309}
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200312{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200313 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315 ssl->handshake->retransmit_timeout ) );
316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200320/*
321 * Convert max_fragment_length codes to length.
322 * RFC 6066 says:
323 * enum{
324 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
325 * } MaxFragmentLength;
326 * and we add 0 -> extension unused
327 */
Angus Grattond8213d02016-05-25 20:56:48 +1000328static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200329{
Angus Grattond8213d02016-05-25 20:56:48 +1000330 switch( mfl )
331 {
332 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
333 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
334 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
335 return 512;
336 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
337 return 1024;
338 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
339 return 2048;
340 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
341 return 4096;
342 default:
343 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
344 }
345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200347
Hanno Becker52055ae2019-02-06 14:30:46 +0000348int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
349 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_ssl_session_free( dst );
352 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000355
356#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200357 if( src->peer_cert != NULL )
358 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200359 int ret;
360
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200361 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200362 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200363 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200368 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371 dst->peer_cert = NULL;
372 return( ret );
373 }
374 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000375#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000376 if( src->peer_cert_digest != NULL )
377 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000378 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000379 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000380 if( dst->peer_cert_digest == NULL )
381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
382
383 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
384 src->peer_cert_digest_len );
385 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000386 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000387 }
388#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200392#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200393 if( src->ticket != NULL )
394 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200395 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200396 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398
399 memcpy( dst->ticket, src->ticket, src->ticket_len );
400 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200401#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200402
403 return( 0 );
404}
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
407int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200408 const unsigned char *key_enc, const unsigned char *key_dec,
409 size_t keylen,
410 const unsigned char *iv_enc, const unsigned char *iv_dec,
411 size_t ivlen,
412 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200413 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
415int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
416int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
417int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
419#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421/*
422 * Key material generation
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200425static int ssl3_prf( const unsigned char *secret, size_t slen,
426 const char *label,
427 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000428 unsigned char *dstbuf, size_t dlen )
429{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100430 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000431 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200432 mbedtls_md5_context md5;
433 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000434 unsigned char padding[16];
435 unsigned char sha1sum[20];
436 ((void)label);
437
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200438 mbedtls_md5_init( &md5 );
439 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200440
Paul Bakker5f70b252012-09-13 14:23:06 +0000441 /*
442 * SSLv3:
443 * block =
444 * MD5( secret + SHA1( 'A' + secret + random ) ) +
445 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
446 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
447 * ...
448 */
449 for( i = 0; i < dlen / 16; i++ )
450 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200451 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000452
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100453 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100454 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100456 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100464 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000472 }
473
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200475 mbedtls_md5_free( &md5 );
476 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000477
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500478 mbedtls_platform_zeroize( padding, sizeof( padding ) );
479 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000480
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200486static int tls1_prf( const unsigned char *secret, size_t slen,
487 const char *label,
488 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000489 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000490{
Paul Bakker23986e52011-04-24 08:57:21 +0000491 size_t nb, hs;
492 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200493 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300494 unsigned char *tmp;
495 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 const mbedtls_md_info_t *md_info;
498 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100499 int ret;
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Ron Eldor3b350852019-05-07 18:31:49 +0300503 tmp_len = 20 + strlen( label ) + rlen;
504 tmp = mbedtls_calloc( 1, tmp_len );
505 if( tmp == NULL )
506 {
507 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
508 goto exit;
509 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 hs = ( slen + 1 ) / 2;
512 S1 = secret;
513 S2 = secret + slen - hs;
514
515 nb = strlen( label );
516 memcpy( tmp + 20, label, nb );
517 memcpy( tmp + 20 + nb, random, rlen );
518 nb += rlen;
519
520 /*
521 * First compute P_md5(secret,label+random)[0..dlen]
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300524 {
525 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
526 goto exit;
527 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300530 {
531 goto exit;
532 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
535 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
536 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538 for( i = 0; i < dlen; i += 16 )
539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_md_hmac_reset ( &md_ctx );
541 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
542 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_md_hmac_reset ( &md_ctx );
545 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
546 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
549
550 for( j = 0; j < k; j++ )
551 dstbuf[i + j] = h_i[j];
552 }
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 /*
557 * XOR out with P_sha1(secret,label+random)[0..dlen]
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300560 {
561 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
562 goto exit;
563 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300566 {
567 goto exit;
568 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
571 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
572 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 for( i = 0; i < dlen; i += 20 )
575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_hmac_reset ( &md_ctx );
577 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
578 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_md_hmac_reset ( &md_ctx );
581 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
582 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
585
586 for( j = 0; j < k; j++ )
587 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
588 }
589
Ron Eldor3b350852019-05-07 18:31:49 +0300590exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100592
Ron Eldor3b350852019-05-07 18:31:49 +0300593 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500594 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Ron Eldor3b350852019-05-07 18:31:49 +0300596 mbedtls_free( tmp );
597 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500602#if defined(MBEDTLS_USE_PSA_CRYPTO)
603static int tls_prf_generic( mbedtls_md_type_t md_type,
604 const unsigned char *secret, size_t slen,
605 const char *label,
606 const unsigned char *random, size_t rlen,
607 unsigned char *dstbuf, size_t dlen )
608{
609 psa_status_t status;
610 psa_algorithm_t alg;
611 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500612 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500613 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
614
Andrzej Kurek2f760752019-01-28 08:08:15 -0500615 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500616 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500617
Andrzej Kurekc929a822019-01-14 03:51:11 -0500618 if( md_type == MBEDTLS_MD_SHA384 )
619 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
620 else
621 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
622
Andrzej Kurek2f760752019-01-28 08:08:15 -0500623 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500624 psa_key_policy_set_usage( &policy,
625 PSA_KEY_USAGE_DERIVE,
626 alg );
627 status = psa_set_key_policy( master_slot, &policy );
628 if( status != PSA_SUCCESS )
629 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
630
631 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
632 if( status != PSA_SUCCESS )
633 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
634
635 status = psa_key_derivation( &generator,
636 master_slot, alg,
637 random, rlen,
638 (unsigned char const *) label,
639 (size_t) strlen( label ),
640 dlen );
641 if( status != PSA_SUCCESS )
642 {
643 psa_generator_abort( &generator );
644 psa_destroy_key( master_slot );
645 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
646 }
647
648 status = psa_generator_read( &generator, dstbuf, dlen );
649 if( status != PSA_SUCCESS )
650 {
651 psa_generator_abort( &generator );
652 psa_destroy_key( master_slot );
653 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
654 }
655
656 status = psa_generator_abort( &generator );
657 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500658 {
659 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500661 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500662
663 status = psa_destroy_key( master_slot );
664 if( status != PSA_SUCCESS )
665 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
666
Andrzej Kurek33171262019-01-15 03:25:18 -0500667 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500668}
669
670#else /* MBEDTLS_USE_PSA_CRYPTO */
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100673 const unsigned char *secret, size_t slen,
674 const char *label,
675 const unsigned char *random, size_t rlen,
676 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000677{
678 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100679 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300680 unsigned char *tmp;
681 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
683 const mbedtls_md_info_t *md_info;
684 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100685 int ret;
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100693
Ron Eldor3b350852019-05-07 18:31:49 +0300694 tmp_len = md_len + strlen( label ) + rlen;
695 tmp = mbedtls_calloc( 1, tmp_len );
696 if( tmp == NULL )
697 {
698 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
699 goto exit;
700 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000701
702 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100703 memcpy( tmp + md_len, label, nb );
704 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000705 nb += rlen;
706
707 /*
708 * Compute P_<hash>(secret, label + random)[0..dlen]
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300711 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
714 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
715 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100716
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100717 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_md_hmac_reset ( &md_ctx );
720 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
721 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_md_hmac_reset ( &md_ctx );
724 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
725 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000726
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100727 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000728
729 for( j = 0; j < k; j++ )
730 dstbuf[i + j] = h_i[j];
731 }
732
Ron Eldor3b350852019-05-07 18:31:49 +0300733exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100735
Ron Eldor3b350852019-05-07 18:31:49 +0300736 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500737 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000738
Ron Eldor3b350852019-05-07 18:31:49 +0300739 mbedtls_free( tmp );
740
741 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000742}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100745static int tls_prf_sha256( const unsigned char *secret, size_t slen,
746 const char *label,
747 const unsigned char *random, size_t rlen,
748 unsigned char *dstbuf, size_t dlen )
749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100751 label, random, rlen, dstbuf, dlen ) );
752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200756static int tls_prf_sha384( const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000759 unsigned char *dstbuf, size_t dlen )
760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_SHA512_C */
765#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
770 defined(MBEDTLS_SSL_PROTO_TLS1_1)
771static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200772#endif
Paul Bakker380da532012-04-18 16:10:25 +0000773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_PROTO_SSL3)
775static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
776static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200777#endif
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
780static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
781static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#endif
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
785#if defined(MBEDTLS_SHA256_C)
786static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
787static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
788static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200789#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SHA512_C)
792static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
793static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
794static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000797
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100798#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100799 defined(MBEDTLS_USE_PSA_CRYPTO)
800static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
801{
802 if( ssl->conf->f_psk != NULL )
803 {
804 /* If we've used a callback to select the PSK,
805 * the static configuration is irrelevant. */
806 if( ssl->handshake->psk_opaque != 0 )
807 return( 1 );
808
809 return( 0 );
810 }
811
812 if( ssl->conf->psk_opaque != 0 )
813 return( 1 );
814
815 return( 0 );
816}
817#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100818 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100819
Ron Eldorcf280092019-05-14 20:19:13 +0300820#if defined(MBEDTLS_SSL_EXPORT_KEYS)
821static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
822{
823#if defined(MBEDTLS_SSL_PROTO_SSL3)
824 if( tls_prf == ssl3_prf )
825 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300826 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300827 }
828 else
829#endif
830#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
831 if( tls_prf == tls1_prf )
832 {
833 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
834 }
835 else
836#endif
837#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
838#if defined(MBEDTLS_SHA512_C)
839 if( tls_prf == tls_prf_sha384 )
840 {
841 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
842 }
843 else
844#endif
845#if defined(MBEDTLS_SHA256_C)
846 if( tls_prf == tls_prf_sha256 )
847 {
848 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
849 }
850 else
851#endif
852#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
853 return( MBEDTLS_SSL_TLS_PRF_NONE );
854}
855#endif /* MBEDTLS_SSL_EXPORT_KEYS */
856
Ron Eldor51d3ab52019-05-12 14:54:30 +0300857int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
858 const unsigned char *secret, size_t slen,
859 const char *label,
860 const unsigned char *random, size_t rlen,
861 unsigned char *dstbuf, size_t dlen )
862{
863 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
864
865 switch( prf )
866 {
867#if defined(MBEDTLS_SSL_PROTO_SSL3)
868 case MBEDTLS_SSL_TLS_PRF_SSL3:
869 tls_prf = ssl3_prf;
870 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300871#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300872#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
873 case MBEDTLS_SSL_TLS_PRF_TLS1:
874 tls_prf = tls1_prf;
875 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300876#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
877
878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300879#if defined(MBEDTLS_SHA512_C)
880 case MBEDTLS_SSL_TLS_PRF_SHA384:
881 tls_prf = tls_prf_sha384;
882 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300883#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300884#if defined(MBEDTLS_SHA256_C)
885 case MBEDTLS_SSL_TLS_PRF_SHA256:
886 tls_prf = tls_prf_sha256;
887 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300888#endif /* MBEDTLS_SHA256_C */
889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300890 default:
891 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
892 }
893
894 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
895}
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000898{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200899 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000900#if defined(MBEDTLS_USE_PSA_CRYPTO)
901 int psa_fallthrough;
902#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 unsigned char keyblk[256];
905 unsigned char *key1;
906 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100907 unsigned char *mac_enc;
908 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000909 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200910 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000911 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000912 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 const mbedtls_cipher_info_t *cipher_info;
914 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100915
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000916 /* cf. RFC 5246, Section 8.1:
917 * "The master secret is always exactly 48 bytes in length." */
918 size_t const master_secret_len = 48;
919
Hanno Becker35b23c72018-10-23 12:10:41 +0100920#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
921 unsigned char session_hash[48];
922#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_ssl_session *session = ssl->session_negotiate;
925 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
926 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000930#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
931 transform->encrypt_then_mac = session->encrypt_then_mac;
932#endif
933 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000934
935 ciphersuite_info = handshake->ciphersuite_info;
936 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100937 if( cipher_info == NULL )
938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000940 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100942 }
943
Hanno Beckere694c3e2017-12-27 21:34:08 +0000944 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100945 if( md_info == NULL )
946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000948 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100950 }
951
Hanno Becker4bf74652019-04-26 16:22:27 +0100952#if defined(MBEDTLS_SSL_CID)
953 /* Copy own and peer's CID if the use of the CID
954 * extension has been negotiated. */
955 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
956 {
957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
958 transform->in_cid_len = ssl->own_cid_len;
959 transform->out_cid_len = ssl->handshake->peer_cid_len;
960 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
961 memcpy( transform->out_cid, ssl->handshake->peer_cid,
962 ssl->handshake->peer_cid_len );
963
964 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
965 transform->out_cid_len );
966 MBEDTLS_SSL_DEBUG_BUF( 3, "Ingoing CID", transform->in_cid,
967 transform->in_cid_len );
968 }
969#endif /* MBEDTLS_SSL_CID */
970
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000972 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_SSL_PROTO_SSL3)
975 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000976 {
Paul Bakker48916f92012-09-16 19:57:18 +0000977 handshake->tls_prf = ssl3_prf;
978 handshake->calc_verify = ssl_calc_verify_ssl;
979 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000980 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200981 else
982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
984 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000985 {
Paul Bakker48916f92012-09-16 19:57:18 +0000986 handshake->tls_prf = tls1_prf;
987 handshake->calc_verify = ssl_calc_verify_tls;
988 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200990 else
991#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
993#if defined(MBEDTLS_SHA512_C)
994 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000995 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000996 {
Paul Bakker48916f92012-09-16 19:57:18 +0000997 handshake->tls_prf = tls_prf_sha384;
998 handshake->calc_verify = ssl_calc_verify_tls_sha384;
999 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001001 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003#if defined(MBEDTLS_SHA256_C)
1004 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001005 {
Paul Bakker48916f92012-09-16 19:57:18 +00001006 handshake->tls_prf = tls_prf_sha256;
1007 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1008 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001010 else
1011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1015 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001016 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001017
1018 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 * SSLv3:
1020 * master =
1021 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1022 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1023 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001024 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001025 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 * master = PRF( premaster, "master secret", randbytes )[0..47]
1027 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001028 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1031 }
1032 else
1033 {
1034 /* The label for the KDF used for key expansion.
1035 * This is either "master secret" or "extended master secret"
1036 * depending on whether the Extended Master Secret extension
1037 * is used. */
1038 char const *lbl = "master secret";
1039
1040 /* The salt for the KDF used for key expansion.
1041 * - If the Extended Master Secret extension is not used,
1042 * this is ClientHello.Random + ServerHello.Random
1043 * (see Sect. 8.1 in RFC 5246).
1044 * - If the Extended Master Secret extension is used,
1045 * this is the transcript of the handshake so far.
1046 * (see Sect. 4 in RFC 7627). */
1047 unsigned char const *salt = handshake->randbytes;
1048 size_t salt_len = 64;
1049
1050#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001054
Hanno Becker35b23c72018-10-23 12:10:41 +01001055 lbl = "extended master secret";
1056 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001057 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1059 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001062 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1063 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001064 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001065 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001066 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001067#endif /* MBEDTLS_SHA512_C */
1068 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001069 }
1070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001072 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001073
Hanno Becker35b23c72018-10-23 12:10:41 +01001074 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001075 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001076#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1077
Hanno Becker7d0a5692018-10-23 15:26:22 +01001078#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1079 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1080 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1081 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1082 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001083 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001084 /* Perform PSK-to-MS expansion in a single step. */
1085 psa_status_t status;
1086 psa_algorithm_t alg;
1087 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001088 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001089
1090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1091
1092 psk = ssl->conf->psk_opaque;
1093 if( ssl->handshake->psk_opaque != 0 )
1094 psk = ssl->handshake->psk_opaque;
1095
Hanno Becker22bf1452019-04-05 11:21:08 +01001096 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001097 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1098 else
1099 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1100
1101 status = psa_key_derivation( &generator, psk, alg,
1102 salt, salt_len,
1103 (unsigned char const *) lbl,
1104 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001105 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001106 if( status != PSA_SUCCESS )
1107 {
1108 psa_generator_abort( &generator );
1109 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1110 }
1111
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001112 status = psa_generator_read( &generator, session->master,
1113 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001114 if( status != PSA_SUCCESS )
1115 {
1116 psa_generator_abort( &generator );
1117 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1118 }
1119
1120 status = psa_generator_abort( &generator );
1121 if( status != PSA_SUCCESS )
1122 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001123 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 else
1125#endif
1126 {
1127 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1128 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001129 session->master,
1130 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001131 if( ret != 0 )
1132 {
1133 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1134 return( ret );
1135 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001136
Hanno Becker7d0a5692018-10-23 15:26:22 +01001137 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1138 handshake->premaster,
1139 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001140
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 mbedtls_platform_zeroize( handshake->premaster,
1142 sizeof(handshake->premaster) );
1143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
1146 /*
1147 * Swap the client and server random values.
1148 */
Paul Bakker48916f92012-09-16 19:57:18 +00001149 memcpy( tmp, handshake->randbytes, 64 );
1150 memcpy( handshake->randbytes, tmp + 32, 32 );
1151 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001152 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 /*
1155 * SSLv3:
1156 * key block =
1157 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1158 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1159 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1160 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1161 * ...
1162 *
1163 * TLSv1:
1164 * key block = PRF( master, "key expansion", randbytes )
1165 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001166 ret = handshake->tls_prf( session->master, 48, "key expansion",
1167 handshake->randbytes, 64, keyblk, 256 );
1168 if( ret != 0 )
1169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001171 return( ret );
1172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1175 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1176 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1177 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1178 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 /*
1181 * Determine the appropriate key, IV and MAC length.
1182 */
Paul Bakker68884e32013-01-07 18:20:04 +01001183
Hanno Becker88aaf652017-12-27 08:17:40 +00001184 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001185
Hanno Becker8031d062018-01-03 15:32:31 +00001186#if defined(MBEDTLS_GCM_C) || \
1187 defined(MBEDTLS_CCM_C) || \
1188 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001190 cipher_info->mode == MBEDTLS_MODE_CCM ||
1191 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001193 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001194
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001195 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001196 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001197 transform->taglen =
1198 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001199
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001200 /* All modes haves 96-bit IVs;
1201 * GCM and CCM has 4 implicit and 8 explicit bytes
1202 * ChachaPoly has all 12 bytes implicit
1203 */
Paul Bakker68884e32013-01-07 18:20:04 +01001204 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001205 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1206 transform->fixed_ivlen = 12;
1207 else
1208 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001209
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001210 /* Minimum length of encrypted record */
1211 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001212 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001213 }
1214 else
Hanno Becker8031d062018-01-03 15:32:31 +00001215#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1216#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1217 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1218 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001219 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001220 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1222 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001225 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001228 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001229 mac_key_len = mbedtls_md_get_size( md_info );
1230 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001233 /*
1234 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1235 * (rfc 6066 page 13 or rfc 2104 section 4),
1236 * so we only need to adjust the length here.
1237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001241
1242#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1243 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001244 * HMAC implementation which also truncates the key
1245 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001246 mac_key_len = transform->maclen;
1247#endif
1248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001250
1251 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001252 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001254 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001256 transform->minlen = transform->maclen;
1257 else
Paul Bakker68884e32013-01-07 18:20:04 +01001258 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001259 /*
1260 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001261 * 1. if EtM is in use: one block plus MAC
1262 * otherwise: * first multiple of blocklen greater than maclen
1263 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1266 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001267 {
1268 transform->minlen = transform->maclen
1269 + cipher_info->block_size;
1270 }
1271 else
1272#endif
1273 {
1274 transform->minlen = transform->maclen
1275 + cipher_info->block_size
1276 - transform->maclen % cipher_info->block_size;
1277 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1280 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1281 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001282 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001283 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1286 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1287 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001288 {
1289 transform->minlen += transform->ivlen;
1290 }
1291 else
1292#endif
1293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001295 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1296 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001297 }
Paul Bakker68884e32013-01-07 18:20:04 +01001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Hanno Becker8031d062018-01-03 15:32:31 +00001300 else
1301#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1302 {
1303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Hanno Becker88aaf652017-12-27 08:17:40 +00001307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1308 (unsigned) keylen,
1309 (unsigned) transform->minlen,
1310 (unsigned) transform->ivlen,
1311 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
1313 /*
1314 * Finally setup the cipher contexts, IVs and MAC secrets.
1315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001317 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001319 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001320 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Paul Bakker68884e32013-01-07 18:20:04 +01001322 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001323 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325 /*
1326 * This is not used in TLS v1.1.
1327 */
Paul Bakker48916f92012-09-16 19:57:18 +00001328 iv_copy_len = ( transform->fixed_ivlen ) ?
1329 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001330 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1331 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001332 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 }
1334 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#endif /* MBEDTLS_SSL_CLI_C */
1336#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001339 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001340 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Hanno Becker81c7b182017-11-09 18:39:33 +00001342 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001343 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001345 /*
1346 * This is not used in TLS v1.1.
1347 */
Paul Bakker48916f92012-09-16 19:57:18 +00001348 iv_copy_len = ( transform->fixed_ivlen ) ?
1349 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001350 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1351 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001352 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001358 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1359 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Hanno Beckerd56ed242018-01-03 15:32:51 +00001362#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_SSL_PROTO_SSL3)
1364 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001365 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001366 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001369 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1370 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001371 }
1372
Hanno Becker81c7b182017-11-09 18:39:33 +00001373 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1374 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001375 }
1376 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1378#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1379 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1380 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001381 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001382 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1383 For AEAD-based ciphersuites, there is nothing to do here. */
1384 if( mac_key_len != 0 )
1385 {
1386 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1387 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1388 }
Paul Bakker68884e32013-01-07 18:20:04 +01001389 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001390 else
1391#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001394 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1395 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001396 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001397#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1400 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001401 {
1402 int ret = 0;
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001405
Hanno Becker88aaf652017-12-27 08:17:40 +00001406 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001407 transform->iv_enc, transform->iv_dec,
1408 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001409 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001410 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001413 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1414 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001415 }
1416 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001417#else
1418 ((void) mac_dec);
1419 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001421
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001422#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1423 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001424 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001425 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1426 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001427 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001428 iv_copy_len );
1429 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001430
1431 if( ssl->conf->f_export_keys_ext != NULL )
1432 {
1433 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1434 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001435 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001436 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001437 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001438 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001439 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001440 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001441#endif
1442
Hanno Beckerf704bef2018-11-16 15:21:18 +00001443#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001444
1445 /* Only use PSA-based ciphers for TLS-1.2.
1446 * That's relevant at least for TLS-1.0, where
1447 * we assume that mbedtls_cipher_crypt() updates
1448 * the structure field for the IV, which the PSA-based
1449 * implementation currently doesn't. */
1450#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1451 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001452 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001453 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001454 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001455 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1456 {
1457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001458 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001459 }
1460
1461 if( ret == 0 )
1462 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001464 psa_fallthrough = 0;
1465 }
1466 else
1467 {
1468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1469 psa_fallthrough = 1;
1470 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001471 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001472 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001473 psa_fallthrough = 1;
1474#else
1475 psa_fallthrough = 1;
1476#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001477
Hanno Beckercb1cc802018-11-17 22:27:38 +00001478 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001479#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001480 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001481 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001484 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001485 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001486
Hanno Beckerf704bef2018-11-16 15:21:18 +00001487#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001488 /* Only use PSA-based ciphers for TLS-1.2.
1489 * That's relevant at least for TLS-1.0, where
1490 * we assume that mbedtls_cipher_crypt() updates
1491 * the structure field for the IV, which the PSA-based
1492 * implementation currently doesn't. */
1493#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1494 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001495 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001496 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001497 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001498 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1499 {
1500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001501 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001502 }
1503
1504 if( ret == 0 )
1505 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001507 psa_fallthrough = 0;
1508 }
1509 else
1510 {
1511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1512 psa_fallthrough = 1;
1513 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001514 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001515 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001516 psa_fallthrough = 1;
1517#else
1518 psa_fallthrough = 1;
1519#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001520
Hanno Beckercb1cc802018-11-17 22:27:38 +00001521 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001522#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001523 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001524 cipher_info ) ) != 0 )
1525 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001527 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001528 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001531 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001535 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001536 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001539 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001543 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_CIPHER_MODE_CBC)
1547 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1550 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001553 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001554 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1557 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001560 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001564
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001567 // Initialize compression
1568 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001570 {
Paul Bakker16770332013-10-11 09:59:44 +02001571 if( ssl->compress_buf == NULL )
1572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001574 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001575 if( ssl->compress_buf == NULL )
1576 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001578 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001579 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1580 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001581 }
1582 }
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001585
Paul Bakker48916f92012-09-16 19:57:18 +00001586 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1587 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001588
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001589 if( deflateInit( &transform->ctx_deflate,
1590 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001591 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001594 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1595 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596 }
1597 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001601end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001602 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1603 mbedtls_platform_zeroize( handshake->randbytes,
1604 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001605 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_SSL_PROTO_SSL3)
1609void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001610{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001611 mbedtls_md5_context md5;
1612 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 unsigned char pad_1[48];
1614 unsigned char pad_2[48];
1615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001618 mbedtls_md5_init( &md5 );
1619 mbedtls_sha1_init( &sha1 );
1620
1621 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1622 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Paul Bakker380da532012-04-18 16:10:25 +00001624 memset( pad_1, 0x36, 48 );
1625 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001627 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1628 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1629 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001631 mbedtls_md5_starts_ret( &md5 );
1632 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1633 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1634 mbedtls_md5_update_ret( &md5, hash, 16 );
1635 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001637 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1638 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1639 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001640
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001641 mbedtls_sha1_starts_ret( &sha1 );
1642 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1643 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1644 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1645 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001649
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001650 mbedtls_md5_free( &md5 );
1651 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001652
Paul Bakker380da532012-04-18 16:10:25 +00001653 return;
1654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1658void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001659{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001660 mbedtls_md5_context md5;
1661 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001664
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001665 mbedtls_md5_init( &md5 );
1666 mbedtls_sha1_init( &sha1 );
1667
1668 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1669 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001670
Andrzej Kurekeb342242019-01-29 09:14:33 -05001671 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001672 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001676
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001677 mbedtls_md5_free( &md5 );
1678 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001679
Paul Bakker380da532012-04-18 16:10:25 +00001680 return;
1681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1685#if defined(MBEDTLS_SHA256_C)
1686void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001687{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001688#if defined(MBEDTLS_USE_PSA_CRYPTO)
1689 size_t hash_size;
1690 psa_status_t status;
1691 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1692
1693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1694 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1695 if( status != PSA_SUCCESS )
1696 {
1697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1698 return;
1699 }
1700
1701 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1702 if( status != PSA_SUCCESS )
1703 {
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1705 return;
1706 }
1707 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1709#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001710 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001711
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001712 mbedtls_sha256_init( &sha256 );
1713
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001715
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001716 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001717 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001721
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001722 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001723#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001724 return;
1725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_SHA512_C)
1729void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001730{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001731#if defined(MBEDTLS_USE_PSA_CRYPTO)
1732 size_t hash_size;
1733 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001734 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001735
1736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001737 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001738 if( status != PSA_SUCCESS )
1739 {
1740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1741 return;
1742 }
1743
Andrzej Kurek972fba52019-01-30 03:29:12 -05001744 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001745 if( status != PSA_SUCCESS )
1746 {
1747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1748 return;
1749 }
1750 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1752#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001753 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001754
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001755 mbedtls_sha512_init( &sha512 );
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001759 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001760 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001765 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001766#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 return;
1768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#endif /* MBEDTLS_SHA512_C */
1770#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1773int 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 +02001774{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001775 unsigned char *p = ssl->handshake->premaster;
1776 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001777 const unsigned char *psk = ssl->conf->psk;
1778 size_t psk_len = ssl->conf->psk_len;
1779
1780 /* If the psk callback was called, use its result */
1781 if( ssl->handshake->psk != NULL )
1782 {
1783 psk = ssl->handshake->psk;
1784 psk_len = ssl->handshake->psk_len;
1785 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001786
1787 /*
1788 * PMS = struct {
1789 * opaque other_secret<0..2^16-1>;
1790 * opaque psk<0..2^16-1>;
1791 * };
1792 * with "other_secret" depending on the particular key exchange
1793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1795 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001796 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001797 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001799
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001800 *(p++) = (unsigned char)( psk_len >> 8 );
1801 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001802
1803 if( end < p || (size_t)( end - p ) < psk_len )
1804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1805
1806 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001807 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001808 }
1809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1811#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1812 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001813 {
1814 /*
1815 * other_secret already set by the ClientKeyExchange message,
1816 * and is 48 bytes long
1817 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001818 if( end - p < 2 )
1819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1820
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001821 *p++ = 0;
1822 *p++ = 48;
1823 p += 48;
1824 }
1825 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1827#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1828 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001829 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001830 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001831 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001832
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001833 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001835 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001836 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001839 return( ret );
1840 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001841 *(p++) = (unsigned char)( len >> 8 );
1842 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001843 p += len;
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001846 }
1847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1849#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1850 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001851 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001852 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853 size_t zlen;
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001856 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001857 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860 return( ret );
1861 }
1862
1863 *(p++) = (unsigned char)( zlen >> 8 );
1864 *(p++) = (unsigned char)( zlen );
1865 p += zlen;
1866
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001867 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1868 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 }
1870 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1874 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001875 }
1876
1877 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001878 if( end - p < 2 )
1879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001880
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001881 *(p++) = (unsigned char)( psk_len >> 8 );
1882 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001883
1884 if( end < p || (size_t)( end - p ) < psk_len )
1885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1886
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001887 memcpy( p, psk, psk_len );
1888 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001889
1890 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1891
1892 return( 0 );
1893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001897/*
1898 * SSLv3.0 MAC functions
1899 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001900#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001901static void ssl_mac( mbedtls_md_context_t *md_ctx,
1902 const unsigned char *secret,
1903 const unsigned char *buf, size_t len,
1904 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001905 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906{
1907 unsigned char header[11];
1908 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001909 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1911 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001912
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001913 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001915 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001916 else
Paul Bakker68884e32013-01-07 18:20:04 +01001917 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 memcpy( header, ctr, 8 );
1920 header[ 8] = (unsigned char) type;
1921 header[ 9] = (unsigned char)( len >> 8 );
1922 header[10] = (unsigned char)( len );
1923
Paul Bakker68884e32013-01-07 18:20:04 +01001924 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_md_starts( md_ctx );
1926 mbedtls_md_update( md_ctx, secret, md_size );
1927 mbedtls_md_update( md_ctx, padding, padlen );
1928 mbedtls_md_update( md_ctx, header, 11 );
1929 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001930 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Paul Bakker68884e32013-01-07 18:20:04 +01001932 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 mbedtls_md_starts( md_ctx );
1934 mbedtls_md_update( md_ctx, secret, md_size );
1935 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001936 mbedtls_md_update( md_ctx, out, md_size );
1937 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001940
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001941/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001942 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001943#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001944 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1945 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1946 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1947/* This function makes sure every byte in the memory region is accessed
1948 * (in ascending addresses order) */
1949static void ssl_read_memory( unsigned char *p, size_t len )
1950{
1951 unsigned char acc = 0;
1952 volatile unsigned char force;
1953
1954 for( ; len != 0; p++, len-- )
1955 acc ^= *p;
1956
1957 force = acc;
1958 (void) force;
1959}
1960#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1961
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001962/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001964 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001965
1966static void ssl_extract_add_data_from_record( unsigned char* add_data,
1967 mbedtls_record *rec )
1968{
1969 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1970 add_data[8] = rec->type;
1971 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1972 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1973 add_data[12] = rec->data_len & 0xFF;
1974}
1975
Hanno Beckera18d1322018-01-03 14:27:32 +00001976int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1977 mbedtls_ssl_transform *transform,
1978 mbedtls_record *rec,
1979 int (*f_rng)(void *, unsigned char *, size_t),
1980 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001984 unsigned char * data;
1985 unsigned char add_data[13];
1986 size_t post_avail;
1987
1988 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001989#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001990 ((void) ssl);
1991#endif
1992
1993 /* The PRNG is used for dynamic IV generation that's used
1994 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1995#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1996 ( defined(MBEDTLS_AES_C) || \
1997 defined(MBEDTLS_ARIA_C) || \
1998 defined(MBEDTLS_CAMELLIA_C) ) && \
1999 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2000 ((void) f_rng);
2001 ((void) p_rng);
2002#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002006 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002007 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2009 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2010 }
2011 if( rec == NULL ||
2012 rec->buf == NULL ||
2013 rec->buf_len < rec->data_offset ||
2014 rec->buf_len - rec->data_offset < rec->data_len )
2015 {
2016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002018 }
2019
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002020 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2021 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002023 data, rec->data_len );
2024
2025 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2026
2027 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2028 {
2029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2030 (unsigned) rec->data_len,
2031 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2033 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002036 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 */
Hanno Becker52344c22018-01-03 15:24:20 +00002038#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 if( mode == MBEDTLS_MODE_STREAM ||
2040 ( mode == MBEDTLS_MODE_CBC
2041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002042 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002043#endif
2044 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002046 if( post_avail < transform->maclen )
2047 {
2048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2049 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2050 }
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002053 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002054 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002055 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002056 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2057 data, rec->data_len, rec->ctr, rec->type, mac );
2058 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002059 }
2060 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2063 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002064 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002065 {
Hanno Becker992b6872017-11-09 18:57:39 +00002066 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2067
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002068 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002069
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002070 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2071 sizeof( add_data ) );
2072 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2073 data, rec->data_len );
2074 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2075 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2076
2077 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002078 }
2079 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002080#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002084 }
2085
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002086 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2087 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002088
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002089 rec->data_len += transform->maclen;
2090 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002091 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002092 }
Hanno Becker52344c22018-01-03 15:24:20 +00002093#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002095 /*
2096 * Encrypt
2097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2099 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002101 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002104 "including %d bytes of padding",
2105 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002106
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002107 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2108 transform->iv_enc, transform->ivlen,
2109 data, rec->data_len,
2110 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002113 return( ret );
2114 }
2115
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002116 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002120 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 }
Paul Bakker68884e32013-01-07 18:20:04 +01002122 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002124
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002125#if defined(MBEDTLS_GCM_C) || \
2126 defined(MBEDTLS_CCM_C) || \
2127 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002129 mode == MBEDTLS_MODE_CCM ||
2130 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002131 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002132 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002133 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002135
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 /* Check that there's space for both the authentication tag
2137 * and the explicit IV before and after the record content. */
2138 if( post_avail < transform->taglen ||
2139 rec->data_offset < explicit_iv_len )
2140 {
2141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2142 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2143 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002144
Paul Bakker68884e32013-01-07 18:20:04 +01002145 /*
2146 * Generate IV
2147 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002148 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2149 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002150 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002151 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002152 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2153 explicit_iv_len );
2154 /* Prefix record content with explicit IV. */
2155 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002156 }
2157 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2158 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002159 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002160 unsigned char i;
2161
2162 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2163
2164 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002165 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002166 }
2167 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002168 {
2169 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002172 }
2173
Hanno Becker1f10d762019-04-26 13:34:37 +01002174 ssl_extract_add_data_from_record( add_data, rec );
2175
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002176 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2177 iv, transform->ivlen );
2178 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179 data - explicit_iv_len, explicit_iv_len );
2180 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2181 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002183 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002184 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002185
Paul Bakker68884e32013-01-07 18:20:04 +01002186 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002187 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002188 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002189
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002190 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002191 iv, transform->ivlen,
2192 add_data, 13, /* add data */
2193 data, rec->data_len, /* source */
2194 data, &rec->data_len, /* destination */
2195 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002198 return( ret );
2199 }
2200
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002201 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2202 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002203
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002204 rec->data_len += transform->taglen + explicit_iv_len;
2205 rec->data_offset -= explicit_iv_len;
2206 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002207 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002208 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2211#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002212 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002215 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 size_t padlen, i;
2217 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002218
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 /* Currently we're always using minimal padding
2220 * (up to 255 bytes would be allowed). */
2221 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2222 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 padlen = 0;
2224
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 /* Check there's enough space in the buffer for the padding. */
2226 if( post_avail < padlen + 1 )
2227 {
2228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2229 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2230 }
2231
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 rec->data_len += padlen + 1;
2236 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002239 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002240 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2241 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002242 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002244 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 if( f_rng == NULL )
2246 {
2247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2249 }
2250
2251 if( rec->data_offset < transform->ivlen )
2252 {
2253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2254 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2255 }
2256
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002257 /*
2258 * Generate IV
2259 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002261 if( ret != 0 )
2262 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002263
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264 memcpy( data - transform->ivlen, transform->iv_enc,
2265 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002266
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002267 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002271 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002272 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002273 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002275 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2276 transform->iv_enc,
2277 transform->ivlen,
2278 data, rec->data_len,
2279 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002282 return( ret );
2283 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002284
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002285 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002289 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002292 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002293 {
2294 /*
2295 * Save IV in SSL3 and TLS1
2296 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002297 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2298 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002300 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002301#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002302 {
2303 data -= transform->ivlen;
2304 rec->data_offset -= transform->ivlen;
2305 rec->data_len += transform->ivlen;
2306 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002309 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002310 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002311 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2312
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002313 /*
2314 * MAC(MAC_write_key, seq_num +
2315 * TLSCipherText.type +
2316 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002317 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002318 * IV + // except for TLS 1.0
2319 * ENC(content + padding + padding_length));
2320 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002321
2322 if( post_avail < transform->maclen)
2323 {
2324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2325 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2326 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002327
Hanno Becker1f10d762019-04-26 13:34:37 +01002328 ssl_extract_add_data_from_record( add_data, rec );
2329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2332 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002333
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2335 sizeof( add_data ) );
2336 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2337 data, rec->data_len );
2338 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2339 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002340
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002341 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002342
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343 rec->data_len += transform->maclen;
2344 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002345 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002349 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002351 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002357 /* Make extra sure authentication was performed, exactly once */
2358 if( auth_done != 1 )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2361 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002362 }
2363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
2366 return( 0 );
2367}
2368
Hanno Beckera18d1322018-01-03 14:27:32 +00002369int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2370 mbedtls_ssl_transform *transform,
2371 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002372{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002373 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002375 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002376#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002377 size_t padlen = 0, correct = 1;
2378#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002379 unsigned char* data;
2380 unsigned char add_data[13];
2381
Hanno Beckera18d1322018-01-03 14:27:32 +00002382#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002383 ((void) ssl);
2384#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002387 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002388 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2390 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2391 }
2392 if( rec == NULL ||
2393 rec->buf == NULL ||
2394 rec->buf_len < rec->data_offset ||
2395 rec->buf_len - rec->data_offset < rec->data_len )
2396 {
2397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002399 }
2400
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002401 data = rec->buf + rec->data_offset;
2402 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2405 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002406 {
2407 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002408 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2409 transform->iv_dec,
2410 transform->ivlen,
2411 data, rec->data_len,
2412 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002415 return( ret );
2416 }
2417
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002418 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
Paul Bakker68884e32013-01-07 18:20:04 +01002424 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002426#if defined(MBEDTLS_GCM_C) || \
2427 defined(MBEDTLS_CCM_C) || \
2428 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002430 mode == MBEDTLS_MODE_CCM ||
2431 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002432 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002433 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002434 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002436 /*
2437 * Compute and update sizes
2438 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002439 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002442 "+ taglen (%d)", rec->data_len,
2443 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002445 }
Paul Bakker68884e32013-01-07 18:20:04 +01002446
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002447 /*
2448 * Prepare IV
2449 */
2450 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2451 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002452 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002453 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002454 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002455
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002456 }
2457 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2458 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002459 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002460 unsigned char i;
2461
2462 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2463
2464 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002465 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002466 }
2467 else
2468 {
2469 /* Reminder if we ever add an AEAD mode with a different size */
2470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2472 }
2473
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002474 data += explicit_iv_len;
2475 rec->data_offset += explicit_iv_len;
2476 rec->data_len -= explicit_iv_len + transform->taglen;
2477
2478 ssl_extract_add_data_from_record( add_data, rec );
2479 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2480 add_data, 13 );
2481
2482 memcpy( transform->iv_dec + transform->fixed_ivlen,
2483 data - explicit_iv_len, explicit_iv_len );
2484
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002485 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002486 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002487 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002488
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002489
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002490 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002491 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002492 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002493 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2494 iv, transform->ivlen,
2495 add_data, 13,
2496 data, rec->data_len,
2497 data, &olen,
2498 data + rec->data_len,
2499 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2504 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002505
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002506 return( ret );
2507 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002508 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002509
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002510 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002514 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2518#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002519 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002522 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 /*
Paul Bakker45829992013-01-03 14:52:21 +01002525 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002528 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2529 {
2530 /* The ciphertext is prefixed with the CBC IV. */
2531 minlen += transform->ivlen;
2532 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif
Paul Bakker45829992013-01-03 14:52:21 +01002534
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002535 /* Size considerations:
2536 *
2537 * - The CBC cipher text must not be empty and hence
2538 * at least of size transform->ivlen.
2539 *
2540 * Together with the potential IV-prefix, this explains
2541 * the first of the two checks below.
2542 *
2543 * - The record must contain a MAC, either in plain or
2544 * encrypted, depending on whether Encrypt-then-MAC
2545 * is used or not.
2546 * - If it is, the message contains the IV-prefix,
2547 * the CBC ciphertext, and the MAC.
2548 * - If it is not, the padded plaintext, and hence
2549 * the CBC ciphertext, has at least length maclen + 1
2550 * because there is at least the padding length byte.
2551 *
2552 * As the CBC ciphertext is not empty, both cases give the
2553 * lower bound minlen + maclen + 1 on the record size, which
2554 * we test for in the second check below.
2555 */
2556 if( rec->data_len < minlen + transform->ivlen ||
2557 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002560 "+ 1 ) ( + expl IV )", rec->data_len,
2561 transform->ivlen,
2562 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002564 }
2565
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002566 /*
2567 * Authenticate before decrypt if enabled
2568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002570 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002571 {
Hanno Becker992b6872017-11-09 18:57:39 +00002572 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002575
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002576 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2577 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002578
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002579 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002580
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002581 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2582 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2583 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2584 data, rec->data_len );
2585 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2586 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002587
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002588 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2589 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002590 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002591 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002592
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2594 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002598 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002599 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002602
2603 /*
2604 * Check length sanity
2605 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002606 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002609 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002611 }
2612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002614 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002615 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002616 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002618 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002619 /* This is safe because data_len >= minlen + maclen + 1 initially,
2620 * and at this point we have at most subtracted maclen (note that
2621 * minlen == transform->ivlen here). */
2622 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002623
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002624 data += transform->ivlen;
2625 rec->data_offset += transform->ivlen;
2626 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002629
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002630 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2631 transform->iv_dec, transform->ivlen,
2632 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002635 return( ret );
2636 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002637
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002642 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002645 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002646 {
2647 /*
2648 * Save IV in SSL3 and TLS1
2649 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002650 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2651 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002655 /* Safe since data_len >= minlen + maclen + 1, so after having
2656 * subtracted at most minlen and maclen up to this point,
2657 * data_len > 0. */
2658 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002659
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660 if( auth_done == 1 )
2661 {
2662 correct *= ( rec->data_len >= padlen + 1 );
2663 padlen *= ( rec->data_len >= padlen + 1 );
2664 }
2665 else
Paul Bakker45829992013-01-03 14:52:21 +01002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 if( rec->data_len < transform->maclen + padlen + 1 )
2669 {
2670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2671 rec->data_len,
2672 transform->maclen,
2673 padlen + 1 ) );
2674 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002675#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676
2677 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2678 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681 padlen++;
2682
2683 /* Regardless of the validity of the padding,
2684 * we have data_len >= padlen here. */
2685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002687 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_DEBUG_ALL)
2692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002693 "should be no more than %d",
2694 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002695#endif
Paul Bakker45829992013-01-03 14:52:21 +01002696 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698 }
2699 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2701#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2702 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002705 /* The padding check involves a series of up to 256
2706 * consecutive memory reads at the end of the record
2707 * plaintext buffer. In order to hide the length and
2708 * validity of the padding, always perform exactly
2709 * `min(256,plaintext_len)` reads (but take into account
2710 * only the last `padlen` bytes for the padding check). */
2711 size_t pad_count = 0;
2712 size_t real_count = 0;
2713 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002714
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002715 /* Index of first padding byte; it has been ensured above
2716 * that the subtraction is safe. */
2717 size_t const padding_idx = rec->data_len - padlen;
2718 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2719 size_t const start_idx = rec->data_len - num_checks;
2720 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002721
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002722 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002723 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 real_count |= ( idx >= padding_idx );
2725 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002726 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002730 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002732#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002733 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002735 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2737 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002741 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002742
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 /* If the padding was found to be invalid, padlen == 0
2744 * and the subtraction is safe. If the padding was found valid,
2745 * padlen hasn't been changed and the previous assertion
2746 * data_len >= padlen still holds. */
2747 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002749 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002751 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002757#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002760#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
2762 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002763 * Authenticate if not done yet.
2764 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 */
Hanno Becker52344c22018-01-03 15:24:20 +00002766#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002767 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002768 {
Hanno Becker992b6872017-11-09 18:57:39 +00002769 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002770
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 /* If the initial value of padlen was such that
2772 * data_len < maclen + padlen + 1, then padlen
2773 * got reset to 1, and the initial check
2774 * data_len >= minlen + maclen + 1
2775 * guarantees that at this point we still
2776 * have at least data_len >= maclen.
2777 *
2778 * If the initial value of padlen was such that
2779 * data_len >= maclen + padlen + 1, then we have
2780 * subtracted either padlen + 1 (if the padding was correct)
2781 * or 0 (if the padding was incorrect) since then,
2782 * hence data_len >= maclen in any case.
2783 */
2784 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002790 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 ssl_mac( &transform->md_ctx_dec,
2792 transform->mac_dec,
2793 data, rec->data_len,
2794 rec->ctr, rec->type,
2795 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002796 }
2797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2799#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2800 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002802 {
2803 /*
2804 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002805 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002806 *
2807 * Known timing attacks:
2808 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2809 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002810 * To compensate for different timings for the MAC calculation
2811 * depending on how much padding was removed (which is determined
2812 * by padlen), process extra_run more blocks through the hash
2813 * function.
2814 *
2815 * The formula in the paper is
2816 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2817 * where L1 is the size of the header plus the decrypted message
2818 * plus CBC padding and L2 is the size of the header plus the
2819 * decrypted message. This is for an underlying hash function
2820 * with 64-byte blocks.
2821 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2822 * correctly. We round down instead of up, so -56 is the correct
2823 * value for our calculations instead of -55.
2824 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002825 * Repeat the formula rather than defining a block_size variable.
2826 * This avoids requiring division by a variable at runtime
2827 * (which would be marginally less efficient and would require
2828 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 */
2830 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002832
2833 /*
2834 * The next two sizes are the minimum and maximum values of
2835 * in_msglen over all padlen values.
2836 *
2837 * They're independent of padlen, since we previously did
2838 * in_msglen -= padlen.
2839 *
2840 * Note that max_len + maclen is never more than the buffer
2841 * length, as we previously did in_msglen -= maclen too.
2842 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002844 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2845
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 memset( tmp, 0, sizeof( tmp ) );
2847
2848 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002849 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002850#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2851 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002852 case MBEDTLS_MD_MD5:
2853 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002854 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002855 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2857 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002858 break;
2859#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002860#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002861 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002862 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2864 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002865 break;
2866#endif
2867 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2870 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002871
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002872 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002873
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2875 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2876 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002877 /* Make sure we access everything even when padlen > 0. This
2878 * makes the synchronisation requirements for just-in-time
2879 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 ssl_read_memory( data + rec->data_len, padlen );
2881 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002882
2883 /* Call mbedtls_md_process at least once due to cache attacks
2884 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002885 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002887
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002889
2890 /* Make sure we access all the memory that could contain the MAC,
2891 * before we check it in the next code block. This makes the
2892 * synchronisation requirements for just-in-time Prime+Probe
2893 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 ssl_read_memory( data + min_len,
2895 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002896 }
2897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2899 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002905#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2907 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002908#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002910 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2911 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#if defined(MBEDTLS_SSL_DEBUG_ALL)
2914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002915#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002916 correct = 0;
2917 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002918 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002919 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002920
2921 /*
2922 * Finally check the correct flag
2923 */
2924 if( correct == 0 )
2925 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002926#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002927
2928 /* Make extra sure authentication was performed, exactly once */
2929 if( auth_done != 1 )
2930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
2937 return( 0 );
2938}
2939
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002940#undef MAC_NONE
2941#undef MAC_PLAINTEXT
2942#undef MAC_CIPHERTEXT
2943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945/*
2946 * Compression/decompression functions
2947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949{
2950 int ret;
2951 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002952 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002954 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002958 if( len_pre == 0 )
2959 return( 0 );
2960
Paul Bakker2770fbd2012-07-03 13:30:23 +00002961 memcpy( msg_pre, ssl->out_msg, len_pre );
2962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002964 ssl->out_msglen ) );
2965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967 ssl->out_msg, ssl->out_msglen );
2968
Paul Bakker48916f92012-09-16 19:57:18 +00002969 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2970 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2971 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002972 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002973
Paul Bakker48916f92012-09-16 19:57:18 +00002974 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975 if( ret != Z_OK )
2976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2978 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002979 }
2980
Angus Grattond8213d02016-05-25 20:56:48 +10002981 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002982 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002985 ssl->out_msglen ) );
2986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002988 ssl->out_msg, ssl->out_msglen );
2989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002991
2992 return( 0 );
2993}
2994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002996{
2997 int ret;
2998 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002999 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003000 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003001 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003004
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003005 if( len_pre == 0 )
3006 return( 0 );
3007
Paul Bakker2770fbd2012-07-03 13:30:23 +00003008 memcpy( msg_pre, ssl->in_msg, len_pre );
3009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003011 ssl->in_msglen ) );
3012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003014 ssl->in_msg, ssl->in_msglen );
3015
Paul Bakker48916f92012-09-16 19:57:18 +00003016 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3017 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3018 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003019 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003020 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003021
Paul Bakker48916f92012-09-16 19:57:18 +00003022 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003023 if( ret != Z_OK )
3024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3026 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003027 }
3028
Angus Grattond8213d02016-05-25 20:56:48 +10003029 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003030 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003033 ssl->in_msglen ) );
3034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003036 ssl->in_msg, ssl->in_msglen );
3037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003039
3040 return( 0 );
3041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3045static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047#if defined(MBEDTLS_SSL_PROTO_DTLS)
3048static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003049{
3050 /* If renegotiation is not enforced, retransmit until we would reach max
3051 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003052 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003053 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003054 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003055 unsigned char doublings = 1;
3056
3057 while( ratio != 0 )
3058 {
3059 ++doublings;
3060 ratio >>= 1;
3061 }
3062
3063 if( ++ssl->renego_records_seen > doublings )
3064 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003066 return( 0 );
3067 }
3068 }
3069
3070 return( ssl_write_hello_request( ssl ) );
3071}
3072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003074
Paul Bakker5121ce52009-01-03 21:22:43 +00003075/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003076 * Fill the input message buffer by appending data to it.
3077 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003078 *
3079 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3080 * available (from this read and/or a previous one). Otherwise, an error code
3081 * is returned (possibly EOF or WANT_READ).
3082 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003083 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3084 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3085 * since we always read a whole datagram at once.
3086 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003087 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003088 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003091{
Paul Bakker23986e52011-04-24 08:57:21 +00003092 int ret;
3093 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003097 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003100 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003102 }
3103
Angus Grattond8213d02016-05-25 20:56:48 +10003104 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003108 }
3109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003113 uint32_t timeout;
3114
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003115 /* Just to be sure */
3116 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3117 {
3118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3119 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3121 }
3122
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003123 /*
3124 * The point is, we need to always read a full datagram at once, so we
3125 * sometimes read more then requested, and handle the additional data.
3126 * It could be the rest of the current record (while fetching the
3127 * header) and/or some other records in the same datagram.
3128 */
3129
3130 /*
3131 * Move to the next record in the already read datagram if applicable
3132 */
3133 if( ssl->next_record_offset != 0 )
3134 {
3135 if( ssl->in_left < ssl->next_record_offset )
3136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003139 }
3140
3141 ssl->in_left -= ssl->next_record_offset;
3142
3143 if( ssl->in_left != 0 )
3144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003146 ssl->next_record_offset ) );
3147 memmove( ssl->in_hdr,
3148 ssl->in_hdr + ssl->next_record_offset,
3149 ssl->in_left );
3150 }
3151
3152 ssl->next_record_offset = 0;
3153 }
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003156 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003157
3158 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003159 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003160 */
3161 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003164 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003165 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003166
3167 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003168 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003169 * are not at the beginning of a new record, the caller did something
3170 * wrong.
3171 */
3172 if( ssl->in_left != 0 )
3173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003176 }
3177
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003178 /*
3179 * Don't even try to read if time's out already.
3180 * This avoids by-passing the timer when repeatedly receiving messages
3181 * that will end up being dropped.
3182 */
3183 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003184 {
3185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003186 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003187 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003188 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003189 {
Angus Grattond8213d02016-05-25 20:56:48 +10003190 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003193 timeout = ssl->handshake->retransmit_timeout;
3194 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003195 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003198
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003199 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003200 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3201 timeout );
3202 else
3203 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003206
3207 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003209 }
3210
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003211 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003214 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003217 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003218 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003221 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003222 }
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003227 return( ret );
3228 }
3229
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003230 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003233 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003235 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003236 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003239 return( ret );
3240 }
3241
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003242 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003245 }
3246
Paul Bakker5121ce52009-01-03 21:22:43 +00003247 if( ret < 0 )
3248 return( ret );
3249
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003250 ssl->in_left = ret;
3251 }
3252 else
3253#endif
3254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003256 ssl->in_left, nb_want ) );
3257
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003258 while( ssl->in_left < nb_want )
3259 {
3260 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003261
3262 if( ssl_check_timer( ssl ) != 0 )
3263 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3264 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003265 {
3266 if( ssl->f_recv_timeout != NULL )
3267 {
3268 ret = ssl->f_recv_timeout( ssl->p_bio,
3269 ssl->in_hdr + ssl->in_left, len,
3270 ssl->conf->read_timeout );
3271 }
3272 else
3273 {
3274 ret = ssl->f_recv( ssl->p_bio,
3275 ssl->in_hdr + ssl->in_left, len );
3276 }
3277 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003280 ssl->in_left, nb_want ) );
3281 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003282
3283 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003285
3286 if( ret < 0 )
3287 return( ret );
3288
mohammad160352aecb92018-03-28 23:41:40 -07003289 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003290 {
Darryl Green11999bb2018-03-13 15:22:58 +00003291 MBEDTLS_SSL_DEBUG_MSG( 1,
3292 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003293 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3295 }
3296
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003297 ssl->in_left += ret;
3298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 }
3300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
3303 return( 0 );
3304}
3305
3306/*
3307 * Flush any data not yet written
3308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003310{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003311 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003312 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003316 if( ssl->f_send == NULL )
3317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003319 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003321 }
3322
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003323 /* Avoid incrementing counter if data is flushed */
3324 if( ssl->out_left == 0 )
3325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003327 return( 0 );
3328 }
3329
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 while( ssl->out_left > 0 )
3331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3333 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Hanno Becker2b1e3542018-08-06 11:19:13 +01003335 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003336 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
3340 if( ret <= 0 )
3341 return( ret );
3342
mohammad160352aecb92018-03-28 23:41:40 -07003343 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003344 {
Darryl Green11999bb2018-03-13 15:22:58 +00003345 MBEDTLS_SSL_DEBUG_MSG( 1,
3346 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003347 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3349 }
3350
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 ssl->out_left -= ret;
3352 }
3353
Hanno Becker2b1e3542018-08-06 11:19:13 +01003354#if defined(MBEDTLS_SSL_PROTO_DTLS)
3355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003356 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003357 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003358 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003359 else
3360#endif
3361 {
3362 ssl->out_hdr = ssl->out_buf + 8;
3363 }
3364 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
3368 return( 0 );
3369}
3370
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003371/*
3372 * Functions to handle the DTLS retransmission state machine
3373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375/*
3376 * Append current handshake message to current outgoing flight
3377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3382 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3383 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384
3385 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003386 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003387 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003390 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003391 }
3392
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003393 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003394 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003398 }
3399
3400 /* Copy current handshake message with headers */
3401 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3402 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003403 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003404 msg->next = NULL;
3405
3406 /* Append to the current flight */
3407 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003408 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003409 else
3410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003412 while( cur->next != NULL )
3413 cur = cur->next;
3414 cur->next = msg;
3415 }
3416
Hanno Becker3b235902018-08-06 09:54:53 +01003417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003418 return( 0 );
3419}
3420
3421/*
3422 * Free the current flight of handshake messages
3423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 mbedtls_ssl_flight_item *cur = flight;
3427 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003428
3429 while( cur != NULL )
3430 {
3431 next = cur->next;
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 mbedtls_free( cur->p );
3434 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003435
3436 cur = next;
3437 }
3438}
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3441static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003442#endif
3443
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003444/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 * Swap transform_out and out_ctr with the alternative ones
3446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003450 unsigned char tmp_out_ctr[8];
3451
3452 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003455 return;
3456 }
3457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003460 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 tmp_transform = ssl->transform_out;
3462 ssl->transform_out = ssl->handshake->alt_transform_out;
3463 ssl->handshake->alt_transform_out = tmp_transform;
3464
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003465 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003466 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3467 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003468 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003469
3470 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003471 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3474 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3479 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003480 }
3481 }
3482#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003483}
3484
3485/*
3486 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003487 */
3488int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3489{
3490 int ret = 0;
3491
3492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3493
3494 ret = mbedtls_ssl_flight_transmit( ssl );
3495
3496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3497
3498 return( ret );
3499}
3500
3501/*
3502 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503 *
3504 * Need to remember the current message in case flush_output returns
3505 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003506 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003507 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003508int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003509{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003514 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003516
3517 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003518 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003519 ssl_swap_epochs( ssl );
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003522 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003523
3524 while( ssl->handshake->cur_msg != NULL )
3525 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003526 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003527 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003528
Hanno Beckere1dcb032018-08-17 16:47:58 +01003529 int const is_finished =
3530 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3531 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3532
Hanno Becker04da1892018-08-14 13:22:10 +01003533 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3534 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3535
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003536 /* Swap epochs before sending Finished: we can't do it after
3537 * sending ChangeCipherSpec, in case write returns WANT_READ.
3538 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003539 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003540 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003542 ssl_swap_epochs( ssl );
3543 }
3544
Hanno Becker67bc7c32018-08-06 11:33:50 +01003545 ret = ssl_get_remaining_payload_in_datagram( ssl );
3546 if( ret < 0 )
3547 return( ret );
3548 max_frag_len = (size_t) ret;
3549
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003550 /* CCS is copied as is, while HS messages may need fragmentation */
3551 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3552 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 if( max_frag_len == 0 )
3554 {
3555 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3556 return( ret );
3557
3558 continue;
3559 }
3560
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003561 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003562 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003563 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003564
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003565 /* Update position inside current message */
3566 ssl->handshake->cur_msg_p += cur->len;
3567 }
3568 else
3569 {
3570 const unsigned char * const p = ssl->handshake->cur_msg_p;
3571 const size_t hs_len = cur->len - 12;
3572 const size_t frag_off = p - ( cur->p + 12 );
3573 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003574 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575
Hanno Beckere1dcb032018-08-17 16:47:58 +01003576 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003577 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003578 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003579 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580
Hanno Becker67bc7c32018-08-06 11:33:50 +01003581 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3582 return( ret );
3583
3584 continue;
3585 }
3586 max_hs_frag_len = max_frag_len - 12;
3587
3588 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3589 max_hs_frag_len : rem_len;
3590
3591 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003592 {
3593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003594 (unsigned) cur_hs_frag_len,
3595 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003596 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003597
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003598 /* Messages are stored with handshake headers as if not fragmented,
3599 * copy beginning of headers then fill fragmentation fields.
3600 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3601 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003602
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003603 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3604 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3605 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3606
Hanno Becker67bc7c32018-08-06 11:33:50 +01003607 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3608 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3609 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003610
3611 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3612
Hanno Becker3f7b9732018-08-28 09:53:25 +01003613 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003614 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3615 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003616 ssl->out_msgtype = cur->type;
3617
3618 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003619 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003620 }
3621
3622 /* If done with the current message move to the next one if any */
3623 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3624 {
3625 if( cur->next != NULL )
3626 {
3627 ssl->handshake->cur_msg = cur->next;
3628 ssl->handshake->cur_msg_p = cur->next->p + 12;
3629 }
3630 else
3631 {
3632 ssl->handshake->cur_msg = NULL;
3633 ssl->handshake->cur_msg_p = NULL;
3634 }
3635 }
3636
3637 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003638 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641 return( ret );
3642 }
3643 }
3644
Hanno Becker67bc7c32018-08-06 11:33:50 +01003645 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3646 return( ret );
3647
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003648 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3650 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003651 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003654 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3655 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003656
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658
3659 return( 0 );
3660}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661
3662/*
3663 * To be called when the last message of an incoming flight is received.
3664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003666{
3667 /* We won't need to resend that one any more */
3668 ssl_flight_free( ssl->handshake->flight );
3669 ssl->handshake->flight = NULL;
3670 ssl->handshake->cur_msg = NULL;
3671
3672 /* The next incoming flight will start with this msg_seq */
3673 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3674
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003675 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003676 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003677
Hanno Becker0271f962018-08-16 13:23:47 +01003678 /* Clear future message buffering structure. */
3679 ssl_buffering_free( ssl );
3680
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003681 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003682 ssl_set_timer( ssl, 0 );
3683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3685 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003688 }
3689 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003691}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003692
3693/*
3694 * To be called when the last message of an outgoing flight is send.
3695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003697{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003698 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003699 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3702 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003705 }
3706 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003710
Paul Bakker5121ce52009-01-03 21:22:43 +00003711/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003712 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003713 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714
3715/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003716 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003717 *
3718 * - fill in handshake headers
3719 * - update handshake checksum
3720 * - DTLS: save message for resending
3721 * - then pass to the record layer
3722 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003723 * DTLS: except for HelloRequest, messages are only queued, and will only be
3724 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003725 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003726 * Inputs:
3727 * - ssl->out_msglen: 4 + actual handshake message len
3728 * (4 is the size of handshake headers for TLS)
3729 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3730 * - ssl->out_msg + 4: the handshake message body
3731 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003732 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003733 * - ssl->out_msglen: the length of the record contents
3734 * (including handshake headers but excluding record headers)
3735 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003736 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003737int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003738{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 int ret;
3740 const size_t hs_len = ssl->out_msglen - 4;
3741 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3744
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 /*
3746 * Sanity checks
3747 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003748 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003749 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3750 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003751 /* In SSLv3, the client might send a NoCertificate alert. */
3752#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3753 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3754 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3755 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3756#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3757 {
3758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3760 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003761 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003762
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003763 /* Whenever we send anything different from a
3764 * HelloRequest we should be in a handshake - double check. */
3765 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3766 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003767 ssl->handshake == NULL )
3768 {
3769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3771 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003775 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003780 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003782
Hanno Beckerb50a2532018-08-06 11:52:54 +01003783 /* Double-check that we did not exceed the bounds
3784 * of the outgoing record buffer.
3785 * This should never fail as the various message
3786 * writing functions must obey the bounds of the
3787 * outgoing record buffer, but better be safe.
3788 *
3789 * Note: We deliberately do not check for the MTU or MFL here.
3790 */
3791 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3794 "size %u, maximum %u",
3795 (unsigned) ssl->out_msglen,
3796 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3797 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3798 }
3799
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003800 /*
3801 * Fill handshake headers
3802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003805 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3806 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3807 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003808
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003809 /*
3810 * DTLS has additional fields in the Handshake layer,
3811 * between the length field and the actual payload:
3812 * uint16 message_seq;
3813 * uint24 fragment_offset;
3814 * uint24 fragment_length;
3815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003817 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003818 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003819 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003820 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003821 {
3822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3823 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003824 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003825 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3827 }
3828
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003829 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003830 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003831
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003832 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003833 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003834 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003835 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3836 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3837 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003838 }
3839 else
3840 {
3841 ssl->out_msg[4] = 0;
3842 ssl->out_msg[5] = 0;
3843 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003844
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003845 /* Handshake hashes are computed without fragmentation,
3846 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003847 memset( ssl->out_msg + 6, 0x00, 3 );
3848 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003851
Hanno Becker0207e532018-08-28 10:28:28 +01003852 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003853 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3854 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003855 }
3856
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003857 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003859 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003860 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3861 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003862 {
3863 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003866 return( ret );
3867 }
3868 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003869 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003870#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003871 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003872 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003873 {
3874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3875 return( ret );
3876 }
3877 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003878
3879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3880
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003881 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003882}
3883
3884/*
3885 * Record layer functions
3886 */
3887
3888/*
3889 * Write current record.
3890 *
3891 * Uses:
3892 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3893 * - ssl->out_msglen: length of the record content (excl headers)
3894 * - ssl->out_msg: record content
3895 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003896int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003897{
3898 int ret, done = 0;
3899 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003900 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003901
3902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003905 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003907 {
3908 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003911 return( ret );
3912 }
3913
3914 len = ssl->out_msglen;
3915 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3919 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 ret = mbedtls_ssl_hw_record_write( ssl );
3924 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3927 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003928 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003929
3930 if( ret == 0 )
3931 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003932 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003934 if( !done )
3935 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003936 unsigned i;
3937 size_t protected_record_size;
3938
Paul Bakker05ef8352012-05-08 09:17:57 +00003939 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003941 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003942
Hanno Becker19859472018-08-06 09:40:20 +01003943 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003944 ssl->out_len[0] = (unsigned char)( len >> 8 );
3945 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003946
Paul Bakker48916f92012-09-16 19:57:18 +00003947 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003948 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003949 mbedtls_record rec;
3950
3951 rec.buf = ssl->out_iv;
3952 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3953 ( ssl->out_iv - ssl->out_buf );
3954 rec.data_len = ssl->out_msglen;
3955 rec.data_offset = ssl->out_msg - rec.buf;
3956
3957 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3958 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3959 ssl->conf->transport, rec.ver );
3960 rec.type = ssl->out_msgtype;
3961
Hanno Beckera18d1322018-01-03 14:27:32 +00003962 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003963 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003966 return( ret );
3967 }
3968
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003969 if( rec.data_offset != 0 )
3970 {
3971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3973 }
3974
Hanno Becker78f839d2019-03-14 12:56:23 +00003975 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003976 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3977 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003978 }
3979
Hanno Becker2b1e3542018-08-06 11:19:13 +01003980 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3981
3982#if defined(MBEDTLS_SSL_PROTO_DTLS)
3983 /* In case of DTLS, double-check that we don't exceed
3984 * the remaining space in the datagram. */
3985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3986 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003987 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003988 if( ret < 0 )
3989 return( ret );
3990
3991 if( protected_record_size > (size_t) ret )
3992 {
3993 /* Should never happen */
3994 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3995 }
3996 }
3997#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004000 "version = [%d:%d], msglen = %d",
4001 ssl->out_hdr[0], ssl->out_hdr[1],
4002 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004005 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004006
4007 ssl->out_left += protected_record_size;
4008 ssl->out_hdr += protected_record_size;
4009 ssl_update_out_pointers( ssl, ssl->transform_out );
4010
Hanno Becker04484622018-08-06 09:49:38 +01004011 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4012 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4013 break;
4014
4015 /* The loop goes to its end iff the counter is wrapping */
4016 if( i == ssl_ep_len( ssl ) )
4017 {
4018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4019 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 }
4022
Hanno Becker67bc7c32018-08-06 11:33:50 +01004023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4025 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004026 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004027 size_t remaining;
4028 ret = ssl_get_remaining_payload_in_datagram( ssl );
4029 if( ret < 0 )
4030 {
4031 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4032 ret );
4033 return( ret );
4034 }
4035
4036 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004037 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004038 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004039 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004040 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004041 else
4042 {
Hanno Becker513815a2018-08-20 11:56:09 +01004043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004044 }
4045 }
4046#endif /* MBEDTLS_SSL_PROTO_DTLS */
4047
4048 if( ( flush == SSL_FORCE_FLUSH ) &&
4049 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004052 return( ret );
4053 }
4054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
4057 return( 0 );
4058}
4059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004061
4062static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4063{
4064 if( ssl->in_msglen < ssl->in_hslen ||
4065 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4066 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4067 {
4068 return( 1 );
4069 }
4070 return( 0 );
4071}
Hanno Becker44650b72018-08-16 12:51:11 +01004072
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004073static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004074{
4075 return( ( ssl->in_msg[9] << 16 ) |
4076 ( ssl->in_msg[10] << 8 ) |
4077 ssl->in_msg[11] );
4078}
4079
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004080static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004081{
4082 return( ( ssl->in_msg[6] << 16 ) |
4083 ( ssl->in_msg[7] << 8 ) |
4084 ssl->in_msg[8] );
4085}
4086
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004087static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004088{
4089 uint32_t msg_len, frag_off, frag_len;
4090
4091 msg_len = ssl_get_hs_total_len( ssl );
4092 frag_off = ssl_get_hs_frag_off( ssl );
4093 frag_len = ssl_get_hs_frag_len( ssl );
4094
4095 if( frag_off > msg_len )
4096 return( -1 );
4097
4098 if( frag_len > msg_len - frag_off )
4099 return( -1 );
4100
4101 if( frag_len + 12 > ssl->in_msglen )
4102 return( -1 );
4103
4104 return( 0 );
4105}
4106
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004107/*
4108 * Mark bits in bitmask (used for DTLS HS reassembly)
4109 */
4110static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4111{
4112 unsigned int start_bits, end_bits;
4113
4114 start_bits = 8 - ( offset % 8 );
4115 if( start_bits != 8 )
4116 {
4117 size_t first_byte_idx = offset / 8;
4118
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004119 /* Special case */
4120 if( len <= start_bits )
4121 {
4122 for( ; len != 0; len-- )
4123 mask[first_byte_idx] |= 1 << ( start_bits - len );
4124
4125 /* Avoid potential issues with offset or len becoming invalid */
4126 return;
4127 }
4128
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004129 offset += start_bits; /* Now offset % 8 == 0 */
4130 len -= start_bits;
4131
4132 for( ; start_bits != 0; start_bits-- )
4133 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4134 }
4135
4136 end_bits = len % 8;
4137 if( end_bits != 0 )
4138 {
4139 size_t last_byte_idx = ( offset + len ) / 8;
4140
4141 len -= end_bits; /* Now len % 8 == 0 */
4142
4143 for( ; end_bits != 0; end_bits-- )
4144 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4145 }
4146
4147 memset( mask + offset / 8, 0xFF, len / 8 );
4148}
4149
4150/*
4151 * Check that bitmask is full
4152 */
4153static int ssl_bitmask_check( unsigned char *mask, size_t len )
4154{
4155 size_t i;
4156
4157 for( i = 0; i < len / 8; i++ )
4158 if( mask[i] != 0xFF )
4159 return( -1 );
4160
4161 for( i = 0; i < len % 8; i++ )
4162 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4163 return( -1 );
4164
4165 return( 0 );
4166}
4167
Hanno Becker56e205e2018-08-16 09:06:12 +01004168/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004169static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004170 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004171{
Hanno Becker56e205e2018-08-16 09:06:12 +01004172 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004173
Hanno Becker56e205e2018-08-16 09:06:12 +01004174 alloc_len = 12; /* Handshake header */
4175 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004176
Hanno Beckerd07df862018-08-16 09:14:58 +01004177 if( add_bitmap )
4178 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004179
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004180 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004181}
Hanno Becker56e205e2018-08-16 09:06:12 +01004182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004184
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004185static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004186{
4187 return( ( ssl->in_msg[1] << 16 ) |
4188 ( ssl->in_msg[2] << 8 ) |
4189 ssl->in_msg[3] );
4190}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004191
Simon Butcher99000142016-10-13 17:21:01 +01004192int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004197 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004199 }
4200
Hanno Becker12555c62018-08-16 12:47:53 +01004201 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004204 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004205 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004208 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004209 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004210 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004211 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004212
Hanno Becker44650b72018-08-16 12:51:11 +01004213 if( ssl_check_hs_header( ssl ) != 0 )
4214 {
4215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4216 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4217 }
4218
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004219 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004220 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4221 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4222 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4223 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004224 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004225 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4226 {
4227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4228 recv_msg_seq,
4229 ssl->handshake->in_msg_seq ) );
4230 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4231 }
4232
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004233 /* Retransmit only on last message from previous flight, to avoid
4234 * too many retransmissions.
4235 * Besides, No sane server ever retransmits HelloVerifyRequest */
4236 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004240 "message_seq = %d, start_of_flight = %d",
4241 recv_msg_seq,
4242 ssl->handshake->in_flight_start_seq ) );
4243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004247 return( ret );
4248 }
4249 }
4250 else
4251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004253 "message_seq = %d, expected = %d",
4254 recv_msg_seq,
4255 ssl->handshake->in_msg_seq ) );
4256 }
4257
Hanno Becker90333da2017-10-10 11:27:13 +01004258 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004259 }
4260 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004261
Hanno Becker6d97ef52018-08-16 13:09:04 +01004262 /* Message reassembly is handled alongside buffering of future
4263 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004264 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004265 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004266 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004269 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004270 }
4271 }
4272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004274 /* With TLS we don't handle fragmentation (for now) */
4275 if( ssl->in_msglen < ssl->in_hslen )
4276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4278 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004279 }
4280
Simon Butcher99000142016-10-13 17:21:01 +01004281 return( 0 );
4282}
4283
4284void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4285{
Hanno Becker0271f962018-08-16 13:23:47 +01004286 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004287
Hanno Becker0271f962018-08-16 13:23:47 +01004288 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004289 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004290 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004291 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004292
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004293 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004296 ssl->handshake != NULL )
4297 {
Hanno Becker0271f962018-08-16 13:23:47 +01004298 unsigned offset;
4299 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004300
Hanno Becker0271f962018-08-16 13:23:47 +01004301 /* Increment handshake sequence number */
4302 hs->in_msg_seq++;
4303
4304 /*
4305 * Clear up handshake buffering and reassembly structure.
4306 */
4307
4308 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004309 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004310
4311 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004312 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4313 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004314 offset++, hs_buf++ )
4315 {
4316 *hs_buf = *(hs_buf + 1);
4317 }
4318
4319 /* Create a fresh last entry */
4320 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004321 }
4322#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004323}
4324
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004325/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326 * DTLS anti-replay: RFC 6347 4.1.2.6
4327 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004328 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4329 * Bit n is set iff record number in_window_top - n has been seen.
4330 *
4331 * Usually, in_window_top is the last record number seen and the lsb of
4332 * in_window is set. The only exception is the initial state (record number 0
4333 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4336static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004337{
4338 ssl->in_window_top = 0;
4339 ssl->in_window = 0;
4340}
4341
4342static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4343{
4344 return( ( (uint64_t) buf[0] << 40 ) |
4345 ( (uint64_t) buf[1] << 32 ) |
4346 ( (uint64_t) buf[2] << 24 ) |
4347 ( (uint64_t) buf[3] << 16 ) |
4348 ( (uint64_t) buf[4] << 8 ) |
4349 ( (uint64_t) buf[5] ) );
4350}
4351
4352/*
4353 * Return 0 if sequence number is acceptable, -1 otherwise
4354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004356{
4357 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4358 uint64_t bit;
4359
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004360 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004361 return( 0 );
4362
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004363 if( rec_seqnum > ssl->in_window_top )
4364 return( 0 );
4365
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004366 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004367
4368 if( bit >= 64 )
4369 return( -1 );
4370
4371 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4372 return( -1 );
4373
4374 return( 0 );
4375}
4376
4377/*
4378 * Update replay window on new validated record
4379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004381{
4382 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4383
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004384 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004385 return;
4386
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004387 if( rec_seqnum > ssl->in_window_top )
4388 {
4389 /* Update window_top and the contents of the window */
4390 uint64_t shift = rec_seqnum - ssl->in_window_top;
4391
4392 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004393 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004394 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004395 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004396 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004397 ssl->in_window |= 1;
4398 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004399
4400 ssl->in_window_top = rec_seqnum;
4401 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004402 else
4403 {
4404 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004405 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004406
4407 if( bit < 64 ) /* Always true, but be extra sure */
4408 ssl->in_window |= (uint64_t) 1 << bit;
4409 }
4410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004412
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004413#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004414/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004415static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4416
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004417/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004418 * Without any SSL context, check if a datagram looks like a ClientHello with
4419 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004420 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004421 *
4422 * - if cookie is valid, return 0
4423 * - if ClientHello looks superficially valid but cookie is not,
4424 * fill obuf and set olen, then
4425 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4426 * - otherwise return a specific error code
4427 */
4428static int ssl_check_dtls_clihlo_cookie(
4429 mbedtls_ssl_cookie_write_t *f_cookie_write,
4430 mbedtls_ssl_cookie_check_t *f_cookie_check,
4431 void *p_cookie,
4432 const unsigned char *cli_id, size_t cli_id_len,
4433 const unsigned char *in, size_t in_len,
4434 unsigned char *obuf, size_t buf_len, size_t *olen )
4435{
4436 size_t sid_len, cookie_len;
4437 unsigned char *p;
4438
4439 if( f_cookie_write == NULL || f_cookie_check == NULL )
4440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4441
4442 /*
4443 * Structure of ClientHello with record and handshake headers,
4444 * and expected values. We don't need to check a lot, more checks will be
4445 * done when actually parsing the ClientHello - skipping those checks
4446 * avoids code duplication and does not make cookie forging any easier.
4447 *
4448 * 0-0 ContentType type; copied, must be handshake
4449 * 1-2 ProtocolVersion version; copied
4450 * 3-4 uint16 epoch; copied, must be 0
4451 * 5-10 uint48 sequence_number; copied
4452 * 11-12 uint16 length; (ignored)
4453 *
4454 * 13-13 HandshakeType msg_type; (ignored)
4455 * 14-16 uint24 length; (ignored)
4456 * 17-18 uint16 message_seq; copied
4457 * 19-21 uint24 fragment_offset; copied, must be 0
4458 * 22-24 uint24 fragment_length; (ignored)
4459 *
4460 * 25-26 ProtocolVersion client_version; (ignored)
4461 * 27-58 Random random; (ignored)
4462 * 59-xx SessionID session_id; 1 byte len + sid_len content
4463 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4464 * ...
4465 *
4466 * Minimum length is 61 bytes.
4467 */
4468 if( in_len < 61 ||
4469 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4470 in[3] != 0 || in[4] != 0 ||
4471 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4472 {
4473 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4474 }
4475
4476 sid_len = in[59];
4477 if( sid_len > in_len - 61 )
4478 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4479
4480 cookie_len = in[60 + sid_len];
4481 if( cookie_len > in_len - 60 )
4482 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4483
4484 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4485 cli_id, cli_id_len ) == 0 )
4486 {
4487 /* Valid cookie */
4488 return( 0 );
4489 }
4490
4491 /*
4492 * If we get here, we've got an invalid cookie, let's prepare HVR.
4493 *
4494 * 0-0 ContentType type; copied
4495 * 1-2 ProtocolVersion version; copied
4496 * 3-4 uint16 epoch; copied
4497 * 5-10 uint48 sequence_number; copied
4498 * 11-12 uint16 length; olen - 13
4499 *
4500 * 13-13 HandshakeType msg_type; hello_verify_request
4501 * 14-16 uint24 length; olen - 25
4502 * 17-18 uint16 message_seq; copied
4503 * 19-21 uint24 fragment_offset; copied
4504 * 22-24 uint24 fragment_length; olen - 25
4505 *
4506 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4507 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4508 *
4509 * Minimum length is 28.
4510 */
4511 if( buf_len < 28 )
4512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4513
4514 /* Copy most fields and adapt others */
4515 memcpy( obuf, in, 25 );
4516 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4517 obuf[25] = 0xfe;
4518 obuf[26] = 0xff;
4519
4520 /* Generate and write actual cookie */
4521 p = obuf + 28;
4522 if( f_cookie_write( p_cookie,
4523 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4524 {
4525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4526 }
4527
4528 *olen = p - obuf;
4529
4530 /* Go back and fill length fields */
4531 obuf[27] = (unsigned char)( *olen - 28 );
4532
4533 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4534 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4535 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4536
4537 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4538 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4539
4540 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4541}
4542
4543/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004544 * Handle possible client reconnect with the same UDP quadruplet
4545 * (RFC 6347 Section 4.2.8).
4546 *
4547 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4548 * that looks like a ClientHello.
4549 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004550 * - if the input looks like a ClientHello without cookies,
4551 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004552 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004553 * - if the input looks like a ClientHello with a valid cookie,
4554 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004555 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004556 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004557 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004558 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004559 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4560 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561 */
4562static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4563{
4564 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004565 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004566
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004567 ret = ssl_check_dtls_clihlo_cookie(
4568 ssl->conf->f_cookie_write,
4569 ssl->conf->f_cookie_check,
4570 ssl->conf->p_cookie,
4571 ssl->cli_id, ssl->cli_id_len,
4572 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004573 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004574
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004575 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4576
4577 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004578 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004579 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004580 * If the error is permanent we'll catch it later,
4581 * if it's not, then hopefully it'll work next time. */
4582 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4583
4584 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004585 }
4586
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004587 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004588 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004589 /* Got a valid cookie, partially reset context */
4590 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4591 {
4592 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4593 return( ret );
4594 }
4595
4596 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004597 }
4598
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004599 return( ret );
4600}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004601#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004602
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004603/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004604 * ContentType type;
4605 * ProtocolVersion version;
4606 * uint16 epoch; // DTLS only
4607 * uint48 sequence_number; // DTLS only
4608 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004609 *
4610 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004611 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004612 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4613 *
4614 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004615 * 1. proceed with the record if this function returns 0
4616 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4617 * 3. return CLIENT_RECONNECT if this function return that value
4618 * 4. drop the whole datagram if this function returns anything else.
4619 * Point 2 is needed when the peer is resending, and we have already received
4620 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004623{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004624 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 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 +02004627
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004629 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004630 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004634 ssl->in_msgtype,
4635 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004637 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4639 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4640 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4641 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004644
4645#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004646 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4647 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004648 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4649#endif /* MBEDTLS_SSL_PROTO_DTLS */
4650 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4651 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004654 }
4655
4656 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004657 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4660 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 }
4662
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004663 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 }
4668
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004669 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004670 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004671 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4674 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004675 }
4676
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004677 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004678 * DTLS-related tests.
4679 * Check epoch before checking length constraint because
4680 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4681 * message gets duplicated before the corresponding Finished message,
4682 * the second ChangeCipherSpec should be discarded because it belongs
4683 * to an old epoch, but not because its length is shorter than
4684 * the minimum record length for packets using the new record transform.
4685 * Note that these two kinds of failures are handled differently,
4686 * as an unexpected record is silently skipped but an invalid
4687 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004688 */
4689#if defined(MBEDTLS_SSL_PROTO_DTLS)
4690 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4691 {
4692 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4693
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004694 /* Check epoch (and sequence number) with DTLS */
4695 if( rec_epoch != ssl->in_epoch )
4696 {
4697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4698 "expected %d, received %d",
4699 ssl->in_epoch, rec_epoch ) );
4700
4701#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4702 /*
4703 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4704 * access the first byte of record content (handshake type), as we
4705 * have an active transform (possibly iv_len != 0), so use the
4706 * fact that the record header len is 13 instead.
4707 */
4708 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4709 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4710 rec_epoch == 0 &&
4711 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4712 ssl->in_left > 13 &&
4713 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4714 {
4715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4716 "from the same port" ) );
4717 return( ssl_handle_possible_reconnect( ssl ) );
4718 }
4719 else
4720#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004721 {
4722 /* Consider buffering the record. */
4723 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4724 {
4725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4726 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4727 }
4728
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004729 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004730 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004731 }
4732
4733#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4734 /* Replay detection only works for the current epoch */
4735 if( rec_epoch == ssl->in_epoch &&
4736 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4737 {
4738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4739 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4740 }
4741#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004742
Hanno Becker52c6dc62017-05-26 16:07:36 +01004743 /* Drop unexpected ApplicationData records,
4744 * except at the beginning of renegotiations */
4745 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4746 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4747#if defined(MBEDTLS_SSL_RENEGOTIATION)
4748 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4749 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4750#endif
4751 )
4752 {
4753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4754 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4755 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004756 }
4757#endif /* MBEDTLS_SSL_PROTO_DTLS */
4758
Hanno Becker52c6dc62017-05-26 16:07:36 +01004759
4760 /* Check length against bounds of the current transform and version */
4761 if( ssl->transform_in == NULL )
4762 {
4763 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004764 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004765 {
4766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4767 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4768 }
4769 }
4770 else
4771 {
4772 if( ssl->in_msglen < ssl->transform_in->minlen )
4773 {
4774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4775 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4776 }
4777
4778#if defined(MBEDTLS_SSL_PROTO_SSL3)
4779 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004780 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004781 {
4782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4783 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4784 }
4785#endif
4786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4787 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4788 /*
4789 * TLS encrypted messages can have up to 256 bytes of padding
4790 */
4791 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4792 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004793 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004794 {
4795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4796 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4797 }
4798#endif
4799 }
4800
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004801 return( 0 );
4802}
Paul Bakker5121ce52009-01-03 21:22:43 +00004803
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004804/*
4805 * If applicable, decrypt (and decompress) record content
4806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808{
4809 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4812 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4815 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 ret = mbedtls_ssl_hw_record_read( ssl );
4820 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4823 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004824 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004825
4826 if( ret == 0 )
4827 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004828 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004830 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004832 mbedtls_record rec;
4833
4834 rec.buf = ssl->in_iv;
4835 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4836 - ( ssl->in_iv - ssl->in_buf );
4837 rec.data_len = ssl->in_msglen;
4838 rec.data_offset = 0;
4839
4840 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4841 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4842 ssl->conf->transport, rec.ver );
4843 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004844 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4845 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 return( ret );
4849 }
4850
Hanno Becker29800d22018-08-07 14:30:18 +01004851 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4852 {
4853 /* Should never happen */
4854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004856
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004857 ssl->in_msglen = rec.data_len;
4858 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4859 ssl->in_len[1] = (unsigned char)( rec.data_len );
4860
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004861 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4862 ssl->in_msg, ssl->in_msglen );
4863
Angus Grattond8213d02016-05-25 20:56:48 +10004864 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4867 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004869 else if( ssl->in_msglen == 0 )
4870 {
4871#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4872 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4873 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4874 {
4875 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4877 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4878 }
4879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4880
4881 ssl->nb_zero++;
4882
4883 /*
4884 * Three or more empty messages may be a DoS attack
4885 * (excessive CPU consumption).
4886 */
4887 if( ssl->nb_zero > 3 )
4888 {
4889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4890 "messages, possible DoS attack" ) );
4891 /* Q: Is that the right error code? */
4892 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4893 }
4894 }
4895 else
4896 ssl->nb_zero = 0;
4897
4898#if defined(MBEDTLS_SSL_PROTO_DTLS)
4899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4900 {
4901 ; /* in_ctr read from peer, not maintained internally */
4902 }
4903 else
4904#endif
4905 {
4906 unsigned i;
4907 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4908 if( ++ssl->in_ctr[i - 1] != 0 )
4909 break;
4910
4911 /* The loop goes to its end iff the counter is wrapping */
4912 if( i == ssl_ep_len( ssl ) )
4913 {
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4915 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4916 }
4917 }
4918
Paul Bakker5121ce52009-01-03 21:22:43 +00004919 }
4920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004921#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004922 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004924 {
4925 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004928 return( ret );
4929 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004930 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004934 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004937 }
4938#endif
4939
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004940 return( 0 );
4941}
4942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004944
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004945/*
4946 * Read a record.
4947 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004948 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4949 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4950 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004951 */
Hanno Becker1097b342018-08-15 14:09:41 +01004952
4953/* Helper functions for mbedtls_ssl_read_record(). */
4954static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004955static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4956static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004957
Hanno Becker327c93b2018-08-15 13:56:18 +01004958int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004959 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004960{
4961 int ret;
4962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004964
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004965 if( ssl->keep_current_message == 0 )
4966 {
4967 do {
Simon Butcher99000142016-10-13 17:21:01 +01004968
Hanno Becker26994592018-08-15 14:14:59 +01004969 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004970 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004971 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004972
Hanno Beckere74d5562018-08-15 14:26:08 +01004973 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004974 {
Hanno Becker40f50842018-08-15 14:48:01 +01004975#if defined(MBEDTLS_SSL_PROTO_DTLS)
4976 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004977
Hanno Becker40f50842018-08-15 14:48:01 +01004978 /* We only check for buffered messages if the
4979 * current datagram is fully consumed. */
4980 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004981 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004982 {
Hanno Becker40f50842018-08-15 14:48:01 +01004983 if( ssl_load_buffered_message( ssl ) == 0 )
4984 have_buffered = 1;
4985 }
4986
4987 if( have_buffered == 0 )
4988#endif /* MBEDTLS_SSL_PROTO_DTLS */
4989 {
4990 ret = ssl_get_next_record( ssl );
4991 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4992 continue;
4993
4994 if( ret != 0 )
4995 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004996 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004997 return( ret );
4998 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004999 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005000 }
5001
5002 ret = mbedtls_ssl_handle_message_type( ssl );
5003
Hanno Becker40f50842018-08-15 14:48:01 +01005004#if defined(MBEDTLS_SSL_PROTO_DTLS)
5005 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5006 {
5007 /* Buffer future message */
5008 ret = ssl_buffer_message( ssl );
5009 if( ret != 0 )
5010 return( ret );
5011
5012 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5013 }
5014#endif /* MBEDTLS_SSL_PROTO_DTLS */
5015
Hanno Becker90333da2017-10-10 11:27:13 +01005016 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5017 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005018
5019 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005020 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005021 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005022 return( ret );
5023 }
5024
Hanno Becker327c93b2018-08-15 13:56:18 +01005025 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005026 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005027 {
5028 mbedtls_ssl_update_handshake_status( ssl );
5029 }
Simon Butcher99000142016-10-13 17:21:01 +01005030 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005031 else
Simon Butcher99000142016-10-13 17:21:01 +01005032 {
Hanno Becker02f59072018-08-15 14:00:24 +01005033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005034 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005035 }
5036
5037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5038
5039 return( 0 );
5040}
5041
Hanno Becker40f50842018-08-15 14:48:01 +01005042#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005043static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005044{
Hanno Becker40f50842018-08-15 14:48:01 +01005045 if( ssl->in_left > ssl->next_record_offset )
5046 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005047
Hanno Becker40f50842018-08-15 14:48:01 +01005048 return( 0 );
5049}
5050
5051static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5052{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005053 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005054 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005055 int ret = 0;
5056
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005057 if( hs == NULL )
5058 return( -1 );
5059
Hanno Beckere00ae372018-08-20 09:39:42 +01005060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5061
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005062 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5063 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5064 {
5065 /* Check if we have seen a ChangeCipherSpec before.
5066 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005067 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005068 {
5069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5070 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005071 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005072 }
5073
Hanno Becker39b8bc92018-08-28 17:17:13 +01005074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005075 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5076 ssl->in_msglen = 1;
5077 ssl->in_msg[0] = 1;
5078
5079 /* As long as they are equal, the exact value doesn't matter. */
5080 ssl->in_left = 0;
5081 ssl->next_record_offset = 0;
5082
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005083 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005084 goto exit;
5085 }
Hanno Becker37f95322018-08-16 13:55:32 +01005086
Hanno Beckerb8f50142018-08-28 10:01:34 +01005087#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005088 /* Debug only */
5089 {
5090 unsigned offset;
5091 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5092 {
5093 hs_buf = &hs->buffering.hs[offset];
5094 if( hs_buf->is_valid == 1 )
5095 {
5096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5097 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005098 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005099 }
5100 }
5101 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005102#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005103
5104 /* Check if we have buffered and/or fully reassembled the
5105 * next handshake message. */
5106 hs_buf = &hs->buffering.hs[0];
5107 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5108 {
5109 /* Synthesize a record containing the buffered HS message. */
5110 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5111 ( hs_buf->data[2] << 8 ) |
5112 hs_buf->data[3];
5113
5114 /* Double-check that we haven't accidentally buffered
5115 * a message that doesn't fit into the input buffer. */
5116 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5117 {
5118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5120 }
5121
5122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5123 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5124 hs_buf->data, msg_len + 12 );
5125
5126 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5127 ssl->in_hslen = msg_len + 12;
5128 ssl->in_msglen = msg_len + 12;
5129 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5130
5131 ret = 0;
5132 goto exit;
5133 }
5134 else
5135 {
5136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5137 hs->in_msg_seq ) );
5138 }
5139
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005140 ret = -1;
5141
5142exit:
5143
5144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5145 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005146}
5147
Hanno Beckera02b0b42018-08-21 17:20:27 +01005148static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5149 size_t desired )
5150{
5151 int offset;
5152 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5154 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005155
Hanno Becker01315ea2018-08-21 17:22:17 +01005156 /* Get rid of future records epoch first, if such exist. */
5157 ssl_free_buffered_record( ssl );
5158
5159 /* Check if we have enough space available now. */
5160 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5161 hs->buffering.total_bytes_buffered ) )
5162 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005164 return( 0 );
5165 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005166
Hanno Becker4f432ad2018-08-28 10:02:32 +01005167 /* We don't have enough space to buffer the next expected handshake
5168 * message. Remove buffers used for future messages to gain space,
5169 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005170 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5171 offset >= 0; offset-- )
5172 {
5173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5174 offset ) );
5175
Hanno Beckerb309b922018-08-23 13:18:05 +01005176 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005177
5178 /* Check if we have enough space available now. */
5179 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5180 hs->buffering.total_bytes_buffered ) )
5181 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005183 return( 0 );
5184 }
5185 }
5186
5187 return( -1 );
5188}
5189
Hanno Becker40f50842018-08-15 14:48:01 +01005190static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5191{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005192 int ret = 0;
5193 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5194
5195 if( hs == NULL )
5196 return( 0 );
5197
5198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5199
5200 switch( ssl->in_msgtype )
5201 {
5202 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005204
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005205 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005206 break;
5207
5208 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005209 {
5210 unsigned recv_msg_seq_offset;
5211 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5212 mbedtls_ssl_hs_buffer *hs_buf;
5213 size_t msg_len = ssl->in_hslen - 12;
5214
5215 /* We should never receive an old handshake
5216 * message - double-check nonetheless. */
5217 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5218 {
5219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5221 }
5222
5223 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5224 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5225 {
5226 /* Silently ignore -- message too far in the future */
5227 MBEDTLS_SSL_DEBUG_MSG( 2,
5228 ( "Ignore future HS message with sequence number %u, "
5229 "buffering window %u - %u",
5230 recv_msg_seq, ssl->handshake->in_msg_seq,
5231 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5232
5233 goto exit;
5234 }
5235
5236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5237 recv_msg_seq, recv_msg_seq_offset ) );
5238
5239 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5240
5241 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005242 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005243 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005244 size_t reassembly_buf_sz;
5245
Hanno Becker37f95322018-08-16 13:55:32 +01005246 hs_buf->is_fragmented =
5247 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5248
5249 /* We copy the message back into the input buffer
5250 * after reassembly, so check that it's not too large.
5251 * This is an implementation-specific limitation
5252 * and not one from the standard, hence it is not
5253 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005254 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005255 {
5256 /* Ignore message */
5257 goto exit;
5258 }
5259
Hanno Beckere0b150f2018-08-21 15:51:03 +01005260 /* Check if we have enough space to buffer the message. */
5261 if( hs->buffering.total_bytes_buffered >
5262 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5263 {
5264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5266 }
5267
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005268 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5269 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005270
5271 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5272 hs->buffering.total_bytes_buffered ) )
5273 {
5274 if( recv_msg_seq_offset > 0 )
5275 {
5276 /* If we can't buffer a future message because
5277 * of space limitations -- ignore. */
5278 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",
5279 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5280 (unsigned) hs->buffering.total_bytes_buffered ) );
5281 goto exit;
5282 }
Hanno Beckere1801392018-08-21 16:51:05 +01005283 else
5284 {
5285 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",
5286 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5287 (unsigned) hs->buffering.total_bytes_buffered ) );
5288 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005289
Hanno Beckera02b0b42018-08-21 17:20:27 +01005290 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005291 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005292 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",
5293 (unsigned) msg_len,
5294 (unsigned) reassembly_buf_sz,
5295 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005296 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005297 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5298 goto exit;
5299 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005300 }
5301
5302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5303 msg_len ) );
5304
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005305 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5306 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005307 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005308 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005309 goto exit;
5310 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005311 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005312
5313 /* Prepare final header: copy msg_type, length and message_seq,
5314 * then add standardised fragment_offset and fragment_length */
5315 memcpy( hs_buf->data, ssl->in_msg, 6 );
5316 memset( hs_buf->data + 6, 0, 3 );
5317 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5318
5319 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005320
5321 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005322 }
5323 else
5324 {
5325 /* Make sure msg_type and length are consistent */
5326 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5327 {
5328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5329 /* Ignore */
5330 goto exit;
5331 }
5332 }
5333
Hanno Becker4422bbb2018-08-20 09:40:19 +01005334 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005335 {
5336 size_t frag_len, frag_off;
5337 unsigned char * const msg = hs_buf->data + 12;
5338
5339 /*
5340 * Check and copy current fragment
5341 */
5342
5343 /* Validation of header fields already done in
5344 * mbedtls_ssl_prepare_handshake_record(). */
5345 frag_off = ssl_get_hs_frag_off( ssl );
5346 frag_len = ssl_get_hs_frag_len( ssl );
5347
5348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5349 frag_off, frag_len ) );
5350 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5351
5352 if( hs_buf->is_fragmented )
5353 {
5354 unsigned char * const bitmask = msg + msg_len;
5355 ssl_bitmask_set( bitmask, frag_off, frag_len );
5356 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5357 msg_len ) == 0 );
5358 }
5359 else
5360 {
5361 hs_buf->is_complete = 1;
5362 }
5363
5364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5365 hs_buf->is_complete ? "" : "not yet " ) );
5366 }
5367
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005368 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005369 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005370
5371 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005372 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005373 break;
5374 }
5375
5376exit:
5377
5378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5379 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005380}
5381#endif /* MBEDTLS_SSL_PROTO_DTLS */
5382
Hanno Becker1097b342018-08-15 14:09:41 +01005383static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005384{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005385 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005386 * Consume last content-layer message and potentially
5387 * update in_msglen which keeps track of the contents'
5388 * consumption state.
5389 *
5390 * (1) Handshake messages:
5391 * Remove last handshake message, move content
5392 * and adapt in_msglen.
5393 *
5394 * (2) Alert messages:
5395 * Consume whole record content, in_msglen = 0.
5396 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005397 * (3) Change cipher spec:
5398 * Consume whole record content, in_msglen = 0.
5399 *
5400 * (4) Application data:
5401 * Don't do anything - the record layer provides
5402 * the application data as a stream transport
5403 * and consumes through mbedtls_ssl_read only.
5404 *
5405 */
5406
5407 /* Case (1): Handshake messages */
5408 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005409 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005410 /* Hard assertion to be sure that no application data
5411 * is in flight, as corrupting ssl->in_msglen during
5412 * ssl->in_offt != NULL is fatal. */
5413 if( ssl->in_offt != NULL )
5414 {
5415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5417 }
5418
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005419 /*
5420 * Get next Handshake message in the current record
5421 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005422
Hanno Becker4a810fb2017-05-24 16:27:30 +01005423 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005424 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005425 * current handshake content: If DTLS handshake
5426 * fragmentation is used, that's the fragment
5427 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005428 * size here is faulty and should be changed at
5429 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005430 * (2) While it doesn't seem to cause problems, one
5431 * has to be very careful not to assume that in_hslen
5432 * is always <= in_msglen in a sensible communication.
5433 * Again, it's wrong for DTLS handshake fragmentation.
5434 * The following check is therefore mandatory, and
5435 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005436 * Additionally, ssl->in_hslen might be arbitrarily out of
5437 * bounds after handling a DTLS message with an unexpected
5438 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005439 */
5440 if( ssl->in_hslen < ssl->in_msglen )
5441 {
5442 ssl->in_msglen -= ssl->in_hslen;
5443 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5444 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445
Hanno Becker4a810fb2017-05-24 16:27:30 +01005446 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5447 ssl->in_msg, ssl->in_msglen );
5448 }
5449 else
5450 {
5451 ssl->in_msglen = 0;
5452 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005453
Hanno Becker4a810fb2017-05-24 16:27:30 +01005454 ssl->in_hslen = 0;
5455 }
5456 /* Case (4): Application data */
5457 else if( ssl->in_offt != NULL )
5458 {
5459 return( 0 );
5460 }
5461 /* Everything else (CCS & Alerts) */
5462 else
5463 {
5464 ssl->in_msglen = 0;
5465 }
5466
Hanno Becker1097b342018-08-15 14:09:41 +01005467 return( 0 );
5468}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005469
Hanno Beckere74d5562018-08-15 14:26:08 +01005470static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5471{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005472 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005473 return( 1 );
5474
5475 return( 0 );
5476}
5477
Hanno Becker5f066e72018-08-16 14:56:31 +01005478#if defined(MBEDTLS_SSL_PROTO_DTLS)
5479
5480static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5481{
5482 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5483 if( hs == NULL )
5484 return;
5485
Hanno Becker01315ea2018-08-21 17:22:17 +01005486 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005487 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005488 hs->buffering.total_bytes_buffered -=
5489 hs->buffering.future_record.len;
5490
5491 mbedtls_free( hs->buffering.future_record.data );
5492 hs->buffering.future_record.data = NULL;
5493 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005494}
5495
5496static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5497{
5498 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5499 unsigned char * rec;
5500 size_t rec_len;
5501 unsigned rec_epoch;
5502
5503 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5504 return( 0 );
5505
5506 if( hs == NULL )
5507 return( 0 );
5508
Hanno Becker5f066e72018-08-16 14:56:31 +01005509 rec = hs->buffering.future_record.data;
5510 rec_len = hs->buffering.future_record.len;
5511 rec_epoch = hs->buffering.future_record.epoch;
5512
5513 if( rec == NULL )
5514 return( 0 );
5515
Hanno Becker4cb782d2018-08-20 11:19:05 +01005516 /* Only consider loading future records if the
5517 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005518 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005519 return( 0 );
5520
Hanno Becker5f066e72018-08-16 14:56:31 +01005521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5522
5523 if( rec_epoch != ssl->in_epoch )
5524 {
5525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5526 goto exit;
5527 }
5528
5529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5530
5531 /* Double-check that the record is not too large */
5532 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5533 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5534 {
5535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5537 }
5538
5539 memcpy( ssl->in_hdr, rec, rec_len );
5540 ssl->in_left = rec_len;
5541 ssl->next_record_offset = 0;
5542
5543 ssl_free_buffered_record( ssl );
5544
5545exit:
5546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5547 return( 0 );
5548}
5549
5550static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5551{
5552 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5553 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005554 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005555
5556 /* Don't buffer future records outside handshakes. */
5557 if( hs == NULL )
5558 return( 0 );
5559
5560 /* Only buffer handshake records (we are only interested
5561 * in Finished messages). */
5562 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5563 return( 0 );
5564
5565 /* Don't buffer more than one future epoch record. */
5566 if( hs->buffering.future_record.data != NULL )
5567 return( 0 );
5568
Hanno Becker01315ea2018-08-21 17:22:17 +01005569 /* Don't buffer record if there's not enough buffering space remaining. */
5570 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5571 hs->buffering.total_bytes_buffered ) )
5572 {
5573 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",
5574 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5575 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005576 return( 0 );
5577 }
5578
Hanno Becker5f066e72018-08-16 14:56:31 +01005579 /* Buffer record */
5580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5581 ssl->in_epoch + 1 ) );
5582 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5583 rec_hdr_len + ssl->in_msglen );
5584
5585 /* ssl_parse_record_header() only considers records
5586 * of the next epoch as candidates for buffering. */
5587 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005588 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005589
5590 hs->buffering.future_record.data =
5591 mbedtls_calloc( 1, hs->buffering.future_record.len );
5592 if( hs->buffering.future_record.data == NULL )
5593 {
5594 /* If we run out of RAM trying to buffer a
5595 * record from the next epoch, just ignore. */
5596 return( 0 );
5597 }
5598
Hanno Becker01315ea2018-08-21 17:22:17 +01005599 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005600
Hanno Becker01315ea2018-08-21 17:22:17 +01005601 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005602 return( 0 );
5603}
5604
5605#endif /* MBEDTLS_SSL_PROTO_DTLS */
5606
Hanno Beckere74d5562018-08-15 14:26:08 +01005607static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005608{
5609 int ret;
5610
Hanno Becker5f066e72018-08-16 14:56:31 +01005611#if defined(MBEDTLS_SSL_PROTO_DTLS)
5612 /* We might have buffered a future record; if so,
5613 * and if the epoch matches now, load it.
5614 * On success, this call will set ssl->in_left to
5615 * the length of the buffered record, so that
5616 * the calls to ssl_fetch_input() below will
5617 * essentially be no-ops. */
5618 ret = ssl_load_buffered_record( ssl );
5619 if( ret != 0 )
5620 return( ret );
5621#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005626 return( ret );
5627 }
5628
5629 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005632 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5633 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005634 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005635 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5636 {
5637 ret = ssl_buffer_future_record( ssl );
5638 if( ret != 0 )
5639 return( ret );
5640
5641 /* Fall through to handling of unexpected records */
5642 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5643 }
5644
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005645 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5646 {
5647 /* Skip unexpected record (but not whole datagram) */
5648 ssl->next_record_offset = ssl->in_msglen
5649 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005650
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5652 "(header)" ) );
5653 }
5654 else
5655 {
5656 /* Skip invalid record and the rest of the datagram */
5657 ssl->next_record_offset = 0;
5658 ssl->in_left = 0;
5659
5660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5661 "(header)" ) );
5662 }
5663
5664 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005665 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005666 }
5667#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005668 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005669 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005670
5671 /*
5672 * Read and optionally decrypt the message contents
5673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5675 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678 return( ret );
5679 }
5680
5681 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005683 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005686 if( ssl->next_record_offset < ssl->in_left )
5687 {
5688 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5689 }
5690 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005691 else
5692#endif
5693 ssl->in_left = 0;
5694
5695 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005698 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005699 {
5700 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5702 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005703 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005704 /* Except when waiting for Finished as a bad mac here
5705 * probably means something went wrong in the handshake
5706 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5707 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5708 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5709 {
5710#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5711 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5712 {
5713 mbedtls_ssl_send_alert_message( ssl,
5714 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5715 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5716 }
5717#endif
5718 return( ret );
5719 }
5720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005722 if( ssl->conf->badmac_limit != 0 &&
5723 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5726 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005727 }
5728#endif
5729
Hanno Becker4a810fb2017-05-24 16:27:30 +01005730 /* As above, invalid records cause
5731 * dismissal of the whole datagram. */
5732
5733 ssl->next_record_offset = 0;
5734 ssl->in_left = 0;
5735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005737 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005738 }
5739
5740 return( ret );
5741 }
5742 else
5743#endif
5744 {
5745 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5747 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 mbedtls_ssl_send_alert_message( ssl,
5750 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5751 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005752 }
5753#endif
5754 return( ret );
5755 }
5756 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005757
Simon Butcher99000142016-10-13 17:21:01 +01005758 return( 0 );
5759}
5760
5761int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5762{
5763 int ret;
5764
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005765 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005766 * Handle particular types of records
5767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005769 {
Simon Butcher99000142016-10-13 17:21:01 +01005770 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5771 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005772 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 }
5775
Hanno Beckere678eaa2018-08-21 14:57:46 +01005776 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005777 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005778 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005779 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5781 ssl->in_msglen ) );
5782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005783 }
5784
Hanno Beckere678eaa2018-08-21 14:57:46 +01005785 if( ssl->in_msg[0] != 1 )
5786 {
5787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5788 ssl->in_msg[0] ) );
5789 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5790 }
5791
5792#if defined(MBEDTLS_SSL_PROTO_DTLS)
5793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5794 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5795 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5796 {
5797 if( ssl->handshake == NULL )
5798 {
5799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5800 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5801 }
5802
5803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5804 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5805 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005806#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005807 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005810 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005811 if( ssl->in_msglen != 2 )
5812 {
5813 /* Note: Standard allows for more than one 2 byte alert
5814 to be packed in a single message, but Mbed TLS doesn't
5815 currently support this. */
5816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5817 ssl->in_msglen ) );
5818 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5819 }
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005822 ssl->in_msg[0], ssl->in_msg[1] ) );
5823
5824 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005825 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005830 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 }
5833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5835 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5838 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005840
5841#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5842 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5843 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5844 {
Hanno Becker90333da2017-10-10 11:27:13 +01005845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005846 /* Will be handled when trying to parse ServerHello */
5847 return( 0 );
5848 }
5849#endif
5850
5851#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5852 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5853 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5854 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5855 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5856 {
5857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5858 /* Will be handled in mbedtls_ssl_parse_certificate() */
5859 return( 0 );
5860 }
5861#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5862
5863 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005864 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005865 }
5866
Hanno Beckerc76c6192017-06-06 10:03:17 +01005867#if defined(MBEDTLS_SSL_PROTO_DTLS)
5868 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5869 ssl->handshake != NULL &&
5870 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5871 {
5872 ssl_handshake_wrapup_free_hs_transform( ssl );
5873 }
5874#endif
5875
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 return( 0 );
5877}
5878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005880{
5881 int ret;
5882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5884 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5885 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005886 {
5887 return( ret );
5888 }
5889
5890 return( 0 );
5891}
5892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005894 unsigned char level,
5895 unsigned char message )
5896{
5897 int ret;
5898
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005899 if( ssl == NULL || ssl->conf == NULL )
5900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005906 ssl->out_msglen = 2;
5907 ssl->out_msg[0] = level;
5908 ssl->out_msg[1] = message;
5909
Hanno Becker67bc7c32018-08-06 11:33:50 +01005910 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005913 return( ret );
5914 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005916
5917 return( 0 );
5918}
5919
Hanno Beckerb9d44792019-02-08 07:19:04 +00005920#if defined(MBEDTLS_X509_CRT_PARSE_C)
5921static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5922{
5923#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5924 if( session->peer_cert != NULL )
5925 {
5926 mbedtls_x509_crt_free( session->peer_cert );
5927 mbedtls_free( session->peer_cert );
5928 session->peer_cert = NULL;
5929 }
5930#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5931 if( session->peer_cert_digest != NULL )
5932 {
5933 /* Zeroization is not necessary. */
5934 mbedtls_free( session->peer_cert_digest );
5935 session->peer_cert_digest = NULL;
5936 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5937 session->peer_cert_digest_len = 0;
5938 }
5939#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5940}
5941#endif /* MBEDTLS_X509_CRT_PARSE_C */
5942
Paul Bakker5121ce52009-01-03 21:22:43 +00005943/*
5944 * Handshake functions
5945 */
Hanno Becker21489932019-02-05 13:20:55 +00005946#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005947/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005949{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005950 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5951 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005954
Hanno Becker7177a882019-02-05 13:36:46 +00005955 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005958 ssl->state++;
5959 return( 0 );
5960 }
5961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5963 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005964}
5965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005967{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005968 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5969 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005972
Hanno Becker7177a882019-02-05 13:36:46 +00005973 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005976 ssl->state++;
5977 return( 0 );
5978 }
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005982}
Gilles Peskinef9828522017-05-03 12:28:43 +02005983
Hanno Becker21489932019-02-05 13:20:55 +00005984#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005985/* Some certificate support -> implement write and parse */
5986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005990 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005992 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5993 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005996
Hanno Becker7177a882019-02-05 13:36:46 +00005997 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006000 ssl->state++;
6001 return( 0 );
6002 }
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006005 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006006 {
6007 if( ssl->client_auth == 0 )
6008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006010 ssl->state++;
6011 return( 0 );
6012 }
6013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 /*
6016 * If using SSLv3 and got no cert, send an Alert message
6017 * (otherwise an empty Certificate message will be sent).
6018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6020 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 {
6022 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6024 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6025 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006028 goto write_msg;
6029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032#endif /* MBEDTLS_SSL_CLI_C */
6033#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6039 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006040 }
6041 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006045
6046 /*
6047 * 0 . 0 handshake type
6048 * 1 . 3 handshake length
6049 * 4 . 6 length of all certs
6050 * 7 . 9 length of cert. 1
6051 * 10 . n-1 peer certificate
6052 * n . n+2 length of cert. 2
6053 * n+3 . ... upper level cert, etc.
6054 */
6055 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006057
Paul Bakker29087132010-03-21 21:03:34 +00006058 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
6060 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006061 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006064 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006066 }
6067
6068 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6069 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6070 ssl->out_msg[i + 2] = (unsigned char)( n );
6071
6072 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6073 i += n; crt = crt->next;
6074 }
6075
6076 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6077 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6078 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6079
6080 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6082 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006084#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006085write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006087
6088 ssl->state++;
6089
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006090 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006091 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 return( ret );
6094 }
6095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Paul Bakkered27a042013-04-18 22:46:23 +02006098 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006099}
6100
Hanno Becker84879e32019-01-31 07:44:03 +00006101#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006102
6103#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006104static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6105 unsigned char *crt_buf,
6106 size_t crt_buf_len )
6107{
6108 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6109
6110 if( peer_crt == NULL )
6111 return( -1 );
6112
6113 if( peer_crt->raw.len != crt_buf_len )
6114 return( -1 );
6115
Hanno Becker46f34d02019-02-08 14:00:04 +00006116 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006117}
Hanno Becker177475a2019-02-05 17:02:46 +00006118#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6119static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6120 unsigned char *crt_buf,
6121 size_t crt_buf_len )
6122{
6123 int ret;
6124 unsigned char const * const peer_cert_digest =
6125 ssl->session->peer_cert_digest;
6126 mbedtls_md_type_t const peer_cert_digest_type =
6127 ssl->session->peer_cert_digest_type;
6128 mbedtls_md_info_t const * const digest_info =
6129 mbedtls_md_info_from_type( peer_cert_digest_type );
6130 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6131 size_t digest_len;
6132
6133 if( peer_cert_digest == NULL || digest_info == NULL )
6134 return( -1 );
6135
6136 digest_len = mbedtls_md_get_size( digest_info );
6137 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6138 return( -1 );
6139
6140 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6141 if( ret != 0 )
6142 return( -1 );
6143
6144 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6145}
6146#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006147#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006148
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006149/*
6150 * Once the certificate message is read, parse it into a cert chain and
6151 * perform basic checks, but leave actual verification to the caller
6152 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006153static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6154 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006156 int ret;
6157#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6158 int crt_cnt=0;
6159#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006160 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006161 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6167 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006169 }
6170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6172 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006175 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6176 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 }
6179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006181
Paul Bakker5121ce52009-01-03 21:22:43 +00006182 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006184 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006185 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006186
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006187 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006191 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6192 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 }
6195
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006196 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6197 i += 3;
6198
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006199 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006200 while( i < ssl->in_hslen )
6201 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006202 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006203 if ( i + 3 > ssl->in_hslen ) {
6204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006205 mbedtls_ssl_send_alert_message( ssl,
6206 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6207 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006208 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6209 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006210 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6211 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 if( ssl->in_msg[i] != 0 )
6213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006215 mbedtls_ssl_send_alert_message( ssl,
6216 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6217 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 }
6220
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006221 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6223 | (unsigned int) ssl->in_msg[i + 2];
6224 i += 3;
6225
6226 if( n < 128 || i + n > ssl->in_hslen )
6227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006229 mbedtls_ssl_send_alert_message( ssl,
6230 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6231 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233 }
6234
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006235 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006236#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6237 if( crt_cnt++ == 0 &&
6238 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6239 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006240 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006241 /* During client-side renegotiation, check that the server's
6242 * end-CRTs hasn't changed compared to the initial handshake,
6243 * mitigating the triple handshake attack. On success, reuse
6244 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6246 if( ssl_check_peer_crt_unchanged( ssl,
6247 &ssl->in_msg[i],
6248 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006249 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6251 mbedtls_ssl_send_alert_message( ssl,
6252 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6253 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6254 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006255 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006256
6257 /* Now we can safely free the original chain. */
6258 ssl_clear_peer_cert( ssl->session );
6259 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006260#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6261
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006262 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006263#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006264 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006265#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006266 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006267 * it in-place from the input buffer instead of making a copy. */
6268 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6269#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006270 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006271 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006272 case 0: /*ok*/
6273 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6274 /* Ignore certificate with an unknown algorithm: maybe a
6275 prior certificate was already trusted. */
6276 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006277
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006278 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6279 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6280 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006281
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006282 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6283 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6284 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006285
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006286 default:
6287 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6288 crt_parse_der_failed:
6289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6290 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6291 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006292 }
6293
6294 i += n;
6295 }
6296
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006297 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006298 return( 0 );
6299}
6300
Hanno Becker4a55f632019-02-05 12:49:06 +00006301#if defined(MBEDTLS_SSL_SRV_C)
6302static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6303{
6304 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6305 return( -1 );
6306
6307#if defined(MBEDTLS_SSL_PROTO_SSL3)
6308 /*
6309 * Check if the client sent an empty certificate
6310 */
6311 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6312 {
6313 if( ssl->in_msglen == 2 &&
6314 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6315 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6316 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6317 {
6318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6319 return( 0 );
6320 }
6321
6322 return( -1 );
6323 }
6324#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6325
6326#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6327 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6328 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6329 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6330 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6331 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6332 {
6333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6334 return( 0 );
6335 }
6336
6337 return( -1 );
6338#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6339 MBEDTLS_SSL_PROTO_TLS1_2 */
6340}
6341#endif /* MBEDTLS_SSL_SRV_C */
6342
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006343/* Check if a certificate message is expected.
6344 * Return either
6345 * - SSL_CERTIFICATE_EXPECTED, or
6346 * - SSL_CERTIFICATE_SKIP
6347 * indicating whether a Certificate message is expected or not.
6348 */
6349#define SSL_CERTIFICATE_EXPECTED 0
6350#define SSL_CERTIFICATE_SKIP 1
6351static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6352 int authmode )
6353{
6354 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006355 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006356
6357 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6358 return( SSL_CERTIFICATE_SKIP );
6359
6360#if defined(MBEDTLS_SSL_SRV_C)
6361 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6362 {
6363 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6364 return( SSL_CERTIFICATE_SKIP );
6365
6366 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6367 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006368 ssl->session_negotiate->verify_result =
6369 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6370 return( SSL_CERTIFICATE_SKIP );
6371 }
6372 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006373#else
6374 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006375#endif /* MBEDTLS_SSL_SRV_C */
6376
6377 return( SSL_CERTIFICATE_EXPECTED );
6378}
6379
Hanno Becker68636192019-02-05 14:36:34 +00006380static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6381 int authmode,
6382 mbedtls_x509_crt *chain,
6383 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006384{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006385 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006386 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006387 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006388 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006389
Hanno Becker8927c832019-04-03 12:52:50 +01006390 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6391 void *p_vrfy;
6392
Hanno Becker68636192019-02-05 14:36:34 +00006393 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6394 return( 0 );
6395
Hanno Becker8927c832019-04-03 12:52:50 +01006396 if( ssl->f_vrfy != NULL )
6397 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006399 f_vrfy = ssl->f_vrfy;
6400 p_vrfy = ssl->p_vrfy;
6401 }
6402 else
6403 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006405 f_vrfy = ssl->conf->f_vrfy;
6406 p_vrfy = ssl->conf->p_vrfy;
6407 }
6408
Hanno Becker68636192019-02-05 14:36:34 +00006409 /*
6410 * Main check: verify certificate
6411 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006412#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6413 if( ssl->conf->f_ca_cb != NULL )
6414 {
6415 ((void) rs_ctx);
6416 have_ca_chain = 1;
6417
6418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006419 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006420 chain,
6421 ssl->conf->f_ca_cb,
6422 ssl->conf->p_ca_cb,
6423 ssl->conf->cert_profile,
6424 ssl->hostname,
6425 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006426 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006427 }
6428 else
6429#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6430 {
6431 mbedtls_x509_crt *ca_chain;
6432 mbedtls_x509_crl *ca_crl;
6433
6434#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6435 if( ssl->handshake->sni_ca_chain != NULL )
6436 {
6437 ca_chain = ssl->handshake->sni_ca_chain;
6438 ca_crl = ssl->handshake->sni_ca_crl;
6439 }
6440 else
6441#endif
6442 {
6443 ca_chain = ssl->conf->ca_chain;
6444 ca_crl = ssl->conf->ca_crl;
6445 }
6446
6447 if( ca_chain != NULL )
6448 have_ca_chain = 1;
6449
6450 ret = mbedtls_x509_crt_verify_restartable(
6451 chain,
6452 ca_chain, ca_crl,
6453 ssl->conf->cert_profile,
6454 ssl->hostname,
6455 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006456 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006457 }
Hanno Becker68636192019-02-05 14:36:34 +00006458
6459 if( ret != 0 )
6460 {
6461 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6462 }
6463
6464#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6465 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6466 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6467#endif
6468
6469 /*
6470 * Secondary checks: always done, but change 'ret' only if it was 0
6471 */
6472
6473#if defined(MBEDTLS_ECP_C)
6474 {
6475 const mbedtls_pk_context *pk = &chain->pk;
6476
6477 /* If certificate uses an EC key, make sure the curve is OK */
6478 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6479 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6480 {
6481 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6482
6483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6484 if( ret == 0 )
6485 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6486 }
6487 }
6488#endif /* MBEDTLS_ECP_C */
6489
6490 if( mbedtls_ssl_check_cert_usage( chain,
6491 ciphersuite_info,
6492 ! ssl->conf->endpoint,
6493 &ssl->session_negotiate->verify_result ) != 0 )
6494 {
6495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6496 if( ret == 0 )
6497 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6498 }
6499
6500 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6501 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6502 * with details encoded in the verification flags. All other kinds
6503 * of error codes, including those from the user provided f_vrfy
6504 * functions, are treated as fatal and lead to a failure of
6505 * ssl_parse_certificate even if verification was optional. */
6506 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6507 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6508 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6509 {
6510 ret = 0;
6511 }
6512
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006513 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006514 {
6515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6516 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6517 }
6518
6519 if( ret != 0 )
6520 {
6521 uint8_t alert;
6522
6523 /* The certificate may have been rejected for several reasons.
6524 Pick one and send the corresponding alert. Which alert to send
6525 may be a subject of debate in some cases. */
6526 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6527 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6528 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6529 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6530 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6531 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6532 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6533 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6534 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6535 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6536 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6537 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6538 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6539 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6540 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6541 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6542 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6543 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6544 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6545 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6546 else
6547 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6548 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6549 alert );
6550 }
6551
6552#if defined(MBEDTLS_DEBUG_C)
6553 if( ssl->session_negotiate->verify_result != 0 )
6554 {
6555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6556 ssl->session_negotiate->verify_result ) );
6557 }
6558 else
6559 {
6560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6561 }
6562#endif /* MBEDTLS_DEBUG_C */
6563
6564 return( ret );
6565}
6566
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006567#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6568static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6569 unsigned char *start, size_t len )
6570{
6571 int ret;
6572 /* Remember digest of the peer's end-CRT. */
6573 ssl->session_negotiate->peer_cert_digest =
6574 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6575 if( ssl->session_negotiate->peer_cert_digest == NULL )
6576 {
6577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6578 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6579 mbedtls_ssl_send_alert_message( ssl,
6580 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6581 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6582
6583 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6584 }
6585
6586 ret = mbedtls_md( mbedtls_md_info_from_type(
6587 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6588 start, len,
6589 ssl->session_negotiate->peer_cert_digest );
6590
6591 ssl->session_negotiate->peer_cert_digest_type =
6592 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6593 ssl->session_negotiate->peer_cert_digest_len =
6594 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6595
6596 return( ret );
6597}
6598
6599static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6600 unsigned char *start, size_t len )
6601{
6602 unsigned char *end = start + len;
6603 int ret;
6604
6605 /* Make a copy of the peer's raw public key. */
6606 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6607 ret = mbedtls_pk_parse_subpubkey( &start, end,
6608 &ssl->handshake->peer_pubkey );
6609 if( ret != 0 )
6610 {
6611 /* We should have parsed the public key before. */
6612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6613 }
6614
6615 return( 0 );
6616}
6617#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6618
Hanno Becker68636192019-02-05 14:36:34 +00006619int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6620{
6621 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006622 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006623#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6624 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6625 ? ssl->handshake->sni_authmode
6626 : ssl->conf->authmode;
6627#else
6628 const int authmode = ssl->conf->authmode;
6629#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006630 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006631 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006632
6633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6634
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006635 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6636 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006637 {
6638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006639 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006640 }
6641
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006642#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6643 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006644 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006645 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006646 chain = ssl->handshake->ecrs_peer_cert;
6647 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006648 goto crt_verify;
6649 }
6650#endif
6651
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006652 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006653 {
6654 /* mbedtls_ssl_read_record may have sent an alert already. We
6655 let it decide whether to alert. */
6656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006657 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006658 }
6659
Hanno Becker4a55f632019-02-05 12:49:06 +00006660#if defined(MBEDTLS_SSL_SRV_C)
6661 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6662 {
6663 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006664
6665 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006666 ret = 0;
6667 else
6668 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006669
Hanno Becker6bdfab22019-02-05 13:11:17 +00006670 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006671 }
6672#endif /* MBEDTLS_SSL_SRV_C */
6673
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006674 /* Clear existing peer CRT structure in case we tried to
6675 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006676 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006677
6678 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6679 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006680 {
6681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6682 sizeof( mbedtls_x509_crt ) ) );
6683 mbedtls_ssl_send_alert_message( ssl,
6684 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6685 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006686
Hanno Becker3dad3112019-02-05 17:19:52 +00006687 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6688 goto exit;
6689 }
6690 mbedtls_x509_crt_init( chain );
6691
6692 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006693 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006694 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006695
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006696#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6697 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006698 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006699
6700crt_verify:
6701 if( ssl->handshake->ecrs_enabled)
6702 rs_ctx = &ssl->handshake->ecrs_ctx;
6703#endif
6704
Hanno Becker68636192019-02-05 14:36:34 +00006705 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006706 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006707 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006708 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006709
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006710#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006711 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006712 unsigned char *crt_start, *pk_start;
6713 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006714
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006715 /* We parse the CRT chain without copying, so
6716 * these pointers point into the input buffer,
6717 * and are hence still valid after freeing the
6718 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006719
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006720 crt_start = chain->raw.p;
6721 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006722
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006723 pk_start = chain->pk_raw.p;
6724 pk_len = chain->pk_raw.len;
6725
6726 /* Free the CRT structures before computing
6727 * digest and copying the peer's public key. */
6728 mbedtls_x509_crt_free( chain );
6729 mbedtls_free( chain );
6730 chain = NULL;
6731
6732 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006733 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006734 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006735
6736 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6737 if( ret != 0 )
6738 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006739 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006740#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6741 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006742 ssl->session_negotiate->peer_cert = chain;
6743 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006744#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006747
Hanno Becker6bdfab22019-02-05 13:11:17 +00006748exit:
6749
Hanno Becker3dad3112019-02-05 17:19:52 +00006750 if( ret == 0 )
6751 ssl->state++;
6752
6753#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6754 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6755 {
6756 ssl->handshake->ecrs_peer_cert = chain;
6757 chain = NULL;
6758 }
6759#endif
6760
6761 if( chain != NULL )
6762 {
6763 mbedtls_x509_crt_free( chain );
6764 mbedtls_free( chain );
6765 }
6766
Paul Bakker5121ce52009-01-03 21:22:43 +00006767 return( ret );
6768}
Hanno Becker21489932019-02-05 13:20:55 +00006769#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006772{
6773 int ret;
6774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 ssl->out_msglen = 1;
6779 ssl->out_msg[0] = 1;
6780
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 ssl->state++;
6782
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006783 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 return( ret );
6787 }
6788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
6791 return( 0 );
6792}
6793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006795{
6796 int ret;
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006799
Hanno Becker327c93b2018-08-15 13:56:18 +01006800 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 return( ret );
6804 }
6805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6810 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812 }
6813
Hanno Beckere678eaa2018-08-21 14:57:46 +01006814 /* CCS records are only accepted if they have length 1 and content '1',
6815 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006817 /*
6818 * Switch to our negotiated transform and session parameters for inbound
6819 * data.
6820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006822 ssl->transform_in = ssl->transform_negotiate;
6823 ssl->session_in = ssl->session_negotiate;
6824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006829 ssl_dtls_replay_reset( ssl );
6830#endif
6831
6832 /* Increment epoch */
6833 if( ++ssl->in_epoch == 0 )
6834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006836 /* This is highly unlikely to happen for legitimate reasons, so
6837 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006839 }
6840 }
6841 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006843 memset( ssl->in_ctr, 0, 8 );
6844
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006845 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6848 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6854 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006856 }
6857 }
6858#endif
6859
Paul Bakker5121ce52009-01-03 21:22:43 +00006860 ssl->state++;
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
6864 return( 0 );
6865}
6866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6868 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006869{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006870 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6873 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6874 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006875 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006876 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006877#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6879#if defined(MBEDTLS_SHA512_C)
6880 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006881 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6882 else
6883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884#if defined(MBEDTLS_SHA256_C)
6885 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006886 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006887 else
6888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006892 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006893 }
Paul Bakker380da532012-04-18 16:10:25 +00006894}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6899 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006900 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6901 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6904#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006905#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006906 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006907 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6908#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006909 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006910#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006913#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006914 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006915 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006916#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006917 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006918#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006921}
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006924 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6927 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006928 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6929 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6932#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006933#if defined(MBEDTLS_USE_PSA_CRYPTO)
6934 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6935#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006936 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006937#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006940#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006941 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006942#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006943 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006944#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006947}
6948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6950 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6951static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006952 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006953{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006954 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6955 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006956}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006957#endif
Paul Bakker380da532012-04-18 16:10:25 +00006958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6960#if defined(MBEDTLS_SHA256_C)
6961static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006962 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006963{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006964#if defined(MBEDTLS_USE_PSA_CRYPTO)
6965 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6966#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006967 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006968#endif
Paul Bakker380da532012-04-18 16:10:25 +00006969}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006970#endif
Paul Bakker380da532012-04-18 16:10:25 +00006971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#if defined(MBEDTLS_SHA512_C)
6973static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006974 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006975{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006976#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006977 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006978#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006979 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006980#endif
Paul Bakker380da532012-04-18 16:10:25 +00006981}
Paul Bakker769075d2012-11-24 11:26:46 +01006982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006986static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006988{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006989 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006990 mbedtls_md5_context md5;
6991 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006992
Paul Bakker5121ce52009-01-03 21:22:43 +00006993 unsigned char padbuf[48];
6994 unsigned char md5sum[16];
6995 unsigned char sha1sum[20];
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006998 if( !session )
6999 session = ssl->session;
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007002
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007003 mbedtls_md5_init( &md5 );
7004 mbedtls_sha1_init( &sha1 );
7005
7006 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7007 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007008
7009 /*
7010 * SSLv3:
7011 * hash =
7012 * MD5( master + pad2 +
7013 * MD5( handshake + sender + master + pad1 ) )
7014 * + SHA1( master + pad2 +
7015 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007016 */
7017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007019 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7020 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007021#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007024 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7025 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007029 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
Paul Bakker1ef83d62012-04-11 12:09:53 +00007031 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007032
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007033 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7034 mbedtls_md5_update_ret( &md5, session->master, 48 );
7035 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7036 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007037
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007038 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7039 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7040 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7041 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007042
Paul Bakker1ef83d62012-04-11 12:09:53 +00007043 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007045 mbedtls_md5_starts_ret( &md5 );
7046 mbedtls_md5_update_ret( &md5, session->master, 48 );
7047 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7048 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7049 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007051 mbedtls_sha1_starts_ret( &sha1 );
7052 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7053 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7054 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7055 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007059 mbedtls_md5_free( &md5 );
7060 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007061
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007062 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7063 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7064 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007071static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007073{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007074 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007075 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007076 mbedtls_md5_context md5;
7077 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007078 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007081 if( !session )
7082 session = ssl->session;
7083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007085
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007086 mbedtls_md5_init( &md5 );
7087 mbedtls_sha1_init( &sha1 );
7088
7089 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7090 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007091
Paul Bakker1ef83d62012-04-11 12:09:53 +00007092 /*
7093 * TLSv1:
7094 * hash = PRF( master, finished_label,
7095 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7096 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007099 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7100 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007101#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007104 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7105 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007106#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007109 ? "client finished"
7110 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007111
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007112 mbedtls_md5_finish_ret( &md5, padbuf );
7113 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007114
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007115 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007116 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007119
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007120 mbedtls_md5_free( &md5 );
7121 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007122
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007123 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7130#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007131static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133{
7134 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007135 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007136 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007137#if defined(MBEDTLS_USE_PSA_CRYPTO)
7138 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007139 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007140 psa_status_t status;
7141#else
7142 mbedtls_sha256_context sha256;
7143#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007146 if( !session )
7147 session = ssl->session;
7148
Andrzej Kurekeb342242019-01-29 09:14:33 -05007149 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7150 ? "client finished"
7151 : "server finished";
7152
7153#if defined(MBEDTLS_USE_PSA_CRYPTO)
7154 sha256_psa = psa_hash_operation_init();
7155
7156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7157
7158 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7159 if( status != PSA_SUCCESS )
7160 {
7161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7162 return;
7163 }
7164
7165 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7166 if( status != PSA_SUCCESS )
7167 {
7168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7169 return;
7170 }
7171 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7172#else
7173
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007174 mbedtls_sha256_init( &sha256 );
7175
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007177
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007178 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007179
7180 /*
7181 * TLSv1.2:
7182 * hash = PRF( master, finished_label,
7183 * Hash( handshake ) )[0.11]
7184 */
7185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if !defined(MBEDTLS_SHA256_ALT)
7187 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007188 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007189#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007191 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007192 mbedtls_sha256_free( &sha256 );
7193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007194
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007195 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007196 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007199
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007200 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007207static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007209{
7210 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007211 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007212 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007213#if defined(MBEDTLS_USE_PSA_CRYPTO)
7214 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007215 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007216 psa_status_t status;
7217#else
7218 mbedtls_sha512_context sha512;
7219#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007222 if( !session )
7223 session = ssl->session;
7224
Andrzej Kurekeb342242019-01-29 09:14:33 -05007225 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7226 ? "client finished"
7227 : "server finished";
7228
7229#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007230 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007231
7232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7233
Andrzej Kurek972fba52019-01-30 03:29:12 -05007234 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007235 if( status != PSA_SUCCESS )
7236 {
7237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7238 return;
7239 }
7240
Andrzej Kurek972fba52019-01-30 03:29:12 -05007241 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007242 if( status != PSA_SUCCESS )
7243 {
7244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7245 return;
7246 }
7247 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7248#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007249 mbedtls_sha512_init( &sha512 );
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007252
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007253 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007254
7255 /*
7256 * TLSv1.2:
7257 * hash = PRF( master, finished_label,
7258 * Hash( handshake ) )[0.11]
7259 */
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007262 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7263 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007264#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007265
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007266 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007267 mbedtls_sha512_free( &sha512 );
7268#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007269
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007270 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007271 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007274
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007275 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#endif /* MBEDTLS_SHA512_C */
7280#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007285
7286 /*
7287 * Free our handshake params
7288 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007289 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007291 ssl->handshake = NULL;
7292
7293 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007294 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007295 */
7296 if( ssl->transform )
7297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 mbedtls_ssl_transform_free( ssl->transform );
7299 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007300 }
7301 ssl->transform = ssl->transform_negotiate;
7302 ssl->transform_negotiate = NULL;
7303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007305}
7306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007308{
7309 int resume = ssl->handshake->resume;
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313#if defined(MBEDTLS_SSL_RENEGOTIATION)
7314 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007317 ssl->renego_records_seen = 0;
7318 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007319#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007320
7321 /*
7322 * Free the previous session and switch in the current one
7323 */
Paul Bakker0a597072012-09-25 21:55:46 +00007324 if( ssl->session )
7325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007327 /* RFC 7366 3.1: keep the EtM state */
7328 ssl->session_negotiate->encrypt_then_mac =
7329 ssl->session->encrypt_then_mac;
7330#endif
7331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 mbedtls_ssl_session_free( ssl->session );
7333 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007334 }
7335 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007336 ssl->session_negotiate = NULL;
7337
Paul Bakker0a597072012-09-25 21:55:46 +00007338 /*
7339 * Add cache entry
7340 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007341 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007342 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007343 resume == 0 )
7344 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007345 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007347 }
Paul Bakker0a597072012-09-25 21:55:46 +00007348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007351 ssl->handshake->flight != NULL )
7352 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007353 /* Cancel handshake timer */
7354 ssl_set_timer( ssl, 0 );
7355
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007356 /* Keep last flight around in case we need to resend it:
7357 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007359 }
7360 else
7361#endif
7362 ssl_handshake_wrapup_free_hs_transform( ssl );
7363
Paul Bakker48916f92012-09-16 19:57:18 +00007364 ssl->state++;
7365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007367}
7368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007370{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007371 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007374
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007375 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007376
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007377 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007379 /*
7380 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7381 * may define some other value. Currently (early 2016), no defined
7382 * ciphersuite does this (and this is unlikely to change as activity has
7383 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007388 ssl->verify_data_len = hash_len;
7389 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007390#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007391
Paul Bakker5121ce52009-01-03 21:22:43 +00007392 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7394 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
7396 /*
7397 * In case of session resuming, invert the client and server
7398 * ChangeCipherSpec messages order.
7399 */
Paul Bakker0a597072012-09-25 21:55:46 +00007400 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007403 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007407 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007409#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007410 }
7411 else
7412 ssl->state++;
7413
Paul Bakker48916f92012-09-16 19:57:18 +00007414 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007415 * Switch to our negotiated transform and session parameters for outbound
7416 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007422 {
7423 unsigned char i;
7424
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007425 /* Remember current epoch settings for resending */
7426 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007427 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007428
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007429 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007430 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007431
7432 /* Increment epoch */
7433 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007434 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007435 break;
7436
7437 /* The loop goes to its end iff the counter is wrapping */
7438 if( i == 0 )
7439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7441 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007442 }
7443 }
7444 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007446 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007447
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007448 ssl->transform_out = ssl->transform_negotiate;
7449 ssl->session_out = ssl->session_negotiate;
7450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7452 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7457 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007458 }
7459 }
7460#endif
7461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007465#endif
7466
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007467 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007468 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007470 return( ret );
7471 }
7472
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007473#if defined(MBEDTLS_SSL_PROTO_DTLS)
7474 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7475 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7476 {
7477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7478 return( ret );
7479 }
7480#endif
7481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007483
7484 return( 0 );
7485}
7486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007488#define SSL_MAX_HASH_LEN 36
7489#else
7490#define SSL_MAX_HASH_LEN 12
7491#endif
7492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007494{
Paul Bakker23986e52011-04-24 08:57:21 +00007495 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007496 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007497 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007500
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007501 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
Hanno Becker327c93b2018-08-15 13:56:18 +01007503 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 return( ret );
7507 }
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007512 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7513 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007515 }
7516
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007517 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SSL_PROTO_SSL3)
7519 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007520 hash_len = 36;
7521 else
7522#endif
7523 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7526 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7530 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007532 }
7533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007535 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007538 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7539 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007541 }
7542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007544 ssl->verify_data_len = hash_len;
7545 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007546#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007547
Paul Bakker0a597072012-09-25 21:55:46 +00007548 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007551 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007558 }
7559 else
7560 ssl->state++;
7561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007565#endif
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007568
7569 return( 0 );
7570}
7571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007573{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7577 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7578 mbedtls_md5_init( &handshake->fin_md5 );
7579 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007580 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7581 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7584#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585#if defined(MBEDTLS_USE_PSA_CRYPTO)
7586 handshake->fin_sha256_psa = psa_hash_operation_init();
7587 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7588#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007590 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007591#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007594#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007595 handshake->fin_sha384_psa = psa_hash_operation_init();
7596 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007597#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007599 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007600#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007603
7604 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007605
7606#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7607 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7608 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7609#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#if defined(MBEDTLS_DHM_C)
7612 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_ECDH_C)
7615 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007616#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007617#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007618 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007619#if defined(MBEDTLS_SSL_CLI_C)
7620 handshake->ecjpake_cache = NULL;
7621 handshake->ecjpake_cache_len = 0;
7622#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007623#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007624
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007625#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007626 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007627#endif
7628
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007629#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7630 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7631#endif
Hanno Becker75173122019-02-06 16:18:31 +00007632
7633#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7634 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7635 mbedtls_pk_init( &handshake->peer_pubkey );
7636#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007637}
7638
Hanno Beckera18d1322018-01-03 14:27:32 +00007639void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7644 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007645
Hanno Beckerd56ed242018-01-03 15:32:51 +00007646#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 mbedtls_md_init( &transform->md_ctx_enc );
7648 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007649#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007650}
7651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007655}
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007658{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007659 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007660 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007662 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007664 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007665 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007666
7667 /*
7668 * Either the pointers are now NULL or cleared properly and can be freed.
7669 * Now allocate missing structures.
7670 */
7671 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007672 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007673 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007674 }
Paul Bakker48916f92012-09-16 19:57:18 +00007675
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007676 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007677 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007678 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007679 }
Paul Bakker48916f92012-09-16 19:57:18 +00007680
Paul Bakker82788fb2014-10-20 13:59:19 +02007681 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007682 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007683 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007684 }
Paul Bakker48916f92012-09-16 19:57:18 +00007685
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007686 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007687 if( ssl->handshake == NULL ||
7688 ssl->transform_negotiate == NULL ||
7689 ssl->session_negotiate == NULL )
7690 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 mbedtls_free( ssl->handshake );
7694 mbedtls_free( ssl->transform_negotiate );
7695 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007696
7697 ssl->handshake = NULL;
7698 ssl->transform_negotiate = NULL;
7699 ssl->session_negotiate = NULL;
7700
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007701 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007702 }
7703
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007704 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007706 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007707 ssl_handshake_params_init( ssl->handshake );
7708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7711 {
7712 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007713
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007714 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7715 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7716 else
7717 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007718
7719 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007720 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007721#endif
7722
Paul Bakker48916f92012-09-16 19:57:18 +00007723 return( 0 );
7724}
7725
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007726#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007727/* Dummy cookie callbacks for defaults */
7728static int ssl_cookie_write_dummy( void *ctx,
7729 unsigned char **p, unsigned char *end,
7730 const unsigned char *cli_id, size_t cli_id_len )
7731{
7732 ((void) ctx);
7733 ((void) p);
7734 ((void) end);
7735 ((void) cli_id);
7736 ((void) cli_id_len);
7737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007739}
7740
7741static int ssl_cookie_check_dummy( void *ctx,
7742 const unsigned char *cookie, size_t cookie_len,
7743 const unsigned char *cli_id, size_t cli_id_len )
7744{
7745 ((void) ctx);
7746 ((void) cookie);
7747 ((void) cookie_len);
7748 ((void) cli_id);
7749 ((void) cli_id_len);
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007752}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007753#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007754
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007755/* Once ssl->out_hdr as the address of the beginning of the
7756 * next outgoing record is set, deduce the other pointers.
7757 *
7758 * Note: For TLS, we save the implicit record sequence number
7759 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7760 * and the caller has to make sure there's space for this.
7761 */
7762
7763static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7764 mbedtls_ssl_transform *transform )
7765{
7766#if defined(MBEDTLS_SSL_PROTO_DTLS)
7767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7768 {
7769 ssl->out_ctr = ssl->out_hdr + 3;
7770 ssl->out_len = ssl->out_hdr + 11;
7771 ssl->out_iv = ssl->out_hdr + 13;
7772 }
7773 else
7774#endif
7775 {
7776 ssl->out_ctr = ssl->out_hdr - 8;
7777 ssl->out_len = ssl->out_hdr + 3;
7778 ssl->out_iv = ssl->out_hdr + 5;
7779 }
7780
7781 /* Adjust out_msg to make space for explicit IV, if used. */
7782 if( transform != NULL &&
7783 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7784 {
7785 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7786 }
7787 else
7788 ssl->out_msg = ssl->out_iv;
7789}
7790
7791/* Once ssl->in_hdr as the address of the beginning of the
7792 * next incoming record is set, deduce the other pointers.
7793 *
7794 * Note: For TLS, we save the implicit record sequence number
7795 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7796 * and the caller has to make sure there's space for this.
7797 */
7798
7799static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7800 mbedtls_ssl_transform *transform )
7801{
7802#if defined(MBEDTLS_SSL_PROTO_DTLS)
7803 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7804 {
7805 ssl->in_ctr = ssl->in_hdr + 3;
7806 ssl->in_len = ssl->in_hdr + 11;
7807 ssl->in_iv = ssl->in_hdr + 13;
7808 }
7809 else
7810#endif
7811 {
7812 ssl->in_ctr = ssl->in_hdr - 8;
7813 ssl->in_len = ssl->in_hdr + 3;
7814 ssl->in_iv = ssl->in_hdr + 5;
7815 }
7816
7817 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7818 if( transform != NULL &&
7819 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7820 {
7821 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7822 }
7823 else
7824 ssl->in_msg = ssl->in_iv;
7825}
7826
Paul Bakker5121ce52009-01-03 21:22:43 +00007827/*
7828 * Initialize an SSL context
7829 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007830void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7831{
7832 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7833}
7834
7835/*
7836 * Setup an SSL context
7837 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007838
7839static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7840{
7841 /* Set the incoming and outgoing record pointers. */
7842#if defined(MBEDTLS_SSL_PROTO_DTLS)
7843 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7844 {
7845 ssl->out_hdr = ssl->out_buf;
7846 ssl->in_hdr = ssl->in_buf;
7847 }
7848 else
7849#endif /* MBEDTLS_SSL_PROTO_DTLS */
7850 {
7851 ssl->out_hdr = ssl->out_buf + 8;
7852 ssl->in_hdr = ssl->in_buf + 8;
7853 }
7854
7855 /* Derive other internal pointers. */
7856 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7857 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7858}
7859
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007860int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007861 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862{
Paul Bakker48916f92012-09-16 19:57:18 +00007863 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007864
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007865 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007866
7867 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007868 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007869 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007870
7871 /* Set to NULL in case of an error condition */
7872 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007873
Angus Grattond8213d02016-05-25 20:56:48 +10007874 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7875 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007876 {
Angus Grattond8213d02016-05-25 20:56:48 +10007877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007878 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007879 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007880 }
7881
7882 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7883 if( ssl->out_buf == NULL )
7884 {
7885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007886 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007887 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 }
7889
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007890 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007891
Paul Bakker48916f92012-09-16 19:57:18 +00007892 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007893 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007894
7895 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007896
7897error:
7898 mbedtls_free( ssl->in_buf );
7899 mbedtls_free( ssl->out_buf );
7900
7901 ssl->conf = NULL;
7902
7903 ssl->in_buf = NULL;
7904 ssl->out_buf = NULL;
7905
7906 ssl->in_hdr = NULL;
7907 ssl->in_ctr = NULL;
7908 ssl->in_len = NULL;
7909 ssl->in_iv = NULL;
7910 ssl->in_msg = NULL;
7911
7912 ssl->out_hdr = NULL;
7913 ssl->out_ctr = NULL;
7914 ssl->out_len = NULL;
7915 ssl->out_iv = NULL;
7916 ssl->out_msg = NULL;
7917
k-stachowiak9f7798e2018-07-31 16:52:32 +02007918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007919}
7920
7921/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007922 * Reset an initialized and used SSL context for re-use while retaining
7923 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007924 *
7925 * If partial is non-zero, keep data in the input buffer and client ID.
7926 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007927 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007928static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007929{
Paul Bakker48916f92012-09-16 19:57:18 +00007930 int ret;
7931
Hanno Becker7e772132018-08-10 12:38:21 +01007932#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7933 !defined(MBEDTLS_SSL_SRV_C)
7934 ((void) partial);
7935#endif
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007938
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007939 /* Cancel any possibly running timer */
7940 ssl_set_timer( ssl, 0 );
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942#if defined(MBEDTLS_SSL_RENEGOTIATION)
7943 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007944 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007945
7946 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7948 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007951
Paul Bakker7eb013f2011-10-06 12:37:39 +00007952 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007953 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007954
7955 ssl->in_msgtype = 0;
7956 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007958 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007959 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007962 ssl_dtls_replay_reset( ssl );
7963#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007964
7965 ssl->in_hslen = 0;
7966 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007967
7968 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007969
7970 ssl->out_msgtype = 0;
7971 ssl->out_msglen = 0;
7972 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7974 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007975 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007976#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007977
Hanno Becker19859472018-08-06 09:40:20 +01007978 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7979
Paul Bakker48916f92012-09-16 19:57:18 +00007980 ssl->transform_in = NULL;
7981 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007982
Hanno Becker78640902018-08-13 16:35:15 +01007983 ssl->session_in = NULL;
7984 ssl->session_out = NULL;
7985
Angus Grattond8213d02016-05-25 20:56:48 +10007986 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007987
7988#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007989 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007990#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7991 {
7992 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007993 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007994 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7997 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8000 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8003 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008004 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008005 }
8006#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008007
Paul Bakker48916f92012-09-16 19:57:18 +00008008 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 mbedtls_ssl_transform_free( ssl->transform );
8011 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008012 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008013 }
Paul Bakker48916f92012-09-16 19:57:18 +00008014
Paul Bakkerc0463502013-02-14 11:19:38 +01008015 if( ssl->session )
8016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 mbedtls_ssl_session_free( ssl->session );
8018 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008019 ssl->session = NULL;
8020 }
8021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008023 ssl->alpn_chosen = NULL;
8024#endif
8025
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008026#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008027#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008028 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008029#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008030 {
8031 mbedtls_free( ssl->cli_id );
8032 ssl->cli_id = NULL;
8033 ssl->cli_id_len = 0;
8034 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008035#endif
8036
Paul Bakker48916f92012-09-16 19:57:18 +00008037 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8038 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008039
8040 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008041}
8042
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008043/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008044 * Reset an initialized and used SSL context for re-use while retaining
8045 * all application-set variables, function pointers and data.
8046 */
8047int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8048{
8049 return( ssl_session_reset_int( ssl, 0 ) );
8050}
8051
8052/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008053 * SSL set accessors
8054 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008055void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008057 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008058}
8059
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008060void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008061{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008062 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008063}
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008066void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008067{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008068 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008069}
8070#endif
8071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008073void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008075 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008076}
8077#endif
8078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008080
Hanno Becker1841b0a2018-08-24 11:13:57 +01008081void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8082 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008083{
8084 ssl->disable_datagram_packing = !allow_packing;
8085}
8086
8087void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8088 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008089{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008090 conf->hs_timeout_min = min;
8091 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008092}
8093#endif
8094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008095void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008097 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008098}
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008101void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008102 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008103 void *p_vrfy )
8104{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008105 conf->f_vrfy = f_vrfy;
8106 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008111 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008112 void *p_rng )
8113{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008114 conf->f_rng = f_rng;
8115 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008116}
8117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008118void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008119 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008120 void *p_dbg )
8121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008122 conf->f_dbg = f_dbg;
8123 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008124}
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008127 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008128 mbedtls_ssl_send_t *f_send,
8129 mbedtls_ssl_recv_t *f_recv,
8130 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008131{
8132 ssl->p_bio = p_bio;
8133 ssl->f_send = f_send;
8134 ssl->f_recv = f_recv;
8135 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008136}
8137
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008138#if defined(MBEDTLS_SSL_PROTO_DTLS)
8139void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8140{
8141 ssl->mtu = mtu;
8142}
8143#endif
8144
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008145void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008146{
8147 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008148}
8149
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008150void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8151 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008152 mbedtls_ssl_set_timer_t *f_set_timer,
8153 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008154{
8155 ssl->p_timer = p_timer;
8156 ssl->f_set_timer = f_set_timer;
8157 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008158
8159 /* Make sure we start with no timer running */
8160 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008161}
8162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008164void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008165 void *p_cache,
8166 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8167 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008168{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008169 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008170 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008171 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SSL_CLI_C)
8176int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008177{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008178 int ret;
8179
8180 if( ssl == NULL ||
8181 session == NULL ||
8182 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008183 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008186 }
8187
Hanno Becker52055ae2019-02-06 14:30:46 +00008188 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8189 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008190 return( ret );
8191
Paul Bakker0a597072012-09-25 21:55:46 +00008192 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008193
8194 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008197
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008198void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008199 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008200{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008201 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8202 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008205}
8206
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008207void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008208 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008209 int major, int minor )
8210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008212 return;
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008215 return;
8216
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008217 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008218}
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008221void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008222 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008223{
8224 conf->cert_profile = profile;
8225}
8226
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008227/* Append a new keycert entry to a (possibly empty) list */
8228static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8229 mbedtls_x509_crt *cert,
8230 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008231{
niisato8ee24222018-06-25 19:05:48 +09008232 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008233
niisato8ee24222018-06-25 19:05:48 +09008234 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8235 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008236 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008237
niisato8ee24222018-06-25 19:05:48 +09008238 new_cert->cert = cert;
8239 new_cert->key = key;
8240 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008241
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008242 /* Update head is the list was null, else add to the end */
8243 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008244 {
niisato8ee24222018-06-25 19:05:48 +09008245 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008246 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008247 else
8248 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008249 mbedtls_ssl_key_cert *cur = *head;
8250 while( cur->next != NULL )
8251 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008252 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008253 }
8254
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008255 return( 0 );
8256}
8257
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008258int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008259 mbedtls_x509_crt *own_cert,
8260 mbedtls_pk_context *pk_key )
8261{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008262 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008263}
8264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008265void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008266 mbedtls_x509_crt *ca_chain,
8267 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008268{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008269 conf->ca_chain = ca_chain;
8270 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008271
8272#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8273 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8274 * cannot be used together. */
8275 conf->f_ca_cb = NULL;
8276 conf->p_ca_cb = NULL;
8277#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008278}
Hanno Becker5adaad92019-03-27 16:54:37 +00008279
8280#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8281void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008282 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008283 void *p_ca_cb )
8284{
8285 conf->f_ca_cb = f_ca_cb;
8286 conf->p_ca_cb = p_ca_cb;
8287
8288 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8289 * cannot be used together. */
8290 conf->ca_chain = NULL;
8291 conf->ca_crl = NULL;
8292}
8293#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008295
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008296#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8297int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8298 mbedtls_x509_crt *own_cert,
8299 mbedtls_pk_context *pk_key )
8300{
8301 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8302 own_cert, pk_key ) );
8303}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008304
8305void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8306 mbedtls_x509_crt *ca_chain,
8307 mbedtls_x509_crl *ca_crl )
8308{
8309 ssl->handshake->sni_ca_chain = ca_chain;
8310 ssl->handshake->sni_ca_crl = ca_crl;
8311}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008312
8313void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8314 int authmode )
8315{
8316 ssl->handshake->sni_authmode = authmode;
8317}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008318#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8319
Hanno Becker8927c832019-04-03 12:52:50 +01008320#if defined(MBEDTLS_X509_CRT_PARSE_C)
8321void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8322 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8323 void *p_vrfy )
8324{
8325 ssl->f_vrfy = f_vrfy;
8326 ssl->p_vrfy = p_vrfy;
8327}
8328#endif
8329
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008330#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008331/*
8332 * Set EC J-PAKE password for current handshake
8333 */
8334int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8335 const unsigned char *pw,
8336 size_t pw_len )
8337{
8338 mbedtls_ecjpake_role role;
8339
Janos Follath8eb64132016-06-03 15:40:57 +01008340 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8342
8343 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8344 role = MBEDTLS_ECJPAKE_SERVER;
8345 else
8346 role = MBEDTLS_ECJPAKE_CLIENT;
8347
8348 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8349 role,
8350 MBEDTLS_MD_SHA256,
8351 MBEDTLS_ECP_DP_SECP256R1,
8352 pw, pw_len ) );
8353}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008354#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008357
8358static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8359{
8360 /* Remove reference to existing PSK, if any. */
8361#if defined(MBEDTLS_USE_PSA_CRYPTO)
8362 if( conf->psk_opaque != 0 )
8363 {
8364 /* The maintenance of the PSK key slot is the
8365 * user's responsibility. */
8366 conf->psk_opaque = 0;
8367 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008368 /* This and the following branch should never
8369 * be taken simultaenously as we maintain the
8370 * invariant that raw and opaque PSKs are never
8371 * configured simultaneously. As a safeguard,
8372 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008373#endif /* MBEDTLS_USE_PSA_CRYPTO */
8374 if( conf->psk != NULL )
8375 {
8376 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8377
8378 mbedtls_free( conf->psk );
8379 conf->psk = NULL;
8380 conf->psk_len = 0;
8381 }
8382
8383 /* Remove reference to PSK identity, if any. */
8384 if( conf->psk_identity != NULL )
8385 {
8386 mbedtls_free( conf->psk_identity );
8387 conf->psk_identity = NULL;
8388 conf->psk_identity_len = 0;
8389 }
8390}
8391
Hanno Becker7390c712018-11-15 13:33:04 +00008392/* This function assumes that PSK identity in the SSL config is unset.
8393 * It checks that the provided identity is well-formed and attempts
8394 * to make a copy of it in the SSL config.
8395 * On failure, the PSK identity in the config remains unset. */
8396static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8397 unsigned char const *psk_identity,
8398 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008399{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008400 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008401 if( psk_identity == NULL ||
8402 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008403 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008404 {
8405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8406 }
8407
Hanno Becker7390c712018-11-15 13:33:04 +00008408 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8409 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008411
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008412 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008413 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008414
8415 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008416}
8417
Hanno Becker7390c712018-11-15 13:33:04 +00008418int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8419 const unsigned char *psk, size_t psk_len,
8420 const unsigned char *psk_identity, size_t psk_identity_len )
8421{
8422 int ret;
8423 /* Remove opaque/raw PSK + PSK Identity */
8424 ssl_conf_remove_psk( conf );
8425
8426 /* Check and set raw PSK */
8427 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8428 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8429 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8430 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8431 conf->psk_len = psk_len;
8432 memcpy( conf->psk, psk, conf->psk_len );
8433
8434 /* Check and set PSK Identity */
8435 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8436 if( ret != 0 )
8437 ssl_conf_remove_psk( conf );
8438
8439 return( ret );
8440}
8441
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008442static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8443{
8444#if defined(MBEDTLS_USE_PSA_CRYPTO)
8445 if( ssl->handshake->psk_opaque != 0 )
8446 {
8447 ssl->handshake->psk_opaque = 0;
8448 }
8449 else
8450#endif /* MBEDTLS_USE_PSA_CRYPTO */
8451 if( ssl->handshake->psk != NULL )
8452 {
8453 mbedtls_platform_zeroize( ssl->handshake->psk,
8454 ssl->handshake->psk_len );
8455 mbedtls_free( ssl->handshake->psk );
8456 ssl->handshake->psk_len = 0;
8457 }
8458}
8459
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008460int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8461 const unsigned char *psk, size_t psk_len )
8462{
8463 if( psk == NULL || ssl->handshake == NULL )
8464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8465
8466 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8468
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008469 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008470
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008471 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008472 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008473
8474 ssl->handshake->psk_len = psk_len;
8475 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8476
8477 return( 0 );
8478}
8479
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008480#if defined(MBEDTLS_USE_PSA_CRYPTO)
8481int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008482 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008483 const unsigned char *psk_identity,
8484 size_t psk_identity_len )
8485{
Hanno Becker7390c712018-11-15 13:33:04 +00008486 int ret;
8487 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008488 ssl_conf_remove_psk( conf );
8489
Hanno Becker7390c712018-11-15 13:33:04 +00008490 /* Check and set opaque PSK */
8491 if( psk_slot == 0 )
8492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008493 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008494
8495 /* Check and set PSK Identity */
8496 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8497 psk_identity_len );
8498 if( ret != 0 )
8499 ssl_conf_remove_psk( conf );
8500
8501 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008502}
8503
8504int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008505 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008506{
8507 if( psk_slot == 0 || ssl->handshake == NULL )
8508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8509
8510 ssl_remove_psk( ssl );
8511 ssl->handshake->psk_opaque = psk_slot;
8512 return( 0 );
8513}
8514#endif /* MBEDTLS_USE_PSA_CRYPTO */
8515
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008518 size_t),
8519 void *p_psk )
8520{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008521 conf->f_psk = f_psk;
8522 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008525
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008526#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008527
8528#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008529int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008530{
8531 int ret;
8532
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008533 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8534 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8535 {
8536 mbedtls_mpi_free( &conf->dhm_P );
8537 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008538 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008540
8541 return( 0 );
8542}
Hanno Becker470a8c42017-10-04 15:28:46 +01008543#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008544
Hanno Beckera90658f2017-10-04 15:29:08 +01008545int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8546 const unsigned char *dhm_P, size_t P_len,
8547 const unsigned char *dhm_G, size_t G_len )
8548{
8549 int ret;
8550
8551 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8552 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8553 {
8554 mbedtls_mpi_free( &conf->dhm_P );
8555 mbedtls_mpi_free( &conf->dhm_G );
8556 return( ret );
8557 }
8558
8559 return( 0 );
8560}
Paul Bakker5121ce52009-01-03 21:22:43 +00008561
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008562int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008563{
8564 int ret;
8565
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008566 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8567 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8568 {
8569 mbedtls_mpi_free( &conf->dhm_P );
8570 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008571 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008572 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008573
8574 return( 0 );
8575}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008576#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008577
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008578#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8579/*
8580 * Set the minimum length for Diffie-Hellman parameters
8581 */
8582void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8583 unsigned int bitlen )
8584{
8585 conf->dhm_min_bitlen = bitlen;
8586}
8587#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8588
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008589#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008590/*
8591 * Set allowed/preferred hashes for handshake signatures
8592 */
8593void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8594 const int *hashes )
8595{
8596 conf->sig_hashes = hashes;
8597}
Hanno Becker947194e2017-04-07 13:25:49 +01008598#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008599
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008600#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008601/*
8602 * Set the allowed elliptic curves
8603 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008604void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008605 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008607 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008608}
Hanno Becker947194e2017-04-07 13:25:49 +01008609#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008610
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008611#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613{
Hanno Becker947194e2017-04-07 13:25:49 +01008614 /* Initialize to suppress unnecessary compiler warning */
8615 size_t hostname_len = 0;
8616
8617 /* Check if new hostname is valid before
8618 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008619 if( hostname != NULL )
8620 {
8621 hostname_len = strlen( hostname );
8622
8623 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8625 }
8626
8627 /* Now it's clear that we will overwrite the old hostname,
8628 * so we can free it safely */
8629
8630 if( ssl->hostname != NULL )
8631 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008632 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008633 mbedtls_free( ssl->hostname );
8634 }
8635
8636 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008637
Paul Bakker5121ce52009-01-03 21:22:43 +00008638 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008639 {
8640 ssl->hostname = NULL;
8641 }
8642 else
8643 {
8644 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008645 if( ssl->hostname == NULL )
8646 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008647
Hanno Becker947194e2017-04-07 13:25:49 +01008648 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008649
Hanno Becker947194e2017-04-07 13:25:49 +01008650 ssl->hostname[hostname_len] = '\0';
8651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008652
8653 return( 0 );
8654}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008655#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008656
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008657#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008658void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008660 const unsigned char *, size_t),
8661 void *p_sni )
8662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008663 conf->f_sni = f_sni;
8664 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008669int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008670{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008671 size_t cur_len, tot_len;
8672 const char **p;
8673
8674 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008675 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8676 * MUST NOT be truncated."
8677 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008678 */
8679 tot_len = 0;
8680 for( p = protos; *p != NULL; p++ )
8681 {
8682 cur_len = strlen( *p );
8683 tot_len += cur_len;
8684
8685 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008687 }
8688
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008689 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008690
8691 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008692}
8693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008695{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008696 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008699
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008700void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008701{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008702 conf->max_major_ver = major;
8703 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008704}
8705
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008706void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008707{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008708 conf->min_major_ver = major;
8709 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008710}
8711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008713void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008714{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008715 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008716}
8717#endif
8718
Janos Follath088ce432017-04-10 12:42:31 +01008719#if defined(MBEDTLS_SSL_SRV_C)
8720void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8721 char cert_req_ca_list )
8722{
8723 conf->cert_req_ca_list = cert_req_ca_list;
8724}
8725#endif
8726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008728void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008729{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008730 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008731}
8732#endif
8733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008735void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008736{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008737 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008738}
8739#endif
8740
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008741#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008742void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008743{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008744 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008745}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008746#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008749int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008751 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008752 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008755 }
8756
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008757 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008758
8759 return( 0 );
8760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008764void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008765{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008766 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008771void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008772{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008773 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008774}
8775#endif
8776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008777void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008778{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008779 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008780}
8781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008783void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008784{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008785 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008786}
8787
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008788void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008789{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008790 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008791}
8792
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008793void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008794 const unsigned char period[8] )
8795{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008796 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008801#if defined(MBEDTLS_SSL_CLI_C)
8802void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008803{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008804 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008805}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008806#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008807
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008808#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008809void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8810 mbedtls_ssl_ticket_write_t *f_ticket_write,
8811 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8812 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008813{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008814 conf->f_ticket_write = f_ticket_write;
8815 conf->f_ticket_parse = f_ticket_parse;
8816 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008817}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008820
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008821#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8822void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8823 mbedtls_ssl_export_keys_t *f_export_keys,
8824 void *p_export_keys )
8825{
8826 conf->f_export_keys = f_export_keys;
8827 conf->p_export_keys = p_export_keys;
8828}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008829
8830void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8831 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8832 void *p_export_keys )
8833{
8834 conf->f_export_keys_ext = f_export_keys_ext;
8835 conf->p_export_keys = p_export_keys;
8836}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008837#endif
8838
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008839#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008840void mbedtls_ssl_conf_async_private_cb(
8841 mbedtls_ssl_config *conf,
8842 mbedtls_ssl_async_sign_t *f_async_sign,
8843 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8844 mbedtls_ssl_async_resume_t *f_async_resume,
8845 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008846 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008847{
8848 conf->f_async_sign_start = f_async_sign;
8849 conf->f_async_decrypt_start = f_async_decrypt;
8850 conf->f_async_resume = f_async_resume;
8851 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008852 conf->p_async_config_data = async_config_data;
8853}
8854
Gilles Peskine8f97af72018-04-26 11:46:10 +02008855void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8856{
8857 return( conf->p_async_config_data );
8858}
8859
Gilles Peskine1febfef2018-04-30 11:54:39 +02008860void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008861{
8862 if( ssl->handshake == NULL )
8863 return( NULL );
8864 else
8865 return( ssl->handshake->user_async_ctx );
8866}
8867
Gilles Peskine1febfef2018-04-30 11:54:39 +02008868void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008869 void *ctx )
8870{
8871 if( ssl->handshake != NULL )
8872 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008873}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008874#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008875
Paul Bakker5121ce52009-01-03 21:22:43 +00008876/*
8877 * SSL get accessors
8878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008880{
8881 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8882}
8883
Hanno Becker8b170a02017-10-10 11:51:19 +01008884int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8885{
8886 /*
8887 * Case A: We're currently holding back
8888 * a message for further processing.
8889 */
8890
8891 if( ssl->keep_current_message == 1 )
8892 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008894 return( 1 );
8895 }
8896
8897 /*
8898 * Case B: Further records are pending in the current datagram.
8899 */
8900
8901#if defined(MBEDTLS_SSL_PROTO_DTLS)
8902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8903 ssl->in_left > ssl->next_record_offset )
8904 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008906 return( 1 );
8907 }
8908#endif /* MBEDTLS_SSL_PROTO_DTLS */
8909
8910 /*
8911 * Case C: A handshake message is being processed.
8912 */
8913
Hanno Becker8b170a02017-10-10 11:51:19 +01008914 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8915 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008917 return( 1 );
8918 }
8919
8920 /*
8921 * Case D: An application data message is being processed
8922 */
8923 if( ssl->in_offt != NULL )
8924 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008926 return( 1 );
8927 }
8928
8929 /*
8930 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008931 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008932 * we implement support for multiple alerts in single records.
8933 */
8934
8935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8936 return( 0 );
8937}
8938
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008939uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008940{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008941 if( ssl->session != NULL )
8942 return( ssl->session->verify_result );
8943
8944 if( ssl->session_negotiate != NULL )
8945 return( ssl->session_negotiate->verify_result );
8946
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008947 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008948}
8949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008951{
Paul Bakker926c8e42013-03-06 10:23:34 +01008952 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008953 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008956}
8957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008961 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008962 {
8963 switch( ssl->minor_ver )
8964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008966 return( "DTLSv1.0" );
8967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008969 return( "DTLSv1.2" );
8970
8971 default:
8972 return( "unknown (DTLS)" );
8973 }
8974 }
8975#endif
8976
Paul Bakker43ca69c2011-01-15 17:35:19 +00008977 switch( ssl->minor_ver )
8978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008979 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008980 return( "SSLv3.0" );
8981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008983 return( "TLSv1.0" );
8984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008985 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008986 return( "TLSv1.1" );
8987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008989 return( "TLSv1.2" );
8990
Paul Bakker43ca69c2011-01-15 17:35:19 +00008991 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008992 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008993 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008994}
8995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008997{
Hanno Becker3136ede2018-08-17 15:28:19 +01008998 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009000 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009001
Hanno Becker78640902018-08-13 16:35:15 +01009002 if( transform == NULL )
9003 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005#if defined(MBEDTLS_ZLIB_SUPPORT)
9006 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9007 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009008#endif
9009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012 case MBEDTLS_MODE_GCM:
9013 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009014 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009016 transform_expansion = transform->minlen;
9017 break;
9018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009020
9021 block_size = mbedtls_cipher_get_block_size(
9022 &transform->cipher_ctx_enc );
9023
Hanno Becker3136ede2018-08-17 15:28:19 +01009024 /* Expansion due to the addition of the MAC. */
9025 transform_expansion += transform->maclen;
9026
9027 /* Expansion due to the addition of CBC padding;
9028 * Theoretically up to 256 bytes, but we never use
9029 * more than the block size of the underlying cipher. */
9030 transform_expansion += block_size;
9031
9032 /* For TLS 1.1 or higher, an explicit IV is added
9033 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009034#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9035 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009036 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009037#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009038
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009039 break;
9040
9041 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009044 }
9045
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009046 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009047}
9048
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009049#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9050size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9051{
9052 size_t max_len;
9053
9054 /*
9055 * Assume mfl_code is correct since it was checked when set
9056 */
Angus Grattond8213d02016-05-25 20:56:48 +10009057 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009058
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009059 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009060 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009061 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009062 {
Angus Grattond8213d02016-05-25 20:56:48 +10009063 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009064 }
9065
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009066 /* During a handshake, use the value being negotiated */
9067 if( ssl->session_negotiate != NULL &&
9068 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9069 {
9070 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9071 }
9072
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009073 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009074}
9075#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9076
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009077#if defined(MBEDTLS_SSL_PROTO_DTLS)
9078static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9079{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009080 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9081 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9082 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9083 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9084 return ( 0 );
9085
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009086 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9087 return( ssl->mtu );
9088
9089 if( ssl->mtu == 0 )
9090 return( ssl->handshake->mtu );
9091
9092 return( ssl->mtu < ssl->handshake->mtu ?
9093 ssl->mtu : ssl->handshake->mtu );
9094}
9095#endif /* MBEDTLS_SSL_PROTO_DTLS */
9096
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009097int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9098{
9099 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9100
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009101#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9102 !defined(MBEDTLS_SSL_PROTO_DTLS)
9103 (void) ssl;
9104#endif
9105
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009106#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9107 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9108
9109 if( max_len > mfl )
9110 max_len = mfl;
9111#endif
9112
9113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009114 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009115 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009116 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009117 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9118 const size_t overhead = (size_t) ret;
9119
9120 if( ret < 0 )
9121 return( ret );
9122
9123 if( mtu <= overhead )
9124 {
9125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9126 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9127 }
9128
9129 if( max_len > mtu - overhead )
9130 max_len = mtu - overhead;
9131 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009132#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009133
Hanno Becker0defedb2018-08-10 12:35:02 +01009134#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9135 !defined(MBEDTLS_SSL_PROTO_DTLS)
9136 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009137#endif
9138
9139 return( (int) max_len );
9140}
9141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_X509_CRT_PARSE_C)
9143const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009144{
9145 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009146 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009147
Hanno Beckere6824572019-02-07 13:18:46 +00009148#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009149 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009150#else
9151 return( NULL );
9152#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009157int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9158 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009159{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009160 if( ssl == NULL ||
9161 dst == NULL ||
9162 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009163 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009166 }
9167
Hanno Becker52055ae2019-02-06 14:30:46 +00009168 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009171
Paul Bakker5121ce52009-01-03 21:22:43 +00009172/*
Paul Bakker1961b702013-01-25 14:49:24 +01009173 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009178
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009179 if( ssl == NULL || ssl->conf == NULL )
9180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009183 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009187 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009189#endif
9190
Paul Bakker1961b702013-01-25 14:49:24 +01009191 return( ret );
9192}
9193
9194/*
9195 * Perform the SSL handshake
9196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009198{
9199 int ret = 0;
9200
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009201 if( ssl == NULL || ssl->conf == NULL )
9202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009209
9210 if( ret != 0 )
9211 break;
9212 }
9213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009215
9216 return( ret );
9217}
9218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219#if defined(MBEDTLS_SSL_RENEGOTIATION)
9220#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009221/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009222 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009225{
9226 int ret;
9227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009229
9230 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9232 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009233
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009234 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009235 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009237 return( ret );
9238 }
9239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009241
9242 return( 0 );
9243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009245
9246/*
9247 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 * - any side: calling mbedtls_ssl_renegotiate(),
9249 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9250 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009251 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009252 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009256{
9257 int ret;
9258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009260
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009261 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9262 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009263
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009264 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9265 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009269 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009271 ssl->handshake->out_msg_seq = 1;
9272 else
9273 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009274 }
9275#endif
9276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9278 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009283 return( ret );
9284 }
9285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009287
9288 return( 0 );
9289}
9290
9291/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009292 * Renegotiate current connection on client,
9293 * or request renegotiation on server
9294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009298
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009299 if( ssl == NULL || ssl->conf == NULL )
9300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009303 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009304 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009310
9311 /* Did we already try/start sending HelloRequest? */
9312 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009314
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009315 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009320 /*
9321 * On client, either start the renegotiation process or,
9322 * if already in progress, continue the handshake
9323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009328
9329 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009332 return( ret );
9333 }
9334 }
9335 else
9336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009340 return( ret );
9341 }
9342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009344
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009345 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009346}
9347
9348/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009349 * Check record counters and renegotiate if they're above the limit.
9350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009352{
Andres AG2196c7f2016-12-15 17:01:16 +00009353 size_t ep_len = ssl_ep_len( ssl );
9354 int in_ctr_cmp;
9355 int out_ctr_cmp;
9356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9358 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009359 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009360 {
9361 return( 0 );
9362 }
9363
Andres AG2196c7f2016-12-15 17:01:16 +00009364 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9365 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009366 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009367 ssl->conf->renego_period + ep_len, 8 - ep_len );
9368
9369 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009370 {
9371 return( 0 );
9372 }
9373
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009378
9379/*
9380 * Receive application data decrypted from the SSL layer
9381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009383{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009384 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009385 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009386
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009387 if( ssl == NULL || ssl->conf == NULL )
9388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009393 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009396 return( ret );
9397
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009398 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009400 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009401 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009402 return( ret );
9403 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009404 }
9405#endif
9406
Hanno Becker4a810fb2017-05-24 16:27:30 +01009407 /*
9408 * Check if renegotiation is necessary and/or handshake is
9409 * in process. If yes, perform/continue, and fall through
9410 * if an unexpected packet is received while the client
9411 * is waiting for the ServerHello.
9412 *
9413 * (There is no equivalent to the last condition on
9414 * the server-side as it is not treated as within
9415 * a handshake while waiting for the ClientHello
9416 * after a renegotiation request.)
9417 */
9418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009420 ret = ssl_check_ctr_renegotiate( ssl );
9421 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9422 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009425 return( ret );
9426 }
9427#endif
9428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009432 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9433 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009436 return( ret );
9437 }
9438 }
9439
Hanno Beckere41158b2017-10-23 13:30:32 +01009440 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009441 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009442 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009443 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009444 if( ssl->f_get_timer != NULL &&
9445 ssl->f_get_timer( ssl->p_timer ) == -1 )
9446 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009447 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009448 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009449
Hanno Becker327c93b2018-08-15 13:56:18 +01009450 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009451 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009452 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9453 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009454
Hanno Becker4a810fb2017-05-24 16:27:30 +01009455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9456 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009457 }
9458
9459 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009461 {
9462 /*
9463 * OpenSSL sends empty messages to randomize the IV
9464 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009465 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009468 return( 0 );
9469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009471 return( ret );
9472 }
9473 }
9474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009478
Hanno Becker4a810fb2017-05-24 16:27:30 +01009479 /*
9480 * - For client-side, expect SERVER_HELLO_REQUEST.
9481 * - For server-side, expect CLIENT_HELLO.
9482 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9483 */
9484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009486 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009488 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009491
9492 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009494 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009495 {
9496 continue;
9497 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009498#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009500 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009501#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009502
Hanno Becker4a810fb2017-05-24 16:27:30 +01009503#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009508
9509 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009512 {
9513 continue;
9514 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009517 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009518#endif /* MBEDTLS_SSL_SRV_C */
9519
Hanno Becker21df7f92017-10-17 11:03:26 +01009520#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009521 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009522 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9523 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9524 ssl->conf->allow_legacy_renegotiation ==
9525 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9526 {
9527 /*
9528 * Accept renegotiation request
9529 */
Paul Bakker48916f92012-09-16 19:57:18 +00009530
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009531 /* DTLS clients need to know renego is server-initiated */
9532#if defined(MBEDTLS_SSL_PROTO_DTLS)
9533 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9534 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9535 {
9536 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9537 }
9538#endif
9539 ret = ssl_start_renegotiation( ssl );
9540 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9541 ret != 0 )
9542 {
9543 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9544 return( ret );
9545 }
9546 }
9547 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009548#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009549 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009550 /*
9551 * Refuse renegotiation
9552 */
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556#if defined(MBEDTLS_SSL_PROTO_SSL3)
9557 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009558 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009559 /* SSLv3 does not have a "no_renegotiation" warning, so
9560 we send a fatal alert and abort the connection. */
9561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9562 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009564 }
9565 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9567#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9568 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9569 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9572 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9573 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009574 {
9575 return( ret );
9576 }
Paul Bakker48916f92012-09-16 19:57:18 +00009577 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009578 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9580 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009584 }
Paul Bakker48916f92012-09-16 19:57:18 +00009585 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009586
Hanno Becker90333da2017-10-10 11:27:13 +01009587 /* At this point, we don't know whether the renegotiation has been
9588 * completed or not. The cases to consider are the following:
9589 * 1) The renegotiation is complete. In this case, no new record
9590 * has been read yet.
9591 * 2) The renegotiation is incomplete because the client received
9592 * an application data record while awaiting the ServerHello.
9593 * 3) The renegotiation is incomplete because the client received
9594 * a non-handshake, non-application data message while awaiting
9595 * the ServerHello.
9596 * In each of these case, looping will be the proper action:
9597 * - For 1), the next iteration will read a new record and check
9598 * if it's application data.
9599 * - For 2), the loop condition isn't satisfied as application data
9600 * is present, hence continue is the same as break
9601 * - For 3), the loop condition is satisfied and read_record
9602 * will re-deliver the message that was held back by the client
9603 * when expecting the ServerHello.
9604 */
9605 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009606 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009607#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009609 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009610 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009611 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009612 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009615 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009617 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009618 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009619 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9623 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009626 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009627 }
9628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9632 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009633 }
9634
9635 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009636
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009637 /* We're going to return something now, cancel timer,
9638 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009640 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009641
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009643 /* If we requested renego but received AppData, resend HelloRequest.
9644 * Do it now, after setting in_offt, to avoid taking this branch
9645 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009647 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009649 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009650 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009653 return( ret );
9654 }
9655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009658 }
9659
9660 n = ( len < ssl->in_msglen )
9661 ? len : ssl->in_msglen;
9662
9663 memcpy( buf, ssl->in_offt, n );
9664 ssl->in_msglen -= n;
9665
9666 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009667 {
9668 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009669 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009670 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009672 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009673 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009674 /* more data available */
9675 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009679
Paul Bakker23986e52011-04-24 08:57:21 +00009680 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009681}
9682
9683/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009684 * Send application data to be encrypted by the SSL layer, taking care of max
9685 * fragment length and buffer size.
9686 *
9687 * According to RFC 5246 Section 6.2.1:
9688 *
9689 * Zero-length fragments of Application data MAY be sent as they are
9690 * potentially useful as a traffic analysis countermeasure.
9691 *
9692 * Therefore, it is possible that the input message length is 0 and the
9693 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009694 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009695static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009696 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009697{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009698 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9699 const size_t max_len = (size_t) ret;
9700
9701 if( ret < 0 )
9702 {
9703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9704 return( ret );
9705 }
9706
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009707 if( len > max_len )
9708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009713 "maximum fragment length: %d > %d",
9714 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009716 }
9717 else
9718#endif
9719 len = max_len;
9720 }
Paul Bakker887bd502011-06-08 13:10:54 +00009721
Paul Bakker5121ce52009-01-03 21:22:43 +00009722 if( ssl->out_left != 0 )
9723 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009724 /*
9725 * The user has previously tried to send the data and
9726 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9727 * written. In this case, we expect the high-level write function
9728 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009733 return( ret );
9734 }
9735 }
Paul Bakker887bd502011-06-08 13:10:54 +00009736 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009737 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009738 /*
9739 * The user is trying to send a message the first time, so we need to
9740 * copy the data into the internal buffers and setup the data structure
9741 * to keep track of partial writes
9742 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009743 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009745 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009746
Hanno Becker67bc7c32018-08-06 11:33:50 +01009747 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009750 return( ret );
9751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009752 }
9753
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009754 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009755}
9756
9757/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009758 * Write application data, doing 1/n-1 splitting if necessary.
9759 *
9760 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009761 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009762 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009765static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009766 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009767{
9768 int ret;
9769
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009770 if( ssl->conf->cbc_record_splitting ==
9771 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009772 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9774 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9775 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009776 {
9777 return( ssl_write_real( ssl, buf, len ) );
9778 }
9779
9780 if( ssl->split_done == 0 )
9781 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009782 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009783 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009784 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009785 }
9786
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009787 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9788 return( ret );
9789 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009790
9791 return( ret + 1 );
9792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009794
9795/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009796 * Write application data (public-facing wrapper)
9797 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009798int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009799{
9800 int ret;
9801
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009803
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009804 if( ssl == NULL || ssl->conf == NULL )
9805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9806
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009807#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009808 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9809 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009810 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009811 return( ret );
9812 }
9813#endif
9814
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009815 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009816 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009817 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009818 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009820 return( ret );
9821 }
9822 }
9823
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009824#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009825 ret = ssl_write_split( ssl, buf, len );
9826#else
9827 ret = ssl_write_real( ssl, buf, len );
9828#endif
9829
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009831
9832 return( ret );
9833}
9834
9835/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009836 * Notify the peer that the connection is being closed
9837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009839{
9840 int ret;
9841
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009842 if( ssl == NULL || ssl->conf == NULL )
9843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009846
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009847 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9853 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9854 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009857 return( ret );
9858 }
9859 }
9860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009862
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009863 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009864}
9865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009867{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009868 if( transform == NULL )
9869 return;
9870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009872 deflateEnd( &transform->ctx_deflate );
9873 inflateEnd( &transform->ctx_inflate );
9874#endif
9875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9877 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009878
Hanno Beckerd56ed242018-01-03 15:32:51 +00009879#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 mbedtls_md_free( &transform->md_ctx_enc );
9881 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009882#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009883
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009884 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009885}
9886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#if defined(MBEDTLS_X509_CRT_PARSE_C)
9888static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009891
9892 while( cur != NULL )
9893 {
9894 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009896 cur = next;
9897 }
9898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009900
Hanno Becker0271f962018-08-16 13:23:47 +01009901#if defined(MBEDTLS_SSL_PROTO_DTLS)
9902
9903static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9904{
9905 unsigned offset;
9906 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9907
9908 if( hs == NULL )
9909 return;
9910
Hanno Becker283f5ef2018-08-24 09:34:47 +01009911 ssl_free_buffered_record( ssl );
9912
Hanno Becker0271f962018-08-16 13:23:47 +01009913 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009914 ssl_buffering_free_slot( ssl, offset );
9915}
9916
9917static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9918 uint8_t slot )
9919{
9920 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9921 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009922
9923 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9924 return;
9925
Hanno Beckere605b192018-08-21 15:59:07 +01009926 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009927 {
Hanno Beckere605b192018-08-21 15:59:07 +01009928 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009929 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009930 mbedtls_free( hs_buf->data );
9931 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009932 }
9933}
9934
9935#endif /* MBEDTLS_SSL_PROTO_DTLS */
9936
Gilles Peskine9b562d52018-04-25 20:32:43 +02009937void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009938{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009939 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9940
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009941 if( handshake == NULL )
9942 return;
9943
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009944#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9945 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9946 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009947 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009948 handshake->async_in_progress = 0;
9949 }
9950#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9951
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009952#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9953 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9954 mbedtls_md5_free( &handshake->fin_md5 );
9955 mbedtls_sha1_free( &handshake->fin_sha1 );
9956#endif
9957#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9958#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009959#if defined(MBEDTLS_USE_PSA_CRYPTO)
9960 psa_hash_abort( &handshake->fin_sha256_psa );
9961#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009962 mbedtls_sha256_free( &handshake->fin_sha256 );
9963#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009964#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009965#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009966#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009967 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009968#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009969 mbedtls_sha512_free( &handshake->fin_sha512 );
9970#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009971#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_DHM_C)
9975 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977#if defined(MBEDTLS_ECDH_C)
9978 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009979#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009980#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009981 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009982#if defined(MBEDTLS_SSL_CLI_C)
9983 mbedtls_free( handshake->ecjpake_cache );
9984 handshake->ecjpake_cache = NULL;
9985 handshake->ecjpake_cache_len = 0;
9986#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009987#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009988
Janos Follath4ae5c292016-02-10 11:27:43 +00009989#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9990 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009991 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009993#endif
9994
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009995#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9996 if( handshake->psk != NULL )
9997 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009998 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009999 mbedtls_free( handshake->psk );
10000 }
10001#endif
10002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10004 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010005 /*
10006 * Free only the linked list wrapper, not the keys themselves
10007 * since the belong to the SNI callback
10008 */
10009 if( handshake->sni_key_cert != NULL )
10010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010012
10013 while( cur != NULL )
10014 {
10015 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010017 cur = next;
10018 }
10019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010021
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010022#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010023 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010024 if( handshake->ecrs_peer_cert != NULL )
10025 {
10026 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10027 mbedtls_free( handshake->ecrs_peer_cert );
10028 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010029#endif
10030
Hanno Becker75173122019-02-06 16:18:31 +000010031#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10032 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10033 mbedtls_pk_free( &handshake->peer_pubkey );
10034#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036#if defined(MBEDTLS_SSL_PROTO_DTLS)
10037 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010038 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010039 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010040#endif
10041
Hanno Becker4a63ed42019-01-08 11:39:35 +000010042#if defined(MBEDTLS_ECDH_C) && \
10043 defined(MBEDTLS_USE_PSA_CRYPTO)
10044 psa_destroy_key( handshake->ecdh_psa_privkey );
10045#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10046
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010047 mbedtls_platform_zeroize( handshake,
10048 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010049}
10050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010052{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010053 if( session == NULL )
10054 return;
10055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010057 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010058#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010059
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010060#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010062#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010063
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010064 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010065}
10066
Paul Bakker5121ce52009-01-03 21:22:43 +000010067/*
10068 * Free an SSL context
10069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010071{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010072 if( ssl == NULL )
10073 return;
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010076
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010077 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010078 {
Angus Grattond8213d02016-05-25 20:56:48 +100010079 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 }
10082
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010083 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010084 {
Angus Grattond8213d02016-05-25 20:56:48 +100010085 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010087 }
10088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010090 if( ssl->compress_buf != NULL )
10091 {
Angus Grattond8213d02016-05-25 20:56:48 +100010092 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010094 }
10095#endif
10096
Paul Bakker48916f92012-09-16 19:57:18 +000010097 if( ssl->transform )
10098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 mbedtls_ssl_transform_free( ssl->transform );
10100 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010101 }
10102
10103 if( ssl->handshake )
10104 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010105 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10107 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 mbedtls_free( ssl->handshake );
10110 mbedtls_free( ssl->transform_negotiate );
10111 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010112 }
10113
Paul Bakkerc0463502013-02-14 11:19:38 +010010114 if( ssl->session )
10115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 mbedtls_ssl_session_free( ssl->session );
10117 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010118 }
10119
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010120#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010121 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010122 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010123 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010125 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10129 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10132 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010133 }
10134#endif
10135
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010136#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010138#endif
10139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010141
Paul Bakker86f04f42013-02-14 11:20:09 +010010142 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010143 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010144}
10145
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010146/*
10147 * Initialze mbedtls_ssl_config
10148 */
10149void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10150{
10151 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10152}
10153
Simon Butcherc97b6972015-12-27 23:48:17 +000010154#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010155static int ssl_preset_default_hashes[] = {
10156#if defined(MBEDTLS_SHA512_C)
10157 MBEDTLS_MD_SHA512,
10158 MBEDTLS_MD_SHA384,
10159#endif
10160#if defined(MBEDTLS_SHA256_C)
10161 MBEDTLS_MD_SHA256,
10162 MBEDTLS_MD_SHA224,
10163#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010164#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010165 MBEDTLS_MD_SHA1,
10166#endif
10167 MBEDTLS_MD_NONE
10168};
Simon Butcherc97b6972015-12-27 23:48:17 +000010169#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010170
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010171static int ssl_preset_suiteb_ciphersuites[] = {
10172 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10173 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10174 0
10175};
10176
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010177#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010178static int ssl_preset_suiteb_hashes[] = {
10179 MBEDTLS_MD_SHA256,
10180 MBEDTLS_MD_SHA384,
10181 MBEDTLS_MD_NONE
10182};
10183#endif
10184
10185#if defined(MBEDTLS_ECP_C)
10186static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10187 MBEDTLS_ECP_DP_SECP256R1,
10188 MBEDTLS_ECP_DP_SECP384R1,
10189 MBEDTLS_ECP_DP_NONE
10190};
10191#endif
10192
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010193/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010194 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010195 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010196int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010197 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010198{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010199#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010200 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010201#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010202
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010203 /* Use the functions here so that they are covered in tests,
10204 * but otherwise access member directly for efficiency */
10205 mbedtls_ssl_conf_endpoint( conf, endpoint );
10206 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010207
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010208 /*
10209 * Things that are common to all presets
10210 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010211#if defined(MBEDTLS_SSL_CLI_C)
10212 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10213 {
10214 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10215#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10216 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10217#endif
10218 }
10219#endif
10220
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010221#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010222 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010223#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010224
10225#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10226 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10227#endif
10228
10229#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10230 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10231#endif
10232
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010233#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10234 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10235#endif
10236
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010237#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010238 conf->f_cookie_write = ssl_cookie_write_dummy;
10239 conf->f_cookie_check = ssl_cookie_check_dummy;
10240#endif
10241
10242#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10243 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10244#endif
10245
Janos Follath088ce432017-04-10 12:42:31 +010010246#if defined(MBEDTLS_SSL_SRV_C)
10247 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10248#endif
10249
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010250#if defined(MBEDTLS_SSL_PROTO_DTLS)
10251 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10252 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10253#endif
10254
10255#if defined(MBEDTLS_SSL_RENEGOTIATION)
10256 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010257 memset( conf->renego_period, 0x00, 2 );
10258 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010259#endif
10260
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010261#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10262 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10263 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010264 const unsigned char dhm_p[] =
10265 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10266 const unsigned char dhm_g[] =
10267 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10268
Hanno Beckera90658f2017-10-04 15:29:08 +010010269 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10270 dhm_p, sizeof( dhm_p ),
10271 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010272 {
10273 return( ret );
10274 }
10275 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010276#endif
10277
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010278 /*
10279 * Preset-specific defaults
10280 */
10281 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010282 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010283 /*
10284 * NSA Suite B
10285 */
10286 case MBEDTLS_SSL_PRESET_SUITEB:
10287 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10288 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10289 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10290 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10291
10292 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10293 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10294 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10295 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10296 ssl_preset_suiteb_ciphersuites;
10297
10298#if defined(MBEDTLS_X509_CRT_PARSE_C)
10299 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010300#endif
10301
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010302#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010303 conf->sig_hashes = ssl_preset_suiteb_hashes;
10304#endif
10305
10306#if defined(MBEDTLS_ECP_C)
10307 conf->curve_list = ssl_preset_suiteb_curves;
10308#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010309 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010310
10311 /*
10312 * Default
10313 */
10314 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010315 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10316 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10317 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10318 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10319 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10320 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10321 MBEDTLS_SSL_MIN_MINOR_VERSION :
10322 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010323 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10324 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10325
10326#if defined(MBEDTLS_SSL_PROTO_DTLS)
10327 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10328 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10329#endif
10330
10331 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10332 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10333 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10334 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10335 mbedtls_ssl_list_ciphersuites();
10336
10337#if defined(MBEDTLS_X509_CRT_PARSE_C)
10338 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10339#endif
10340
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010341#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010342 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010343#endif
10344
10345#if defined(MBEDTLS_ECP_C)
10346 conf->curve_list = mbedtls_ecp_grp_id_list();
10347#endif
10348
10349#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10350 conf->dhm_min_bitlen = 1024;
10351#endif
10352 }
10353
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010354 return( 0 );
10355}
10356
10357/*
10358 * Free mbedtls_ssl_config
10359 */
10360void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10361{
10362#if defined(MBEDTLS_DHM_C)
10363 mbedtls_mpi_free( &conf->dhm_P );
10364 mbedtls_mpi_free( &conf->dhm_G );
10365#endif
10366
10367#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10368 if( conf->psk != NULL )
10369 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010370 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010371 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010372 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010373 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010374 }
10375
10376 if( conf->psk_identity != NULL )
10377 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010378 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010379 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010380 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010381 conf->psk_identity_len = 0;
10382 }
10383#endif
10384
10385#if defined(MBEDTLS_X509_CRT_PARSE_C)
10386 ssl_key_cert_free( conf->key_cert );
10387#endif
10388
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010389 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010390}
10391
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010392#if defined(MBEDTLS_PK_C) && \
10393 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010394/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399#if defined(MBEDTLS_RSA_C)
10400 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10401 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403#if defined(MBEDTLS_ECDSA_C)
10404 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10405 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010408}
10409
Hanno Becker7e5437a2017-04-28 17:15:26 +010010410unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10411{
10412 switch( type ) {
10413 case MBEDTLS_PK_RSA:
10414 return( MBEDTLS_SSL_SIG_RSA );
10415 case MBEDTLS_PK_ECDSA:
10416 case MBEDTLS_PK_ECKEY:
10417 return( MBEDTLS_SSL_SIG_ECDSA );
10418 default:
10419 return( MBEDTLS_SSL_SIG_ANON );
10420 }
10421}
10422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010424{
10425 switch( sig )
10426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427#if defined(MBEDTLS_RSA_C)
10428 case MBEDTLS_SSL_SIG_RSA:
10429 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431#if defined(MBEDTLS_ECDSA_C)
10432 case MBEDTLS_SSL_SIG_ECDSA:
10433 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010434#endif
10435 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010437 }
10438}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010439#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010440
Hanno Becker7e5437a2017-04-28 17:15:26 +010010441#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10442 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10443
10444/* Find an entry in a signature-hash set matching a given hash algorithm. */
10445mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10446 mbedtls_pk_type_t sig_alg )
10447{
10448 switch( sig_alg )
10449 {
10450 case MBEDTLS_PK_RSA:
10451 return( set->rsa );
10452 case MBEDTLS_PK_ECDSA:
10453 return( set->ecdsa );
10454 default:
10455 return( MBEDTLS_MD_NONE );
10456 }
10457}
10458
10459/* Add a signature-hash-pair to a signature-hash set */
10460void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10461 mbedtls_pk_type_t sig_alg,
10462 mbedtls_md_type_t md_alg )
10463{
10464 switch( sig_alg )
10465 {
10466 case MBEDTLS_PK_RSA:
10467 if( set->rsa == MBEDTLS_MD_NONE )
10468 set->rsa = md_alg;
10469 break;
10470
10471 case MBEDTLS_PK_ECDSA:
10472 if( set->ecdsa == MBEDTLS_MD_NONE )
10473 set->ecdsa = md_alg;
10474 break;
10475
10476 default:
10477 break;
10478 }
10479}
10480
10481/* Allow exactly one hash algorithm for each signature. */
10482void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10483 mbedtls_md_type_t md_alg )
10484{
10485 set->rsa = md_alg;
10486 set->ecdsa = md_alg;
10487}
10488
10489#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10490 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10491
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010492/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010493 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010496{
10497 switch( hash )
10498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010499#if defined(MBEDTLS_MD5_C)
10500 case MBEDTLS_SSL_HASH_MD5:
10501 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010502#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503#if defined(MBEDTLS_SHA1_C)
10504 case MBEDTLS_SSL_HASH_SHA1:
10505 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010507#if defined(MBEDTLS_SHA256_C)
10508 case MBEDTLS_SSL_HASH_SHA224:
10509 return( MBEDTLS_MD_SHA224 );
10510 case MBEDTLS_SSL_HASH_SHA256:
10511 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513#if defined(MBEDTLS_SHA512_C)
10514 case MBEDTLS_SSL_HASH_SHA384:
10515 return( MBEDTLS_MD_SHA384 );
10516 case MBEDTLS_SSL_HASH_SHA512:
10517 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010518#endif
10519 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010521 }
10522}
10523
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010524/*
10525 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10526 */
10527unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10528{
10529 switch( md )
10530 {
10531#if defined(MBEDTLS_MD5_C)
10532 case MBEDTLS_MD_MD5:
10533 return( MBEDTLS_SSL_HASH_MD5 );
10534#endif
10535#if defined(MBEDTLS_SHA1_C)
10536 case MBEDTLS_MD_SHA1:
10537 return( MBEDTLS_SSL_HASH_SHA1 );
10538#endif
10539#if defined(MBEDTLS_SHA256_C)
10540 case MBEDTLS_MD_SHA224:
10541 return( MBEDTLS_SSL_HASH_SHA224 );
10542 case MBEDTLS_MD_SHA256:
10543 return( MBEDTLS_SSL_HASH_SHA256 );
10544#endif
10545#if defined(MBEDTLS_SHA512_C)
10546 case MBEDTLS_MD_SHA384:
10547 return( MBEDTLS_SSL_HASH_SHA384 );
10548 case MBEDTLS_MD_SHA512:
10549 return( MBEDTLS_SSL_HASH_SHA512 );
10550#endif
10551 default:
10552 return( MBEDTLS_SSL_HASH_NONE );
10553 }
10554}
10555
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010556#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010557/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010558 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010559 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010560 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010561int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010563 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010564
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010565 if( ssl->conf->curve_list == NULL )
10566 return( -1 );
10567
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010568 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010569 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010570 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010571
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010572 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010573}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010574#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010575
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010576#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010577/*
10578 * Check if a hash proposed by the peer is in our list.
10579 * Return 0 if we're willing to use it, -1 otherwise.
10580 */
10581int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10582 mbedtls_md_type_t md )
10583{
10584 const int *cur;
10585
10586 if( ssl->conf->sig_hashes == NULL )
10587 return( -1 );
10588
10589 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10590 if( *cur == (int) md )
10591 return( 0 );
10592
10593 return( -1 );
10594}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010595#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#if defined(MBEDTLS_X509_CRT_PARSE_C)
10598int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10599 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010600 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010601 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010602{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010603 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010605 int usage = 0;
10606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010608 const char *ext_oid;
10609 size_t ext_len;
10610#endif
10611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10613 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010614 ((void) cert);
10615 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010616 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010617#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10620 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010621 {
10622 /* Server part of the key exchange */
10623 switch( ciphersuite->key_exchange )
10624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625 case MBEDTLS_KEY_EXCHANGE_RSA:
10626 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010627 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010628 break;
10629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10631 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10632 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10633 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010634 break;
10635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10637 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010638 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010639 break;
10640
10641 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 case MBEDTLS_KEY_EXCHANGE_NONE:
10643 case MBEDTLS_KEY_EXCHANGE_PSK:
10644 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10645 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010646 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010647 usage = 0;
10648 }
10649 }
10650 else
10651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10653 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010654 }
10655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010657 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010658 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010659 ret = -1;
10660 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010661#else
10662 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10666 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10669 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010670 }
10671 else
10672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10674 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010675 }
10676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010678 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010679 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010680 ret = -1;
10681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010683
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010684 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010687
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010688/*
10689 * Convert version numbers to/from wire format
10690 * and, for DTLS, to/from TLS equivalent.
10691 *
10692 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010693 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010694 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10695 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010698 unsigned char ver[2] )
10699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700#if defined(MBEDTLS_SSL_PROTO_DTLS)
10701 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010704 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10705
10706 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10707 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10708 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010709 else
10710#else
10711 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010712#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010713 {
10714 ver[0] = (unsigned char) major;
10715 ver[1] = (unsigned char) minor;
10716 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010717}
10718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010720 const unsigned char ver[2] )
10721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722#if defined(MBEDTLS_SSL_PROTO_DTLS)
10723 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010724 {
10725 *major = 255 - ver[0] + 2;
10726 *minor = 255 - ver[1] + 1;
10727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010729 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10730 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010731 else
10732#else
10733 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010734#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010735 {
10736 *major = ver[0];
10737 *minor = ver[1];
10738 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010739}
10740
Simon Butcher99000142016-10-13 17:21:01 +010010741int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10742{
10743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10744 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10745 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10746
10747 switch( md )
10748 {
10749#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10750#if defined(MBEDTLS_MD5_C)
10751 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010752 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010753#endif
10754#if defined(MBEDTLS_SHA1_C)
10755 case MBEDTLS_SSL_HASH_SHA1:
10756 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10757 break;
10758#endif
10759#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10760#if defined(MBEDTLS_SHA512_C)
10761 case MBEDTLS_SSL_HASH_SHA384:
10762 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10763 break;
10764#endif
10765#if defined(MBEDTLS_SHA256_C)
10766 case MBEDTLS_SSL_HASH_SHA256:
10767 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10768 break;
10769#endif
10770 default:
10771 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10772 }
10773
10774 return 0;
10775#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10776 (void) ssl;
10777 (void) md;
10778
10779 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10781}
10782
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010783#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10784 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10785int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10786 unsigned char *output,
10787 unsigned char *data, size_t data_len )
10788{
10789 int ret = 0;
10790 mbedtls_md5_context mbedtls_md5;
10791 mbedtls_sha1_context mbedtls_sha1;
10792
10793 mbedtls_md5_init( &mbedtls_md5 );
10794 mbedtls_sha1_init( &mbedtls_sha1 );
10795
10796 /*
10797 * digitally-signed struct {
10798 * opaque md5_hash[16];
10799 * opaque sha_hash[20];
10800 * };
10801 *
10802 * md5_hash
10803 * MD5(ClientHello.random + ServerHello.random
10804 * + ServerParams);
10805 * sha_hash
10806 * SHA(ClientHello.random + ServerHello.random
10807 * + ServerParams);
10808 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010809 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010810 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010812 goto exit;
10813 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010814 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010815 ssl->handshake->randbytes, 64 ) ) != 0 )
10816 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010818 goto exit;
10819 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010820 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010821 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010823 goto exit;
10824 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010825 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010826 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010828 goto exit;
10829 }
10830
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010831 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010832 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010834 goto exit;
10835 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010836 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010837 ssl->handshake->randbytes, 64 ) ) != 0 )
10838 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010840 goto exit;
10841 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010842 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010843 data_len ) ) != 0 )
10844 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010846 goto exit;
10847 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010848 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010849 output + 16 ) ) != 0 )
10850 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010852 goto exit;
10853 }
10854
10855exit:
10856 mbedtls_md5_free( &mbedtls_md5 );
10857 mbedtls_sha1_free( &mbedtls_sha1 );
10858
10859 if( ret != 0 )
10860 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10861 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10862
10863 return( ret );
10864
10865}
10866#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10867 MBEDTLS_SSL_PROTO_TLS1_1 */
10868
10869#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10870 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010871
10872#if defined(MBEDTLS_USE_PSA_CRYPTO)
10873int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10874 unsigned char *hash, size_t *hashlen,
10875 unsigned char *data, size_t data_len,
10876 mbedtls_md_type_t md_alg )
10877{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010878 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010879 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010880 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10881
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010883
10884 if( ( status = psa_hash_setup( &hash_operation,
10885 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010886 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010887 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010888 goto exit;
10889 }
10890
Andrzej Kurek814feff2019-01-14 04:35:19 -050010891 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10892 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010893 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010894 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010895 goto exit;
10896 }
10897
Andrzej Kurek814feff2019-01-14 04:35:19 -050010898 if( ( status = psa_hash_update( &hash_operation,
10899 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010900 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010901 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010902 goto exit;
10903 }
10904
Andrzej Kurek814feff2019-01-14 04:35:19 -050010905 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10906 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010907 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010908 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010909 goto exit;
10910 }
10911
10912exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010913 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010914 {
10915 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10916 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010917 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010918 {
10919 case PSA_ERROR_NOT_SUPPORTED:
10920 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010921 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010922 case PSA_ERROR_BUFFER_TOO_SMALL:
10923 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10924 case PSA_ERROR_INSUFFICIENT_MEMORY:
10925 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10926 default:
10927 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10928 }
10929 }
10930 return( 0 );
10931}
10932
10933#else
10934
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010935int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010936 unsigned char *hash, size_t *hashlen,
10937 unsigned char *data, size_t data_len,
10938 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010939{
10940 int ret = 0;
10941 mbedtls_md_context_t ctx;
10942 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010943 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010944
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010946
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010947 mbedtls_md_init( &ctx );
10948
10949 /*
10950 * digitally-signed struct {
10951 * opaque client_random[32];
10952 * opaque server_random[32];
10953 * ServerDHParams params;
10954 * };
10955 */
10956 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10957 {
10958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10959 goto exit;
10960 }
10961 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10962 {
10963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10964 goto exit;
10965 }
10966 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10967 {
10968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10969 goto exit;
10970 }
10971 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10972 {
10973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10974 goto exit;
10975 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010976 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010977 {
10978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10979 goto exit;
10980 }
10981
10982exit:
10983 mbedtls_md_free( &ctx );
10984
10985 if( ret != 0 )
10986 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10987 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10988
10989 return( ret );
10990}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010991#endif /* MBEDTLS_USE_PSA_CRYPTO */
10992
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010993#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10994 MBEDTLS_SSL_PROTO_TLS1_2 */
10995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010996#endif /* MBEDTLS_SSL_TLS_C */