blob: 8c3c74d23b39bd6898811a7bb131343ec62190b8 [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 );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
115#define SSL_DONT_FORCE_FLUSH 0
116#define SSL_FORCE_FLUSH 1
117
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100119
Hanno Becker35c36a62019-04-23 12:31:42 +0100120#if defined(MBEDTLS_SSL_CID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100121/* Top-level Connection ID API */
122
Hanno Beckerca092242019-04-25 16:01:49 +0100123/* WARNING: The CID feature isn't fully implemented yet
124 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100125int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
126 int enable,
127 unsigned char const *own_cid,
128 size_t own_cid_len )
129{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100130 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
132
Hanno Beckerca092242019-04-25 16:01:49 +0100133 ssl->negotiate_cid = enable;
134 if( enable == MBEDTLS_SSL_CID_DISABLED )
135 {
136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
137 return( 0 );
138 }
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
140
141 if( own_cid_len > MBEDTLS_SSL_CID_IN_LEN_MAX )
142 {
143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID too large: Maximum %u, actual %u",
144 (unsigned) MBEDTLS_SSL_CID_IN_LEN_MAX,
145 (unsigned) own_cid_len ) );
146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
147 }
148
149 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100150 /* Truncation is not an issue here because
151 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
152 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100153
154 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100155 return( 0 );
156}
157
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100158/* WARNING: The CID feature isn't fully implemented yet
159 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100160int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
161 int *enabled,
162 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
163 size_t *peer_cid_len )
164{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100165 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100166
Hanno Becker76a79ab2019-05-03 14:38:32 +0100167 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
168 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
169 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100171 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100172
Hanno Beckerc5f24222019-05-03 12:54:52 +0100173 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
174 * were used, but client and server requested the empty CID.
175 * This is indistinguishable from not using the CID extension
176 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177 if( ssl->transform_in->in_cid_len == 0 &&
178 ssl->transform_in->out_cid_len == 0 )
179 {
180 return( 0 );
181 }
182
183 *peer_cid_len = ssl->transform_in->out_cid_len;
184 memcpy( peer_cid, ssl->transform_in->out_cid,
185 ssl->transform_in->out_cid_len );
186
187 *enabled = MBEDTLS_SSL_CID_ENABLED;
188
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100189 return( 0 );
190}
Hanno Becker35c36a62019-04-23 12:31:42 +0100191#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100192
Hanno Beckerd5847772018-08-28 10:09:23 +0100193/* Forward declarations for functions related to message buffering. */
194static void ssl_buffering_free( mbedtls_ssl_context *ssl );
195static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
196 uint8_t slot );
197static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
198static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
199static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
200static int ssl_buffer_message( mbedtls_ssl_context *ssl );
201static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100202static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100203
Hanno Beckera67dee22018-08-22 10:05:20 +0100204static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100205static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100206{
Hanno Becker11682cc2018-08-22 14:41:02 +0100207 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100208
209 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100210 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100211
212 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
213}
214
Hanno Becker67bc7c32018-08-06 11:33:50 +0100215static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
216{
Hanno Becker11682cc2018-08-22 14:41:02 +0100217 size_t const bytes_written = ssl->out_left;
218 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100219
220 /* Double-check that the write-index hasn't gone
221 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100222 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100223 {
224 /* Should never happen... */
225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
226 }
227
228 return( (int) ( mtu - bytes_written ) );
229}
230
231static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
232{
233 int ret;
234 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400235 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
238 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
239
240 if( max_len > mfl )
241 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100242
243 /* By the standard (RFC 6066 Sect. 4), the MFL extension
244 * only limits the maximum record payload size, so in theory
245 * we would be allowed to pack multiple records of payload size
246 * MFL into a single datagram. However, this would mean that there's
247 * no way to explicitly communicate MTU restrictions to the peer.
248 *
249 * The following reduction of max_len makes sure that we never
250 * write datagrams larger than MFL + Record Expansion Overhead.
251 */
252 if( max_len <= ssl->out_left )
253 return( 0 );
254
255 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100256#endif
257
258 ret = ssl_get_remaining_space_in_datagram( ssl );
259 if( ret < 0 )
260 return( ret );
261 remaining = (size_t) ret;
262
263 ret = mbedtls_ssl_get_record_expansion( ssl );
264 if( ret < 0 )
265 return( ret );
266 expansion = (size_t) ret;
267
268 if( remaining <= expansion )
269 return( 0 );
270
271 remaining -= expansion;
272 if( remaining >= max_len )
273 remaining = max_len;
274
275 return( (int) remaining );
276}
277
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200278/*
279 * Double the retransmit timeout value, within the allowed range,
280 * returning -1 if the maximum value has already been reached.
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283{
284 uint32_t new_timeout;
285
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200286 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200287 return( -1 );
288
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200289 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
290 * in the following way: after the initial transmission and a first
291 * retransmission, back off to a temporary estimated MTU of 508 bytes.
292 * This value is guaranteed to be deliverable (if not guaranteed to be
293 * delivered) of any compliant IPv4 (and IPv6) network, and should work
294 * on most non-IP stacks too. */
295 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400296 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200297 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
299 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200300
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200301 new_timeout = 2 * ssl->handshake->retransmit_timeout;
302
303 /* Avoid arithmetic overflow and range overflow */
304 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200305 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200306 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200307 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200308 }
309
310 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200312 ssl->handshake->retransmit_timeout ) );
313
314 return( 0 );
315}
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200319 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200321 ssl->handshake->retransmit_timeout ) );
322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200326/*
327 * Convert max_fragment_length codes to length.
328 * RFC 6066 says:
329 * enum{
330 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
331 * } MaxFragmentLength;
332 * and we add 0 -> extension unused
333 */
Angus Grattond8213d02016-05-25 20:56:48 +1000334static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200335{
Angus Grattond8213d02016-05-25 20:56:48 +1000336 switch( mfl )
337 {
338 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
339 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
340 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
341 return 512;
342 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
343 return 1024;
344 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
345 return 2048;
346 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
347 return 4096;
348 default:
349 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
350 }
351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200353
Hanno Becker52055ae2019-02-06 14:30:46 +0000354int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
355 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_ssl_session_free( dst );
358 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000361
362#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200363 if( src->peer_cert != NULL )
364 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200365 int ret;
366
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200367 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200368 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200374 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200377 dst->peer_cert = NULL;
378 return( ret );
379 }
380 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000381#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000382 if( src->peer_cert_digest != NULL )
383 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000384 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000385 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000386 if( dst->peer_cert_digest == NULL )
387 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
388
389 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
390 src->peer_cert_digest_len );
391 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000392 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000393 }
394#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399 if( src->ticket != NULL )
400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200401 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200402 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
405 memcpy( dst->ticket, src->ticket, src->ticket_len );
406 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200407#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408
409 return( 0 );
410}
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
413int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200414 const unsigned char *key_enc, const unsigned char *key_dec,
415 size_t keylen,
416 const unsigned char *iv_enc, const unsigned char *iv_dec,
417 size_t ivlen,
418 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200419 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
421int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
422int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
423int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
424int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
425#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000426
Paul Bakker5121ce52009-01-03 21:22:43 +0000427/*
428 * Key material generation
429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200431static int ssl3_prf( const unsigned char *secret, size_t slen,
432 const char *label,
433 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000434 unsigned char *dstbuf, size_t dlen )
435{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100436 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000437 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200438 mbedtls_md5_context md5;
439 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000440 unsigned char padding[16];
441 unsigned char sha1sum[20];
442 ((void)label);
443
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200444 mbedtls_md5_init( &md5 );
445 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200446
Paul Bakker5f70b252012-09-13 14:23:06 +0000447 /*
448 * SSLv3:
449 * block =
450 * MD5( secret + SHA1( 'A' + secret + random ) ) +
451 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
452 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
453 * ...
454 */
455 for( i = 0; i < dlen / 16; i++ )
456 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200457 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100467 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100468 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000469
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100474 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000478 }
479
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200481 mbedtls_md5_free( &md5 );
482 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000483
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500484 mbedtls_platform_zeroize( padding, sizeof( padding ) );
485 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200492static int tls1_prf( const unsigned char *secret, size_t slen,
493 const char *label,
494 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000495 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 size_t nb, hs;
498 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200499 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300500 unsigned char *tmp;
501 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 const mbedtls_md_info_t *md_info;
504 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100505 int ret;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Ron Eldor3b350852019-05-07 18:31:49 +0300509 tmp_len = 20 + strlen( label ) + rlen;
510 tmp = mbedtls_calloc( 1, tmp_len );
511 if( tmp == NULL )
512 {
513 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
514 goto exit;
515 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517 hs = ( slen + 1 ) / 2;
518 S1 = secret;
519 S2 = secret + slen - hs;
520
521 nb = strlen( label );
522 memcpy( tmp + 20, label, nb );
523 memcpy( tmp + 20 + nb, random, rlen );
524 nb += rlen;
525
526 /*
527 * First compute P_md5(secret,label+random)[0..dlen]
528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300530 {
531 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
532 goto exit;
533 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300536 {
537 goto exit;
538 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
541 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
542 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 for( i = 0; i < dlen; i += 16 )
545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_reset ( &md_ctx );
547 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
548 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md_hmac_reset ( &md_ctx );
551 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
552 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
555
556 for( j = 0; j < k; j++ )
557 dstbuf[i + j] = h_i[j];
558 }
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 /*
563 * XOR out with P_sha1(secret,label+random)[0..dlen]
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300566 {
567 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
568 goto exit;
569 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300572 {
573 goto exit;
574 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
577 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
578 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 for( i = 0; i < dlen; i += 20 )
581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
584 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_hmac_reset ( &md_ctx );
587 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
588 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
591
592 for( j = 0; j < k; j++ )
593 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
594 }
595
Ron Eldor3b350852019-05-07 18:31:49 +0300596exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Ron Eldor3b350852019-05-07 18:31:49 +0300599 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Ron Eldor3b350852019-05-07 18:31:49 +0300602 mbedtls_free( tmp );
603 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500608#if defined(MBEDTLS_USE_PSA_CRYPTO)
609static int tls_prf_generic( mbedtls_md_type_t md_type,
610 const unsigned char *secret, size_t slen,
611 const char *label,
612 const unsigned char *random, size_t rlen,
613 unsigned char *dstbuf, size_t dlen )
614{
615 psa_status_t status;
616 psa_algorithm_t alg;
617 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500618 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500619 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
620
Andrzej Kurek2f760752019-01-28 08:08:15 -0500621 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500622 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500623
Andrzej Kurekc929a822019-01-14 03:51:11 -0500624 if( md_type == MBEDTLS_MD_SHA384 )
625 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
626 else
627 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
628
Andrzej Kurek2f760752019-01-28 08:08:15 -0500629 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500630 psa_key_policy_set_usage( &policy,
631 PSA_KEY_USAGE_DERIVE,
632 alg );
633 status = psa_set_key_policy( master_slot, &policy );
634 if( status != PSA_SUCCESS )
635 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
636
637 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
638 if( status != PSA_SUCCESS )
639 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
640
641 status = psa_key_derivation( &generator,
642 master_slot, alg,
643 random, rlen,
644 (unsigned char const *) label,
645 (size_t) strlen( label ),
646 dlen );
647 if( status != PSA_SUCCESS )
648 {
649 psa_generator_abort( &generator );
650 psa_destroy_key( master_slot );
651 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
652 }
653
654 status = psa_generator_read( &generator, dstbuf, dlen );
655 if( status != PSA_SUCCESS )
656 {
657 psa_generator_abort( &generator );
658 psa_destroy_key( master_slot );
659 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
660 }
661
662 status = psa_generator_abort( &generator );
663 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500664 {
665 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500666 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500667 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500668
669 status = psa_destroy_key( master_slot );
670 if( status != PSA_SUCCESS )
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672
Andrzej Kurek33171262019-01-15 03:25:18 -0500673 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500674}
675
676#else /* MBEDTLS_USE_PSA_CRYPTO */
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100679 const unsigned char *secret, size_t slen,
680 const char *label,
681 const unsigned char *random, size_t rlen,
682 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000683{
684 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100685 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300686 unsigned char *tmp;
687 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
689 const mbedtls_md_info_t *md_info;
690 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100691 int ret;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100699
Ron Eldor3b350852019-05-07 18:31:49 +0300700 tmp_len = md_len + strlen( label ) + rlen;
701 tmp = mbedtls_calloc( 1, tmp_len );
702 if( tmp == NULL )
703 {
704 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
705 goto exit;
706 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000707
708 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100709 memcpy( tmp + md_len, label, nb );
710 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000711 nb += rlen;
712
713 /*
714 * Compute P_<hash>(secret, label + random)[0..dlen]
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300717 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
720 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
721 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100722
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100723 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_md_hmac_reset ( &md_ctx );
726 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
727 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_md_hmac_reset ( &md_ctx );
730 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
731 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000732
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100733 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000734
735 for( j = 0; j < k; j++ )
736 dstbuf[i + j] = h_i[j];
737 }
738
Ron Eldor3b350852019-05-07 18:31:49 +0300739exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100741
Ron Eldor3b350852019-05-07 18:31:49 +0300742 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744
Ron Eldor3b350852019-05-07 18:31:49 +0300745 mbedtls_free( tmp );
746
747 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000748}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500749#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100751static int tls_prf_sha256( const unsigned char *secret, size_t slen,
752 const char *label,
753 const unsigned char *random, size_t rlen,
754 unsigned char *dstbuf, size_t dlen )
755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 label, random, rlen, dstbuf, dlen ) );
758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200762static int tls_prf_sha384( const unsigned char *secret, size_t slen,
763 const char *label,
764 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765 unsigned char *dstbuf, size_t dlen )
766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100768 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* MBEDTLS_SHA512_C */
771#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
776 defined(MBEDTLS_SSL_PROTO_TLS1_1)
777static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778#endif
Paul Bakker380da532012-04-18 16:10:25 +0000779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_PROTO_SSL3)
781static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
782static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#endif
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
786static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
787static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788#endif
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
791#if defined(MBEDTLS_SHA256_C)
792static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
793static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
794static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200795#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#if defined(MBEDTLS_SHA512_C)
798static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
799static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
800static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000803
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100804#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100805 defined(MBEDTLS_USE_PSA_CRYPTO)
806static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
807{
808 if( ssl->conf->f_psk != NULL )
809 {
810 /* If we've used a callback to select the PSK,
811 * the static configuration is irrelevant. */
812 if( ssl->handshake->psk_opaque != 0 )
813 return( 1 );
814
815 return( 0 );
816 }
817
818 if( ssl->conf->psk_opaque != 0 )
819 return( 1 );
820
821 return( 0 );
822}
823#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100824 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100825
Ron Eldorcf280092019-05-14 20:19:13 +0300826#if defined(MBEDTLS_SSL_EXPORT_KEYS)
827static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
828{
829#if defined(MBEDTLS_SSL_PROTO_SSL3)
830 if( tls_prf == ssl3_prf )
831 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300832 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300833 }
834 else
835#endif
836#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
837 if( tls_prf == tls1_prf )
838 {
839 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
840 }
841 else
842#endif
843#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
844#if defined(MBEDTLS_SHA512_C)
845 if( tls_prf == tls_prf_sha384 )
846 {
847 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
848 }
849 else
850#endif
851#if defined(MBEDTLS_SHA256_C)
852 if( tls_prf == tls_prf_sha256 )
853 {
854 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
855 }
856 else
857#endif
858#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
859 return( MBEDTLS_SSL_TLS_PRF_NONE );
860}
861#endif /* MBEDTLS_SSL_EXPORT_KEYS */
862
Ron Eldor51d3ab52019-05-12 14:54:30 +0300863int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
864 const unsigned char *secret, size_t slen,
865 const char *label,
866 const unsigned char *random, size_t rlen,
867 unsigned char *dstbuf, size_t dlen )
868{
869 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
870
871 switch( prf )
872 {
873#if defined(MBEDTLS_SSL_PROTO_SSL3)
874 case MBEDTLS_SSL_TLS_PRF_SSL3:
875 tls_prf = ssl3_prf;
876 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300877#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300878#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
879 case MBEDTLS_SSL_TLS_PRF_TLS1:
880 tls_prf = tls1_prf;
881 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300882#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
883
884#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300885#if defined(MBEDTLS_SHA512_C)
886 case MBEDTLS_SSL_TLS_PRF_SHA384:
887 tls_prf = tls_prf_sha384;
888 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300889#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300890#if defined(MBEDTLS_SHA256_C)
891 case MBEDTLS_SSL_TLS_PRF_SHA256:
892 tls_prf = tls_prf_sha256;
893 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300894#endif /* MBEDTLS_SHA256_C */
895#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300896 default:
897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
898 }
899
900 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
901}
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000904{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200905 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000906#if defined(MBEDTLS_USE_PSA_CRYPTO)
907 int psa_fallthrough;
908#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 unsigned char keyblk[256];
911 unsigned char *key1;
912 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100913 unsigned char *mac_enc;
914 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000915 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200916 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000917 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000918 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 const mbedtls_cipher_info_t *cipher_info;
920 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100921
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000922 /* cf. RFC 5246, Section 8.1:
923 * "The master secret is always exactly 48 bytes in length." */
924 size_t const master_secret_len = 48;
925
Hanno Becker35b23c72018-10-23 12:10:41 +0100926#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
927 unsigned char session_hash[48];
928#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_ssl_session *session = ssl->session_negotiate;
931 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
932 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000936#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
937 transform->encrypt_then_mac = session->encrypt_then_mac;
938#endif
939 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000940
941 ciphersuite_info = handshake->ciphersuite_info;
942 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100943 if( cipher_info == NULL )
944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000946 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100948 }
949
Hanno Beckere694c3e2017-12-27 21:34:08 +0000950 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100951 if( md_info == NULL )
952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000954 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100956 }
957
Hanno Becker4bf74652019-04-26 16:22:27 +0100958#if defined(MBEDTLS_SSL_CID)
959 /* Copy own and peer's CID if the use of the CID
960 * extension has been negotiated. */
961 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
962 {
963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100964
965 /* Uncomment this once CID-parsing and support for a change
966 * record content type during record decryption are added. */
967 /* transform->in_cid_len = ssl->own_cid_len; */
968 /* transform->out_cid_len = ssl->handshake->peer_cid_len; */
969 /* memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); */
970 /* memcpy( transform->out_cid, ssl->handshake->peer_cid, */
971 /* ssl->handshake->peer_cid_len ); */
Hanno Becker4bf74652019-04-26 16:22:27 +0100972
973 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
974 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100975 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100976 transform->in_cid_len );
977 }
978#endif /* MBEDTLS_SSL_CID */
979
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000981 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_SSL_PROTO_SSL3)
984 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000985 {
Paul Bakker48916f92012-09-16 19:57:18 +0000986 handshake->tls_prf = ssl3_prf;
987 handshake->calc_verify = ssl_calc_verify_ssl;
988 handshake->calc_finished = ssl_calc_finished_ssl;
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) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
993 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000994 {
Paul Bakker48916f92012-09-16 19:57:18 +0000995 handshake->tls_prf = tls1_prf;
996 handshake->calc_verify = ssl_calc_verify_tls;
997 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000998 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200999 else
1000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1002#if defined(MBEDTLS_SHA512_C)
1003 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001004 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001005 {
Paul Bakker48916f92012-09-16 19:57:18 +00001006 handshake->tls_prf = tls_prf_sha384;
1007 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1008 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001010 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_SHA256_C)
1013 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001014 {
Paul Bakker48916f92012-09-16 19:57:18 +00001015 handshake->tls_prf = tls_prf_sha256;
1016 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1017 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001018 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001019 else
1020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001025 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001026
1027 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 * SSLv3:
1029 * master =
1030 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1031 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1032 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001033 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001034 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 * master = PRF( premaster, "master secret", randbytes )[0..47]
1036 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001037 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1040 }
1041 else
1042 {
1043 /* The label for the KDF used for key expansion.
1044 * This is either "master secret" or "extended master secret"
1045 * depending on whether the Extended Master Secret extension
1046 * is used. */
1047 char const *lbl = "master secret";
1048
1049 /* The salt for the KDF used for key expansion.
1050 * - If the Extended Master Secret extension is not used,
1051 * this is ClientHello.Random + ServerHello.Random
1052 * (see Sect. 8.1 in RFC 5246).
1053 * - If the Extended Master Secret extension is used,
1054 * this is the transcript of the handshake so far.
1055 * (see Sect. 4 in RFC 7627). */
1056 unsigned char const *salt = handshake->randbytes;
1057 size_t salt_len = 64;
1058
1059#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001063
Hanno Becker35b23c72018-10-23 12:10:41 +01001064 lbl = "extended master secret";
1065 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001066 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1068 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001071 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1072 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001073 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001074 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001075 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001076#endif /* MBEDTLS_SHA512_C */
1077 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001078 }
1079 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001081 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001082
Hanno Becker35b23c72018-10-23 12:10:41 +01001083 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001084 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001085#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1086
Hanno Becker7d0a5692018-10-23 15:26:22 +01001087#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1088 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1089 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1090 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1091 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001092 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001093 /* Perform PSK-to-MS expansion in a single step. */
1094 psa_status_t status;
1095 psa_algorithm_t alg;
1096 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001097 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001098
1099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1100
1101 psk = ssl->conf->psk_opaque;
1102 if( ssl->handshake->psk_opaque != 0 )
1103 psk = ssl->handshake->psk_opaque;
1104
Hanno Becker22bf1452019-04-05 11:21:08 +01001105 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001106 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1107 else
1108 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1109
1110 status = psa_key_derivation( &generator, psk, alg,
1111 salt, salt_len,
1112 (unsigned char const *) lbl,
1113 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001114 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001115 if( status != PSA_SUCCESS )
1116 {
1117 psa_generator_abort( &generator );
1118 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1119 }
1120
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001121 status = psa_generator_read( &generator, session->master,
1122 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001123 if( status != PSA_SUCCESS )
1124 {
1125 psa_generator_abort( &generator );
1126 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1127 }
1128
1129 status = psa_generator_abort( &generator );
1130 if( status != PSA_SUCCESS )
1131 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001132 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001133 else
1134#endif
1135 {
1136 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1137 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001138 session->master,
1139 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001140 if( ret != 0 )
1141 {
1142 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1143 return( ret );
1144 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001145
Hanno Becker7d0a5692018-10-23 15:26:22 +01001146 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1147 handshake->premaster,
1148 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001149
Hanno Becker7d0a5692018-10-23 15:26:22 +01001150 mbedtls_platform_zeroize( handshake->premaster,
1151 sizeof(handshake->premaster) );
1152 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155 /*
1156 * Swap the client and server random values.
1157 */
Paul Bakker48916f92012-09-16 19:57:18 +00001158 memcpy( tmp, handshake->randbytes, 64 );
1159 memcpy( handshake->randbytes, tmp + 32, 32 );
1160 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001161 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
1163 /*
1164 * SSLv3:
1165 * key block =
1166 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1167 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1168 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1169 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1170 * ...
1171 *
1172 * TLSv1:
1173 * key block = PRF( master, "key expansion", randbytes )
1174 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001175 ret = handshake->tls_prf( session->master, 48, "key expansion",
1176 handshake->randbytes, 64, keyblk, 256 );
1177 if( ret != 0 )
1178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001180 return( ret );
1181 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1184 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1185 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1186 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1187 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 /*
1190 * Determine the appropriate key, IV and MAC length.
1191 */
Paul Bakker68884e32013-01-07 18:20:04 +01001192
Hanno Becker88aaf652017-12-27 08:17:40 +00001193 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001194
Hanno Becker8031d062018-01-03 15:32:31 +00001195#if defined(MBEDTLS_GCM_C) || \
1196 defined(MBEDTLS_CCM_C) || \
1197 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001199 cipher_info->mode == MBEDTLS_MODE_CCM ||
1200 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001202 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001203
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001204 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001205 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001206 transform->taglen =
1207 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001208
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001209 /* All modes haves 96-bit IVs;
1210 * GCM and CCM has 4 implicit and 8 explicit bytes
1211 * ChachaPoly has all 12 bytes implicit
1212 */
Paul Bakker68884e32013-01-07 18:20:04 +01001213 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001214 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1215 transform->fixed_ivlen = 12;
1216 else
1217 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001218
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001219 /* Minimum length of encrypted record */
1220 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001221 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001222 }
1223 else
Hanno Becker8031d062018-01-03 15:32:31 +00001224#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1225#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1226 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1227 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001228 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001229 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1231 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001234 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001237 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001238 mac_key_len = mbedtls_md_get_size( md_info );
1239 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001242 /*
1243 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1244 * (rfc 6066 page 13 or rfc 2104 section 4),
1245 * so we only need to adjust the length here.
1246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001250
1251#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1252 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001253 * HMAC implementation which also truncates the key
1254 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001255 mac_key_len = transform->maclen;
1256#endif
1257 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001259
1260 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001261 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001263 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001265 transform->minlen = transform->maclen;
1266 else
Paul Bakker68884e32013-01-07 18:20:04 +01001267 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001268 /*
1269 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001270 * 1. if EtM is in use: one block plus MAC
1271 * otherwise: * first multiple of blocklen greater than maclen
1272 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1275 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001276 {
1277 transform->minlen = transform->maclen
1278 + cipher_info->block_size;
1279 }
1280 else
1281#endif
1282 {
1283 transform->minlen = transform->maclen
1284 + cipher_info->block_size
1285 - transform->maclen % cipher_info->block_size;
1286 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1289 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1290 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001291 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001292 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1295 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1296 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001297 {
1298 transform->minlen += transform->ivlen;
1299 }
1300 else
1301#endif
1302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001304 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1305 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001306 }
Paul Bakker68884e32013-01-07 18:20:04 +01001307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 }
Hanno Becker8031d062018-01-03 15:32:31 +00001309 else
1310#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1311 {
1312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Hanno Becker88aaf652017-12-27 08:17:40 +00001316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1317 (unsigned) keylen,
1318 (unsigned) transform->minlen,
1319 (unsigned) transform->ivlen,
1320 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
1322 /*
1323 * Finally setup the cipher contexts, IVs and MAC secrets.
1324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001326 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001328 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001329 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Paul Bakker68884e32013-01-07 18:20:04 +01001331 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001332 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001334 /*
1335 * This is not used in TLS v1.1.
1336 */
Paul Bakker48916f92012-09-16 19:57:18 +00001337 iv_copy_len = ( transform->fixed_ivlen ) ?
1338 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001339 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1340 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001341 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
1343 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_SSL_CLI_C */
1345#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001346 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001348 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001349 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Hanno Becker81c7b182017-11-09 18:39:33 +00001351 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001352 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001354 /*
1355 * This is not used in TLS v1.1.
1356 */
Paul Bakker48916f92012-09-16 19:57:18 +00001357 iv_copy_len = ( transform->fixed_ivlen ) ?
1358 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001359 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1360 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001363 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001367 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1368 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Hanno Beckerd56ed242018-01-03 15:32:51 +00001371#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_SSL_PROTO_SSL3)
1373 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001374 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001375 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001378 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1379 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001380 }
1381
Hanno Becker81c7b182017-11-09 18:39:33 +00001382 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1383 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001384 }
1385 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1387#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1388 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1389 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001390 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001391 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1392 For AEAD-based ciphersuites, there is nothing to do here. */
1393 if( mac_key_len != 0 )
1394 {
1395 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1396 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1397 }
Paul Bakker68884e32013-01-07 18:20:04 +01001398 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001399 else
1400#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001403 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1404 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001405 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001406#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1409 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001410 {
1411 int ret = 0;
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001414
Hanno Becker88aaf652017-12-27 08:17:40 +00001415 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001416 transform->iv_enc, transform->iv_dec,
1417 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001418 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001419 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001422 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1423 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001424 }
1425 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001426#else
1427 ((void) mac_dec);
1428 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001430
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001431#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1432 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001433 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001434 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1435 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001436 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001437 iv_copy_len );
1438 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001439
1440 if( ssl->conf->f_export_keys_ext != NULL )
1441 {
1442 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1443 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001444 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001445 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001446 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001447 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001448 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001449 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001450#endif
1451
Hanno Beckerf704bef2018-11-16 15:21:18 +00001452#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001453
1454 /* Only use PSA-based ciphers for TLS-1.2.
1455 * That's relevant at least for TLS-1.0, where
1456 * we assume that mbedtls_cipher_crypt() updates
1457 * the structure field for the IV, which the PSA-based
1458 * implementation currently doesn't. */
1459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1460 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001461 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001462 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001463 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001464 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1465 {
1466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001467 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001468 }
1469
1470 if( ret == 0 )
1471 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001473 psa_fallthrough = 0;
1474 }
1475 else
1476 {
1477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1478 psa_fallthrough = 1;
1479 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001480 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001481 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001482 psa_fallthrough = 1;
1483#else
1484 psa_fallthrough = 1;
1485#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001486
Hanno Beckercb1cc802018-11-17 22:27:38 +00001487 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001488#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001489 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001490 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001493 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001494 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001495
Hanno Beckerf704bef2018-11-16 15:21:18 +00001496#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001497 /* Only use PSA-based ciphers for TLS-1.2.
1498 * That's relevant at least for TLS-1.0, where
1499 * we assume that mbedtls_cipher_crypt() updates
1500 * the structure field for the IV, which the PSA-based
1501 * implementation currently doesn't. */
1502#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1503 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001504 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001506 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001507 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1508 {
1509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001510 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001511 }
1512
1513 if( ret == 0 )
1514 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001516 psa_fallthrough = 0;
1517 }
1518 else
1519 {
1520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1521 psa_fallthrough = 1;
1522 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001523 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001524 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001525 psa_fallthrough = 1;
1526#else
1527 psa_fallthrough = 1;
1528#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001529
Hanno Beckercb1cc802018-11-17 22:27:38 +00001530 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001531#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001532 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001533 cipher_info ) ) != 0 )
1534 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001536 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001537 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001540 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001544 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001548 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001552 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001553 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_CIPHER_MODE_CBC)
1556 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1559 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001562 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001563 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1566 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001569 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001576 // Initialize compression
1577 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001579 {
Paul Bakker16770332013-10-11 09:59:44 +02001580 if( ssl->compress_buf == NULL )
1581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001583 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001584 if( ssl->compress_buf == NULL )
1585 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001587 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001588 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1589 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001590 }
1591 }
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594
Paul Bakker48916f92012-09-16 19:57:18 +00001595 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1596 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001598 if( deflateInit( &transform->ctx_deflate,
1599 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001600 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001603 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1604 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605 }
1606 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001610end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001611 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1612 mbedtls_platform_zeroize( handshake->randbytes,
1613 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001614 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615}
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#if defined(MBEDTLS_SSL_PROTO_SSL3)
1618void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001619{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001620 mbedtls_md5_context md5;
1621 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 unsigned char pad_1[48];
1623 unsigned char pad_2[48];
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001627 mbedtls_md5_init( &md5 );
1628 mbedtls_sha1_init( &sha1 );
1629
1630 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1631 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Paul Bakker380da532012-04-18 16:10:25 +00001633 memset( pad_1, 0x36, 48 );
1634 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1638 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001640 mbedtls_md5_starts_ret( &md5 );
1641 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1642 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1643 mbedtls_md5_update_ret( &md5, hash, 16 );
1644 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1648 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001649
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001650 mbedtls_sha1_starts_ret( &sha1 );
1651 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1652 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1653 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1654 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001658
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001659 mbedtls_md5_free( &md5 );
1660 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001661
Paul Bakker380da532012-04-18 16:10:25 +00001662 return;
1663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1667void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001668{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001669 mbedtls_md5_context md5;
1670 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001673
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001674 mbedtls_md5_init( &md5 );
1675 mbedtls_sha1_init( &sha1 );
1676
1677 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1678 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001679
Andrzej Kurekeb342242019-01-29 09:14:33 -05001680 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001681 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001685
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001686 mbedtls_md5_free( &md5 );
1687 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001688
Paul Bakker380da532012-04-18 16:10:25 +00001689 return;
1690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1694#if defined(MBEDTLS_SHA256_C)
1695void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001696{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001697#if defined(MBEDTLS_USE_PSA_CRYPTO)
1698 size_t hash_size;
1699 psa_status_t status;
1700 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1701
1702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1703 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1704 if( status != PSA_SUCCESS )
1705 {
1706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1707 return;
1708 }
1709
1710 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1711 if( status != PSA_SUCCESS )
1712 {
1713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1714 return;
1715 }
1716 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1718#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001719 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001720
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001721 mbedtls_sha256_init( &sha256 );
1722
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001724
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001725 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001726 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001730
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001731 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001733 return;
1734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SHA512_C)
1738void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001739{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001740#if defined(MBEDTLS_USE_PSA_CRYPTO)
1741 size_t hash_size;
1742 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001743 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001744
1745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001746 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001747 if( status != PSA_SUCCESS )
1748 {
1749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1750 return;
1751 }
1752
Andrzej Kurek972fba52019-01-30 03:29:12 -05001753 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001754 if( status != PSA_SUCCESS )
1755 {
1756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1757 return;
1758 }
1759 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1761#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001762 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001763
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001764 mbedtls_sha512_init( &sha512 );
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001767
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001768 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001769 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001774 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001775#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 return;
1777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#endif /* MBEDTLS_SHA512_C */
1779#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1782int 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 +02001783{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001784 unsigned char *p = ssl->handshake->premaster;
1785 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001786 const unsigned char *psk = ssl->conf->psk;
1787 size_t psk_len = ssl->conf->psk_len;
1788
1789 /* If the psk callback was called, use its result */
1790 if( ssl->handshake->psk != NULL )
1791 {
1792 psk = ssl->handshake->psk;
1793 psk_len = ssl->handshake->psk_len;
1794 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001795
1796 /*
1797 * PMS = struct {
1798 * opaque other_secret<0..2^16-1>;
1799 * opaque psk<0..2^16-1>;
1800 * };
1801 * with "other_secret" depending on the particular key exchange
1802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1804 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001805 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001806 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001808
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001809 *(p++) = (unsigned char)( psk_len >> 8 );
1810 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001811
1812 if( end < p || (size_t)( end - p ) < psk_len )
1813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1814
1815 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001816 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001817 }
1818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1820#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1821 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001822 {
1823 /*
1824 * other_secret already set by the ClientKeyExchange message,
1825 * and is 48 bytes long
1826 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001827 if( end - p < 2 )
1828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1829
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001830 *p++ = 0;
1831 *p++ = 48;
1832 p += 48;
1833 }
1834 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1836#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1837 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001838 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001839 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001840 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001841
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001842 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001844 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001845 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001848 return( ret );
1849 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001850 *(p++) = (unsigned char)( len >> 8 );
1851 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001852 p += len;
1853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001855 }
1856 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1858#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1859 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001861 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 size_t zlen;
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001865 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001866 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 return( ret );
1870 }
1871
1872 *(p++) = (unsigned char)( zlen >> 8 );
1873 *(p++) = (unsigned char)( zlen );
1874 p += zlen;
1875
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001876 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1877 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001878 }
1879 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001884 }
1885
1886 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001887 if( end - p < 2 )
1888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001889
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001890 *(p++) = (unsigned char)( psk_len >> 8 );
1891 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001892
1893 if( end < p || (size_t)( end - p ) < psk_len )
1894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1895
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001896 memcpy( p, psk, psk_len );
1897 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001898
1899 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1900
1901 return( 0 );
1902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001906/*
1907 * SSLv3.0 MAC functions
1908 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001909#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001910static void ssl_mac( mbedtls_md_context_t *md_ctx,
1911 const unsigned char *secret,
1912 const unsigned char *buf, size_t len,
1913 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001914 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915{
1916 unsigned char header[11];
1917 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001918 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1920 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001921
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001922 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001924 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001925 else
Paul Bakker68884e32013-01-07 18:20:04 +01001926 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
1928 memcpy( header, ctr, 8 );
1929 header[ 8] = (unsigned char) type;
1930 header[ 9] = (unsigned char)( len >> 8 );
1931 header[10] = (unsigned char)( len );
1932
Paul Bakker68884e32013-01-07 18:20:04 +01001933 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_md_starts( md_ctx );
1935 mbedtls_md_update( md_ctx, secret, md_size );
1936 mbedtls_md_update( md_ctx, padding, padlen );
1937 mbedtls_md_update( md_ctx, header, 11 );
1938 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001939 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
Paul Bakker68884e32013-01-07 18:20:04 +01001941 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 mbedtls_md_starts( md_ctx );
1943 mbedtls_md_update( md_ctx, secret, md_size );
1944 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001945 mbedtls_md_update( md_ctx, out, md_size );
1946 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001949
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001950/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001951 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001952#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001953 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1954 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1955 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1956/* This function makes sure every byte in the memory region is accessed
1957 * (in ascending addresses order) */
1958static void ssl_read_memory( unsigned char *p, size_t len )
1959{
1960 unsigned char acc = 0;
1961 volatile unsigned char force;
1962
1963 for( ; len != 0; p++, len-- )
1964 acc ^= *p;
1965
1966 force = acc;
1967 (void) force;
1968}
1969#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1970
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001971/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001973 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001974
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001975#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001976/* This functions transforms a DTLS plaintext fragment and a record content
1977 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001978 *
1979 * struct {
1980 * opaque content[DTLSPlaintext.length];
1981 * ContentType real_type;
1982 * uint8 zeros[length_of_padding];
1983 * } DTLSInnerPlaintext;
1984 *
1985 * Input:
1986 * - `content`: The beginning of the buffer holding the
1987 * plaintext to be wrapped.
1988 * - `*content_size`: The length of the plaintext in Bytes.
1989 * - `max_len`: The number of Bytes available starting from
1990 * `content`. This must be `>= *content_size`.
1991 * - `rec_type`: The desired record content type.
1992 *
1993 * Output:
1994 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1995 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1996 *
1997 * Returns:
1998 * - `0` on success.
1999 * - A negative error code if `max_len` didn't offer enough space
2000 * for the expansion.
2001 */
2002static int ssl_cid_build_inner_plaintext( unsigned char *content,
2003 size_t *content_size,
2004 size_t remaining,
2005 uint8_t rec_type )
2006{
2007 size_t len = *content_size;
2008 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2009
2010 /* Write real content type */
2011 if( remaining == 0 )
2012 return( -1 );
2013 content[ len ] = rec_type;
2014 len++;
2015 remaining--;
2016
2017 if( remaining < pad )
2018 return( -1 );
2019 memset( content + len, 0, pad );
2020 len += pad;
2021 remaining -= pad;
2022
2023 *content_size = len;
2024 return( 0 );
2025}
2026
Hanno Becker07dc97d2019-05-20 15:08:01 +01002027/* This function parses a DTLSInnerPlaintext structure.
2028 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002029static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2030 size_t *content_size,
2031 uint8_t *rec_type )
2032{
2033 size_t remaining = *content_size;
2034
2035 /* Determine length of padding by skipping zeroes from the back. */
2036 do
2037 {
2038 if( remaining == 0 )
2039 return( -1 );
2040 remaining--;
2041 } while( content[ remaining ] == 0 );
2042
2043 *content_size = remaining;
2044 *rec_type = content[ remaining ];
2045
2046 return( 0 );
2047}
2048#endif /* MBEDTLS_SSL_CID */
2049
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002050/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002051 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002052static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002053 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002054 mbedtls_record *rec )
2055{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002056 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002057 *
2058 * additional_data = seq_num + TLSCompressed.type +
2059 * TLSCompressed.version + TLSCompressed.length;
2060 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002061 * For the CID extension, this is extended as follows
2062 * (quoting draft-ietf-tls-dtls-connection-id-05,
2063 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002064 *
2065 * additional_data = seq_num + DTLSPlaintext.type +
2066 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002067 * cid +
2068 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002069 * length_of_DTLSInnerPlaintext;
2070 */
2071
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2073 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002074 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002075
2076#if defined(MBEDTLS_SSL_CID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002077 if( rec->cid_len != 0 )
2078 {
2079 memcpy( add_data + 11, rec->cid, rec->cid_len );
2080 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2081 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2082 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2083 *add_data_len = 13 + 1 + rec->cid_len;
2084 }
2085 else
Hanno Beckercab87e62019-04-29 13:52:53 +01002086#endif /* MBEDTLS_SSL_CID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002087 {
2088 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2089 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2090 *add_data_len = 13;
2091 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092}
2093
Hanno Beckera18d1322018-01-03 14:27:32 +00002094int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2095 mbedtls_ssl_transform *transform,
2096 mbedtls_record *rec,
2097 int (*f_rng)(void *, unsigned char *, size_t),
2098 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002101 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002103 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002104 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105 size_t post_avail;
2106
2107 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002108#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109 ((void) ssl);
2110#endif
2111
2112 /* The PRNG is used for dynamic IV generation that's used
2113 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2114#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2115 ( defined(MBEDTLS_AES_C) || \
2116 defined(MBEDTLS_ARIA_C) || \
2117 defined(MBEDTLS_CAMELLIA_C) ) && \
2118 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2119 ((void) f_rng);
2120 ((void) p_rng);
2121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002126 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2129 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002130 if( rec == NULL
2131 || rec->buf == NULL
2132 || rec->buf_len < rec->data_offset
2133 || rec->buf_len - rec->data_offset < rec->data_len
2134#if defined(MBEDTLS_SSL_CID)
2135 || rec->cid_len != 0
2136#endif
2137 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002141 }
2142
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002144 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002146 data, rec->data_len );
2147
2148 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2149
2150 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2151 {
2152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2153 (unsigned) rec->data_len,
2154 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2156 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002157
Hanno Beckercab87e62019-04-29 13:52:53 +01002158#if defined(MBEDTLS_SSL_CID)
2159 /*
2160 * Add CID information
2161 */
2162 rec->cid_len = transform->out_cid_len;
2163 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2164 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002165
2166 if( rec->cid_len != 0 )
2167 {
2168 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002169 * Wrap plaintext into DTLSInnerPlaintext structure.
2170 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002171 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002172 * Note that this changes `rec->data_len`, and hence
2173 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002174 */
2175 if( ssl_cid_build_inner_plaintext( data,
2176 &rec->data_len,
2177 post_avail,
2178 rec->type ) != 0 )
2179 {
2180 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2181 }
2182
2183 rec->type = MBEDTLS_SSL_MSG_CID;
2184 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002185#endif /* MBEDTLS_SSL_CID */
2186
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002187 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2188
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002190 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 */
Hanno Becker52344c22018-01-03 15:24:20 +00002192#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 if( mode == MBEDTLS_MODE_STREAM ||
2194 ( mode == MBEDTLS_MODE_CBC
2195#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002196 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002197#endif
2198 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002200 if( post_avail < transform->maclen )
2201 {
2202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2203 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2204 }
2205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002207 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002208 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002209 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002210 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2211 data, rec->data_len, rec->ctr, rec->type, mac );
2212 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002213 }
2214 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2217 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002218 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002219 {
Hanno Becker992b6872017-11-09 18:57:39 +00002220 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2221
Hanno Beckercab87e62019-04-29 13:52:53 +01002222 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002223
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002225 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2227 data, rec->data_len );
2228 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2229 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2230
2231 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002232 }
2233 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002234#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002238 }
2239
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2241 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002242
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 rec->data_len += transform->maclen;
2244 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002245 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002246 }
Hanno Becker52344c22018-01-03 15:24:20 +00002247#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002249 /*
2250 * Encrypt
2251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2253 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002255 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002256 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002258 "including %d bytes of padding",
2259 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002261 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2262 transform->iv_enc, transform->ivlen,
2263 data, rec->data_len,
2264 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002267 return( ret );
2268 }
2269
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002270 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
Paul Bakker68884e32013-01-07 18:20:04 +01002276 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002278
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002279#if defined(MBEDTLS_GCM_C) || \
2280 defined(MBEDTLS_CCM_C) || \
2281 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002283 mode == MBEDTLS_MODE_CCM ||
2284 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002285 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002286 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002287 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002288 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002289
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002290 /* Check that there's space for both the authentication tag
2291 * and the explicit IV before and after the record content. */
2292 if( post_avail < transform->taglen ||
2293 rec->data_offset < explicit_iv_len )
2294 {
2295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2296 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2297 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002298
Paul Bakker68884e32013-01-07 18:20:04 +01002299 /*
2300 * Generate IV
2301 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002302 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2303 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002304 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002305 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002306 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2307 explicit_iv_len );
2308 /* Prefix record content with explicit IV. */
2309 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002310 }
2311 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2312 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002313 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002314 unsigned char i;
2315
2316 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2317
2318 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002319 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 }
2321 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002322 {
2323 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002326 }
2327
Hanno Beckercab87e62019-04-29 13:52:53 +01002328 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002329
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2331 iv, transform->ivlen );
2332 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 data - explicit_iv_len, explicit_iv_len );
2334 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002335 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002337 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002338 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002339
Paul Bakker68884e32013-01-07 18:20:04 +01002340 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002341 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002342 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002343
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002344 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002346 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 data, rec->data_len, /* source */
2348 data, &rec->data_len, /* destination */
2349 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002352 return( ret );
2353 }
2354
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2356 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002357
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002358 rec->data_len += transform->taglen + explicit_iv_len;
2359 rec->data_offset -= explicit_iv_len;
2360 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002361 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2365#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002366 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002368 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002369 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 size_t padlen, i;
2371 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002372
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002373 /* Currently we're always using minimal padding
2374 * (up to 255 bytes would be allowed). */
2375 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2376 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 padlen = 0;
2378
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 /* Check there's enough space in the buffer for the padding. */
2380 if( post_avail < padlen + 1 )
2381 {
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2383 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2384 }
2385
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002389 rec->data_len += padlen + 1;
2390 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002393 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002394 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2395 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002396 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002398 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 if( f_rng == NULL )
2400 {
2401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2403 }
2404
2405 if( rec->data_offset < transform->ivlen )
2406 {
2407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2408 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2409 }
2410
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411 /*
2412 * Generate IV
2413 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002414 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002415 if( ret != 0 )
2416 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002418 memcpy( data - transform->ivlen, transform->iv_enc,
2419 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002421 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002426 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002427 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002429 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2430 transform->iv_enc,
2431 transform->ivlen,
2432 data, rec->data_len,
2433 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002436 return( ret );
2437 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002438
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002443 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002447 {
2448 /*
2449 * Save IV in SSL3 and TLS1
2450 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2452 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002454 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002455#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 {
2457 data -= transform->ivlen;
2458 rec->data_offset -= transform->ivlen;
2459 rec->data_len += transform->ivlen;
2460 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002463 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002464 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002465 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2466
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002467 /*
2468 * MAC(MAC_write_key, seq_num +
2469 * TLSCipherText.type +
2470 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002471 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002472 * IV + // except for TLS 1.0
2473 * ENC(content + padding + padding_length));
2474 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002475
2476 if( post_avail < transform->maclen)
2477 {
2478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2479 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2480 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481
Hanno Beckercab87e62019-04-29 13:52:53 +01002482 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002485 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002486 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002487
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002489 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002490 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2491 data, rec->data_len );
2492 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2493 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002495 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002496
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002497 rec->data_len += transform->maclen;
2498 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002499 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002503 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002505 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002511 /* Make extra sure authentication was performed, exactly once */
2512 if( auth_done != 1 )
2513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002516 }
2517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
2520 return( 0 );
2521}
2522
Hanno Beckera18d1322018-01-03 14:27:32 +00002523int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2524 mbedtls_ssl_transform *transform,
2525 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002526{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002527 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002529 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002530#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002531 size_t padlen = 0, correct = 1;
2532#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002533 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002534 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002535 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536
Hanno Beckera18d1322018-01-03 14:27:32 +00002537#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002538 ((void) ssl);
2539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002543 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2546 }
2547 if( rec == NULL ||
2548 rec->buf == NULL ||
2549 rec->buf_len < rec->data_offset ||
2550 rec->buf_len - rec->data_offset < rec->data_len )
2551 {
2552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002554 }
2555
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002556 data = rec->buf + rec->data_offset;
2557 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Hanno Beckercab87e62019-04-29 13:52:53 +01002559#if defined(MBEDTLS_SSL_CID)
2560 /*
2561 * Match record's CID with incoming CID.
2562 */
2563
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002564 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002565 /* if( rec->cid_len != transform->in_cid_len || */
2566 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2567 /* { */
2568 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2569 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002570
2571 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002572 rec->cid_len = transform->in_cid_len;
2573 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2574 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2575#endif /* MBEDTLS_SSL_CID */
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2578 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002579 {
2580 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002581 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2582 transform->iv_dec,
2583 transform->ivlen,
2584 data, rec->data_len,
2585 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002588 return( ret );
2589 }
2590
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002591 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 }
Paul Bakker68884e32013-01-07 18:20:04 +01002597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599#if defined(MBEDTLS_GCM_C) || \
2600 defined(MBEDTLS_CCM_C) || \
2601 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002603 mode == MBEDTLS_MODE_CCM ||
2604 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002605 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002606 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002607 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002608
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 /*
2610 * Compute and update sizes
2611 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002612 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002615 "+ taglen (%d)", rec->data_len,
2616 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002618 }
Paul Bakker68884e32013-01-07 18:20:04 +01002619
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620 /*
2621 * Prepare IV
2622 */
2623 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2624 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002625 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002628
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 }
2630 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2631 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002632 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 unsigned char i;
2634
2635 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2636
2637 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 }
2640 else
2641 {
2642 /* Reminder if we ever add an AEAD mode with a different size */
2643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2645 }
2646
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002647 data += explicit_iv_len;
2648 rec->data_offset += explicit_iv_len;
2649 rec->data_len -= explicit_iv_len + transform->taglen;
2650
Hanno Beckercab87e62019-04-29 13:52:53 +01002651 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002653 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002654
2655 memcpy( transform->iv_dec + transform->fixed_ivlen,
2656 data - explicit_iv_len, explicit_iv_len );
2657
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002658 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002660 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002661
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002663 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002664 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002665 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2667 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002668 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669 data, rec->data_len,
2670 data, &olen,
2671 data + rec->data_len,
2672 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2677 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002678
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002679 return( ret );
2680 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002681 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2686 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002687 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2691#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002692 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002695 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002696
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 /*
Paul Bakker45829992013-01-03 14:52:21 +01002698 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002701 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2702 {
2703 /* The ciphertext is prefixed with the CBC IV. */
2704 minlen += transform->ivlen;
2705 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002706#endif
Paul Bakker45829992013-01-03 14:52:21 +01002707
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 /* Size considerations:
2709 *
2710 * - The CBC cipher text must not be empty and hence
2711 * at least of size transform->ivlen.
2712 *
2713 * Together with the potential IV-prefix, this explains
2714 * the first of the two checks below.
2715 *
2716 * - The record must contain a MAC, either in plain or
2717 * encrypted, depending on whether Encrypt-then-MAC
2718 * is used or not.
2719 * - If it is, the message contains the IV-prefix,
2720 * the CBC ciphertext, and the MAC.
2721 * - If it is not, the padded plaintext, and hence
2722 * the CBC ciphertext, has at least length maclen + 1
2723 * because there is at least the padding length byte.
2724 *
2725 * As the CBC ciphertext is not empty, both cases give the
2726 * lower bound minlen + maclen + 1 on the record size, which
2727 * we test for in the second check below.
2728 */
2729 if( rec->data_len < minlen + transform->ivlen ||
2730 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002733 "+ 1 ) ( + expl IV )", rec->data_len,
2734 transform->ivlen,
2735 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002737 }
2738
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002739 /*
2740 * Authenticate before decrypt if enabled
2741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002744 {
Hanno Becker992b6872017-11-09 18:57:39 +00002745 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002748
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2750 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002751
Hanno Beckercab87e62019-04-29 13:52:53 +01002752 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002753
Hanno Beckercab87e62019-04-29 13:52:53 +01002754 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2755 add_data_len );
2756 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2757 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002758 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2759 data, rec->data_len );
2760 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2761 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002762
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002763 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2764 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002765 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002766 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002767
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2769 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002774 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777
2778 /*
2779 * Check length sanity
2780 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002781 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786 }
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002789 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002790 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002791 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002792 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002793 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 /* This is safe because data_len >= minlen + maclen + 1 initially,
2795 * and at this point we have at most subtracted maclen (note that
2796 * minlen == transform->ivlen here). */
2797 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002798
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002799 data += transform->ivlen;
2800 rec->data_offset += transform->ivlen;
2801 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002802 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2806 transform->iv_dec, transform->ivlen,
2807 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002810 return( ret );
2811 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002812
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002817 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002820 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002821 {
2822 /*
2823 * Save IV in SSL3 and TLS1
2824 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2826 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 /* Safe since data_len >= minlen + maclen + 1, so after having
2831 * subtracted at most minlen and maclen up to this point,
2832 * data_len > 0. */
2833 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002834
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002835 if( auth_done == 1 )
2836 {
2837 correct *= ( rec->data_len >= padlen + 1 );
2838 padlen *= ( rec->data_len >= padlen + 1 );
2839 }
2840 else
Paul Bakker45829992013-01-03 14:52:21 +01002841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 if( rec->data_len < transform->maclen + padlen + 1 )
2844 {
2845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2846 rec->data_len,
2847 transform->maclen,
2848 padlen + 1 ) );
2849 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002850#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851
2852 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2853 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 padlen++;
2857
2858 /* Regardless of the validity of the padding,
2859 * we have data_len >= padlen here. */
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002862 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002863 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#if defined(MBEDTLS_SSL_DEBUG_ALL)
2867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002868 "should be no more than %d",
2869 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002870#endif
Paul Bakker45829992013-01-03 14:52:21 +01002871 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 }
2873 }
2874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2876#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2877 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002878 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 /* The padding check involves a series of up to 256
2881 * consecutive memory reads at the end of the record
2882 * plaintext buffer. In order to hide the length and
2883 * validity of the padding, always perform exactly
2884 * `min(256,plaintext_len)` reads (but take into account
2885 * only the last `padlen` bytes for the padding check). */
2886 size_t pad_count = 0;
2887 size_t real_count = 0;
2888 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002889
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 /* Index of first padding byte; it has been ensured above
2891 * that the subtraction is safe. */
2892 size_t const padding_idx = rec->data_len - padlen;
2893 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2894 size_t const start_idx = rec->data_len - num_checks;
2895 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002896
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002898 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 real_count |= ( idx >= padding_idx );
2900 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002901 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002905 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002907#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002908 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002909 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2912 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002916 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002917
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002918 /* If the padding was found to be invalid, padlen == 0
2919 * and the subtraction is safe. If the padding was found valid,
2920 * padlen hasn't been changed and the previous assertion
2921 * data_len >= padlen still holds. */
2922 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002924 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002926 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002930 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002932#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002934 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002935#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
2937 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002938 * Authenticate if not done yet.
2939 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 */
Hanno Becker52344c22018-01-03 15:24:20 +00002941#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002942 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002943 {
Hanno Becker992b6872017-11-09 18:57:39 +00002944 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002945
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002946 /* If the initial value of padlen was such that
2947 * data_len < maclen + padlen + 1, then padlen
2948 * got reset to 1, and the initial check
2949 * data_len >= minlen + maclen + 1
2950 * guarantees that at this point we still
2951 * have at least data_len >= maclen.
2952 *
2953 * If the initial value of padlen was such that
2954 * data_len >= maclen + padlen + 1, then we have
2955 * subtracted either padlen + 1 (if the padding was correct)
2956 * or 0 (if the padding was incorrect) since then,
2957 * hence data_len >= maclen in any case.
2958 */
2959 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Hanno Beckercab87e62019-04-29 13:52:53 +01002961 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002964 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002965 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 ssl_mac( &transform->md_ctx_dec,
2967 transform->mac_dec,
2968 data, rec->data_len,
2969 rec->ctr, rec->type,
2970 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002971 }
2972 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2974#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2975 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002976 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002977 {
2978 /*
2979 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002980 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002981 *
2982 * Known timing attacks:
2983 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2984 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002985 * To compensate for different timings for the MAC calculation
2986 * depending on how much padding was removed (which is determined
2987 * by padlen), process extra_run more blocks through the hash
2988 * function.
2989 *
2990 * The formula in the paper is
2991 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2992 * where L1 is the size of the header plus the decrypted message
2993 * plus CBC padding and L2 is the size of the header plus the
2994 * decrypted message. This is for an underlying hash function
2995 * with 64-byte blocks.
2996 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2997 * correctly. We round down instead of up, so -56 is the correct
2998 * value for our calculations instead of -55.
2999 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003000 * Repeat the formula rather than defining a block_size variable.
3001 * This avoids requiring division by a variable at runtime
3002 * (which would be marginally less efficient and would require
3003 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003004 */
3005 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003007
3008 /*
3009 * The next two sizes are the minimum and maximum values of
3010 * in_msglen over all padlen values.
3011 *
3012 * They're independent of padlen, since we previously did
3013 * in_msglen -= padlen.
3014 *
3015 * Note that max_len + maclen is never more than the buffer
3016 * length, as we previously did in_msglen -= maclen too.
3017 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003019 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3020
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003021 memset( tmp, 0, sizeof( tmp ) );
3022
3023 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003024 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003025#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3026 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003027 case MBEDTLS_MD_MD5:
3028 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003029 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003030 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003031 extra_run =
3032 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3033 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003034 break;
3035#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003036#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003038 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003039 extra_run =
3040 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3041 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 break;
3043#endif
3044 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3047 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003048
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003049 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003050
Hanno Beckercab87e62019-04-29 13:52:53 +01003051 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3052 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003053 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3054 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003055 /* Make sure we access everything even when padlen > 0. This
3056 * makes the synchronisation requirements for just-in-time
3057 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003058 ssl_read_memory( data + rec->data_len, padlen );
3059 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003060
3061 /* Call mbedtls_md_process at least once due to cache attacks
3062 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003063 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003064 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003065
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003066 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003067
3068 /* Make sure we access all the memory that could contain the MAC,
3069 * before we check it in the next code block. This makes the
3070 * synchronisation requirements for just-in-time Prime+Probe
3071 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 ssl_read_memory( data + min_len,
3073 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003074 }
3075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3077 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003083#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3085 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003088 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3089 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_SSL_DEBUG_ALL)
3092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003093#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003094 correct = 0;
3095 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003096 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003097 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003098
3099 /*
3100 * Finally check the correct flag
3101 */
3102 if( correct == 0 )
3103 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003104#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003105
3106 /* Make extra sure authentication was performed, exactly once */
3107 if( auth_done != 1 )
3108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003113#if defined(MBEDTLS_SSL_CID)
3114 if( rec->cid_len != 0 )
3115 {
3116 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3117 &rec->type );
3118 if( ret != 0 )
3119 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3120 }
3121#endif /* MBEDTLS_SSL_CID */
3122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
3125 return( 0 );
3126}
3127
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003128#undef MAC_NONE
3129#undef MAC_PLAINTEXT
3130#undef MAC_CIPHERTEXT
3131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003133/*
3134 * Compression/decompression functions
3135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003137{
3138 int ret;
3139 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003140 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003141 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003142 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003145
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003146 if( len_pre == 0 )
3147 return( 0 );
3148
Paul Bakker2770fbd2012-07-03 13:30:23 +00003149 memcpy( msg_pre, ssl->out_msg, len_pre );
3150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003152 ssl->out_msglen ) );
3153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155 ssl->out_msg, ssl->out_msglen );
3156
Paul Bakker48916f92012-09-16 19:57:18 +00003157 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3158 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3159 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003160 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003161
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163 if( ret != Z_OK )
3164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3166 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 }
3168
Angus Grattond8213d02016-05-25 20:56:48 +10003169 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003170 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173 ssl->out_msglen ) );
3174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003176 ssl->out_msg, ssl->out_msglen );
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179
3180 return( 0 );
3181}
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003184{
3185 int ret;
3186 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003187 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003188 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003189 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003192
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003193 if( len_pre == 0 )
3194 return( 0 );
3195
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196 memcpy( msg_pre, ssl->in_msg, len_pre );
3197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003199 ssl->in_msglen ) );
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 ssl->in_msg, ssl->in_msglen );
3203
Paul Bakker48916f92012-09-16 19:57:18 +00003204 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3205 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3206 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003207 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003208 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003209
Paul Bakker48916f92012-09-16 19:57:18 +00003210 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003211 if( ret != Z_OK )
3212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3214 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215 }
3216
Angus Grattond8213d02016-05-25 20:56:48 +10003217 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003218 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221 ssl->in_msglen ) );
3222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224 ssl->in_msg, ssl->in_msglen );
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227
3228 return( 0 );
3229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3233static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_SSL_PROTO_DTLS)
3236static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003237{
3238 /* If renegotiation is not enforced, retransmit until we would reach max
3239 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003240 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003241 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003242 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003243 unsigned char doublings = 1;
3244
3245 while( ratio != 0 )
3246 {
3247 ++doublings;
3248 ratio >>= 1;
3249 }
3250
3251 if( ++ssl->renego_records_seen > doublings )
3252 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003254 return( 0 );
3255 }
3256 }
3257
3258 return( ssl_write_hello_request( ssl ) );
3259}
3260#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003262
Paul Bakker5121ce52009-01-03 21:22:43 +00003263/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003264 * Fill the input message buffer by appending data to it.
3265 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003266 *
3267 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3268 * available (from this read and/or a previous one). Otherwise, an error code
3269 * is returned (possibly EOF or WANT_READ).
3270 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003271 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3272 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3273 * since we always read a whole datagram at once.
3274 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003275 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003276 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279{
Paul Bakker23986e52011-04-24 08:57:21 +00003280 int ret;
3281 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003285 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003288 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003290 }
3291
Angus Grattond8213d02016-05-25 20:56:48 +10003292 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003296 }
3297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003299 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003301 uint32_t timeout;
3302
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003303 /* Just to be sure */
3304 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3305 {
3306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3307 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3309 }
3310
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003311 /*
3312 * The point is, we need to always read a full datagram at once, so we
3313 * sometimes read more then requested, and handle the additional data.
3314 * It could be the rest of the current record (while fetching the
3315 * header) and/or some other records in the same datagram.
3316 */
3317
3318 /*
3319 * Move to the next record in the already read datagram if applicable
3320 */
3321 if( ssl->next_record_offset != 0 )
3322 {
3323 if( ssl->in_left < ssl->next_record_offset )
3324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003327 }
3328
3329 ssl->in_left -= ssl->next_record_offset;
3330
3331 if( ssl->in_left != 0 )
3332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003334 ssl->next_record_offset ) );
3335 memmove( ssl->in_hdr,
3336 ssl->in_hdr + ssl->next_record_offset,
3337 ssl->in_left );
3338 }
3339
3340 ssl->next_record_offset = 0;
3341 }
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003345
3346 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003347 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003348 */
3349 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003352 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003353 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003354
3355 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003356 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003357 * are not at the beginning of a new record, the caller did something
3358 * wrong.
3359 */
3360 if( ssl->in_left != 0 )
3361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003364 }
3365
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003366 /*
3367 * Don't even try to read if time's out already.
3368 * This avoids by-passing the timer when repeatedly receiving messages
3369 * that will end up being dropped.
3370 */
3371 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003372 {
3373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003374 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003375 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003376 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003377 {
Angus Grattond8213d02016-05-25 20:56:48 +10003378 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003381 timeout = ssl->handshake->retransmit_timeout;
3382 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003383 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003386
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003387 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003388 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3389 timeout );
3390 else
3391 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003394
3395 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397 }
3398
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003399 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003402 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003405 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003406 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003409 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003410 }
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003415 return( ret );
3416 }
3417
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003418 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003419 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003421 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003423 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003424 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003427 return( ret );
3428 }
3429
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003430 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003433 }
3434
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 if( ret < 0 )
3436 return( ret );
3437
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003438 ssl->in_left = ret;
3439 }
3440 else
3441#endif
3442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003444 ssl->in_left, nb_want ) );
3445
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003446 while( ssl->in_left < nb_want )
3447 {
3448 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003449
3450 if( ssl_check_timer( ssl ) != 0 )
3451 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3452 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003453 {
3454 if( ssl->f_recv_timeout != NULL )
3455 {
3456 ret = ssl->f_recv_timeout( ssl->p_bio,
3457 ssl->in_hdr + ssl->in_left, len,
3458 ssl->conf->read_timeout );
3459 }
3460 else
3461 {
3462 ret = ssl->f_recv( ssl->p_bio,
3463 ssl->in_hdr + ssl->in_left, len );
3464 }
3465 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003468 ssl->in_left, nb_want ) );
3469 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003470
3471 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003473
3474 if( ret < 0 )
3475 return( ret );
3476
mohammad160352aecb92018-03-28 23:41:40 -07003477 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003478 {
Darryl Green11999bb2018-03-13 15:22:58 +00003479 MBEDTLS_SSL_DEBUG_MSG( 1,
3480 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003481 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3483 }
3484
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003485 ssl->in_left += ret;
3486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 }
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
3491 return( 0 );
3492}
3493
3494/*
3495 * Flush any data not yet written
3496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003498{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003499 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003500 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003504 if( ssl->f_send == NULL )
3505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003507 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003509 }
3510
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003511 /* Avoid incrementing counter if data is flushed */
3512 if( ssl->out_left == 0 )
3513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003515 return( 0 );
3516 }
3517
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 while( ssl->out_left > 0 )
3519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3521 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
Hanno Becker2b1e3542018-08-06 11:19:13 +01003523 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003524 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
3528 if( ret <= 0 )
3529 return( ret );
3530
mohammad160352aecb92018-03-28 23:41:40 -07003531 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003532 {
Darryl Green11999bb2018-03-13 15:22:58 +00003533 MBEDTLS_SSL_DEBUG_MSG( 1,
3534 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003535 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3537 }
3538
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 ssl->out_left -= ret;
3540 }
3541
Hanno Becker2b1e3542018-08-06 11:19:13 +01003542#if defined(MBEDTLS_SSL_PROTO_DTLS)
3543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003544 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003545 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003546 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003547 else
3548#endif
3549 {
3550 ssl->out_hdr = ssl->out_buf + 8;
3551 }
3552 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
3556 return( 0 );
3557}
3558
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003559/*
3560 * Functions to handle the DTLS retransmission state machine
3561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003563/*
3564 * Append current handshake message to current outgoing flight
3565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3570 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3571 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003572
3573 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003574 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003578 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579 }
3580
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003581 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586 }
3587
3588 /* Copy current handshake message with headers */
3589 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3590 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003591 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 msg->next = NULL;
3593
3594 /* Append to the current flight */
3595 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003596 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597 else
3598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003600 while( cur->next != NULL )
3601 cur = cur->next;
3602 cur->next = msg;
3603 }
3604
Hanno Becker3b235902018-08-06 09:54:53 +01003605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606 return( 0 );
3607}
3608
3609/*
3610 * Free the current flight of handshake messages
3611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003613{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 mbedtls_ssl_flight_item *cur = flight;
3615 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003616
3617 while( cur != NULL )
3618 {
3619 next = cur->next;
3620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 mbedtls_free( cur->p );
3622 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623
3624 cur = next;
3625 }
3626}
3627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3629static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003630#endif
3631
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003632/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003633 * Swap transform_out and out_ctr with the alternative ones
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003638 unsigned char tmp_out_ctr[8];
3639
3640 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003643 return;
3644 }
3645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003647
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003648 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649 tmp_transform = ssl->transform_out;
3650 ssl->transform_out = ssl->handshake->alt_transform_out;
3651 ssl->handshake->alt_transform_out = tmp_transform;
3652
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003653 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003654 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3655 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003657
3658 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003659 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3662 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003668 }
3669 }
3670#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003671}
3672
3673/*
3674 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003675 */
3676int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3677{
3678 int ret = 0;
3679
3680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3681
3682 ret = mbedtls_ssl_flight_transmit( ssl );
3683
3684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3685
3686 return( ret );
3687}
3688
3689/*
3690 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003691 *
3692 * Need to remember the current message in case flush_output returns
3693 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003694 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003695 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003696int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003698 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003702 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003704
3705 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003706 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003707 ssl_swap_epochs( ssl );
3708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003710 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003711
3712 while( ssl->handshake->cur_msg != NULL )
3713 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003714 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003715 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003716
Hanno Beckere1dcb032018-08-17 16:47:58 +01003717 int const is_finished =
3718 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3719 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3720
Hanno Becker04da1892018-08-14 13:22:10 +01003721 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3722 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3723
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003724 /* Swap epochs before sending Finished: we can't do it after
3725 * sending ChangeCipherSpec, in case write returns WANT_READ.
3726 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003727 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003728 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003730 ssl_swap_epochs( ssl );
3731 }
3732
Hanno Becker67bc7c32018-08-06 11:33:50 +01003733 ret = ssl_get_remaining_payload_in_datagram( ssl );
3734 if( ret < 0 )
3735 return( ret );
3736 max_frag_len = (size_t) ret;
3737
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003738 /* CCS is copied as is, while HS messages may need fragmentation */
3739 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3740 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003741 if( max_frag_len == 0 )
3742 {
3743 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3744 return( ret );
3745
3746 continue;
3747 }
3748
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003749 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003750 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003751 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003752
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003753 /* Update position inside current message */
3754 ssl->handshake->cur_msg_p += cur->len;
3755 }
3756 else
3757 {
3758 const unsigned char * const p = ssl->handshake->cur_msg_p;
3759 const size_t hs_len = cur->len - 12;
3760 const size_t frag_off = p - ( cur->p + 12 );
3761 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003763
Hanno Beckere1dcb032018-08-17 16:47:58 +01003764 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003765 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003766 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003767 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003768
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3770 return( ret );
3771
3772 continue;
3773 }
3774 max_hs_frag_len = max_frag_len - 12;
3775
3776 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3777 max_hs_frag_len : rem_len;
3778
3779 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003780 {
3781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003782 (unsigned) cur_hs_frag_len,
3783 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003784 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003785
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003786 /* Messages are stored with handshake headers as if not fragmented,
3787 * copy beginning of headers then fill fragmentation fields.
3788 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3789 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003791 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3792 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3793 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3794
Hanno Becker67bc7c32018-08-06 11:33:50 +01003795 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3796 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3797 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003798
3799 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3800
Hanno Becker3f7b9732018-08-28 09:53:25 +01003801 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003802 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3803 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804 ssl->out_msgtype = cur->type;
3805
3806 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003807 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003808 }
3809
3810 /* If done with the current message move to the next one if any */
3811 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3812 {
3813 if( cur->next != NULL )
3814 {
3815 ssl->handshake->cur_msg = cur->next;
3816 ssl->handshake->cur_msg_p = cur->next->p + 12;
3817 }
3818 else
3819 {
3820 ssl->handshake->cur_msg = NULL;
3821 ssl->handshake->cur_msg_p = NULL;
3822 }
3823 }
3824
3825 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003826 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003829 return( ret );
3830 }
3831 }
3832
Hanno Becker67bc7c32018-08-06 11:33:50 +01003833 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3834 return( ret );
3835
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003836 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3838 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003839 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003842 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3843 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003844
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003846
3847 return( 0 );
3848}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003849
3850/*
3851 * To be called when the last message of an incoming flight is received.
3852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003854{
3855 /* We won't need to resend that one any more */
3856 ssl_flight_free( ssl->handshake->flight );
3857 ssl->handshake->flight = NULL;
3858 ssl->handshake->cur_msg = NULL;
3859
3860 /* The next incoming flight will start with this msg_seq */
3861 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3862
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003863 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003864 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003865
Hanno Becker0271f962018-08-16 13:23:47 +01003866 /* Clear future message buffering structure. */
3867 ssl_buffering_free( ssl );
3868
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003869 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003870 ssl_set_timer( ssl, 0 );
3871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3873 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003876 }
3877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003879}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003880
3881/*
3882 * To be called when the last message of an outgoing flight is send.
3883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003885{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003886 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003887 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3890 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003893 }
3894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003898
Paul Bakker5121ce52009-01-03 21:22:43 +00003899/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003900 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003902
3903/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003904 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003905 *
3906 * - fill in handshake headers
3907 * - update handshake checksum
3908 * - DTLS: save message for resending
3909 * - then pass to the record layer
3910 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003911 * DTLS: except for HelloRequest, messages are only queued, and will only be
3912 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003913 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003914 * Inputs:
3915 * - ssl->out_msglen: 4 + actual handshake message len
3916 * (4 is the size of handshake headers for TLS)
3917 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3918 * - ssl->out_msg + 4: the handshake message body
3919 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003920 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003921 * - ssl->out_msglen: the length of the record contents
3922 * (including handshake headers but excluding record headers)
3923 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003925int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003926{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003927 int ret;
3928 const size_t hs_len = ssl->out_msglen - 4;
3929 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3932
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003933 /*
3934 * Sanity checks
3935 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003936 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003937 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3938 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003939 /* In SSLv3, the client might send a NoCertificate alert. */
3940#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3941 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3942 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3943 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3944#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3945 {
3946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3948 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003950
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003951 /* Whenever we send anything different from a
3952 * HelloRequest we should be in a handshake - double check. */
3953 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3954 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003955 ssl->handshake == NULL )
3956 {
3957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3958 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003963 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3967 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003970
Hanno Beckerb50a2532018-08-06 11:52:54 +01003971 /* Double-check that we did not exceed the bounds
3972 * of the outgoing record buffer.
3973 * This should never fail as the various message
3974 * writing functions must obey the bounds of the
3975 * outgoing record buffer, but better be safe.
3976 *
3977 * Note: We deliberately do not check for the MTU or MFL here.
3978 */
3979 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3980 {
3981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3982 "size %u, maximum %u",
3983 (unsigned) ssl->out_msglen,
3984 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3985 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3986 }
3987
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003988 /*
3989 * Fill handshake headers
3990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003992 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003993 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3994 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3995 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003997 /*
3998 * DTLS has additional fields in the Handshake layer,
3999 * between the length field and the actual payload:
4000 * uint16 message_seq;
4001 * uint24 fragment_offset;
4002 * uint24 fragment_length;
4003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004005 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004006 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004007 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004008 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004009 {
4010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4011 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004012 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004013 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4015 }
4016
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004017 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004018 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004019
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004020 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004021 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004022 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004023 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4024 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4025 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004026 }
4027 else
4028 {
4029 ssl->out_msg[4] = 0;
4030 ssl->out_msg[5] = 0;
4031 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004032
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004033 /* Handshake hashes are computed without fragmentation,
4034 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004035 memset( ssl->out_msg + 6, 0x00, 3 );
4036 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004039
Hanno Becker0207e532018-08-28 10:28:28 +01004040 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004041 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4042 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 }
4044
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004045 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004047 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004048 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4049 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 {
4051 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004054 return( ret );
4055 }
4056 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004057 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004058#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004059 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004060 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004061 {
4062 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4063 return( ret );
4064 }
4065 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004066
4067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4068
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004069 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004070}
4071
4072/*
4073 * Record layer functions
4074 */
4075
4076/*
4077 * Write current record.
4078 *
4079 * Uses:
4080 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4081 * - ssl->out_msglen: length of the record content (excl headers)
4082 * - ssl->out_msg: record content
4083 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004084int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004085{
4086 int ret, done = 0;
4087 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004088 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004089
4090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004093 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004095 {
4096 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004099 return( ret );
4100 }
4101
4102 len = ssl->out_msglen;
4103 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4107 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 ret = mbedtls_ssl_hw_record_write( ssl );
4112 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4115 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004116 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004117
4118 if( ret == 0 )
4119 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004120 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004122 if( !done )
4123 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004124 unsigned i;
4125 size_t protected_record_size;
4126
Paul Bakker05ef8352012-05-08 09:17:57 +00004127 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004129 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004130
Hanno Becker19859472018-08-06 09:40:20 +01004131 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004132 ssl->out_len[0] = (unsigned char)( len >> 8 );
4133 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004134
Paul Bakker48916f92012-09-16 19:57:18 +00004135 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004136 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004137 mbedtls_record rec;
4138
4139 rec.buf = ssl->out_iv;
4140 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4141 ( ssl->out_iv - ssl->out_buf );
4142 rec.data_len = ssl->out_msglen;
4143 rec.data_offset = ssl->out_msg - rec.buf;
4144
4145 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4146 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4147 ssl->conf->transport, rec.ver );
4148 rec.type = ssl->out_msgtype;
4149
Hanno Becker43c24b82019-05-01 09:45:57 +01004150#if defined(MBEDTLS_SSL_CID)
4151 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004152 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004153#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004154
Hanno Beckera18d1322018-01-03 14:27:32 +00004155 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004156 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004159 return( ret );
4160 }
4161
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004162 if( rec.data_offset != 0 )
4163 {
4164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4166 }
4167
Hanno Becker78f839d2019-03-14 12:56:23 +00004168 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004169 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4170 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004171 }
4172
Hanno Becker2b1e3542018-08-06 11:19:13 +01004173 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4174
4175#if defined(MBEDTLS_SSL_PROTO_DTLS)
4176 /* In case of DTLS, double-check that we don't exceed
4177 * the remaining space in the datagram. */
4178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4179 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004180 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004181 if( ret < 0 )
4182 return( ret );
4183
4184 if( protected_record_size > (size_t) ret )
4185 {
4186 /* Should never happen */
4187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4188 }
4189 }
4190#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004193 "version = [%d:%d], msglen = %d",
4194 ssl->out_hdr[0], ssl->out_hdr[1],
4195 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004198 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004199
4200 ssl->out_left += protected_record_size;
4201 ssl->out_hdr += protected_record_size;
4202 ssl_update_out_pointers( ssl, ssl->transform_out );
4203
Hanno Becker04484622018-08-06 09:49:38 +01004204 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4205 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4206 break;
4207
4208 /* The loop goes to its end iff the counter is wrapping */
4209 if( i == ssl_ep_len( ssl ) )
4210 {
4211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4212 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004214 }
4215
Hanno Becker67bc7c32018-08-06 11:33:50 +01004216#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004217 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4218 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004219 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004220 size_t remaining;
4221 ret = ssl_get_remaining_payload_in_datagram( ssl );
4222 if( ret < 0 )
4223 {
4224 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4225 ret );
4226 return( ret );
4227 }
4228
4229 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004230 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004231 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004232 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004233 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004234 else
4235 {
Hanno Becker513815a2018-08-20 11:56:09 +01004236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004237 }
4238 }
4239#endif /* MBEDTLS_SSL_PROTO_DTLS */
4240
4241 if( ( flush == SSL_FORCE_FLUSH ) &&
4242 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004245 return( ret );
4246 }
4247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004249
4250 return( 0 );
4251}
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004254
4255static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4256{
4257 if( ssl->in_msglen < ssl->in_hslen ||
4258 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4259 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4260 {
4261 return( 1 );
4262 }
4263 return( 0 );
4264}
Hanno Becker44650b72018-08-16 12:51:11 +01004265
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004266static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004267{
4268 return( ( ssl->in_msg[9] << 16 ) |
4269 ( ssl->in_msg[10] << 8 ) |
4270 ssl->in_msg[11] );
4271}
4272
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004273static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004274{
4275 return( ( ssl->in_msg[6] << 16 ) |
4276 ( ssl->in_msg[7] << 8 ) |
4277 ssl->in_msg[8] );
4278}
4279
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004280static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004281{
4282 uint32_t msg_len, frag_off, frag_len;
4283
4284 msg_len = ssl_get_hs_total_len( ssl );
4285 frag_off = ssl_get_hs_frag_off( ssl );
4286 frag_len = ssl_get_hs_frag_len( ssl );
4287
4288 if( frag_off > msg_len )
4289 return( -1 );
4290
4291 if( frag_len > msg_len - frag_off )
4292 return( -1 );
4293
4294 if( frag_len + 12 > ssl->in_msglen )
4295 return( -1 );
4296
4297 return( 0 );
4298}
4299
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004300/*
4301 * Mark bits in bitmask (used for DTLS HS reassembly)
4302 */
4303static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4304{
4305 unsigned int start_bits, end_bits;
4306
4307 start_bits = 8 - ( offset % 8 );
4308 if( start_bits != 8 )
4309 {
4310 size_t first_byte_idx = offset / 8;
4311
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004312 /* Special case */
4313 if( len <= start_bits )
4314 {
4315 for( ; len != 0; len-- )
4316 mask[first_byte_idx] |= 1 << ( start_bits - len );
4317
4318 /* Avoid potential issues with offset or len becoming invalid */
4319 return;
4320 }
4321
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004322 offset += start_bits; /* Now offset % 8 == 0 */
4323 len -= start_bits;
4324
4325 for( ; start_bits != 0; start_bits-- )
4326 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4327 }
4328
4329 end_bits = len % 8;
4330 if( end_bits != 0 )
4331 {
4332 size_t last_byte_idx = ( offset + len ) / 8;
4333
4334 len -= end_bits; /* Now len % 8 == 0 */
4335
4336 for( ; end_bits != 0; end_bits-- )
4337 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4338 }
4339
4340 memset( mask + offset / 8, 0xFF, len / 8 );
4341}
4342
4343/*
4344 * Check that bitmask is full
4345 */
4346static int ssl_bitmask_check( unsigned char *mask, size_t len )
4347{
4348 size_t i;
4349
4350 for( i = 0; i < len / 8; i++ )
4351 if( mask[i] != 0xFF )
4352 return( -1 );
4353
4354 for( i = 0; i < len % 8; i++ )
4355 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4356 return( -1 );
4357
4358 return( 0 );
4359}
4360
Hanno Becker56e205e2018-08-16 09:06:12 +01004361/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004362static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004363 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004364{
Hanno Becker56e205e2018-08-16 09:06:12 +01004365 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004366
Hanno Becker56e205e2018-08-16 09:06:12 +01004367 alloc_len = 12; /* Handshake header */
4368 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004369
Hanno Beckerd07df862018-08-16 09:14:58 +01004370 if( add_bitmap )
4371 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004372
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004373 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004374}
Hanno Becker56e205e2018-08-16 09:06:12 +01004375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004377
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004378static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004379{
4380 return( ( ssl->in_msg[1] << 16 ) |
4381 ( ssl->in_msg[2] << 8 ) |
4382 ssl->in_msg[3] );
4383}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004384
Simon Butcher99000142016-10-13 17:21:01 +01004385int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004390 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004392 }
4393
Hanno Becker12555c62018-08-16 12:47:53 +01004394 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004397 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004398 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004401 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004402 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004403 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004404 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004405
Hanno Becker44650b72018-08-16 12:51:11 +01004406 if( ssl_check_hs_header( ssl ) != 0 )
4407 {
4408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4409 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4410 }
4411
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004412 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004413 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4414 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4415 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4416 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004417 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004418 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4419 {
4420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4421 recv_msg_seq,
4422 ssl->handshake->in_msg_seq ) );
4423 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4424 }
4425
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004426 /* Retransmit only on last message from previous flight, to avoid
4427 * too many retransmissions.
4428 * Besides, No sane server ever retransmits HelloVerifyRequest */
4429 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004433 "message_seq = %d, start_of_flight = %d",
4434 recv_msg_seq,
4435 ssl->handshake->in_flight_start_seq ) );
4436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004440 return( ret );
4441 }
4442 }
4443 else
4444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004446 "message_seq = %d, expected = %d",
4447 recv_msg_seq,
4448 ssl->handshake->in_msg_seq ) );
4449 }
4450
Hanno Becker90333da2017-10-10 11:27:13 +01004451 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004452 }
4453 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004454
Hanno Becker6d97ef52018-08-16 13:09:04 +01004455 /* Message reassembly is handled alongside buffering of future
4456 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004457 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004458 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004459 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004462 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004463 }
4464 }
4465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004467 /* With TLS we don't handle fragmentation (for now) */
4468 if( ssl->in_msglen < ssl->in_hslen )
4469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4471 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004472 }
4473
Simon Butcher99000142016-10-13 17:21:01 +01004474 return( 0 );
4475}
4476
4477void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4478{
Hanno Becker0271f962018-08-16 13:23:47 +01004479 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004480
Hanno Becker0271f962018-08-16 13:23:47 +01004481 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004482 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004483 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004484 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004485
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004486 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004489 ssl->handshake != NULL )
4490 {
Hanno Becker0271f962018-08-16 13:23:47 +01004491 unsigned offset;
4492 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004493
Hanno Becker0271f962018-08-16 13:23:47 +01004494 /* Increment handshake sequence number */
4495 hs->in_msg_seq++;
4496
4497 /*
4498 * Clear up handshake buffering and reassembly structure.
4499 */
4500
4501 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004502 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004503
4504 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004505 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4506 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004507 offset++, hs_buf++ )
4508 {
4509 *hs_buf = *(hs_buf + 1);
4510 }
4511
4512 /* Create a fresh last entry */
4513 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004514 }
4515#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004516}
4517
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004518/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004519 * DTLS anti-replay: RFC 6347 4.1.2.6
4520 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004521 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4522 * Bit n is set iff record number in_window_top - n has been seen.
4523 *
4524 * Usually, in_window_top is the last record number seen and the lsb of
4525 * in_window is set. The only exception is the initial state (record number 0
4526 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4529static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004530{
4531 ssl->in_window_top = 0;
4532 ssl->in_window = 0;
4533}
4534
4535static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4536{
4537 return( ( (uint64_t) buf[0] << 40 ) |
4538 ( (uint64_t) buf[1] << 32 ) |
4539 ( (uint64_t) buf[2] << 24 ) |
4540 ( (uint64_t) buf[3] << 16 ) |
4541 ( (uint64_t) buf[4] << 8 ) |
4542 ( (uint64_t) buf[5] ) );
4543}
4544
4545/*
4546 * Return 0 if sequence number is acceptable, -1 otherwise
4547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004549{
4550 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4551 uint64_t bit;
4552
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004553 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004554 return( 0 );
4555
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004556 if( rec_seqnum > ssl->in_window_top )
4557 return( 0 );
4558
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004559 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004560
4561 if( bit >= 64 )
4562 return( -1 );
4563
4564 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4565 return( -1 );
4566
4567 return( 0 );
4568}
4569
4570/*
4571 * Update replay window on new validated record
4572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004574{
4575 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4576
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004577 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004578 return;
4579
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580 if( rec_seqnum > ssl->in_window_top )
4581 {
4582 /* Update window_top and the contents of the window */
4583 uint64_t shift = rec_seqnum - ssl->in_window_top;
4584
4585 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004586 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004588 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004589 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004590 ssl->in_window |= 1;
4591 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004592
4593 ssl->in_window_top = rec_seqnum;
4594 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004595 else
4596 {
4597 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004598 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004599
4600 if( bit < 64 ) /* Always true, but be extra sure */
4601 ssl->in_window |= (uint64_t) 1 << bit;
4602 }
4603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004605
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004606#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004607/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004608static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4609
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004610/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004611 * Without any SSL context, check if a datagram looks like a ClientHello with
4612 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004613 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004614 *
4615 * - if cookie is valid, return 0
4616 * - if ClientHello looks superficially valid but cookie is not,
4617 * fill obuf and set olen, then
4618 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4619 * - otherwise return a specific error code
4620 */
4621static int ssl_check_dtls_clihlo_cookie(
4622 mbedtls_ssl_cookie_write_t *f_cookie_write,
4623 mbedtls_ssl_cookie_check_t *f_cookie_check,
4624 void *p_cookie,
4625 const unsigned char *cli_id, size_t cli_id_len,
4626 const unsigned char *in, size_t in_len,
4627 unsigned char *obuf, size_t buf_len, size_t *olen )
4628{
4629 size_t sid_len, cookie_len;
4630 unsigned char *p;
4631
4632 if( f_cookie_write == NULL || f_cookie_check == NULL )
4633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4634
4635 /*
4636 * Structure of ClientHello with record and handshake headers,
4637 * and expected values. We don't need to check a lot, more checks will be
4638 * done when actually parsing the ClientHello - skipping those checks
4639 * avoids code duplication and does not make cookie forging any easier.
4640 *
4641 * 0-0 ContentType type; copied, must be handshake
4642 * 1-2 ProtocolVersion version; copied
4643 * 3-4 uint16 epoch; copied, must be 0
4644 * 5-10 uint48 sequence_number; copied
4645 * 11-12 uint16 length; (ignored)
4646 *
4647 * 13-13 HandshakeType msg_type; (ignored)
4648 * 14-16 uint24 length; (ignored)
4649 * 17-18 uint16 message_seq; copied
4650 * 19-21 uint24 fragment_offset; copied, must be 0
4651 * 22-24 uint24 fragment_length; (ignored)
4652 *
4653 * 25-26 ProtocolVersion client_version; (ignored)
4654 * 27-58 Random random; (ignored)
4655 * 59-xx SessionID session_id; 1 byte len + sid_len content
4656 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4657 * ...
4658 *
4659 * Minimum length is 61 bytes.
4660 */
4661 if( in_len < 61 ||
4662 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4663 in[3] != 0 || in[4] != 0 ||
4664 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4665 {
4666 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4667 }
4668
4669 sid_len = in[59];
4670 if( sid_len > in_len - 61 )
4671 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4672
4673 cookie_len = in[60 + sid_len];
4674 if( cookie_len > in_len - 60 )
4675 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4676
4677 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4678 cli_id, cli_id_len ) == 0 )
4679 {
4680 /* Valid cookie */
4681 return( 0 );
4682 }
4683
4684 /*
4685 * If we get here, we've got an invalid cookie, let's prepare HVR.
4686 *
4687 * 0-0 ContentType type; copied
4688 * 1-2 ProtocolVersion version; copied
4689 * 3-4 uint16 epoch; copied
4690 * 5-10 uint48 sequence_number; copied
4691 * 11-12 uint16 length; olen - 13
4692 *
4693 * 13-13 HandshakeType msg_type; hello_verify_request
4694 * 14-16 uint24 length; olen - 25
4695 * 17-18 uint16 message_seq; copied
4696 * 19-21 uint24 fragment_offset; copied
4697 * 22-24 uint24 fragment_length; olen - 25
4698 *
4699 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4700 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4701 *
4702 * Minimum length is 28.
4703 */
4704 if( buf_len < 28 )
4705 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4706
4707 /* Copy most fields and adapt others */
4708 memcpy( obuf, in, 25 );
4709 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4710 obuf[25] = 0xfe;
4711 obuf[26] = 0xff;
4712
4713 /* Generate and write actual cookie */
4714 p = obuf + 28;
4715 if( f_cookie_write( p_cookie,
4716 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4717 {
4718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4719 }
4720
4721 *olen = p - obuf;
4722
4723 /* Go back and fill length fields */
4724 obuf[27] = (unsigned char)( *olen - 28 );
4725
4726 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4727 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4728 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4729
4730 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4731 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4732
4733 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4734}
4735
4736/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004737 * Handle possible client reconnect with the same UDP quadruplet
4738 * (RFC 6347 Section 4.2.8).
4739 *
4740 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4741 * that looks like a ClientHello.
4742 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004743 * - if the input looks like a ClientHello without cookies,
4744 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004745 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004746 * - if the input looks like a ClientHello with a valid cookie,
4747 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004748 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004749 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004750 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004751 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004752 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4753 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004754 */
4755static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4756{
4757 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004758 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004759
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004760 ret = ssl_check_dtls_clihlo_cookie(
4761 ssl->conf->f_cookie_write,
4762 ssl->conf->f_cookie_check,
4763 ssl->conf->p_cookie,
4764 ssl->cli_id, ssl->cli_id_len,
4765 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004766 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004767
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004768 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4769
4770 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004771 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004772 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 * If the error is permanent we'll catch it later,
4774 * if it's not, then hopefully it'll work next time. */
4775 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4776
4777 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004778 }
4779
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004781 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004782 /* Got a valid cookie, partially reset context */
4783 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4784 {
4785 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4786 return( ret );
4787 }
4788
4789 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790 }
4791
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004792 return( ret );
4793}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004794#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004795
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004796static int ssl_check_record_type( uint8_t record_type )
4797{
4798 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4799 record_type != MBEDTLS_SSL_MSG_ALERT &&
4800 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4801 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4802 {
4803 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4804 }
4805
4806 return( 0 );
4807}
4808
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004809/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004810 * ContentType type;
4811 * ProtocolVersion version;
4812 * uint16 epoch; // DTLS only
4813 * uint48 sequence_number; // DTLS only
4814 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004815 *
4816 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004817 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004818 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4819 *
4820 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004821 * 1. proceed with the record if this function returns 0
4822 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4823 * 3. return CLIENT_RECONNECT if this function return that value
4824 * 4. drop the whole datagram if this function returns anything else.
4825 * Point 2 is needed when the peer is resending, and we have already received
4826 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004829{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004830 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 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 +02004833
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004835 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004836 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004840 ssl->in_msgtype,
4841 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004843 /* Check record type */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004844 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004847
4848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004849 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4850 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004851 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4852#endif /* MBEDTLS_SSL_PROTO_DTLS */
4853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4854 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004857 }
4858
4859 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004860 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4863 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004864 }
4865
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004866 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004870 }
4871
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004872 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004873 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004874 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4877 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004878 }
4879
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004880 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004881 * DTLS-related tests.
4882 * Check epoch before checking length constraint because
4883 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4884 * message gets duplicated before the corresponding Finished message,
4885 * the second ChangeCipherSpec should be discarded because it belongs
4886 * to an old epoch, but not because its length is shorter than
4887 * the minimum record length for packets using the new record transform.
4888 * Note that these two kinds of failures are handled differently,
4889 * as an unexpected record is silently skipped but an invalid
4890 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004891 */
4892#if defined(MBEDTLS_SSL_PROTO_DTLS)
4893 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4894 {
4895 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4896
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004897 /* Check epoch (and sequence number) with DTLS */
4898 if( rec_epoch != ssl->in_epoch )
4899 {
4900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4901 "expected %d, received %d",
4902 ssl->in_epoch, rec_epoch ) );
4903
4904#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4905 /*
4906 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4907 * access the first byte of record content (handshake type), as we
4908 * have an active transform (possibly iv_len != 0), so use the
4909 * fact that the record header len is 13 instead.
4910 */
4911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4912 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4913 rec_epoch == 0 &&
4914 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4915 ssl->in_left > 13 &&
4916 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4917 {
4918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4919 "from the same port" ) );
4920 return( ssl_handle_possible_reconnect( ssl ) );
4921 }
4922 else
4923#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004924 {
4925 /* Consider buffering the record. */
4926 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4927 {
4928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4929 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4930 }
4931
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004932 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004933 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004934 }
4935
4936#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4937 /* Replay detection only works for the current epoch */
4938 if( rec_epoch == ssl->in_epoch &&
4939 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4940 {
4941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4942 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4943 }
4944#endif
4945 }
4946#endif /* MBEDTLS_SSL_PROTO_DTLS */
4947
Hanno Becker52c6dc62017-05-26 16:07:36 +01004948
4949 /* Check length against bounds of the current transform and version */
4950 if( ssl->transform_in == NULL )
4951 {
4952 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004953 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004954 {
4955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4956 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4957 }
4958 }
4959 else
4960 {
4961 if( ssl->in_msglen < ssl->transform_in->minlen )
4962 {
4963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4964 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4965 }
4966
4967#if defined(MBEDTLS_SSL_PROTO_SSL3)
4968 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004969 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004970 {
4971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4972 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4973 }
4974#endif
4975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4976 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4977 /*
4978 * TLS encrypted messages can have up to 256 bytes of padding
4979 */
4980 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4981 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004982 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004983 {
4984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4985 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4986 }
4987#endif
4988 }
4989
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004990 return( 0 );
4991}
Paul Bakker5121ce52009-01-03 21:22:43 +00004992
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004993/*
4994 * If applicable, decrypt (and decompress) record content
4995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004997{
4998 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
5001 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5004 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 ret = mbedtls_ssl_hw_record_read( ssl );
5009 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5012 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005013 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005014
5015 if( ret == 0 )
5016 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005019 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005021 mbedtls_record rec;
5022
5023 rec.buf = ssl->in_iv;
5024 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5025 - ( ssl->in_iv - ssl->in_buf );
5026 rec.data_len = ssl->in_msglen;
5027 rec.data_offset = 0;
5028
5029 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5030 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5031 ssl->conf->transport, rec.ver );
5032 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005033 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5034 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005037 return( ret );
5038 }
5039
Hanno Becker79594fd2019-05-08 09:38:41 +01005040 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005041 ssl->in_msglen = rec.data_len;
5042 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5043 ssl->in_len[1] = (unsigned char)( rec.data_len );
5044
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005045 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5046 ssl->in_msg, ssl->in_msglen );
5047
Angus Grattond8213d02016-05-25 20:56:48 +10005048 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5051 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005052 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005053 else if( ssl->in_msglen == 0 )
5054 {
5055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5056 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5057 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5058 {
5059 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5061 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5062 }
5063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5064
5065 ssl->nb_zero++;
5066
5067 /*
5068 * Three or more empty messages may be a DoS attack
5069 * (excessive CPU consumption).
5070 */
5071 if( ssl->nb_zero > 3 )
5072 {
5073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005074 "messages, possible DoS attack" ) );
5075 /* Treat the records as if they were not properly authenticated,
5076 * thereby failing the connection if we see more than allowed
5077 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005078 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5079 }
5080 }
5081 else
5082 ssl->nb_zero = 0;
5083
5084#if defined(MBEDTLS_SSL_PROTO_DTLS)
5085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5086 {
5087 ; /* in_ctr read from peer, not maintained internally */
5088 }
5089 else
5090#endif
5091 {
5092 unsigned i;
5093 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5094 if( ++ssl->in_ctr[i - 1] != 0 )
5095 break;
5096
5097 /* The loop goes to its end iff the counter is wrapping */
5098 if( i == ssl_ep_len( ssl ) )
5099 {
5100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5101 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5102 }
5103 }
5104
Paul Bakker5121ce52009-01-03 21:22:43 +00005105 }
5106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005108 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005110 {
5111 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005114 return( ret );
5115 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005116 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005120 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005123 }
5124#endif
5125
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005126 return( 0 );
5127}
5128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005130
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005131/*
5132 * Read a record.
5133 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005134 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5135 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5136 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005137 */
Hanno Becker1097b342018-08-15 14:09:41 +01005138
5139/* Helper functions for mbedtls_ssl_read_record(). */
5140static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005141static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5142static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005143
Hanno Becker327c93b2018-08-15 13:56:18 +01005144int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005145 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005146{
5147 int ret;
5148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005150
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005151 if( ssl->keep_current_message == 0 )
5152 {
5153 do {
Simon Butcher99000142016-10-13 17:21:01 +01005154
Hanno Becker26994592018-08-15 14:14:59 +01005155 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005156 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005157 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005158
Hanno Beckere74d5562018-08-15 14:26:08 +01005159 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005160 {
Hanno Becker40f50842018-08-15 14:48:01 +01005161#if defined(MBEDTLS_SSL_PROTO_DTLS)
5162 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005163
Hanno Becker40f50842018-08-15 14:48:01 +01005164 /* We only check for buffered messages if the
5165 * current datagram is fully consumed. */
5166 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005167 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005168 {
Hanno Becker40f50842018-08-15 14:48:01 +01005169 if( ssl_load_buffered_message( ssl ) == 0 )
5170 have_buffered = 1;
5171 }
5172
5173 if( have_buffered == 0 )
5174#endif /* MBEDTLS_SSL_PROTO_DTLS */
5175 {
5176 ret = ssl_get_next_record( ssl );
5177 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5178 continue;
5179
5180 if( ret != 0 )
5181 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005182 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005183 return( ret );
5184 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005185 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005186 }
5187
5188 ret = mbedtls_ssl_handle_message_type( ssl );
5189
Hanno Becker40f50842018-08-15 14:48:01 +01005190#if defined(MBEDTLS_SSL_PROTO_DTLS)
5191 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5192 {
5193 /* Buffer future message */
5194 ret = ssl_buffer_message( ssl );
5195 if( ret != 0 )
5196 return( ret );
5197
5198 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5199 }
5200#endif /* MBEDTLS_SSL_PROTO_DTLS */
5201
Hanno Becker90333da2017-10-10 11:27:13 +01005202 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5203 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005204
5205 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005206 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005207 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005208 return( ret );
5209 }
5210
Hanno Becker327c93b2018-08-15 13:56:18 +01005211 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005212 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005213 {
5214 mbedtls_ssl_update_handshake_status( ssl );
5215 }
Simon Butcher99000142016-10-13 17:21:01 +01005216 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005217 else
Simon Butcher99000142016-10-13 17:21:01 +01005218 {
Hanno Becker02f59072018-08-15 14:00:24 +01005219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005220 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005221 }
5222
5223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5224
5225 return( 0 );
5226}
5227
Hanno Becker40f50842018-08-15 14:48:01 +01005228#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005229static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005230{
Hanno Becker40f50842018-08-15 14:48:01 +01005231 if( ssl->in_left > ssl->next_record_offset )
5232 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005233
Hanno Becker40f50842018-08-15 14:48:01 +01005234 return( 0 );
5235}
5236
5237static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5238{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005239 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005240 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005241 int ret = 0;
5242
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005243 if( hs == NULL )
5244 return( -1 );
5245
Hanno Beckere00ae372018-08-20 09:39:42 +01005246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5247
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005248 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5249 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5250 {
5251 /* Check if we have seen a ChangeCipherSpec before.
5252 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005253 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5256 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005257 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005258 }
5259
Hanno Becker39b8bc92018-08-28 17:17:13 +01005260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005261 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5262 ssl->in_msglen = 1;
5263 ssl->in_msg[0] = 1;
5264
5265 /* As long as they are equal, the exact value doesn't matter. */
5266 ssl->in_left = 0;
5267 ssl->next_record_offset = 0;
5268
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005269 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005270 goto exit;
5271 }
Hanno Becker37f95322018-08-16 13:55:32 +01005272
Hanno Beckerb8f50142018-08-28 10:01:34 +01005273#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005274 /* Debug only */
5275 {
5276 unsigned offset;
5277 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5278 {
5279 hs_buf = &hs->buffering.hs[offset];
5280 if( hs_buf->is_valid == 1 )
5281 {
5282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5283 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005284 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005285 }
5286 }
5287 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005288#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005289
5290 /* Check if we have buffered and/or fully reassembled the
5291 * next handshake message. */
5292 hs_buf = &hs->buffering.hs[0];
5293 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5294 {
5295 /* Synthesize a record containing the buffered HS message. */
5296 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5297 ( hs_buf->data[2] << 8 ) |
5298 hs_buf->data[3];
5299
5300 /* Double-check that we haven't accidentally buffered
5301 * a message that doesn't fit into the input buffer. */
5302 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5303 {
5304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5306 }
5307
5308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5309 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5310 hs_buf->data, msg_len + 12 );
5311
5312 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5313 ssl->in_hslen = msg_len + 12;
5314 ssl->in_msglen = msg_len + 12;
5315 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5316
5317 ret = 0;
5318 goto exit;
5319 }
5320 else
5321 {
5322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5323 hs->in_msg_seq ) );
5324 }
5325
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005326 ret = -1;
5327
5328exit:
5329
5330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5331 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005332}
5333
Hanno Beckera02b0b42018-08-21 17:20:27 +01005334static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5335 size_t desired )
5336{
5337 int offset;
5338 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5340 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005341
Hanno Becker01315ea2018-08-21 17:22:17 +01005342 /* Get rid of future records epoch first, if such exist. */
5343 ssl_free_buffered_record( ssl );
5344
5345 /* Check if we have enough space available now. */
5346 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5347 hs->buffering.total_bytes_buffered ) )
5348 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005350 return( 0 );
5351 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005352
Hanno Becker4f432ad2018-08-28 10:02:32 +01005353 /* We don't have enough space to buffer the next expected handshake
5354 * message. Remove buffers used for future messages to gain space,
5355 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005356 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5357 offset >= 0; offset-- )
5358 {
5359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5360 offset ) );
5361
Hanno Beckerb309b922018-08-23 13:18:05 +01005362 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005363
5364 /* Check if we have enough space available now. */
5365 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5366 hs->buffering.total_bytes_buffered ) )
5367 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005369 return( 0 );
5370 }
5371 }
5372
5373 return( -1 );
5374}
5375
Hanno Becker40f50842018-08-15 14:48:01 +01005376static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5377{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005378 int ret = 0;
5379 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5380
5381 if( hs == NULL )
5382 return( 0 );
5383
5384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5385
5386 switch( ssl->in_msgtype )
5387 {
5388 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005390
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005391 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005392 break;
5393
5394 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005395 {
5396 unsigned recv_msg_seq_offset;
5397 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5398 mbedtls_ssl_hs_buffer *hs_buf;
5399 size_t msg_len = ssl->in_hslen - 12;
5400
5401 /* We should never receive an old handshake
5402 * message - double-check nonetheless. */
5403 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5404 {
5405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5406 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5407 }
5408
5409 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5410 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5411 {
5412 /* Silently ignore -- message too far in the future */
5413 MBEDTLS_SSL_DEBUG_MSG( 2,
5414 ( "Ignore future HS message with sequence number %u, "
5415 "buffering window %u - %u",
5416 recv_msg_seq, ssl->handshake->in_msg_seq,
5417 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5418
5419 goto exit;
5420 }
5421
5422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5423 recv_msg_seq, recv_msg_seq_offset ) );
5424
5425 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5426
5427 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005428 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005429 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005430 size_t reassembly_buf_sz;
5431
Hanno Becker37f95322018-08-16 13:55:32 +01005432 hs_buf->is_fragmented =
5433 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5434
5435 /* We copy the message back into the input buffer
5436 * after reassembly, so check that it's not too large.
5437 * This is an implementation-specific limitation
5438 * and not one from the standard, hence it is not
5439 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005440 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005441 {
5442 /* Ignore message */
5443 goto exit;
5444 }
5445
Hanno Beckere0b150f2018-08-21 15:51:03 +01005446 /* Check if we have enough space to buffer the message. */
5447 if( hs->buffering.total_bytes_buffered >
5448 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5452 }
5453
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005454 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5455 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005456
5457 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5458 hs->buffering.total_bytes_buffered ) )
5459 {
5460 if( recv_msg_seq_offset > 0 )
5461 {
5462 /* If we can't buffer a future message because
5463 * of space limitations -- ignore. */
5464 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",
5465 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5466 (unsigned) hs->buffering.total_bytes_buffered ) );
5467 goto exit;
5468 }
Hanno Beckere1801392018-08-21 16:51:05 +01005469 else
5470 {
5471 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",
5472 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5473 (unsigned) hs->buffering.total_bytes_buffered ) );
5474 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005475
Hanno Beckera02b0b42018-08-21 17:20:27 +01005476 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005477 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005478 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",
5479 (unsigned) msg_len,
5480 (unsigned) reassembly_buf_sz,
5481 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005482 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005483 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5484 goto exit;
5485 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005486 }
5487
5488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5489 msg_len ) );
5490
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005491 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5492 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005493 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005494 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005495 goto exit;
5496 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005497 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005498
5499 /* Prepare final header: copy msg_type, length and message_seq,
5500 * then add standardised fragment_offset and fragment_length */
5501 memcpy( hs_buf->data, ssl->in_msg, 6 );
5502 memset( hs_buf->data + 6, 0, 3 );
5503 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5504
5505 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005506
5507 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005508 }
5509 else
5510 {
5511 /* Make sure msg_type and length are consistent */
5512 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5513 {
5514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5515 /* Ignore */
5516 goto exit;
5517 }
5518 }
5519
Hanno Becker4422bbb2018-08-20 09:40:19 +01005520 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005521 {
5522 size_t frag_len, frag_off;
5523 unsigned char * const msg = hs_buf->data + 12;
5524
5525 /*
5526 * Check and copy current fragment
5527 */
5528
5529 /* Validation of header fields already done in
5530 * mbedtls_ssl_prepare_handshake_record(). */
5531 frag_off = ssl_get_hs_frag_off( ssl );
5532 frag_len = ssl_get_hs_frag_len( ssl );
5533
5534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5535 frag_off, frag_len ) );
5536 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5537
5538 if( hs_buf->is_fragmented )
5539 {
5540 unsigned char * const bitmask = msg + msg_len;
5541 ssl_bitmask_set( bitmask, frag_off, frag_len );
5542 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5543 msg_len ) == 0 );
5544 }
5545 else
5546 {
5547 hs_buf->is_complete = 1;
5548 }
5549
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5551 hs_buf->is_complete ? "" : "not yet " ) );
5552 }
5553
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005554 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005555 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005556
5557 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005558 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005559 break;
5560 }
5561
5562exit:
5563
5564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5565 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005566}
5567#endif /* MBEDTLS_SSL_PROTO_DTLS */
5568
Hanno Becker1097b342018-08-15 14:09:41 +01005569static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005570{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005571 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005572 * Consume last content-layer message and potentially
5573 * update in_msglen which keeps track of the contents'
5574 * consumption state.
5575 *
5576 * (1) Handshake messages:
5577 * Remove last handshake message, move content
5578 * and adapt in_msglen.
5579 *
5580 * (2) Alert messages:
5581 * Consume whole record content, in_msglen = 0.
5582 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005583 * (3) Change cipher spec:
5584 * Consume whole record content, in_msglen = 0.
5585 *
5586 * (4) Application data:
5587 * Don't do anything - the record layer provides
5588 * the application data as a stream transport
5589 * and consumes through mbedtls_ssl_read only.
5590 *
5591 */
5592
5593 /* Case (1): Handshake messages */
5594 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005595 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005596 /* Hard assertion to be sure that no application data
5597 * is in flight, as corrupting ssl->in_msglen during
5598 * ssl->in_offt != NULL is fatal. */
5599 if( ssl->in_offt != NULL )
5600 {
5601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5603 }
5604
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005605 /*
5606 * Get next Handshake message in the current record
5607 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005608
Hanno Becker4a810fb2017-05-24 16:27:30 +01005609 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005610 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005611 * current handshake content: If DTLS handshake
5612 * fragmentation is used, that's the fragment
5613 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005614 * size here is faulty and should be changed at
5615 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005616 * (2) While it doesn't seem to cause problems, one
5617 * has to be very careful not to assume that in_hslen
5618 * is always <= in_msglen in a sensible communication.
5619 * Again, it's wrong for DTLS handshake fragmentation.
5620 * The following check is therefore mandatory, and
5621 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005622 * Additionally, ssl->in_hslen might be arbitrarily out of
5623 * bounds after handling a DTLS message with an unexpected
5624 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005625 */
5626 if( ssl->in_hslen < ssl->in_msglen )
5627 {
5628 ssl->in_msglen -= ssl->in_hslen;
5629 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5630 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005631
Hanno Becker4a810fb2017-05-24 16:27:30 +01005632 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5633 ssl->in_msg, ssl->in_msglen );
5634 }
5635 else
5636 {
5637 ssl->in_msglen = 0;
5638 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005639
Hanno Becker4a810fb2017-05-24 16:27:30 +01005640 ssl->in_hslen = 0;
5641 }
5642 /* Case (4): Application data */
5643 else if( ssl->in_offt != NULL )
5644 {
5645 return( 0 );
5646 }
5647 /* Everything else (CCS & Alerts) */
5648 else
5649 {
5650 ssl->in_msglen = 0;
5651 }
5652
Hanno Becker1097b342018-08-15 14:09:41 +01005653 return( 0 );
5654}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005655
Hanno Beckere74d5562018-08-15 14:26:08 +01005656static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5657{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005658 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005659 return( 1 );
5660
5661 return( 0 );
5662}
5663
Hanno Becker5f066e72018-08-16 14:56:31 +01005664#if defined(MBEDTLS_SSL_PROTO_DTLS)
5665
5666static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5667{
5668 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5669 if( hs == NULL )
5670 return;
5671
Hanno Becker01315ea2018-08-21 17:22:17 +01005672 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005673 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005674 hs->buffering.total_bytes_buffered -=
5675 hs->buffering.future_record.len;
5676
5677 mbedtls_free( hs->buffering.future_record.data );
5678 hs->buffering.future_record.data = NULL;
5679 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005680}
5681
5682static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5683{
5684 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5685 unsigned char * rec;
5686 size_t rec_len;
5687 unsigned rec_epoch;
5688
5689 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5690 return( 0 );
5691
5692 if( hs == NULL )
5693 return( 0 );
5694
Hanno Becker5f066e72018-08-16 14:56:31 +01005695 rec = hs->buffering.future_record.data;
5696 rec_len = hs->buffering.future_record.len;
5697 rec_epoch = hs->buffering.future_record.epoch;
5698
5699 if( rec == NULL )
5700 return( 0 );
5701
Hanno Becker4cb782d2018-08-20 11:19:05 +01005702 /* Only consider loading future records if the
5703 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005704 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005705 return( 0 );
5706
Hanno Becker5f066e72018-08-16 14:56:31 +01005707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5708
5709 if( rec_epoch != ssl->in_epoch )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5712 goto exit;
5713 }
5714
5715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5716
5717 /* Double-check that the record is not too large */
5718 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5719 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5720 {
5721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5723 }
5724
5725 memcpy( ssl->in_hdr, rec, rec_len );
5726 ssl->in_left = rec_len;
5727 ssl->next_record_offset = 0;
5728
5729 ssl_free_buffered_record( ssl );
5730
5731exit:
5732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5733 return( 0 );
5734}
5735
5736static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5737{
5738 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5739 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005740 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005741
5742 /* Don't buffer future records outside handshakes. */
5743 if( hs == NULL )
5744 return( 0 );
5745
5746 /* Only buffer handshake records (we are only interested
5747 * in Finished messages). */
5748 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5749 return( 0 );
5750
5751 /* Don't buffer more than one future epoch record. */
5752 if( hs->buffering.future_record.data != NULL )
5753 return( 0 );
5754
Hanno Becker01315ea2018-08-21 17:22:17 +01005755 /* Don't buffer record if there's not enough buffering space remaining. */
5756 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5757 hs->buffering.total_bytes_buffered ) )
5758 {
5759 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",
5760 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5761 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005762 return( 0 );
5763 }
5764
Hanno Becker5f066e72018-08-16 14:56:31 +01005765 /* Buffer record */
5766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5767 ssl->in_epoch + 1 ) );
5768 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5769 rec_hdr_len + ssl->in_msglen );
5770
5771 /* ssl_parse_record_header() only considers records
5772 * of the next epoch as candidates for buffering. */
5773 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005774 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005775
5776 hs->buffering.future_record.data =
5777 mbedtls_calloc( 1, hs->buffering.future_record.len );
5778 if( hs->buffering.future_record.data == NULL )
5779 {
5780 /* If we run out of RAM trying to buffer a
5781 * record from the next epoch, just ignore. */
5782 return( 0 );
5783 }
5784
Hanno Becker01315ea2018-08-21 17:22:17 +01005785 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005786
Hanno Becker01315ea2018-08-21 17:22:17 +01005787 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005788 return( 0 );
5789}
5790
5791#endif /* MBEDTLS_SSL_PROTO_DTLS */
5792
Hanno Beckere74d5562018-08-15 14:26:08 +01005793static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005794{
5795 int ret;
5796
Hanno Becker5f066e72018-08-16 14:56:31 +01005797#if defined(MBEDTLS_SSL_PROTO_DTLS)
5798 /* We might have buffered a future record; if so,
5799 * and if the epoch matches now, load it.
5800 * On success, this call will set ssl->in_left to
5801 * the length of the buffered record, so that
5802 * the calls to ssl_fetch_input() below will
5803 * essentially be no-ops. */
5804 ret = ssl_load_buffered_record( ssl );
5805 if( ret != 0 )
5806 return( ret );
5807#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005812 return( ret );
5813 }
5814
5815 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005818 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5819 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005820 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005821 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5822 {
5823 ret = ssl_buffer_future_record( ssl );
5824 if( ret != 0 )
5825 return( ret );
5826
5827 /* Fall through to handling of unexpected records */
5828 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5829 }
5830
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005831 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5832 {
5833 /* Skip unexpected record (but not whole datagram) */
5834 ssl->next_record_offset = ssl->in_msglen
5835 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005836
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5838 "(header)" ) );
5839 }
5840 else
5841 {
5842 /* Skip invalid record and the rest of the datagram */
5843 ssl->next_record_offset = 0;
5844 ssl->in_left = 0;
5845
5846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5847 "(header)" ) );
5848 }
5849
5850 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005851 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005852 }
5853#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005854 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005855 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005856
5857 /*
5858 * Read and optionally decrypt the message contents
5859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5861 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005864 return( ret );
5865 }
5866
5867 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005872 if( ssl->next_record_offset < ssl->in_left )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5875 }
5876 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005877 else
5878#endif
5879 ssl->in_left = 0;
5880
5881 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005885 {
5886 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005887 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005888 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005889 /* Except when waiting for Finished as a bad mac here
5890 * probably means something went wrong in the handshake
5891 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5892 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5893 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5894 {
5895#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5896 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5897 {
5898 mbedtls_ssl_send_alert_message( ssl,
5899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5900 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5901 }
5902#endif
5903 return( ret );
5904 }
5905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005907 if( ssl->conf->badmac_limit != 0 &&
5908 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5911 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005912 }
5913#endif
5914
Hanno Becker4a810fb2017-05-24 16:27:30 +01005915 /* As above, invalid records cause
5916 * dismissal of the whole datagram. */
5917
5918 ssl->next_record_offset = 0;
5919 ssl->in_left = 0;
5920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005922 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005923 }
5924
5925 return( ret );
5926 }
5927 else
5928#endif
5929 {
5930 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5932 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 mbedtls_ssl_send_alert_message( ssl,
5935 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5936 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005937 }
5938#endif
5939 return( ret );
5940 }
5941 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005942
Simon Butcher99000142016-10-13 17:21:01 +01005943 return( 0 );
5944}
5945
5946int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5947{
5948 int ret;
5949
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005950 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005951 * Handle particular types of records
5952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005954 {
Simon Butcher99000142016-10-13 17:21:01 +01005955 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5956 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005957 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005959 }
5960
Hanno Beckere678eaa2018-08-21 14:57:46 +01005961 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005962 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005963 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005964 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5966 ssl->in_msglen ) );
5967 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005968 }
5969
Hanno Beckere678eaa2018-08-21 14:57:46 +01005970 if( ssl->in_msg[0] != 1 )
5971 {
5972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5973 ssl->in_msg[0] ) );
5974 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5975 }
5976
5977#if defined(MBEDTLS_SSL_PROTO_DTLS)
5978 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5979 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5980 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5981 {
5982 if( ssl->handshake == NULL )
5983 {
5984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5985 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5986 }
5987
5988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5989 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5990 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005991#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005992 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005995 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005996 if( ssl->in_msglen != 2 )
5997 {
5998 /* Note: Standard allows for more than one 2 byte alert
5999 to be packed in a single message, but Mbed TLS doesn't
6000 currently support this. */
6001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6002 ssl->in_msglen ) );
6003 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6004 }
6005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006007 ssl->in_msg[0], ssl->in_msg[1] ) );
6008
6009 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006010 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006015 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 }
6018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6020 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6023 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006025
6026#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6027 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6028 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6029 {
Hanno Becker90333da2017-10-10 11:27:13 +01006030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006031 /* Will be handled when trying to parse ServerHello */
6032 return( 0 );
6033 }
6034#endif
6035
6036#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6037 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6038 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6039 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6040 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6041 {
6042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6043 /* Will be handled in mbedtls_ssl_parse_certificate() */
6044 return( 0 );
6045 }
6046#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6047
6048 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006049 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 }
6051
Hanno Beckerc76c6192017-06-06 10:03:17 +01006052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006054 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006055 /* Drop unexpected ApplicationData records,
6056 * except at the beginning of renegotiations */
6057 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6058 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6059#if defined(MBEDTLS_SSL_RENEGOTIATION)
6060 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6061 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006062#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006063 )
6064 {
6065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6066 return( MBEDTLS_ERR_SSL_NON_FATAL );
6067 }
6068
6069 if( ssl->handshake != NULL &&
6070 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6071 {
6072 ssl_handshake_wrapup_free_hs_transform( ssl );
6073 }
6074 }
6075#endif /* MBEDTLS_SSL_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006076
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 return( 0 );
6078}
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006081{
6082 int ret;
6083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6085 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6086 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006087 {
6088 return( ret );
6089 }
6090
6091 return( 0 );
6092}
6093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006095 unsigned char level,
6096 unsigned char message )
6097{
6098 int ret;
6099
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006100 if( ssl == NULL || ssl->conf == NULL )
6101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006107 ssl->out_msglen = 2;
6108 ssl->out_msg[0] = level;
6109 ssl->out_msg[1] = message;
6110
Hanno Becker67bc7c32018-08-06 11:33:50 +01006111 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006114 return( ret );
6115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006117
6118 return( 0 );
6119}
6120
Hanno Beckerb9d44792019-02-08 07:19:04 +00006121#if defined(MBEDTLS_X509_CRT_PARSE_C)
6122static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6123{
6124#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6125 if( session->peer_cert != NULL )
6126 {
6127 mbedtls_x509_crt_free( session->peer_cert );
6128 mbedtls_free( session->peer_cert );
6129 session->peer_cert = NULL;
6130 }
6131#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6132 if( session->peer_cert_digest != NULL )
6133 {
6134 /* Zeroization is not necessary. */
6135 mbedtls_free( session->peer_cert_digest );
6136 session->peer_cert_digest = NULL;
6137 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6138 session->peer_cert_digest_len = 0;
6139 }
6140#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6141}
6142#endif /* MBEDTLS_X509_CRT_PARSE_C */
6143
Paul Bakker5121ce52009-01-03 21:22:43 +00006144/*
6145 * Handshake functions
6146 */
Hanno Becker21489932019-02-05 13:20:55 +00006147#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006148/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006150{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006151 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6152 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006155
Hanno Becker7177a882019-02-05 13:36:46 +00006156 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006159 ssl->state++;
6160 return( 0 );
6161 }
6162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006165}
6166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006168{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006169 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6170 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006173
Hanno Becker7177a882019-02-05 13:36:46 +00006174 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006177 ssl->state++;
6178 return( 0 );
6179 }
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006183}
Gilles Peskinef9828522017-05-03 12:28:43 +02006184
Hanno Becker21489932019-02-05 13:20:55 +00006185#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006186/* Some certificate support -> implement write and parse */
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006191 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006193 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6194 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006197
Hanno Becker7177a882019-02-05 13:36:46 +00006198 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006201 ssl->state++;
6202 return( 0 );
6203 }
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006206 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006207 {
6208 if( ssl->client_auth == 0 )
6209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006211 ssl->state++;
6212 return( 0 );
6213 }
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006216 /*
6217 * If using SSLv3 and got no cert, send an Alert message
6218 * (otherwise an empty Certificate message will be sent).
6219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6221 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 {
6223 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6225 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6226 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 goto write_msg;
6230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233#endif /* MBEDTLS_SSL_CLI_C */
6234#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006235 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6240 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 }
6242 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006243#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
6247 /*
6248 * 0 . 0 handshake type
6249 * 1 . 3 handshake length
6250 * 4 . 6 length of all certs
6251 * 7 . 9 length of cert. 1
6252 * 10 . n-1 peer certificate
6253 * n . n+2 length of cert. 2
6254 * n+3 . ... upper level cert, etc.
6255 */
6256 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Paul Bakker29087132010-03-21 21:03:34 +00006259 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 {
6261 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006262 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006265 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006267 }
6268
6269 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6270 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6271 ssl->out_msg[i + 2] = (unsigned char)( n );
6272
6273 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6274 i += n; crt = crt->next;
6275 }
6276
6277 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6278 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6279 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6280
6281 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6283 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006284
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006285#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006286write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006287#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006288
6289 ssl->state++;
6290
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006291 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006292 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 return( ret );
6295 }
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006298
Paul Bakkered27a042013-04-18 22:46:23 +02006299 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006300}
6301
Hanno Becker84879e32019-01-31 07:44:03 +00006302#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006303
6304#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006305static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6306 unsigned char *crt_buf,
6307 size_t crt_buf_len )
6308{
6309 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6310
6311 if( peer_crt == NULL )
6312 return( -1 );
6313
6314 if( peer_crt->raw.len != crt_buf_len )
6315 return( -1 );
6316
Hanno Becker46f34d02019-02-08 14:00:04 +00006317 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006318}
Hanno Becker177475a2019-02-05 17:02:46 +00006319#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6320static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6321 unsigned char *crt_buf,
6322 size_t crt_buf_len )
6323{
6324 int ret;
6325 unsigned char const * const peer_cert_digest =
6326 ssl->session->peer_cert_digest;
6327 mbedtls_md_type_t const peer_cert_digest_type =
6328 ssl->session->peer_cert_digest_type;
6329 mbedtls_md_info_t const * const digest_info =
6330 mbedtls_md_info_from_type( peer_cert_digest_type );
6331 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6332 size_t digest_len;
6333
6334 if( peer_cert_digest == NULL || digest_info == NULL )
6335 return( -1 );
6336
6337 digest_len = mbedtls_md_get_size( digest_info );
6338 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6339 return( -1 );
6340
6341 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6342 if( ret != 0 )
6343 return( -1 );
6344
6345 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6346}
6347#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006348#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006349
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006350/*
6351 * Once the certificate message is read, parse it into a cert chain and
6352 * perform basic checks, but leave actual verification to the caller
6353 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006354static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6355 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006356{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006357 int ret;
6358#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6359 int crt_cnt=0;
6360#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006361 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006362 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006367 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6368 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370 }
6371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6373 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6377 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 }
6380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006382
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006386 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006387
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006388 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006392 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6393 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006395 }
6396
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006397 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6398 i += 3;
6399
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006400 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 while( i < ssl->in_hslen )
6402 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006403 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006404 if ( i + 3 > ssl->in_hslen ) {
6405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006406 mbedtls_ssl_send_alert_message( ssl,
6407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6408 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006409 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6410 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006411 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6412 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 if( ssl->in_msg[i] != 0 )
6414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006416 mbedtls_ssl_send_alert_message( ssl,
6417 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6418 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006420 }
6421
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006422 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6424 | (unsigned int) ssl->in_msg[i + 2];
6425 i += 3;
6426
6427 if( n < 128 || i + n > ssl->in_hslen )
6428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006430 mbedtls_ssl_send_alert_message( ssl,
6431 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6432 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 }
6435
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006436 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006437#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6438 if( crt_cnt++ == 0 &&
6439 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6440 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006441 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006442 /* During client-side renegotiation, check that the server's
6443 * end-CRTs hasn't changed compared to the initial handshake,
6444 * mitigating the triple handshake attack. On success, reuse
6445 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6447 if( ssl_check_peer_crt_unchanged( ssl,
6448 &ssl->in_msg[i],
6449 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006450 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6452 mbedtls_ssl_send_alert_message( ssl,
6453 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6454 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6455 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006456 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006457
6458 /* Now we can safely free the original chain. */
6459 ssl_clear_peer_cert( ssl->session );
6460 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006461#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6462
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006463 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006464#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006465 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006466#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006467 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006468 * it in-place from the input buffer instead of making a copy. */
6469 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6470#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006471 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006472 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006473 case 0: /*ok*/
6474 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6475 /* Ignore certificate with an unknown algorithm: maybe a
6476 prior certificate was already trusted. */
6477 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006478
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006479 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6480 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6481 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006482
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006483 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6484 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6485 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006486
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006487 default:
6488 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6489 crt_parse_der_failed:
6490 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6491 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6492 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 }
6494
6495 i += n;
6496 }
6497
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006498 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006499 return( 0 );
6500}
6501
Hanno Becker4a55f632019-02-05 12:49:06 +00006502#if defined(MBEDTLS_SSL_SRV_C)
6503static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6504{
6505 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6506 return( -1 );
6507
6508#if defined(MBEDTLS_SSL_PROTO_SSL3)
6509 /*
6510 * Check if the client sent an empty certificate
6511 */
6512 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6513 {
6514 if( ssl->in_msglen == 2 &&
6515 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6516 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6517 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6518 {
6519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6520 return( 0 );
6521 }
6522
6523 return( -1 );
6524 }
6525#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6526
6527#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6528 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6529 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6530 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6531 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6532 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6533 {
6534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6535 return( 0 );
6536 }
6537
6538 return( -1 );
6539#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6540 MBEDTLS_SSL_PROTO_TLS1_2 */
6541}
6542#endif /* MBEDTLS_SSL_SRV_C */
6543
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006544/* Check if a certificate message is expected.
6545 * Return either
6546 * - SSL_CERTIFICATE_EXPECTED, or
6547 * - SSL_CERTIFICATE_SKIP
6548 * indicating whether a Certificate message is expected or not.
6549 */
6550#define SSL_CERTIFICATE_EXPECTED 0
6551#define SSL_CERTIFICATE_SKIP 1
6552static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6553 int authmode )
6554{
6555 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006556 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006557
6558 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6559 return( SSL_CERTIFICATE_SKIP );
6560
6561#if defined(MBEDTLS_SSL_SRV_C)
6562 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6563 {
6564 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6565 return( SSL_CERTIFICATE_SKIP );
6566
6567 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6568 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006569 ssl->session_negotiate->verify_result =
6570 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6571 return( SSL_CERTIFICATE_SKIP );
6572 }
6573 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006574#else
6575 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006576#endif /* MBEDTLS_SSL_SRV_C */
6577
6578 return( SSL_CERTIFICATE_EXPECTED );
6579}
6580
Hanno Becker68636192019-02-05 14:36:34 +00006581static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6582 int authmode,
6583 mbedtls_x509_crt *chain,
6584 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006585{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006586 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006587 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006588 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006589 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006590
Hanno Becker8927c832019-04-03 12:52:50 +01006591 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6592 void *p_vrfy;
6593
Hanno Becker68636192019-02-05 14:36:34 +00006594 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6595 return( 0 );
6596
Hanno Becker8927c832019-04-03 12:52:50 +01006597 if( ssl->f_vrfy != NULL )
6598 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006600 f_vrfy = ssl->f_vrfy;
6601 p_vrfy = ssl->p_vrfy;
6602 }
6603 else
6604 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006606 f_vrfy = ssl->conf->f_vrfy;
6607 p_vrfy = ssl->conf->p_vrfy;
6608 }
6609
Hanno Becker68636192019-02-05 14:36:34 +00006610 /*
6611 * Main check: verify certificate
6612 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006613#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6614 if( ssl->conf->f_ca_cb != NULL )
6615 {
6616 ((void) rs_ctx);
6617 have_ca_chain = 1;
6618
6619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006620 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006621 chain,
6622 ssl->conf->f_ca_cb,
6623 ssl->conf->p_ca_cb,
6624 ssl->conf->cert_profile,
6625 ssl->hostname,
6626 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006627 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006628 }
6629 else
6630#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6631 {
6632 mbedtls_x509_crt *ca_chain;
6633 mbedtls_x509_crl *ca_crl;
6634
6635#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6636 if( ssl->handshake->sni_ca_chain != NULL )
6637 {
6638 ca_chain = ssl->handshake->sni_ca_chain;
6639 ca_crl = ssl->handshake->sni_ca_crl;
6640 }
6641 else
6642#endif
6643 {
6644 ca_chain = ssl->conf->ca_chain;
6645 ca_crl = ssl->conf->ca_crl;
6646 }
6647
6648 if( ca_chain != NULL )
6649 have_ca_chain = 1;
6650
6651 ret = mbedtls_x509_crt_verify_restartable(
6652 chain,
6653 ca_chain, ca_crl,
6654 ssl->conf->cert_profile,
6655 ssl->hostname,
6656 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006657 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006658 }
Hanno Becker68636192019-02-05 14:36:34 +00006659
6660 if( ret != 0 )
6661 {
6662 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6663 }
6664
6665#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6666 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6667 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6668#endif
6669
6670 /*
6671 * Secondary checks: always done, but change 'ret' only if it was 0
6672 */
6673
6674#if defined(MBEDTLS_ECP_C)
6675 {
6676 const mbedtls_pk_context *pk = &chain->pk;
6677
6678 /* If certificate uses an EC key, make sure the curve is OK */
6679 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6680 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6681 {
6682 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6683
6684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6685 if( ret == 0 )
6686 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6687 }
6688 }
6689#endif /* MBEDTLS_ECP_C */
6690
6691 if( mbedtls_ssl_check_cert_usage( chain,
6692 ciphersuite_info,
6693 ! ssl->conf->endpoint,
6694 &ssl->session_negotiate->verify_result ) != 0 )
6695 {
6696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6697 if( ret == 0 )
6698 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6699 }
6700
6701 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6702 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6703 * with details encoded in the verification flags. All other kinds
6704 * of error codes, including those from the user provided f_vrfy
6705 * functions, are treated as fatal and lead to a failure of
6706 * ssl_parse_certificate even if verification was optional. */
6707 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6708 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6709 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6710 {
6711 ret = 0;
6712 }
6713
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006714 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006715 {
6716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6717 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6718 }
6719
6720 if( ret != 0 )
6721 {
6722 uint8_t alert;
6723
6724 /* The certificate may have been rejected for several reasons.
6725 Pick one and send the corresponding alert. Which alert to send
6726 may be a subject of debate in some cases. */
6727 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6728 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6729 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6730 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6731 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6732 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6733 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6734 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6735 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6736 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6737 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6738 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6739 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6740 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6741 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6742 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6743 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6744 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6745 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6746 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6747 else
6748 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6749 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6750 alert );
6751 }
6752
6753#if defined(MBEDTLS_DEBUG_C)
6754 if( ssl->session_negotiate->verify_result != 0 )
6755 {
6756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6757 ssl->session_negotiate->verify_result ) );
6758 }
6759 else
6760 {
6761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6762 }
6763#endif /* MBEDTLS_DEBUG_C */
6764
6765 return( ret );
6766}
6767
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006768#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6769static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6770 unsigned char *start, size_t len )
6771{
6772 int ret;
6773 /* Remember digest of the peer's end-CRT. */
6774 ssl->session_negotiate->peer_cert_digest =
6775 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6776 if( ssl->session_negotiate->peer_cert_digest == NULL )
6777 {
6778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6779 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6780 mbedtls_ssl_send_alert_message( ssl,
6781 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6782 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6783
6784 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6785 }
6786
6787 ret = mbedtls_md( mbedtls_md_info_from_type(
6788 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6789 start, len,
6790 ssl->session_negotiate->peer_cert_digest );
6791
6792 ssl->session_negotiate->peer_cert_digest_type =
6793 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6794 ssl->session_negotiate->peer_cert_digest_len =
6795 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6796
6797 return( ret );
6798}
6799
6800static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6801 unsigned char *start, size_t len )
6802{
6803 unsigned char *end = start + len;
6804 int ret;
6805
6806 /* Make a copy of the peer's raw public key. */
6807 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6808 ret = mbedtls_pk_parse_subpubkey( &start, end,
6809 &ssl->handshake->peer_pubkey );
6810 if( ret != 0 )
6811 {
6812 /* We should have parsed the public key before. */
6813 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6814 }
6815
6816 return( 0 );
6817}
6818#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6819
Hanno Becker68636192019-02-05 14:36:34 +00006820int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6821{
6822 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006823 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006824#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6825 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6826 ? ssl->handshake->sni_authmode
6827 : ssl->conf->authmode;
6828#else
6829 const int authmode = ssl->conf->authmode;
6830#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006831 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006832 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006833
6834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6835
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006836 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6837 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006838 {
6839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006840 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006841 }
6842
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006843#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6844 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006845 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006846 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006847 chain = ssl->handshake->ecrs_peer_cert;
6848 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006849 goto crt_verify;
6850 }
6851#endif
6852
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006853 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006854 {
6855 /* mbedtls_ssl_read_record may have sent an alert already. We
6856 let it decide whether to alert. */
6857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006858 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006859 }
6860
Hanno Becker4a55f632019-02-05 12:49:06 +00006861#if defined(MBEDTLS_SSL_SRV_C)
6862 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6863 {
6864 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006865
6866 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006867 ret = 0;
6868 else
6869 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006870
Hanno Becker6bdfab22019-02-05 13:11:17 +00006871 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006872 }
6873#endif /* MBEDTLS_SSL_SRV_C */
6874
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006875 /* Clear existing peer CRT structure in case we tried to
6876 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006877 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006878
6879 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6880 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006881 {
6882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6883 sizeof( mbedtls_x509_crt ) ) );
6884 mbedtls_ssl_send_alert_message( ssl,
6885 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6886 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006887
Hanno Becker3dad3112019-02-05 17:19:52 +00006888 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6889 goto exit;
6890 }
6891 mbedtls_x509_crt_init( chain );
6892
6893 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006894 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006895 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006896
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006897#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6898 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006899 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006900
6901crt_verify:
6902 if( ssl->handshake->ecrs_enabled)
6903 rs_ctx = &ssl->handshake->ecrs_ctx;
6904#endif
6905
Hanno Becker68636192019-02-05 14:36:34 +00006906 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006907 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006908 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006909 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006910
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006911#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006912 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006913 unsigned char *crt_start, *pk_start;
6914 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006915
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006916 /* We parse the CRT chain without copying, so
6917 * these pointers point into the input buffer,
6918 * and are hence still valid after freeing the
6919 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006920
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006921 crt_start = chain->raw.p;
6922 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006923
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006924 pk_start = chain->pk_raw.p;
6925 pk_len = chain->pk_raw.len;
6926
6927 /* Free the CRT structures before computing
6928 * digest and copying the peer's public key. */
6929 mbedtls_x509_crt_free( chain );
6930 mbedtls_free( chain );
6931 chain = NULL;
6932
6933 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006934 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006935 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006936
6937 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6938 if( ret != 0 )
6939 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006940 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006941#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6942 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006943 ssl->session_negotiate->peer_cert = chain;
6944 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006945#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006948
Hanno Becker6bdfab22019-02-05 13:11:17 +00006949exit:
6950
Hanno Becker3dad3112019-02-05 17:19:52 +00006951 if( ret == 0 )
6952 ssl->state++;
6953
6954#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6955 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6956 {
6957 ssl->handshake->ecrs_peer_cert = chain;
6958 chain = NULL;
6959 }
6960#endif
6961
6962 if( chain != NULL )
6963 {
6964 mbedtls_x509_crt_free( chain );
6965 mbedtls_free( chain );
6966 }
6967
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 return( ret );
6969}
Hanno Becker21489932019-02-05 13:20:55 +00006970#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006973{
6974 int ret;
6975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 ssl->out_msglen = 1;
6980 ssl->out_msg[0] = 1;
6981
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 ssl->state++;
6983
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006984 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006987 return( ret );
6988 }
6989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991
6992 return( 0 );
6993}
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006996{
6997 int ret;
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007000
Hanno Becker327c93b2018-08-15 13:56:18 +01007001 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007004 return( ret );
7005 }
7006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007010 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7011 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013 }
7014
Hanno Beckere678eaa2018-08-21 14:57:46 +01007015 /* CCS records are only accepted if they have length 1 and content '1',
7016 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007017
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007018 /*
7019 * Switch to our negotiated transform and session parameters for inbound
7020 * data.
7021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007023 ssl->transform_in = ssl->transform_negotiate;
7024 ssl->session_in = ssl->session_negotiate;
7025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007027 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007030 ssl_dtls_replay_reset( ssl );
7031#endif
7032
7033 /* Increment epoch */
7034 if( ++ssl->in_epoch == 0 )
7035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007037 /* This is highly unlikely to happen for legitimate reasons, so
7038 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007040 }
7041 }
7042 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007044 memset( ssl->in_ctr, 0, 8 );
7045
Hanno Becker79594fd2019-05-08 09:38:41 +01007046 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7049 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7055 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007057 }
7058 }
7059#endif
7060
Paul Bakker5121ce52009-01-03 21:22:43 +00007061 ssl->state++;
7062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
7065 return( 0 );
7066}
7067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7069 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007070{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007071 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7074 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7075 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007076 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007077 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7080#if defined(MBEDTLS_SHA512_C)
7081 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007082 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7083 else
7084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085#if defined(MBEDTLS_SHA256_C)
7086 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007087 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007088 else
7089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007093 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007094 }
Paul Bakker380da532012-04-18 16:10:25 +00007095}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7100 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007101 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7102 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7105#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007106#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007107 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007108 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7109#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007110 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007111#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007114#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007115 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007116 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007117#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007118 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007119#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007122}
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007125 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7128 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007129 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7130 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7133#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007134#if defined(MBEDTLS_USE_PSA_CRYPTO)
7135 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7136#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007137 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007138#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007142 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007143#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007144 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007145#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007148}
7149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7151 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7152static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007153 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007154{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007155 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7156 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007157}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007158#endif
Paul Bakker380da532012-04-18 16:10:25 +00007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7161#if defined(MBEDTLS_SHA256_C)
7162static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007163 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007164{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007165#if defined(MBEDTLS_USE_PSA_CRYPTO)
7166 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7167#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007168 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007169#endif
Paul Bakker380da532012-04-18 16:10:25 +00007170}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007171#endif
Paul Bakker380da532012-04-18 16:10:25 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_SHA512_C)
7174static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007175 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007176{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007177#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007178 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007179#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007180 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007181#endif
Paul Bakker380da532012-04-18 16:10:25 +00007182}
Paul Bakker769075d2012-11-24 11:26:46 +01007183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007189{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007190 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007191 mbedtls_md5_context md5;
7192 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007193
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 unsigned char padbuf[48];
7195 unsigned char md5sum[16];
7196 unsigned char sha1sum[20];
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007199 if( !session )
7200 session = ssl->session;
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007203
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007204 mbedtls_md5_init( &md5 );
7205 mbedtls_sha1_init( &sha1 );
7206
7207 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7208 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007209
7210 /*
7211 * SSLv3:
7212 * hash =
7213 * MD5( master + pad2 +
7214 * MD5( handshake + sender + master + pad1 ) )
7215 * + SHA1( master + pad2 +
7216 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007217 */
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007220 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7221 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007222#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007225 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7226 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007230 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007231
Paul Bakker1ef83d62012-04-11 12:09:53 +00007232 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007233
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007234 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7235 mbedtls_md5_update_ret( &md5, session->master, 48 );
7236 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7237 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007238
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007239 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7240 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7241 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7242 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007243
Paul Bakker1ef83d62012-04-11 12:09:53 +00007244 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007245
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007246 mbedtls_md5_starts_ret( &md5 );
7247 mbedtls_md5_update_ret( &md5, session->master, 48 );
7248 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7249 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7250 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007252 mbedtls_sha1_starts_ret( &sha1 );
7253 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7254 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7255 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7256 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007259
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007260 mbedtls_md5_free( &md5 );
7261 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007262
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007263 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7264 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7265 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007272static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007274{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007275 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007276 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007277 mbedtls_md5_context md5;
7278 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007279 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007282 if( !session )
7283 session = ssl->session;
7284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007286
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007287 mbedtls_md5_init( &md5 );
7288 mbedtls_sha1_init( &sha1 );
7289
7290 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7291 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007292
Paul Bakker1ef83d62012-04-11 12:09:53 +00007293 /*
7294 * TLSv1:
7295 * hash = PRF( master, finished_label,
7296 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7297 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007300 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7301 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007302#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007305 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7306 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007307#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007310 ? "client finished"
7311 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007313 mbedtls_md5_finish_ret( &md5, padbuf );
7314 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007315
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007316 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007317 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007321 mbedtls_md5_free( &md5 );
7322 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007323
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007324 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7331#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007332static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007334{
7335 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007336 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007337 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007338#if defined(MBEDTLS_USE_PSA_CRYPTO)
7339 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007340 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007341 psa_status_t status;
7342#else
7343 mbedtls_sha256_context sha256;
7344#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007347 if( !session )
7348 session = ssl->session;
7349
Andrzej Kurekeb342242019-01-29 09:14:33 -05007350 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7351 ? "client finished"
7352 : "server finished";
7353
7354#if defined(MBEDTLS_USE_PSA_CRYPTO)
7355 sha256_psa = psa_hash_operation_init();
7356
7357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7358
7359 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7360 if( status != PSA_SUCCESS )
7361 {
7362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7363 return;
7364 }
7365
7366 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7367 if( status != PSA_SUCCESS )
7368 {
7369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7370 return;
7371 }
7372 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7373#else
7374
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007375 mbedtls_sha256_init( &sha256 );
7376
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007379 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007380
7381 /*
7382 * TLSv1.2:
7383 * hash = PRF( master, finished_label,
7384 * Hash( handshake ) )[0.11]
7385 */
7386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if !defined(MBEDTLS_SHA256_ALT)
7388 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007389 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007390#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007392 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007393 mbedtls_sha256_free( &sha256 );
7394#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007395
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007396 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007397 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007400
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007401 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007408static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007410{
7411 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007412 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007413 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007414#if defined(MBEDTLS_USE_PSA_CRYPTO)
7415 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007416 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007417 psa_status_t status;
7418#else
7419 mbedtls_sha512_context sha512;
7420#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007423 if( !session )
7424 session = ssl->session;
7425
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7427 ? "client finished"
7428 : "server finished";
7429
7430#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007431 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007432
7433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7434
Andrzej Kurek972fba52019-01-30 03:29:12 -05007435 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007436 if( status != PSA_SUCCESS )
7437 {
7438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7439 return;
7440 }
7441
Andrzej Kurek972fba52019-01-30 03:29:12 -05007442 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007443 if( status != PSA_SUCCESS )
7444 {
7445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7446 return;
7447 }
7448 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7449#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007450 mbedtls_sha512_init( &sha512 );
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007453
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007454 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007455
7456 /*
7457 * TLSv1.2:
7458 * hash = PRF( master, finished_label,
7459 * Hash( handshake ) )[0.11]
7460 */
7461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007463 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7464 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007465#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007466
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007467 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007468 mbedtls_sha512_free( &sha512 );
7469#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007470
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007471 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007472 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007475
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007476 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#endif /* MBEDTLS_SHA512_C */
7481#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007486
7487 /*
7488 * Free our handshake params
7489 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007490 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007492 ssl->handshake = NULL;
7493
7494 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007495 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007496 */
7497 if( ssl->transform )
7498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 mbedtls_ssl_transform_free( ssl->transform );
7500 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007501 }
7502 ssl->transform = ssl->transform_negotiate;
7503 ssl->transform_negotiate = NULL;
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007506}
7507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007509{
7510 int resume = ssl->handshake->resume;
7511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SSL_RENEGOTIATION)
7515 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007518 ssl->renego_records_seen = 0;
7519 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007520#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007521
7522 /*
7523 * Free the previous session and switch in the current one
7524 */
Paul Bakker0a597072012-09-25 21:55:46 +00007525 if( ssl->session )
7526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007528 /* RFC 7366 3.1: keep the EtM state */
7529 ssl->session_negotiate->encrypt_then_mac =
7530 ssl->session->encrypt_then_mac;
7531#endif
7532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 mbedtls_ssl_session_free( ssl->session );
7534 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007535 }
7536 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007537 ssl->session_negotiate = NULL;
7538
Paul Bakker0a597072012-09-25 21:55:46 +00007539 /*
7540 * Add cache entry
7541 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007542 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007543 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007544 resume == 0 )
7545 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007546 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007548 }
Paul Bakker0a597072012-09-25 21:55:46 +00007549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007551 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007552 ssl->handshake->flight != NULL )
7553 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007554 /* Cancel handshake timer */
7555 ssl_set_timer( ssl, 0 );
7556
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007557 /* Keep last flight around in case we need to resend it:
7558 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007560 }
7561 else
7562#endif
7563 ssl_handshake_wrapup_free_hs_transform( ssl );
7564
Paul Bakker48916f92012-09-16 19:57:18 +00007565 ssl->state++;
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007568}
7569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007571{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007572 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007575
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007576 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007577
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007578 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007579
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007580 /*
7581 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7582 * may define some other value. Currently (early 2016), no defined
7583 * ciphersuite does this (and this is unlikely to change as activity has
7584 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007589 ssl->verify_data_len = hash_len;
7590 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007591#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007592
Paul Bakker5121ce52009-01-03 21:22:43 +00007593 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7595 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007596
7597 /*
7598 * In case of session resuming, invert the client and server
7599 * ChangeCipherSpec messages order.
7600 */
Paul Bakker0a597072012-09-25 21:55:46 +00007601 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007604 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007611 }
7612 else
7613 ssl->state++;
7614
Paul Bakker48916f92012-09-16 19:57:18 +00007615 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007616 * Switch to our negotiated transform and session parameters for outbound
7617 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007623 {
7624 unsigned char i;
7625
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007626 /* Remember current epoch settings for resending */
7627 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007628 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007629
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007630 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007631 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007632
7633 /* Increment epoch */
7634 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007635 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007636 break;
7637
7638 /* The loop goes to its end iff the counter is wrapping */
7639 if( i == 0 )
7640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7642 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007643 }
7644 }
7645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007647 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007648
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007649 ssl->transform_out = ssl->transform_negotiate;
7650 ssl->session_out = ssl->session_negotiate;
7651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7653 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7658 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007659 }
7660 }
7661#endif
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007664 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007666#endif
7667
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007668 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007669 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007671 return( ret );
7672 }
7673
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007674#if defined(MBEDTLS_SSL_PROTO_DTLS)
7675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7676 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7677 {
7678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7679 return( ret );
7680 }
7681#endif
7682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
7685 return( 0 );
7686}
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007689#define SSL_MAX_HASH_LEN 36
7690#else
7691#define SSL_MAX_HASH_LEN 12
7692#endif
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007695{
Paul Bakker23986e52011-04-24 08:57:21 +00007696 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007697 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007698 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007702 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007703
Hanno Becker327c93b2018-08-15 13:56:18 +01007704 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007707 return( ret );
7708 }
7709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7714 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007716 }
7717
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007718 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_SSL_PROTO_SSL3)
7720 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007721 hash_len = 36;
7722 else
7723#endif
7724 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7727 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007730 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7731 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007733 }
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007736 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007742 }
7743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007745 ssl->verify_data_len = hash_len;
7746 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007747#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007748
Paul Bakker0a597072012-09-25 21:55:46 +00007749 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007752 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007756 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007758#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 }
7760 else
7761 ssl->state++;
7762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007766#endif
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007769
7770 return( 0 );
7771}
7772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7778 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7779 mbedtls_md5_init( &handshake->fin_md5 );
7780 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007781 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7782 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7785#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007786#if defined(MBEDTLS_USE_PSA_CRYPTO)
7787 handshake->fin_sha256_psa = psa_hash_operation_init();
7788 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7789#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007791 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007792#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007793#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007795#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007796 handshake->fin_sha384_psa = psa_hash_operation_init();
7797 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007798#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007800 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007801#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007804
7805 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007806
7807#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7808 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7809 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7810#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_DHM_C)
7813 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815#if defined(MBEDTLS_ECDH_C)
7816 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007817#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007818#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007819 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007820#if defined(MBEDTLS_SSL_CLI_C)
7821 handshake->ecjpake_cache = NULL;
7822 handshake->ecjpake_cache_len = 0;
7823#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007824#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007825
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007826#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007827 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007828#endif
7829
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007830#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7831 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7832#endif
Hanno Becker75173122019-02-06 16:18:31 +00007833
7834#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7835 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7836 mbedtls_pk_init( &handshake->peer_pubkey );
7837#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007838}
7839
Hanno Beckera18d1322018-01-03 14:27:32 +00007840void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7845 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007846
Hanno Beckerd56ed242018-01-03 15:32:51 +00007847#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 mbedtls_md_init( &transform->md_ctx_enc );
7849 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007850#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007851}
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007856}
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007859{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007860 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007861 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007863 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007865 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007866 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007867
7868 /*
7869 * Either the pointers are now NULL or cleared properly and can be freed.
7870 * Now allocate missing structures.
7871 */
7872 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007873 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007874 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007875 }
Paul Bakker48916f92012-09-16 19:57:18 +00007876
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007877 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007878 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007879 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007880 }
Paul Bakker48916f92012-09-16 19:57:18 +00007881
Paul Bakker82788fb2014-10-20 13:59:19 +02007882 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007883 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007884 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007885 }
Paul Bakker48916f92012-09-16 19:57:18 +00007886
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007887 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007888 if( ssl->handshake == NULL ||
7889 ssl->transform_negotiate == NULL ||
7890 ssl->session_negotiate == NULL )
7891 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 mbedtls_free( ssl->handshake );
7895 mbedtls_free( ssl->transform_negotiate );
7896 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007897
7898 ssl->handshake = NULL;
7899 ssl->transform_negotiate = NULL;
7900 ssl->session_negotiate = NULL;
7901
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007902 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007903 }
7904
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007905 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007907 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007908 ssl_handshake_params_init( ssl->handshake );
7909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7912 {
7913 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007914
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7916 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7917 else
7918 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007919
7920 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007921 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007922#endif
7923
Paul Bakker48916f92012-09-16 19:57:18 +00007924 return( 0 );
7925}
7926
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007927#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007928/* Dummy cookie callbacks for defaults */
7929static int ssl_cookie_write_dummy( void *ctx,
7930 unsigned char **p, unsigned char *end,
7931 const unsigned char *cli_id, size_t cli_id_len )
7932{
7933 ((void) ctx);
7934 ((void) p);
7935 ((void) end);
7936 ((void) cli_id);
7937 ((void) cli_id_len);
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007940}
7941
7942static int ssl_cookie_check_dummy( void *ctx,
7943 const unsigned char *cookie, size_t cookie_len,
7944 const unsigned char *cli_id, size_t cli_id_len )
7945{
7946 ((void) ctx);
7947 ((void) cookie);
7948 ((void) cookie_len);
7949 ((void) cli_id);
7950 ((void) cli_id_len);
7951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007953}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007954#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007955
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007956/* Once ssl->out_hdr as the address of the beginning of the
7957 * next outgoing record is set, deduce the other pointers.
7958 *
7959 * Note: For TLS, we save the implicit record sequence number
7960 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7961 * and the caller has to make sure there's space for this.
7962 */
7963
7964static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7965 mbedtls_ssl_transform *transform )
7966{
7967#if defined(MBEDTLS_SSL_PROTO_DTLS)
7968 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7969 {
7970 ssl->out_ctr = ssl->out_hdr + 3;
7971 ssl->out_len = ssl->out_hdr + 11;
7972 ssl->out_iv = ssl->out_hdr + 13;
7973 }
7974 else
7975#endif
7976 {
7977 ssl->out_ctr = ssl->out_hdr - 8;
7978 ssl->out_len = ssl->out_hdr + 3;
7979 ssl->out_iv = ssl->out_hdr + 5;
7980 }
7981
7982 /* Adjust out_msg to make space for explicit IV, if used. */
7983 if( transform != NULL &&
7984 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7985 {
7986 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7987 }
7988 else
7989 ssl->out_msg = ssl->out_iv;
7990}
7991
7992/* Once ssl->in_hdr as the address of the beginning of the
7993 * next incoming record is set, deduce the other pointers.
7994 *
7995 * Note: For TLS, we save the implicit record sequence number
7996 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7997 * and the caller has to make sure there's space for this.
7998 */
7999
Hanno Becker79594fd2019-05-08 09:38:41 +01008000static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008001{
Hanno Becker79594fd2019-05-08 09:38:41 +01008002 /* This function sets the pointers to match the case
8003 * of unprotected TLS/DTLS records, with both ssl->in_iv
8004 * and ssl->in_msg pointing to the beginning of the record
8005 * content.
8006 *
8007 * When decrypting a protected record, ssl->in_msg
8008 * will be shifted to point to the beginning of the
8009 * record plaintext.
8010 */
8011
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008012#if defined(MBEDTLS_SSL_PROTO_DTLS)
8013 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8014 {
8015 ssl->in_ctr = ssl->in_hdr + 3;
8016 ssl->in_len = ssl->in_hdr + 11;
8017 ssl->in_iv = ssl->in_hdr + 13;
8018 }
8019 else
8020#endif
8021 {
8022 ssl->in_ctr = ssl->in_hdr - 8;
8023 ssl->in_len = ssl->in_hdr + 3;
8024 ssl->in_iv = ssl->in_hdr + 5;
8025 }
8026
Hanno Becker79594fd2019-05-08 09:38:41 +01008027 /* This will be adjusted at record decryption time. */
8028 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008029}
8030
Paul Bakker5121ce52009-01-03 21:22:43 +00008031/*
8032 * Initialize an SSL context
8033 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008034void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8035{
8036 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8037}
8038
8039/*
8040 * Setup an SSL context
8041 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008042
8043static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8044{
8045 /* Set the incoming and outgoing record pointers. */
8046#if defined(MBEDTLS_SSL_PROTO_DTLS)
8047 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8048 {
8049 ssl->out_hdr = ssl->out_buf;
8050 ssl->in_hdr = ssl->in_buf;
8051 }
8052 else
8053#endif /* MBEDTLS_SSL_PROTO_DTLS */
8054 {
8055 ssl->out_hdr = ssl->out_buf + 8;
8056 ssl->in_hdr = ssl->in_buf + 8;
8057 }
8058
8059 /* Derive other internal pointers. */
8060 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008061 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008062}
8063
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008064int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008065 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008066{
Paul Bakker48916f92012-09-16 19:57:18 +00008067 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008068
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008069 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008070
8071 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008072 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008073 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008074
8075 /* Set to NULL in case of an error condition */
8076 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008077
Angus Grattond8213d02016-05-25 20:56:48 +10008078 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8079 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080 {
Angus Grattond8213d02016-05-25 20:56:48 +10008081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008082 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008083 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008084 }
8085
8086 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8087 if( ssl->out_buf == NULL )
8088 {
8089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008090 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008091 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008092 }
8093
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008094 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008095
Paul Bakker48916f92012-09-16 19:57:18 +00008096 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008097 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008098
8099 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008100
8101error:
8102 mbedtls_free( ssl->in_buf );
8103 mbedtls_free( ssl->out_buf );
8104
8105 ssl->conf = NULL;
8106
8107 ssl->in_buf = NULL;
8108 ssl->out_buf = NULL;
8109
8110 ssl->in_hdr = NULL;
8111 ssl->in_ctr = NULL;
8112 ssl->in_len = NULL;
8113 ssl->in_iv = NULL;
8114 ssl->in_msg = NULL;
8115
8116 ssl->out_hdr = NULL;
8117 ssl->out_ctr = NULL;
8118 ssl->out_len = NULL;
8119 ssl->out_iv = NULL;
8120 ssl->out_msg = NULL;
8121
k-stachowiak9f7798e2018-07-31 16:52:32 +02008122 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008123}
8124
8125/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008126 * Reset an initialized and used SSL context for re-use while retaining
8127 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008128 *
8129 * If partial is non-zero, keep data in the input buffer and client ID.
8130 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008131 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008132static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008133{
Paul Bakker48916f92012-09-16 19:57:18 +00008134 int ret;
8135
Hanno Becker7e772132018-08-10 12:38:21 +01008136#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8137 !defined(MBEDTLS_SSL_SRV_C)
8138 ((void) partial);
8139#endif
8140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008142
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008143 /* Cancel any possibly running timer */
8144 ssl_set_timer( ssl, 0 );
8145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#if defined(MBEDTLS_SSL_RENEGOTIATION)
8147 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008148 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008149
8150 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8152 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008155
Paul Bakker7eb013f2011-10-06 12:37:39 +00008156 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008157 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008158
8159 ssl->in_msgtype = 0;
8160 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008162 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008163 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008166 ssl_dtls_replay_reset( ssl );
8167#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008168
8169 ssl->in_hslen = 0;
8170 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008171
8172 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008173
8174 ssl->out_msgtype = 0;
8175 ssl->out_msglen = 0;
8176 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8178 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008179 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008180#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008181
Hanno Becker19859472018-08-06 09:40:20 +01008182 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8183
Paul Bakker48916f92012-09-16 19:57:18 +00008184 ssl->transform_in = NULL;
8185 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008186
Hanno Becker78640902018-08-13 16:35:15 +01008187 ssl->session_in = NULL;
8188 ssl->session_out = NULL;
8189
Angus Grattond8213d02016-05-25 20:56:48 +10008190 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008191
8192#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008193 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008194#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8195 {
8196 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008197 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008198 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8201 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8204 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8207 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008208 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008209 }
8210#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008211
Paul Bakker48916f92012-09-16 19:57:18 +00008212 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 mbedtls_ssl_transform_free( ssl->transform );
8215 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008216 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008217 }
Paul Bakker48916f92012-09-16 19:57:18 +00008218
Paul Bakkerc0463502013-02-14 11:19:38 +01008219 if( ssl->session )
8220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 mbedtls_ssl_session_free( ssl->session );
8222 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008223 ssl->session = NULL;
8224 }
8225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008227 ssl->alpn_chosen = NULL;
8228#endif
8229
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008230#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008231#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008232 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008233#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008234 {
8235 mbedtls_free( ssl->cli_id );
8236 ssl->cli_id = NULL;
8237 ssl->cli_id_len = 0;
8238 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008239#endif
8240
Paul Bakker48916f92012-09-16 19:57:18 +00008241 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8242 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008243
8244 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008245}
8246
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008247/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008248 * Reset an initialized and used SSL context for re-use while retaining
8249 * all application-set variables, function pointers and data.
8250 */
8251int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8252{
8253 return( ssl_session_reset_int( ssl, 0 ) );
8254}
8255
8256/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008257 * SSL set accessors
8258 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008259void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008261 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008262}
8263
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008264void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008266 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008267}
8268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008270void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008272 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008273}
8274#endif
8275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008277void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008278{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008279 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008280}
8281#endif
8282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008284
Hanno Becker1841b0a2018-08-24 11:13:57 +01008285void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8286 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008287{
8288 ssl->disable_datagram_packing = !allow_packing;
8289}
8290
8291void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8292 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008293{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008294 conf->hs_timeout_min = min;
8295 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008296}
8297#endif
8298
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008299void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008301 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008302}
8303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008305void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008306 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008307 void *p_vrfy )
8308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008309 conf->f_vrfy = f_vrfy;
8310 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008313
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008314void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008315 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008316 void *p_rng )
8317{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008318 conf->f_rng = f_rng;
8319 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008320}
8321
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008322void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008323 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008324 void *p_dbg )
8325{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008326 conf->f_dbg = f_dbg;
8327 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008328}
8329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008331 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008332 mbedtls_ssl_send_t *f_send,
8333 mbedtls_ssl_recv_t *f_recv,
8334 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008335{
8336 ssl->p_bio = p_bio;
8337 ssl->f_send = f_send;
8338 ssl->f_recv = f_recv;
8339 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008340}
8341
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008342#if defined(MBEDTLS_SSL_PROTO_DTLS)
8343void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8344{
8345 ssl->mtu = mtu;
8346}
8347#endif
8348
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008349void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008350{
8351 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008352}
8353
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008354void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8355 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008356 mbedtls_ssl_set_timer_t *f_set_timer,
8357 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008358{
8359 ssl->p_timer = p_timer;
8360 ssl->f_set_timer = f_set_timer;
8361 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008362
8363 /* Make sure we start with no timer running */
8364 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008365}
8366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008368void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008369 void *p_cache,
8370 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8371 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008372{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008373 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008374 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008375 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379#if defined(MBEDTLS_SSL_CLI_C)
8380int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008381{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008382 int ret;
8383
8384 if( ssl == NULL ||
8385 session == NULL ||
8386 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008387 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008390 }
8391
Hanno Becker52055ae2019-02-06 14:30:46 +00008392 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8393 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008394 return( ret );
8395
Paul Bakker0a597072012-09-25 21:55:46 +00008396 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008397
8398 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008401
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008402void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008403 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8406 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8407 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8408 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008409}
8410
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008411void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008412 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008413 int major, int minor )
8414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008416 return;
8417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008419 return;
8420
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008421 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008422}
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008425void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008426 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008427{
8428 conf->cert_profile = profile;
8429}
8430
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008431/* Append a new keycert entry to a (possibly empty) list */
8432static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8433 mbedtls_x509_crt *cert,
8434 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008435{
niisato8ee24222018-06-25 19:05:48 +09008436 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008437
niisato8ee24222018-06-25 19:05:48 +09008438 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8439 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008440 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008441
niisato8ee24222018-06-25 19:05:48 +09008442 new_cert->cert = cert;
8443 new_cert->key = key;
8444 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008445
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008446 /* Update head is the list was null, else add to the end */
8447 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008448 {
niisato8ee24222018-06-25 19:05:48 +09008449 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008450 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008451 else
8452 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008453 mbedtls_ssl_key_cert *cur = *head;
8454 while( cur->next != NULL )
8455 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008456 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008457 }
8458
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008459 return( 0 );
8460}
8461
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008462int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008463 mbedtls_x509_crt *own_cert,
8464 mbedtls_pk_context *pk_key )
8465{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008466 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008467}
8468
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008469void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008470 mbedtls_x509_crt *ca_chain,
8471 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008472{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008473 conf->ca_chain = ca_chain;
8474 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008475
8476#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8477 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8478 * cannot be used together. */
8479 conf->f_ca_cb = NULL;
8480 conf->p_ca_cb = NULL;
8481#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008482}
Hanno Becker5adaad92019-03-27 16:54:37 +00008483
8484#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8485void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008486 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008487 void *p_ca_cb )
8488{
8489 conf->f_ca_cb = f_ca_cb;
8490 conf->p_ca_cb = p_ca_cb;
8491
8492 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8493 * cannot be used together. */
8494 conf->ca_chain = NULL;
8495 conf->ca_crl = NULL;
8496}
8497#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008499
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008500#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8501int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8502 mbedtls_x509_crt *own_cert,
8503 mbedtls_pk_context *pk_key )
8504{
8505 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8506 own_cert, pk_key ) );
8507}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008508
8509void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8510 mbedtls_x509_crt *ca_chain,
8511 mbedtls_x509_crl *ca_crl )
8512{
8513 ssl->handshake->sni_ca_chain = ca_chain;
8514 ssl->handshake->sni_ca_crl = ca_crl;
8515}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008516
8517void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8518 int authmode )
8519{
8520 ssl->handshake->sni_authmode = authmode;
8521}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008522#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8523
Hanno Becker8927c832019-04-03 12:52:50 +01008524#if defined(MBEDTLS_X509_CRT_PARSE_C)
8525void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8526 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8527 void *p_vrfy )
8528{
8529 ssl->f_vrfy = f_vrfy;
8530 ssl->p_vrfy = p_vrfy;
8531}
8532#endif
8533
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008534#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008535/*
8536 * Set EC J-PAKE password for current handshake
8537 */
8538int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8539 const unsigned char *pw,
8540 size_t pw_len )
8541{
8542 mbedtls_ecjpake_role role;
8543
Janos Follath8eb64132016-06-03 15:40:57 +01008544 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8546
8547 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8548 role = MBEDTLS_ECJPAKE_SERVER;
8549 else
8550 role = MBEDTLS_ECJPAKE_CLIENT;
8551
8552 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8553 role,
8554 MBEDTLS_MD_SHA256,
8555 MBEDTLS_ECP_DP_SECP256R1,
8556 pw, pw_len ) );
8557}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008558#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008561
8562static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8563{
8564 /* Remove reference to existing PSK, if any. */
8565#if defined(MBEDTLS_USE_PSA_CRYPTO)
8566 if( conf->psk_opaque != 0 )
8567 {
8568 /* The maintenance of the PSK key slot is the
8569 * user's responsibility. */
8570 conf->psk_opaque = 0;
8571 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008572 /* This and the following branch should never
8573 * be taken simultaenously as we maintain the
8574 * invariant that raw and opaque PSKs are never
8575 * configured simultaneously. As a safeguard,
8576 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008577#endif /* MBEDTLS_USE_PSA_CRYPTO */
8578 if( conf->psk != NULL )
8579 {
8580 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8581
8582 mbedtls_free( conf->psk );
8583 conf->psk = NULL;
8584 conf->psk_len = 0;
8585 }
8586
8587 /* Remove reference to PSK identity, if any. */
8588 if( conf->psk_identity != NULL )
8589 {
8590 mbedtls_free( conf->psk_identity );
8591 conf->psk_identity = NULL;
8592 conf->psk_identity_len = 0;
8593 }
8594}
8595
Hanno Becker7390c712018-11-15 13:33:04 +00008596/* This function assumes that PSK identity in the SSL config is unset.
8597 * It checks that the provided identity is well-formed and attempts
8598 * to make a copy of it in the SSL config.
8599 * On failure, the PSK identity in the config remains unset. */
8600static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8601 unsigned char const *psk_identity,
8602 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008603{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008604 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008605 if( psk_identity == NULL ||
8606 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008607 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008608 {
8609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8610 }
8611
Hanno Becker7390c712018-11-15 13:33:04 +00008612 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8613 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008614 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008615
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008616 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008617 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008618
8619 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008620}
8621
Hanno Becker7390c712018-11-15 13:33:04 +00008622int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8623 const unsigned char *psk, size_t psk_len,
8624 const unsigned char *psk_identity, size_t psk_identity_len )
8625{
8626 int ret;
8627 /* Remove opaque/raw PSK + PSK Identity */
8628 ssl_conf_remove_psk( conf );
8629
8630 /* Check and set raw PSK */
8631 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8633 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8634 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8635 conf->psk_len = psk_len;
8636 memcpy( conf->psk, psk, conf->psk_len );
8637
8638 /* Check and set PSK Identity */
8639 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8640 if( ret != 0 )
8641 ssl_conf_remove_psk( conf );
8642
8643 return( ret );
8644}
8645
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008646static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8647{
8648#if defined(MBEDTLS_USE_PSA_CRYPTO)
8649 if( ssl->handshake->psk_opaque != 0 )
8650 {
8651 ssl->handshake->psk_opaque = 0;
8652 }
8653 else
8654#endif /* MBEDTLS_USE_PSA_CRYPTO */
8655 if( ssl->handshake->psk != NULL )
8656 {
8657 mbedtls_platform_zeroize( ssl->handshake->psk,
8658 ssl->handshake->psk_len );
8659 mbedtls_free( ssl->handshake->psk );
8660 ssl->handshake->psk_len = 0;
8661 }
8662}
8663
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008664int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8665 const unsigned char *psk, size_t psk_len )
8666{
8667 if( psk == NULL || ssl->handshake == NULL )
8668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8669
8670 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8672
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008673 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008674
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008675 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008676 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008677
8678 ssl->handshake->psk_len = psk_len;
8679 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8680
8681 return( 0 );
8682}
8683
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008684#if defined(MBEDTLS_USE_PSA_CRYPTO)
8685int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008686 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008687 const unsigned char *psk_identity,
8688 size_t psk_identity_len )
8689{
Hanno Becker7390c712018-11-15 13:33:04 +00008690 int ret;
8691 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008692 ssl_conf_remove_psk( conf );
8693
Hanno Becker7390c712018-11-15 13:33:04 +00008694 /* Check and set opaque PSK */
8695 if( psk_slot == 0 )
8696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008697 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008698
8699 /* Check and set PSK Identity */
8700 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8701 psk_identity_len );
8702 if( ret != 0 )
8703 ssl_conf_remove_psk( conf );
8704
8705 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008706}
8707
8708int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008709 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008710{
8711 if( psk_slot == 0 || ssl->handshake == NULL )
8712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8713
8714 ssl_remove_psk( ssl );
8715 ssl->handshake->psk_opaque = psk_slot;
8716 return( 0 );
8717}
8718#endif /* MBEDTLS_USE_PSA_CRYPTO */
8719
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008720void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008722 size_t),
8723 void *p_psk )
8724{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008725 conf->f_psk = f_psk;
8726 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008729
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008730#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008731
8732#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008733int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008734{
8735 int ret;
8736
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008737 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8738 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8739 {
8740 mbedtls_mpi_free( &conf->dhm_P );
8741 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008742 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008744
8745 return( 0 );
8746}
Hanno Becker470a8c42017-10-04 15:28:46 +01008747#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008748
Hanno Beckera90658f2017-10-04 15:29:08 +01008749int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8750 const unsigned char *dhm_P, size_t P_len,
8751 const unsigned char *dhm_G, size_t G_len )
8752{
8753 int ret;
8754
8755 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8756 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8757 {
8758 mbedtls_mpi_free( &conf->dhm_P );
8759 mbedtls_mpi_free( &conf->dhm_G );
8760 return( ret );
8761 }
8762
8763 return( 0 );
8764}
Paul Bakker5121ce52009-01-03 21:22:43 +00008765
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008766int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008767{
8768 int ret;
8769
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008770 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8771 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8772 {
8773 mbedtls_mpi_free( &conf->dhm_P );
8774 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008775 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008776 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008777
8778 return( 0 );
8779}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008780#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008781
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008782#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8783/*
8784 * Set the minimum length for Diffie-Hellman parameters
8785 */
8786void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8787 unsigned int bitlen )
8788{
8789 conf->dhm_min_bitlen = bitlen;
8790}
8791#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8792
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008793#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008794/*
8795 * Set allowed/preferred hashes for handshake signatures
8796 */
8797void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8798 const int *hashes )
8799{
8800 conf->sig_hashes = hashes;
8801}
Hanno Becker947194e2017-04-07 13:25:49 +01008802#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008803
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008804#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008805/*
8806 * Set the allowed elliptic curves
8807 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008808void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008809 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008810{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008811 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008812}
Hanno Becker947194e2017-04-07 13:25:49 +01008813#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008814
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008815#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008817{
Hanno Becker947194e2017-04-07 13:25:49 +01008818 /* Initialize to suppress unnecessary compiler warning */
8819 size_t hostname_len = 0;
8820
8821 /* Check if new hostname is valid before
8822 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008823 if( hostname != NULL )
8824 {
8825 hostname_len = strlen( hostname );
8826
8827 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8829 }
8830
8831 /* Now it's clear that we will overwrite the old hostname,
8832 * so we can free it safely */
8833
8834 if( ssl->hostname != NULL )
8835 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008836 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008837 mbedtls_free( ssl->hostname );
8838 }
8839
8840 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008841
Paul Bakker5121ce52009-01-03 21:22:43 +00008842 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008843 {
8844 ssl->hostname = NULL;
8845 }
8846 else
8847 {
8848 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008849 if( ssl->hostname == NULL )
8850 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008851
Hanno Becker947194e2017-04-07 13:25:49 +01008852 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008853
Hanno Becker947194e2017-04-07 13:25:49 +01008854 ssl->hostname[hostname_len] = '\0';
8855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008856
8857 return( 0 );
8858}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008859#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008860
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008861#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008862void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008864 const unsigned char *, size_t),
8865 void *p_sni )
8866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008867 conf->f_sni = f_sni;
8868 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008873int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008874{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008875 size_t cur_len, tot_len;
8876 const char **p;
8877
8878 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008879 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8880 * MUST NOT be truncated."
8881 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008882 */
8883 tot_len = 0;
8884 for( p = protos; *p != NULL; p++ )
8885 {
8886 cur_len = strlen( *p );
8887 tot_len += cur_len;
8888
8889 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008891 }
8892
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008893 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008894
8895 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008896}
8897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008899{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008900 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008902#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008903
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008904void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008905{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008906 conf->max_major_ver = major;
8907 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008908}
8909
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008910void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008911{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008912 conf->min_major_ver = major;
8913 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008914}
8915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008917void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008918{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008919 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008920}
8921#endif
8922
Janos Follath088ce432017-04-10 12:42:31 +01008923#if defined(MBEDTLS_SSL_SRV_C)
8924void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8925 char cert_req_ca_list )
8926{
8927 conf->cert_req_ca_list = cert_req_ca_list;
8928}
8929#endif
8930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008932void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008934 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008935}
8936#endif
8937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008939void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008940{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008941 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008942}
8943#endif
8944
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008945#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008946void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008947{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008948 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008949}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008950#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008953int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008956 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008959 }
8960
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008961 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008962
8963 return( 0 );
8964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008968void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008969{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008970 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008974#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008975void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008976{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008977 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008978}
8979#endif
8980
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008981void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008982{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008983 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008984}
8985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008987void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008989 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008990}
8991
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008992void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008994 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008995}
8996
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008997void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008998 const unsigned char period[8] )
8999{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009000 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009005#if defined(MBEDTLS_SSL_CLI_C)
9006void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009007{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009008 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009009}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009010#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009011
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009012#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009013void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9014 mbedtls_ssl_ticket_write_t *f_ticket_write,
9015 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9016 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009017{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009018 conf->f_ticket_write = f_ticket_write;
9019 conf->f_ticket_parse = f_ticket_parse;
9020 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009021}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009024
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009025#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9026void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9027 mbedtls_ssl_export_keys_t *f_export_keys,
9028 void *p_export_keys )
9029{
9030 conf->f_export_keys = f_export_keys;
9031 conf->p_export_keys = p_export_keys;
9032}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009033
9034void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9035 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9036 void *p_export_keys )
9037{
9038 conf->f_export_keys_ext = f_export_keys_ext;
9039 conf->p_export_keys = p_export_keys;
9040}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009041#endif
9042
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009043#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009044void mbedtls_ssl_conf_async_private_cb(
9045 mbedtls_ssl_config *conf,
9046 mbedtls_ssl_async_sign_t *f_async_sign,
9047 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9048 mbedtls_ssl_async_resume_t *f_async_resume,
9049 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009050 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009051{
9052 conf->f_async_sign_start = f_async_sign;
9053 conf->f_async_decrypt_start = f_async_decrypt;
9054 conf->f_async_resume = f_async_resume;
9055 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009056 conf->p_async_config_data = async_config_data;
9057}
9058
Gilles Peskine8f97af72018-04-26 11:46:10 +02009059void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9060{
9061 return( conf->p_async_config_data );
9062}
9063
Gilles Peskine1febfef2018-04-30 11:54:39 +02009064void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009065{
9066 if( ssl->handshake == NULL )
9067 return( NULL );
9068 else
9069 return( ssl->handshake->user_async_ctx );
9070}
9071
Gilles Peskine1febfef2018-04-30 11:54:39 +02009072void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009073 void *ctx )
9074{
9075 if( ssl->handshake != NULL )
9076 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009077}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009078#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009079
Paul Bakker5121ce52009-01-03 21:22:43 +00009080/*
9081 * SSL get accessors
9082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009084{
9085 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9086}
9087
Hanno Becker8b170a02017-10-10 11:51:19 +01009088int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9089{
9090 /*
9091 * Case A: We're currently holding back
9092 * a message for further processing.
9093 */
9094
9095 if( ssl->keep_current_message == 1 )
9096 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009098 return( 1 );
9099 }
9100
9101 /*
9102 * Case B: Further records are pending in the current datagram.
9103 */
9104
9105#if defined(MBEDTLS_SSL_PROTO_DTLS)
9106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9107 ssl->in_left > ssl->next_record_offset )
9108 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009110 return( 1 );
9111 }
9112#endif /* MBEDTLS_SSL_PROTO_DTLS */
9113
9114 /*
9115 * Case C: A handshake message is being processed.
9116 */
9117
Hanno Becker8b170a02017-10-10 11:51:19 +01009118 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9119 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009121 return( 1 );
9122 }
9123
9124 /*
9125 * Case D: An application data message is being processed
9126 */
9127 if( ssl->in_offt != NULL )
9128 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009130 return( 1 );
9131 }
9132
9133 /*
9134 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009135 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009136 * we implement support for multiple alerts in single records.
9137 */
9138
9139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9140 return( 0 );
9141}
9142
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009143uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009144{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009145 if( ssl->session != NULL )
9146 return( ssl->session->verify_result );
9147
9148 if( ssl->session_negotiate != NULL )
9149 return( ssl->session_negotiate->verify_result );
9150
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009151 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009152}
9153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009155{
Paul Bakker926c8e42013-03-06 10:23:34 +01009156 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009157 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009160}
9161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009166 {
9167 switch( ssl->minor_ver )
9168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009170 return( "DTLSv1.0" );
9171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009173 return( "DTLSv1.2" );
9174
9175 default:
9176 return( "unknown (DTLS)" );
9177 }
9178 }
9179#endif
9180
Paul Bakker43ca69c2011-01-15 17:35:19 +00009181 switch( ssl->minor_ver )
9182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009184 return( "SSLv3.0" );
9185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009187 return( "TLSv1.0" );
9188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009190 return( "TLSv1.1" );
9191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009193 return( "TLSv1.2" );
9194
Paul Bakker43ca69c2011-01-15 17:35:19 +00009195 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009196 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009197 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009198}
9199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009201{
Hanno Becker3136ede2018-08-17 15:28:19 +01009202 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009204 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009205
Hanno Becker78640902018-08-13 16:35:15 +01009206 if( transform == NULL )
9207 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#if defined(MBEDTLS_ZLIB_SUPPORT)
9210 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9211 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009212#endif
9213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216 case MBEDTLS_MODE_GCM:
9217 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009218 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009220 transform_expansion = transform->minlen;
9221 break;
9222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009224
9225 block_size = mbedtls_cipher_get_block_size(
9226 &transform->cipher_ctx_enc );
9227
Hanno Becker3136ede2018-08-17 15:28:19 +01009228 /* Expansion due to the addition of the MAC. */
9229 transform_expansion += transform->maclen;
9230
9231 /* Expansion due to the addition of CBC padding;
9232 * Theoretically up to 256 bytes, but we never use
9233 * more than the block size of the underlying cipher. */
9234 transform_expansion += block_size;
9235
9236 /* For TLS 1.1 or higher, an explicit IV is added
9237 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009238#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9239 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009240 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009241#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009242
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009243 break;
9244
9245 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009248 }
9249
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009250 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009251}
9252
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9254size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9255{
9256 size_t max_len;
9257
9258 /*
9259 * Assume mfl_code is correct since it was checked when set
9260 */
Angus Grattond8213d02016-05-25 20:56:48 +10009261 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009262
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009263 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009264 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009265 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009266 {
Angus Grattond8213d02016-05-25 20:56:48 +10009267 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009268 }
9269
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009270 /* During a handshake, use the value being negotiated */
9271 if( ssl->session_negotiate != NULL &&
9272 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9273 {
9274 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9275 }
9276
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009277 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009278}
9279#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9280
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009281#if defined(MBEDTLS_SSL_PROTO_DTLS)
9282static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9283{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009284 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9285 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9286 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9287 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9288 return ( 0 );
9289
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009290 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9291 return( ssl->mtu );
9292
9293 if( ssl->mtu == 0 )
9294 return( ssl->handshake->mtu );
9295
9296 return( ssl->mtu < ssl->handshake->mtu ?
9297 ssl->mtu : ssl->handshake->mtu );
9298}
9299#endif /* MBEDTLS_SSL_PROTO_DTLS */
9300
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009301int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9302{
9303 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9304
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009305#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9306 !defined(MBEDTLS_SSL_PROTO_DTLS)
9307 (void) ssl;
9308#endif
9309
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009310#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9311 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9312
9313 if( max_len > mfl )
9314 max_len = mfl;
9315#endif
9316
9317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009318 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009319 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009320 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009321 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9322 const size_t overhead = (size_t) ret;
9323
9324 if( ret < 0 )
9325 return( ret );
9326
9327 if( mtu <= overhead )
9328 {
9329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9330 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9331 }
9332
9333 if( max_len > mtu - overhead )
9334 max_len = mtu - overhead;
9335 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009337
Hanno Becker0defedb2018-08-10 12:35:02 +01009338#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9339 !defined(MBEDTLS_SSL_PROTO_DTLS)
9340 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009341#endif
9342
9343 return( (int) max_len );
9344}
9345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346#if defined(MBEDTLS_X509_CRT_PARSE_C)
9347const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009348{
9349 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009350 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009351
Hanno Beckere6824572019-02-07 13:18:46 +00009352#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009353 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009354#else
9355 return( NULL );
9356#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009361int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9362 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009363{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009364 if( ssl == NULL ||
9365 dst == NULL ||
9366 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009367 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009370 }
9371
Hanno Becker52055ae2019-02-06 14:30:46 +00009372 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009375
Paul Bakker5121ce52009-01-03 21:22:43 +00009376/*
Paul Bakker1961b702013-01-25 14:49:24 +01009377 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009382
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009383 if( ssl == NULL || ssl->conf == NULL )
9384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009387 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009391 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009393#endif
9394
Paul Bakker1961b702013-01-25 14:49:24 +01009395 return( ret );
9396}
9397
9398/*
9399 * Perform the SSL handshake
9400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009402{
9403 int ret = 0;
9404
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009405 if( ssl == NULL || ssl->conf == NULL )
9406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009413
9414 if( ret != 0 )
9415 break;
9416 }
9417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009419
9420 return( ret );
9421}
9422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423#if defined(MBEDTLS_SSL_RENEGOTIATION)
9424#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009425/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009426 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009429{
9430 int ret;
9431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009433
9434 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9436 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009437
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009438 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009439 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009441 return( ret );
9442 }
9443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009445
9446 return( 0 );
9447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009449
9450/*
9451 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 * - any side: calling mbedtls_ssl_renegotiate(),
9453 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9454 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009455 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009456 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009460{
9461 int ret;
9462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009464
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009465 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9466 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009467
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009468 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9469 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009471 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009473 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009474 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009475 ssl->handshake->out_msg_seq = 1;
9476 else
9477 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009478 }
9479#endif
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9482 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009487 return( ret );
9488 }
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009491
9492 return( 0 );
9493}
9494
9495/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009496 * Renegotiate current connection on client,
9497 * or request renegotiation on server
9498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009502
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009503 if( ssl == NULL || ssl->conf == NULL )
9504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009507 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009508 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009514
9515 /* Did we already try/start sending HelloRequest? */
9516 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009518
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009519 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009524 /*
9525 * On client, either start the renegotiation process or,
9526 * if already in progress, continue the handshake
9527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009532
9533 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009536 return( ret );
9537 }
9538 }
9539 else
9540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009544 return( ret );
9545 }
9546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009548
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009549 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009550}
9551
9552/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009553 * Check record counters and renegotiate if they're above the limit.
9554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009556{
Andres AG2196c7f2016-12-15 17:01:16 +00009557 size_t ep_len = ssl_ep_len( ssl );
9558 int in_ctr_cmp;
9559 int out_ctr_cmp;
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9562 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009563 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009564 {
9565 return( 0 );
9566 }
9567
Andres AG2196c7f2016-12-15 17:01:16 +00009568 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9569 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009570 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009571 ssl->conf->renego_period + ep_len, 8 - ep_len );
9572
9573 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009574 {
9575 return( 0 );
9576 }
9577
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009582
9583/*
9584 * Receive application data decrypted from the SSL layer
9585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009587{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009588 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009589 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009590
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009591 if( ssl == NULL || ssl->conf == NULL )
9592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009600 return( ret );
9601
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009602 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009604 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009605 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009606 return( ret );
9607 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009608 }
9609#endif
9610
Hanno Becker4a810fb2017-05-24 16:27:30 +01009611 /*
9612 * Check if renegotiation is necessary and/or handshake is
9613 * in process. If yes, perform/continue, and fall through
9614 * if an unexpected packet is received while the client
9615 * is waiting for the ServerHello.
9616 *
9617 * (There is no equivalent to the last condition on
9618 * the server-side as it is not treated as within
9619 * a handshake while waiting for the ClientHello
9620 * after a renegotiation request.)
9621 */
9622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009624 ret = ssl_check_ctr_renegotiate( ssl );
9625 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9626 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009629 return( ret );
9630 }
9631#endif
9632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009636 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9637 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009640 return( ret );
9641 }
9642 }
9643
Hanno Beckere41158b2017-10-23 13:30:32 +01009644 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009645 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009646 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009647 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009648 if( ssl->f_get_timer != NULL &&
9649 ssl->f_get_timer( ssl->p_timer ) == -1 )
9650 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009651 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009652 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009653
Hanno Becker327c93b2018-08-15 13:56:18 +01009654 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009655 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009656 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9657 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009658
Hanno Becker4a810fb2017-05-24 16:27:30 +01009659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9660 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009661 }
9662
9663 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009665 {
9666 /*
9667 * OpenSSL sends empty messages to randomize the IV
9668 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009669 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009672 return( 0 );
9673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009675 return( ret );
9676 }
9677 }
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009682
Hanno Becker4a810fb2017-05-24 16:27:30 +01009683 /*
9684 * - For client-side, expect SERVER_HELLO_REQUEST.
9685 * - For server-side, expect CLIENT_HELLO.
9686 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9687 */
9688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009690 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009692 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009695
9696 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009698 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009699 {
9700 continue;
9701 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009704 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009705#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009706
Hanno Becker4a810fb2017-05-24 16:27:30 +01009707#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009708 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009712
9713 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009716 {
9717 continue;
9718 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009721 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009722#endif /* MBEDTLS_SSL_SRV_C */
9723
Hanno Becker21df7f92017-10-17 11:03:26 +01009724#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009725 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009726 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9727 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9728 ssl->conf->allow_legacy_renegotiation ==
9729 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9730 {
9731 /*
9732 * Accept renegotiation request
9733 */
Paul Bakker48916f92012-09-16 19:57:18 +00009734
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009735 /* DTLS clients need to know renego is server-initiated */
9736#if defined(MBEDTLS_SSL_PROTO_DTLS)
9737 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9738 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9739 {
9740 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9741 }
9742#endif
9743 ret = ssl_start_renegotiation( ssl );
9744 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9745 ret != 0 )
9746 {
9747 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9748 return( ret );
9749 }
9750 }
9751 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009752#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009753 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009754 /*
9755 * Refuse renegotiation
9756 */
9757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760#if defined(MBEDTLS_SSL_PROTO_SSL3)
9761 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009762 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009763 /* SSLv3 does not have a "no_renegotiation" warning, so
9764 we send a fatal alert and abort the connection. */
9765 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9766 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9767 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009768 }
9769 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9771#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9772 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9773 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9776 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9777 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009778 {
9779 return( ret );
9780 }
Paul Bakker48916f92012-09-16 19:57:18 +00009781 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009782 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9784 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9787 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009788 }
Paul Bakker48916f92012-09-16 19:57:18 +00009789 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009790
Hanno Becker90333da2017-10-10 11:27:13 +01009791 /* At this point, we don't know whether the renegotiation has been
9792 * completed or not. The cases to consider are the following:
9793 * 1) The renegotiation is complete. In this case, no new record
9794 * has been read yet.
9795 * 2) The renegotiation is incomplete because the client received
9796 * an application data record while awaiting the ServerHello.
9797 * 3) The renegotiation is incomplete because the client received
9798 * a non-handshake, non-application data message while awaiting
9799 * the ServerHello.
9800 * In each of these case, looping will be the proper action:
9801 * - For 1), the next iteration will read a new record and check
9802 * if it's application data.
9803 * - For 2), the loop condition isn't satisfied as application data
9804 * is present, hence continue is the same as break
9805 * - For 3), the loop condition is satisfied and read_record
9806 * will re-deliver the message that was held back by the client
9807 * when expecting the ServerHello.
9808 */
9809 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009810 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009811#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009813 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009814 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009815 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009816 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009819 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009821 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009822 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009823 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009830 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009831 }
9832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9836 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009837 }
9838
9839 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009840
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009841 /* We're going to return something now, cancel timer,
9842 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009844 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009845
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009847 /* If we requested renego but received AppData, resend HelloRequest.
9848 * Do it now, after setting in_offt, to avoid taking this branch
9849 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009851 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009853 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009854 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009857 return( ret );
9858 }
9859 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009861#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009862 }
9863
9864 n = ( len < ssl->in_msglen )
9865 ? len : ssl->in_msglen;
9866
9867 memcpy( buf, ssl->in_offt, n );
9868 ssl->in_msglen -= n;
9869
9870 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 {
9872 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009873 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009874 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009876 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009877 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009878 /* more data available */
9879 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009883
Paul Bakker23986e52011-04-24 08:57:21 +00009884 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009885}
9886
9887/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009888 * Send application data to be encrypted by the SSL layer, taking care of max
9889 * fragment length and buffer size.
9890 *
9891 * According to RFC 5246 Section 6.2.1:
9892 *
9893 * Zero-length fragments of Application data MAY be sent as they are
9894 * potentially useful as a traffic analysis countermeasure.
9895 *
9896 * Therefore, it is possible that the input message length is 0 and the
9897 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009898 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009899static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009900 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009901{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009902 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9903 const size_t max_len = (size_t) ret;
9904
9905 if( ret < 0 )
9906 {
9907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9908 return( ret );
9909 }
9910
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009911 if( len > max_len )
9912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009914 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009917 "maximum fragment length: %d > %d",
9918 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009920 }
9921 else
9922#endif
9923 len = max_len;
9924 }
Paul Bakker887bd502011-06-08 13:10:54 +00009925
Paul Bakker5121ce52009-01-03 21:22:43 +00009926 if( ssl->out_left != 0 )
9927 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009928 /*
9929 * The user has previously tried to send the data and
9930 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9931 * written. In this case, we expect the high-level write function
9932 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009937 return( ret );
9938 }
9939 }
Paul Bakker887bd502011-06-08 13:10:54 +00009940 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009941 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009942 /*
9943 * The user is trying to send a message the first time, so we need to
9944 * copy the data into the internal buffers and setup the data structure
9945 * to keep track of partial writes
9946 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009947 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009949 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009950
Hanno Becker67bc7c32018-08-06 11:33:50 +01009951 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009954 return( ret );
9955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009956 }
9957
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009958 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009959}
9960
9961/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009962 * Write application data, doing 1/n-1 splitting if necessary.
9963 *
9964 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009965 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009966 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009969static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009970 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009971{
9972 int ret;
9973
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009974 if( ssl->conf->cbc_record_splitting ==
9975 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009976 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9978 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9979 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009980 {
9981 return( ssl_write_real( ssl, buf, len ) );
9982 }
9983
9984 if( ssl->split_done == 0 )
9985 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009986 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009987 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009988 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009989 }
9990
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009991 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9992 return( ret );
9993 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009994
9995 return( ret + 1 );
9996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009998
9999/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010000 * Write application data (public-facing wrapper)
10001 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010002int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010003{
10004 int ret;
10005
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010007
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010008 if( ssl == NULL || ssl->conf == NULL )
10009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10010
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010011#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010012 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10013 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010014 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010015 return( ret );
10016 }
10017#endif
10018
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010019 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010020 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010021 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010022 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010024 return( ret );
10025 }
10026 }
10027
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010028#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010029 ret = ssl_write_split( ssl, buf, len );
10030#else
10031 ret = ssl_write_real( ssl, buf, len );
10032#endif
10033
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010035
10036 return( ret );
10037}
10038
10039/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010040 * Notify the peer that the connection is being closed
10041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010043{
10044 int ret;
10045
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010046 if( ssl == NULL || ssl->conf == NULL )
10047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010050
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010051 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10057 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10058 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010061 return( ret );
10062 }
10063 }
10064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010066
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010067 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010068}
10069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010071{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010072 if( transform == NULL )
10073 return;
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010076 deflateEnd( &transform->ctx_deflate );
10077 inflateEnd( &transform->ctx_inflate );
10078#endif
10079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10081 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010082
Hanno Beckerd56ed242018-01-03 15:32:51 +000010083#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 mbedtls_md_free( &transform->md_ctx_enc );
10085 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010086#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010087
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010088 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010089}
10090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091#if defined(MBEDTLS_X509_CRT_PARSE_C)
10092static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010095
10096 while( cur != NULL )
10097 {
10098 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010100 cur = next;
10101 }
10102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010104
Hanno Becker0271f962018-08-16 13:23:47 +010010105#if defined(MBEDTLS_SSL_PROTO_DTLS)
10106
10107static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10108{
10109 unsigned offset;
10110 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10111
10112 if( hs == NULL )
10113 return;
10114
Hanno Becker283f5ef2018-08-24 09:34:47 +010010115 ssl_free_buffered_record( ssl );
10116
Hanno Becker0271f962018-08-16 13:23:47 +010010117 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010118 ssl_buffering_free_slot( ssl, offset );
10119}
10120
10121static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10122 uint8_t slot )
10123{
10124 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10125 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010126
10127 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10128 return;
10129
Hanno Beckere605b192018-08-21 15:59:07 +010010130 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010131 {
Hanno Beckere605b192018-08-21 15:59:07 +010010132 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010133 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010134 mbedtls_free( hs_buf->data );
10135 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010136 }
10137}
10138
10139#endif /* MBEDTLS_SSL_PROTO_DTLS */
10140
Gilles Peskine9b562d52018-04-25 20:32:43 +020010141void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010142{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010143 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10144
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010145 if( handshake == NULL )
10146 return;
10147
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010148#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10149 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10150 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010151 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010152 handshake->async_in_progress = 0;
10153 }
10154#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10155
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010156#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10157 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10158 mbedtls_md5_free( &handshake->fin_md5 );
10159 mbedtls_sha1_free( &handshake->fin_sha1 );
10160#endif
10161#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10162#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010163#if defined(MBEDTLS_USE_PSA_CRYPTO)
10164 psa_hash_abort( &handshake->fin_sha256_psa );
10165#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010166 mbedtls_sha256_free( &handshake->fin_sha256 );
10167#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010168#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010169#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010170#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010171 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010172#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010173 mbedtls_sha512_free( &handshake->fin_sha512 );
10174#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010175#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178#if defined(MBEDTLS_DHM_C)
10179 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181#if defined(MBEDTLS_ECDH_C)
10182 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010183#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010184#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010185 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010186#if defined(MBEDTLS_SSL_CLI_C)
10187 mbedtls_free( handshake->ecjpake_cache );
10188 handshake->ecjpake_cache = NULL;
10189 handshake->ecjpake_cache_len = 0;
10190#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010191#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010192
Janos Follath4ae5c292016-02-10 11:27:43 +000010193#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10194 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010195 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010197#endif
10198
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010199#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10200 if( handshake->psk != NULL )
10201 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010202 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010203 mbedtls_free( handshake->psk );
10204 }
10205#endif
10206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10208 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010209 /*
10210 * Free only the linked list wrapper, not the keys themselves
10211 * since the belong to the SNI callback
10212 */
10213 if( handshake->sni_key_cert != NULL )
10214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010216
10217 while( cur != NULL )
10218 {
10219 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010221 cur = next;
10222 }
10223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010225
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010226#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010227 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010228 if( handshake->ecrs_peer_cert != NULL )
10229 {
10230 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10231 mbedtls_free( handshake->ecrs_peer_cert );
10232 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010233#endif
10234
Hanno Becker75173122019-02-06 16:18:31 +000010235#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10236 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10237 mbedtls_pk_free( &handshake->peer_pubkey );
10238#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240#if defined(MBEDTLS_SSL_PROTO_DTLS)
10241 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010242 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010243 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010244#endif
10245
Hanno Becker4a63ed42019-01-08 11:39:35 +000010246#if defined(MBEDTLS_ECDH_C) && \
10247 defined(MBEDTLS_USE_PSA_CRYPTO)
10248 psa_destroy_key( handshake->ecdh_psa_privkey );
10249#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10250
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010251 mbedtls_platform_zeroize( handshake,
10252 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010253}
10254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010256{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010257 if( session == NULL )
10258 return;
10259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010261 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010262#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010263
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010264#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010266#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010267
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010268 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010269}
10270
Paul Bakker5121ce52009-01-03 21:22:43 +000010271/*
10272 * Free an SSL context
10273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010275{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010276 if( ssl == NULL )
10277 return;
10278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010280
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010281 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010282 {
Angus Grattond8213d02016-05-25 20:56:48 +100010283 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010285 }
10286
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010287 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010288 {
Angus Grattond8213d02016-05-25 20:56:48 +100010289 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010291 }
10292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010294 if( ssl->compress_buf != NULL )
10295 {
Angus Grattond8213d02016-05-25 20:56:48 +100010296 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010298 }
10299#endif
10300
Paul Bakker48916f92012-09-16 19:57:18 +000010301 if( ssl->transform )
10302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 mbedtls_ssl_transform_free( ssl->transform );
10304 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010305 }
10306
10307 if( ssl->handshake )
10308 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010309 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10311 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 mbedtls_free( ssl->handshake );
10314 mbedtls_free( ssl->transform_negotiate );
10315 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010316 }
10317
Paul Bakkerc0463502013-02-14 11:19:38 +010010318 if( ssl->session )
10319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 mbedtls_ssl_session_free( ssl->session );
10321 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010322 }
10323
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010324#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010325 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010326 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010327 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010329 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10333 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10336 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010337 }
10338#endif
10339
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010340#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010342#endif
10343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010345
Paul Bakker86f04f42013-02-14 11:20:09 +010010346 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010347 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010348}
10349
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010350/*
10351 * Initialze mbedtls_ssl_config
10352 */
10353void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10354{
10355 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10356}
10357
Simon Butcherc97b6972015-12-27 23:48:17 +000010358#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010359static int ssl_preset_default_hashes[] = {
10360#if defined(MBEDTLS_SHA512_C)
10361 MBEDTLS_MD_SHA512,
10362 MBEDTLS_MD_SHA384,
10363#endif
10364#if defined(MBEDTLS_SHA256_C)
10365 MBEDTLS_MD_SHA256,
10366 MBEDTLS_MD_SHA224,
10367#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010368#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010369 MBEDTLS_MD_SHA1,
10370#endif
10371 MBEDTLS_MD_NONE
10372};
Simon Butcherc97b6972015-12-27 23:48:17 +000010373#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010374
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010375static int ssl_preset_suiteb_ciphersuites[] = {
10376 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10377 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10378 0
10379};
10380
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010381#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010382static int ssl_preset_suiteb_hashes[] = {
10383 MBEDTLS_MD_SHA256,
10384 MBEDTLS_MD_SHA384,
10385 MBEDTLS_MD_NONE
10386};
10387#endif
10388
10389#if defined(MBEDTLS_ECP_C)
10390static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10391 MBEDTLS_ECP_DP_SECP256R1,
10392 MBEDTLS_ECP_DP_SECP384R1,
10393 MBEDTLS_ECP_DP_NONE
10394};
10395#endif
10396
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010397/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010398 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010399 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010400int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010401 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010402{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010403#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010404 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010405#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010406
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010407 /* Use the functions here so that they are covered in tests,
10408 * but otherwise access member directly for efficiency */
10409 mbedtls_ssl_conf_endpoint( conf, endpoint );
10410 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010411
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010412 /*
10413 * Things that are common to all presets
10414 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010415#if defined(MBEDTLS_SSL_CLI_C)
10416 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10417 {
10418 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10420 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10421#endif
10422 }
10423#endif
10424
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010425#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010426 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010427#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010428
10429#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10430 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10431#endif
10432
10433#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10434 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10435#endif
10436
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010437#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10438 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10439#endif
10440
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010441#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010442 conf->f_cookie_write = ssl_cookie_write_dummy;
10443 conf->f_cookie_check = ssl_cookie_check_dummy;
10444#endif
10445
10446#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10447 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10448#endif
10449
Janos Follath088ce432017-04-10 12:42:31 +010010450#if defined(MBEDTLS_SSL_SRV_C)
10451 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10452#endif
10453
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010454#if defined(MBEDTLS_SSL_PROTO_DTLS)
10455 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10456 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10457#endif
10458
10459#if defined(MBEDTLS_SSL_RENEGOTIATION)
10460 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010461 memset( conf->renego_period, 0x00, 2 );
10462 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010463#endif
10464
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010465#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10466 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10467 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010468 const unsigned char dhm_p[] =
10469 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10470 const unsigned char dhm_g[] =
10471 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10472
Hanno Beckera90658f2017-10-04 15:29:08 +010010473 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10474 dhm_p, sizeof( dhm_p ),
10475 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010476 {
10477 return( ret );
10478 }
10479 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010480#endif
10481
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010482 /*
10483 * Preset-specific defaults
10484 */
10485 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010486 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010487 /*
10488 * NSA Suite B
10489 */
10490 case MBEDTLS_SSL_PRESET_SUITEB:
10491 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10492 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10493 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10494 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10495
10496 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10497 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10498 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10499 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10500 ssl_preset_suiteb_ciphersuites;
10501
10502#if defined(MBEDTLS_X509_CRT_PARSE_C)
10503 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010504#endif
10505
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010506#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010507 conf->sig_hashes = ssl_preset_suiteb_hashes;
10508#endif
10509
10510#if defined(MBEDTLS_ECP_C)
10511 conf->curve_list = ssl_preset_suiteb_curves;
10512#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010513 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010514
10515 /*
10516 * Default
10517 */
10518 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010519 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10520 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10521 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10522 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10523 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10524 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10525 MBEDTLS_SSL_MIN_MINOR_VERSION :
10526 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010527 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10528 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10529
10530#if defined(MBEDTLS_SSL_PROTO_DTLS)
10531 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10532 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10533#endif
10534
10535 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10536 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10537 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10538 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10539 mbedtls_ssl_list_ciphersuites();
10540
10541#if defined(MBEDTLS_X509_CRT_PARSE_C)
10542 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10543#endif
10544
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010545#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010546 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010547#endif
10548
10549#if defined(MBEDTLS_ECP_C)
10550 conf->curve_list = mbedtls_ecp_grp_id_list();
10551#endif
10552
10553#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10554 conf->dhm_min_bitlen = 1024;
10555#endif
10556 }
10557
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010558 return( 0 );
10559}
10560
10561/*
10562 * Free mbedtls_ssl_config
10563 */
10564void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10565{
10566#if defined(MBEDTLS_DHM_C)
10567 mbedtls_mpi_free( &conf->dhm_P );
10568 mbedtls_mpi_free( &conf->dhm_G );
10569#endif
10570
10571#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10572 if( conf->psk != NULL )
10573 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010574 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010575 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010576 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010577 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010578 }
10579
10580 if( conf->psk_identity != NULL )
10581 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010582 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010583 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010584 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010585 conf->psk_identity_len = 0;
10586 }
10587#endif
10588
10589#if defined(MBEDTLS_X509_CRT_PARSE_C)
10590 ssl_key_cert_free( conf->key_cert );
10591#endif
10592
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010593 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010594}
10595
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010596#if defined(MBEDTLS_PK_C) && \
10597 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010598/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603#if defined(MBEDTLS_RSA_C)
10604 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10605 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607#if defined(MBEDTLS_ECDSA_C)
10608 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10609 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010612}
10613
Hanno Becker7e5437a2017-04-28 17:15:26 +010010614unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10615{
10616 switch( type ) {
10617 case MBEDTLS_PK_RSA:
10618 return( MBEDTLS_SSL_SIG_RSA );
10619 case MBEDTLS_PK_ECDSA:
10620 case MBEDTLS_PK_ECKEY:
10621 return( MBEDTLS_SSL_SIG_ECDSA );
10622 default:
10623 return( MBEDTLS_SSL_SIG_ANON );
10624 }
10625}
10626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010628{
10629 switch( sig )
10630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631#if defined(MBEDTLS_RSA_C)
10632 case MBEDTLS_SSL_SIG_RSA:
10633 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#if defined(MBEDTLS_ECDSA_C)
10636 case MBEDTLS_SSL_SIG_ECDSA:
10637 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010638#endif
10639 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010641 }
10642}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010643#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010644
Hanno Becker7e5437a2017-04-28 17:15:26 +010010645#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10646 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10647
10648/* Find an entry in a signature-hash set matching a given hash algorithm. */
10649mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10650 mbedtls_pk_type_t sig_alg )
10651{
10652 switch( sig_alg )
10653 {
10654 case MBEDTLS_PK_RSA:
10655 return( set->rsa );
10656 case MBEDTLS_PK_ECDSA:
10657 return( set->ecdsa );
10658 default:
10659 return( MBEDTLS_MD_NONE );
10660 }
10661}
10662
10663/* Add a signature-hash-pair to a signature-hash set */
10664void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10665 mbedtls_pk_type_t sig_alg,
10666 mbedtls_md_type_t md_alg )
10667{
10668 switch( sig_alg )
10669 {
10670 case MBEDTLS_PK_RSA:
10671 if( set->rsa == MBEDTLS_MD_NONE )
10672 set->rsa = md_alg;
10673 break;
10674
10675 case MBEDTLS_PK_ECDSA:
10676 if( set->ecdsa == MBEDTLS_MD_NONE )
10677 set->ecdsa = md_alg;
10678 break;
10679
10680 default:
10681 break;
10682 }
10683}
10684
10685/* Allow exactly one hash algorithm for each signature. */
10686void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10687 mbedtls_md_type_t md_alg )
10688{
10689 set->rsa = md_alg;
10690 set->ecdsa = md_alg;
10691}
10692
10693#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10694 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10695
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010696/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010697 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010700{
10701 switch( hash )
10702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_MD5_C)
10704 case MBEDTLS_SSL_HASH_MD5:
10705 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707#if defined(MBEDTLS_SHA1_C)
10708 case MBEDTLS_SSL_HASH_SHA1:
10709 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711#if defined(MBEDTLS_SHA256_C)
10712 case MBEDTLS_SSL_HASH_SHA224:
10713 return( MBEDTLS_MD_SHA224 );
10714 case MBEDTLS_SSL_HASH_SHA256:
10715 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010717#if defined(MBEDTLS_SHA512_C)
10718 case MBEDTLS_SSL_HASH_SHA384:
10719 return( MBEDTLS_MD_SHA384 );
10720 case MBEDTLS_SSL_HASH_SHA512:
10721 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010722#endif
10723 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010725 }
10726}
10727
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010728/*
10729 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10730 */
10731unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10732{
10733 switch( md )
10734 {
10735#if defined(MBEDTLS_MD5_C)
10736 case MBEDTLS_MD_MD5:
10737 return( MBEDTLS_SSL_HASH_MD5 );
10738#endif
10739#if defined(MBEDTLS_SHA1_C)
10740 case MBEDTLS_MD_SHA1:
10741 return( MBEDTLS_SSL_HASH_SHA1 );
10742#endif
10743#if defined(MBEDTLS_SHA256_C)
10744 case MBEDTLS_MD_SHA224:
10745 return( MBEDTLS_SSL_HASH_SHA224 );
10746 case MBEDTLS_MD_SHA256:
10747 return( MBEDTLS_SSL_HASH_SHA256 );
10748#endif
10749#if defined(MBEDTLS_SHA512_C)
10750 case MBEDTLS_MD_SHA384:
10751 return( MBEDTLS_SSL_HASH_SHA384 );
10752 case MBEDTLS_MD_SHA512:
10753 return( MBEDTLS_SSL_HASH_SHA512 );
10754#endif
10755 default:
10756 return( MBEDTLS_SSL_HASH_NONE );
10757 }
10758}
10759
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010760#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010761/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010762 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010763 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010764 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010765int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010768
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010769 if( ssl->conf->curve_list == NULL )
10770 return( -1 );
10771
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010772 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010773 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010774 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010775
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010776 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010777}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010778#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010779
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010780#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010781/*
10782 * Check if a hash proposed by the peer is in our list.
10783 * Return 0 if we're willing to use it, -1 otherwise.
10784 */
10785int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10786 mbedtls_md_type_t md )
10787{
10788 const int *cur;
10789
10790 if( ssl->conf->sig_hashes == NULL )
10791 return( -1 );
10792
10793 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10794 if( *cur == (int) md )
10795 return( 0 );
10796
10797 return( -1 );
10798}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010799#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801#if defined(MBEDTLS_X509_CRT_PARSE_C)
10802int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10803 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010804 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010805 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010806{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010807 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010808#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010809 int usage = 0;
10810#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010812 const char *ext_oid;
10813 size_t ext_len;
10814#endif
10815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10817 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010818 ((void) cert);
10819 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010820 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010821#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010823#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10824 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010825 {
10826 /* Server part of the key exchange */
10827 switch( ciphersuite->key_exchange )
10828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010829 case MBEDTLS_KEY_EXCHANGE_RSA:
10830 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010831 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010832 break;
10833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10835 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10836 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10837 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010838 break;
10839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10841 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010842 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010843 break;
10844
10845 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846 case MBEDTLS_KEY_EXCHANGE_NONE:
10847 case MBEDTLS_KEY_EXCHANGE_PSK:
10848 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10849 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010850 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010851 usage = 0;
10852 }
10853 }
10854 else
10855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10857 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010858 }
10859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010861 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010862 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010863 ret = -1;
10864 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010865#else
10866 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10870 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10873 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010874 }
10875 else
10876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10878 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010879 }
10880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010882 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010883 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010884 ret = -1;
10885 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010887
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010888 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010890#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010891
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010892/*
10893 * Convert version numbers to/from wire format
10894 * and, for DTLS, to/from TLS equivalent.
10895 *
10896 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010897 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010898 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10899 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010902 unsigned char ver[2] )
10903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904#if defined(MBEDTLS_SSL_PROTO_DTLS)
10905 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10909
10910 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10911 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10912 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010913 else
10914#else
10915 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010916#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010917 {
10918 ver[0] = (unsigned char) major;
10919 ver[1] = (unsigned char) minor;
10920 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010921}
10922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010923void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010924 const unsigned char ver[2] )
10925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926#if defined(MBEDTLS_SSL_PROTO_DTLS)
10927 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010928 {
10929 *major = 255 - ver[0] + 2;
10930 *minor = 255 - ver[1] + 1;
10931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010933 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10934 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010935 else
10936#else
10937 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010938#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010939 {
10940 *major = ver[0];
10941 *minor = ver[1];
10942 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010943}
10944
Simon Butcher99000142016-10-13 17:21:01 +010010945int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10946{
10947#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10948 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10949 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10950
10951 switch( md )
10952 {
10953#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10954#if defined(MBEDTLS_MD5_C)
10955 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010956 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010957#endif
10958#if defined(MBEDTLS_SHA1_C)
10959 case MBEDTLS_SSL_HASH_SHA1:
10960 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10961 break;
10962#endif
10963#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10964#if defined(MBEDTLS_SHA512_C)
10965 case MBEDTLS_SSL_HASH_SHA384:
10966 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10967 break;
10968#endif
10969#if defined(MBEDTLS_SHA256_C)
10970 case MBEDTLS_SSL_HASH_SHA256:
10971 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10972 break;
10973#endif
10974 default:
10975 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10976 }
10977
10978 return 0;
10979#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10980 (void) ssl;
10981 (void) md;
10982
10983 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10985}
10986
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010987#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10988 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10989int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10990 unsigned char *output,
10991 unsigned char *data, size_t data_len )
10992{
10993 int ret = 0;
10994 mbedtls_md5_context mbedtls_md5;
10995 mbedtls_sha1_context mbedtls_sha1;
10996
10997 mbedtls_md5_init( &mbedtls_md5 );
10998 mbedtls_sha1_init( &mbedtls_sha1 );
10999
11000 /*
11001 * digitally-signed struct {
11002 * opaque md5_hash[16];
11003 * opaque sha_hash[20];
11004 * };
11005 *
11006 * md5_hash
11007 * MD5(ClientHello.random + ServerHello.random
11008 * + ServerParams);
11009 * sha_hash
11010 * SHA(ClientHello.random + ServerHello.random
11011 * + ServerParams);
11012 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011013 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011014 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011016 goto exit;
11017 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011018 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011019 ssl->handshake->randbytes, 64 ) ) != 0 )
11020 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011022 goto exit;
11023 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011024 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011025 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011027 goto exit;
11028 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011029 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011030 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011032 goto exit;
11033 }
11034
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011035 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011036 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011038 goto exit;
11039 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011040 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011041 ssl->handshake->randbytes, 64 ) ) != 0 )
11042 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011044 goto exit;
11045 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011046 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011047 data_len ) ) != 0 )
11048 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011050 goto exit;
11051 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011052 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011053 output + 16 ) ) != 0 )
11054 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011056 goto exit;
11057 }
11058
11059exit:
11060 mbedtls_md5_free( &mbedtls_md5 );
11061 mbedtls_sha1_free( &mbedtls_sha1 );
11062
11063 if( ret != 0 )
11064 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11065 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11066
11067 return( ret );
11068
11069}
11070#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11071 MBEDTLS_SSL_PROTO_TLS1_1 */
11072
11073#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11074 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011075
11076#if defined(MBEDTLS_USE_PSA_CRYPTO)
11077int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11078 unsigned char *hash, size_t *hashlen,
11079 unsigned char *data, size_t data_len,
11080 mbedtls_md_type_t md_alg )
11081{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011082 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011083 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011084 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11085
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011087
11088 if( ( status = psa_hash_setup( &hash_operation,
11089 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011090 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011091 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011092 goto exit;
11093 }
11094
Andrzej Kurek814feff2019-01-14 04:35:19 -050011095 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11096 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011097 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011098 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011099 goto exit;
11100 }
11101
Andrzej Kurek814feff2019-01-14 04:35:19 -050011102 if( ( status = psa_hash_update( &hash_operation,
11103 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011104 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011105 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011106 goto exit;
11107 }
11108
Andrzej Kurek814feff2019-01-14 04:35:19 -050011109 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11110 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011111 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011112 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011113 goto exit;
11114 }
11115
11116exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011117 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011118 {
11119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11120 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011121 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011122 {
11123 case PSA_ERROR_NOT_SUPPORTED:
11124 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011125 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011126 case PSA_ERROR_BUFFER_TOO_SMALL:
11127 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11128 case PSA_ERROR_INSUFFICIENT_MEMORY:
11129 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11130 default:
11131 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11132 }
11133 }
11134 return( 0 );
11135}
11136
11137#else
11138
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011139int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011140 unsigned char *hash, size_t *hashlen,
11141 unsigned char *data, size_t data_len,
11142 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011143{
11144 int ret = 0;
11145 mbedtls_md_context_t ctx;
11146 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011147 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011148
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011150
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011151 mbedtls_md_init( &ctx );
11152
11153 /*
11154 * digitally-signed struct {
11155 * opaque client_random[32];
11156 * opaque server_random[32];
11157 * ServerDHParams params;
11158 * };
11159 */
11160 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11161 {
11162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11163 goto exit;
11164 }
11165 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11166 {
11167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11168 goto exit;
11169 }
11170 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11171 {
11172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11173 goto exit;
11174 }
11175 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11176 {
11177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11178 goto exit;
11179 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011180 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181 {
11182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11183 goto exit;
11184 }
11185
11186exit:
11187 mbedtls_md_free( &ctx );
11188
11189 if( ret != 0 )
11190 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11191 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11192
11193 return( ret );
11194}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011195#endif /* MBEDTLS_USE_PSA_CRYPTO */
11196
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011197#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11198 MBEDTLS_SSL_PROTO_TLS1_2 */
11199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011200#endif /* MBEDTLS_SSL_TLS_C */