blob: 1df40725e8b7639247837585f6f67c05b8f66b80 [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
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010060/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020064 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010066#else
67 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068#endif
69 return( 0 );
70}
71
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072/*
73 * Start a timer.
74 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_set_timer == NULL )
79 return;
80
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
82 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083}
84
85/*
86 * Return -1 is timer is expired, 0 if it isn't.
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020090 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020091 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092
93 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
99 return( 0 );
100}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
103 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
Hanno Beckera5a2b082019-05-15 14:03:01 +0100111#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100112/* Top-level Connection ID API */
113
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100114int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
115 size_t len,
116 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100117{
118 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
120
Hanno Becker791ec6b2019-05-14 11:45:26 +0100121 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
122 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
123 {
124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100128 conf->cid_len = len;
129 return( 0 );
130}
131
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100132int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
133 int enable,
134 unsigned char const *own_cid,
135 size_t own_cid_len )
136{
Hanno Becker78c43022019-05-03 14:38:32 +0100137 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
139
Hanno Becker07489862019-04-25 16:01:49 +0100140 ssl->negotiate_cid = enable;
141 if( enable == MBEDTLS_SSL_CID_DISABLED )
142 {
143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
144 return( 0 );
145 }
146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100147 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100148
Hanno Beckereec2be92019-05-03 13:06:44 +0100149 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100150 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
152 (unsigned) own_cid_len,
153 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
155 }
156
157 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100158 /* Truncation is not an issue here because
159 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
160 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100161
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100162 return( 0 );
163}
164
165int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
166 int *enabled,
167 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
168 size_t *peer_cid_len )
169{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100170 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100171
Hanno Becker78c43022019-05-03 14:38:32 +0100172 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
173 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
174 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100176 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Hanno Beckercb063f52019-05-03 12:54:52 +0100178 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
179 * were used, but client and server requested the empty CID.
180 * This is indistinguishable from not using the CID extension
181 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100182 if( ssl->transform_in->in_cid_len == 0 &&
183 ssl->transform_in->out_cid_len == 0 )
184 {
185 return( 0 );
186 }
187
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 memcpy( peer_cid, ssl->transform_in->out_cid,
190 ssl->transform_in->out_cid_len );
191
192 *enabled = MBEDTLS_SSL_CID_ENABLED;
193
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100194 return( 0 );
195}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100197
Hanno Beckerd5847772018-08-28 10:09:23 +0100198/* Forward declarations for functions related to message buffering. */
199static void ssl_buffering_free( mbedtls_ssl_context *ssl );
200static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
201 uint8_t slot );
202static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
203static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
204static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
205static int ssl_buffer_message( mbedtls_ssl_context *ssl );
206static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100207static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100208
Hanno Beckera67dee22018-08-22 10:05:20 +0100209static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100210static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100211{
Hanno Becker11682cc2018-08-22 14:41:02 +0100212 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100213
214 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100215 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100216
217 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
218}
219
Hanno Becker67bc7c32018-08-06 11:33:50 +0100220static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
221{
Hanno Becker11682cc2018-08-22 14:41:02 +0100222 size_t const bytes_written = ssl->out_left;
223 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100224
225 /* Double-check that the write-index hasn't gone
226 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100228 {
229 /* Should never happen... */
230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
231 }
232
233 return( (int) ( mtu - bytes_written ) );
234}
235
236static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
237{
238 int ret;
239 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400240 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100241
242#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
243 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
244
245 if( max_len > mfl )
246 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100247
248 /* By the standard (RFC 6066 Sect. 4), the MFL extension
249 * only limits the maximum record payload size, so in theory
250 * we would be allowed to pack multiple records of payload size
251 * MFL into a single datagram. However, this would mean that there's
252 * no way to explicitly communicate MTU restrictions to the peer.
253 *
254 * The following reduction of max_len makes sure that we never
255 * write datagrams larger than MFL + Record Expansion Overhead.
256 */
257 if( max_len <= ssl->out_left )
258 return( 0 );
259
260 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100261#endif
262
263 ret = ssl_get_remaining_space_in_datagram( ssl );
264 if( ret < 0 )
265 return( ret );
266 remaining = (size_t) ret;
267
268 ret = mbedtls_ssl_get_record_expansion( ssl );
269 if( ret < 0 )
270 return( ret );
271 expansion = (size_t) ret;
272
273 if( remaining <= expansion )
274 return( 0 );
275
276 remaining -= expansion;
277 if( remaining >= max_len )
278 remaining = max_len;
279
280 return( (int) remaining );
281}
282
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283/*
284 * Double the retransmit timeout value, within the allowed range,
285 * returning -1 if the maximum value has already been reached.
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200288{
289 uint32_t new_timeout;
290
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200291 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200292 return( -1 );
293
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200294 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
295 * in the following way: after the initial transmission and a first
296 * retransmission, back off to a temporary estimated MTU of 508 bytes.
297 * This value is guaranteed to be deliverable (if not guaranteed to be
298 * delivered) of any compliant IPv4 (and IPv6) network, and should work
299 * on most non-IP stacks too. */
300 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400301 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200302 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
304 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200305
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200306 new_timeout = 2 * ssl->handshake->retransmit_timeout;
307
308 /* Avoid arithmetic overflow and range overflow */
309 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200310 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200311 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200312 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313 }
314
315 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200317 ssl->handshake->retransmit_timeout ) );
318
319 return( 0 );
320}
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200326 ssl->handshake->retransmit_timeout ) );
327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200331/*
332 * Convert max_fragment_length codes to length.
333 * RFC 6066 says:
334 * enum{
335 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
336 * } MaxFragmentLength;
337 * and we add 0 -> extension unused
338 */
Angus Grattond8213d02016-05-25 20:56:48 +1000339static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200340{
Angus Grattond8213d02016-05-25 20:56:48 +1000341 switch( mfl )
342 {
343 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
344 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
345 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
346 return 512;
347 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
348 return 1024;
349 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
350 return 2048;
351 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
352 return 4096;
353 default:
354 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
355 }
356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200358
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200359#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_ssl_session_free( dst );
363 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200366 if( src->peer_cert != NULL )
367 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200368 int ret;
369
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200370 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200371 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200372 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200377 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 dst->peer_cert = NULL;
381 return( ret );
382 }
383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200386#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 if( src->ticket != NULL )
388 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200389 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200390 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200391 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
393 memcpy( dst->ticket, src->ticket, src->ticket_len );
394 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200395#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200396
397 return( 0 );
398}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200399#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
402int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200403 const unsigned char *key_enc, const unsigned char *key_dec,
404 size_t keylen,
405 const unsigned char *iv_enc, const unsigned char *iv_dec,
406 size_t ivlen,
407 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200408 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
410int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
411int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
412int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
413int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
414#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000415
Paul Bakker5121ce52009-01-03 21:22:43 +0000416/*
417 * Key material generation
418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int ssl3_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000423 unsigned char *dstbuf, size_t dlen )
424{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100425 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000426 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200427 mbedtls_md5_context md5;
428 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000429 unsigned char padding[16];
430 unsigned char sha1sum[20];
431 ((void)label);
432
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200433 mbedtls_md5_init( &md5 );
434 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200435
Paul Bakker5f70b252012-09-13 14:23:06 +0000436 /*
437 * SSLv3:
438 * block =
439 * MD5( secret + SHA1( 'A' + secret + random ) ) +
440 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
441 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
442 * ...
443 */
444 for( i = 0; i < dlen / 16; i++ )
445 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200446 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000447
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100448 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100449 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100450 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100451 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100452 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100453 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100456 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100457 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000467 }
468
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200470 mbedtls_md5_free( &md5 );
471 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000472
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500473 mbedtls_platform_zeroize( padding, sizeof( padding ) );
474 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200481static int tls1_prf( const unsigned char *secret, size_t slen,
482 const char *label,
483 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000484 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000485{
Paul Bakker23986e52011-04-24 08:57:21 +0000486 size_t nb, hs;
487 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200488 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 unsigned char tmp[128];
490 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 const mbedtls_md_info_t *md_info;
492 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100493 int ret;
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 hs = ( slen + 1 ) / 2;
501 S1 = secret;
502 S2 = secret + slen - hs;
503
504 nb = strlen( label );
505 memcpy( tmp + 20, label, nb );
506 memcpy( tmp + 20 + nb, random, rlen );
507 nb += rlen;
508
509 /*
510 * First compute P_md5(secret,label+random)[0..dlen]
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100516 return( ret );
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
519 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
520 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 for( i = 0; i < dlen; i += 16 )
523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_md_hmac_reset ( &md_ctx );
525 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
526 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_md_hmac_reset ( &md_ctx );
529 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
530 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
533
534 for( j = 0; j < k; j++ )
535 dstbuf[i + j] = h_i[j];
536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 /*
541 * XOR out with P_sha1(secret,label+random)[0..dlen]
542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100547 return( ret );
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
550 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
551 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 for( i = 0; i < dlen; i += 20 )
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
557 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_md_hmac_reset ( &md_ctx );
560 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
561 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
564
565 for( j = 0; j < k; j++ )
566 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
567 }
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500571 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
572 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 return( 0 );
575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
579static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100580 const unsigned char *secret, size_t slen,
581 const char *label,
582 const unsigned char *random, size_t rlen,
583 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000584{
585 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100586 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000587 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
589 const mbedtls_md_info_t *md_info;
590 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100591 int ret;
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100599
600 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602
603 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100604 memcpy( tmp + md_len, label, nb );
605 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000606 nb += rlen;
607
608 /*
609 * Compute P_<hash>(secret, label + random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100612 return( ret );
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
615 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
616 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100617
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100618 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_hmac_reset ( &md_ctx );
621 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
622 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_md_hmac_reset ( &md_ctx );
625 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
626 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100628 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000629
630 for( j = 0; j < k; j++ )
631 dstbuf[i + j] = h_i[j];
632 }
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100635
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500636 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
637 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
639 return( 0 );
640}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100643static int tls_prf_sha256( const unsigned char *secret, size_t slen,
644 const char *label,
645 const unsigned char *random, size_t rlen,
646 unsigned char *dstbuf, size_t dlen )
647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100649 label, random, rlen, dstbuf, dlen ) );
650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654static int tls_prf_sha384( const unsigned char *secret, size_t slen,
655 const char *label,
656 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000657 unsigned char *dstbuf, size_t dlen )
658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100660 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_SHA512_C */
663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
668 defined(MBEDTLS_SSL_PROTO_TLS1_1)
669static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670#endif
Paul Bakker380da532012-04-18 16:10:25 +0000671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_SSL_PROTO_SSL3)
673static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
674static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675#endif
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
678static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
679static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200680#endif
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
683#if defined(MBEDTLS_SHA256_C)
684static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
685static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
686static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SHA512_C)
690static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
691static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
692static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000697{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200698 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 unsigned char keyblk[256];
701 unsigned char *key1;
702 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100703 unsigned char *mac_enc;
704 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000705 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200706 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000707 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000708 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 const mbedtls_cipher_info_t *cipher_info;
710 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ssl_session *session = ssl->session_negotiate;
713 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
714 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Hanno Becker3307b532017-12-27 21:37:21 +0000718#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
719 transform->encrypt_then_mac = session->encrypt_then_mac;
720#endif
721 transform->minor_ver = ssl->minor_ver;
722
Hanno Becker8759e162017-12-27 21:34:08 +0000723 ciphersuite_info = handshake->ciphersuite_info;
724 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100725 if( cipher_info == NULL )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000728 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100730 }
731
Hanno Becker8759e162017-12-27 21:34:08 +0000732 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100733 if( md_info == NULL )
734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000736 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100738 }
739
Hanno Beckera5a2b082019-05-15 14:03:01 +0100740#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100741 /* Copy own and peer's CID if the use of the CID
742 * extension has been negotiated. */
743 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
744 {
745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100746
Hanno Becker4932f9f2019-05-03 15:23:51 +0100747 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100748 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100749 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100750 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100751
752 transform->out_cid_len = ssl->handshake->peer_cid_len;
753 memcpy( transform->out_cid, ssl->handshake->peer_cid,
754 ssl->handshake->peer_cid_len );
755 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
756 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100757 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100758#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100759
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000761 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_SSL_PROTO_SSL3)
764 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765 {
Paul Bakker48916f92012-09-16 19:57:18 +0000766 handshake->tls_prf = ssl3_prf;
767 handshake->calc_verify = ssl_calc_verify_ssl;
768 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000769 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200770 else
771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
773 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000774 {
Paul Bakker48916f92012-09-16 19:57:18 +0000775 handshake->tls_prf = tls1_prf;
776 handshake->calc_verify = ssl_calc_verify_tls;
777 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000778 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200779 else
780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
782#if defined(MBEDTLS_SHA512_C)
783 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker8759e162017-12-27 21:34:08 +0000784 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 {
Paul Bakker48916f92012-09-16 19:57:18 +0000786 handshake->tls_prf = tls_prf_sha384;
787 handshake->calc_verify = ssl_calc_verify_tls_sha384;
788 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000790 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SHA256_C)
793 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000794 {
Paul Bakker48916f92012-09-16 19:57:18 +0000795 handshake->tls_prf = tls_prf_sha256;
796 handshake->calc_verify = ssl_calc_verify_tls_sha256;
797 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000798 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200799 else
800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200805 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 * SSLv3:
809 * master =
810 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
811 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
812 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200813 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200814 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 * master = PRF( premaster, "master secret", randbytes )[0..47]
816 */
Paul Bakker0a597072012-09-25 21:55:46 +0000817 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000820 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
823 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200824 {
825 unsigned char session_hash[48];
826 size_t hash_len;
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200829
830 ssl->handshake->calc_verify( ssl, session_hash );
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
833 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#if defined(MBEDTLS_SHA512_C)
Hanno Becker8759e162017-12-27 21:34:08 +0000836 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200837 {
838 hash_len = 48;
839 }
840 else
841#endif
842 hash_len = 32;
843 }
844 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200846 hash_len = 36;
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200849
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100850 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
851 "extended master secret",
852 session_hash, hash_len,
853 session->master, 48 );
854 if( ret != 0 )
855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100857 return( ret );
858 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200859
860 }
861 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200862#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100863 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
864 "master secret",
865 handshake->randbytes, 64,
866 session->master, 48 );
867 if( ret != 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100870 return( ret );
871 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200872
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500873 mbedtls_platform_zeroize( handshake->premaster,
874 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 }
876 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
879 /*
880 * Swap the client and server random values.
881 */
Paul Bakker48916f92012-09-16 19:57:18 +0000882 memcpy( tmp, handshake->randbytes, 64 );
883 memcpy( handshake->randbytes, tmp + 32, 32 );
884 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500885 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
887 /*
888 * SSLv3:
889 * key block =
890 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
891 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
892 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
893 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
894 * ...
895 *
896 * TLSv1:
897 * key block = PRF( master, "key expansion", randbytes )
898 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100899 ret = handshake->tls_prf( session->master, 48, "key expansion",
900 handshake->randbytes, 64, keyblk, 256 );
901 if( ret != 0 )
902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100904 return( ret );
905 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
908 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
909 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
910 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
911 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500913 mbedtls_platform_zeroize( handshake->randbytes,
914 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
916 /*
917 * Determine the appropriate key, IV and MAC length.
918 */
Paul Bakker68884e32013-01-07 18:20:04 +0100919
Hanno Beckere7f2df02017-12-27 08:17:40 +0000920 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200921
Hanno Beckerf1229442018-01-03 15:32:31 +0000922#if defined(MBEDTLS_GCM_C) || \
923 defined(MBEDTLS_CCM_C) || \
924 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200926 cipher_info->mode == MBEDTLS_MODE_CCM ||
927 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 {
Hanno Becker8759e162017-12-27 21:34:08 +0000929 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200930
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000932 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000933 transform->taglen =
934 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200935
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200936 /* All modes haves 96-bit IVs;
937 * GCM and CCM has 4 implicit and 8 explicit bytes
938 * ChachaPoly has all 12 bytes implicit
939 */
Paul Bakker68884e32013-01-07 18:20:04 +0100940 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200941 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
942 transform->fixed_ivlen = 12;
943 else
944 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200945
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200946 /* Minimum length of encrypted record */
947 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000948 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100949 }
950 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000951#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
952#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
953 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
954 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100955 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200956 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
958 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200961 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100962 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200964 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000965 mac_key_len = mbedtls_md_get_size( md_info );
966 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200969 /*
970 * If HMAC is to be truncated, we shall keep the leftmost bytes,
971 * (rfc 6066 page 13 or rfc 2104 section 4),
972 * so we only need to adjust the length here.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000977
978#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
979 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000980 * HMAC implementation which also truncates the key
981 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000982 mac_key_len = transform->maclen;
983#endif
984 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200986
987 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100988 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200990 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200992 transform->minlen = transform->maclen;
993 else
Paul Bakker68884e32013-01-07 18:20:04 +0100994 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200995 /*
996 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100997 * 1. if EtM is in use: one block plus MAC
998 * otherwise: * first multiple of blocklen greater than maclen
999 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1002 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001003 {
1004 transform->minlen = transform->maclen
1005 + cipher_info->block_size;
1006 }
1007 else
1008#endif
1009 {
1010 transform->minlen = transform->maclen
1011 + cipher_info->block_size
1012 - transform->maclen % cipher_info->block_size;
1013 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1016 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1017 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001018 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001019 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1022 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1023 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001024 {
1025 transform->minlen += transform->ivlen;
1026 }
1027 else
1028#endif
1029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001032 }
Paul Bakker68884e32013-01-07 18:20:04 +01001033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001035 else
1036#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1037 {
1038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Hanno Beckere7f2df02017-12-27 08:17:40 +00001042 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1043 (unsigned) keylen,
1044 (unsigned) transform->minlen,
1045 (unsigned) transform->ivlen,
1046 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
1048 /*
1049 * Finally setup the cipher contexts, IVs and MAC secrets.
1050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001052 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001054 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001055 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Paul Bakker68884e32013-01-07 18:20:04 +01001057 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001058 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001060 /*
1061 * This is not used in TLS v1.1.
1062 */
Paul Bakker48916f92012-09-16 19:57:18 +00001063 iv_copy_len = ( transform->fixed_ivlen ) ?
1064 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001065 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1066 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001067 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 }
1069 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#endif /* MBEDTLS_SSL_CLI_C */
1071#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001072 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001074 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001075 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Hanno Becker81c7b182017-11-09 18:39:33 +00001077 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001078 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001080 /*
1081 * This is not used in TLS v1.1.
1082 */
Paul Bakker48916f92012-09-16 19:57:18 +00001083 iv_copy_len = ( transform->fixed_ivlen ) ?
1084 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001085 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1086 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Hanno Becker92231322018-01-03 15:32:51 +00001096#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SSL_PROTO_SSL3)
1098 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001099 {
Hanno Becker92231322018-01-03 15:32:51 +00001100 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001104 }
1105
Hanno Becker81c7b182017-11-09 18:39:33 +00001106 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1107 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001108 }
1109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1111#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1112 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1113 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001114 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001115 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1116 For AEAD-based ciphersuites, there is nothing to do here. */
1117 if( mac_key_len != 0 )
1118 {
1119 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1120 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1121 }
Paul Bakker68884e32013-01-07 18:20:04 +01001122 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001123 else
1124#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001128 }
Hanno Becker92231322018-01-03 15:32:51 +00001129#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1132 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001133 {
1134 int ret = 0;
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001137
Hanno Beckere7f2df02017-12-27 08:17:40 +00001138 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001139 transform->iv_enc, transform->iv_dec,
1140 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001141 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001142 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1145 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001146 }
1147 }
Hanno Becker92231322018-01-03 15:32:51 +00001148#else
1149 ((void) mac_dec);
1150 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001152
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001153#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1154 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001155 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001156 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1157 session->master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001158 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001159 iv_copy_len );
1160 }
1161#endif
1162
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001163 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001164 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001167 return( ret );
1168 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001169
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001170 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001171 cipher_info ) ) != 0 )
1172 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001174 return( ret );
1175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001178 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001182 return( ret );
1183 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001186 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190 return( ret );
1191 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_CIPHER_MODE_CBC)
1194 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1197 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001200 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001201 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1204 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001207 return( ret );
1208 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001212 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001215 // Initialize compression
1216 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001218 {
Paul Bakker16770332013-10-11 09:59:44 +02001219 if( ssl->compress_buf == NULL )
1220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001222 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001223 if( ssl->compress_buf == NULL )
1224 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001226 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001227 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001228 }
1229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001232
Paul Bakker48916f92012-09-16 19:57:18 +00001233 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1234 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001235
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 if( deflateInit( &transform->ctx_deflate,
1237 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001238 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1241 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001242 }
1243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 return( 0 );
1249}
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_PROTO_SSL3)
1252void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001254 mbedtls_md5_context md5;
1255 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 unsigned char pad_1[48];
1257 unsigned char pad_2[48];
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001261 mbedtls_md5_init( &md5 );
1262 mbedtls_sha1_init( &sha1 );
1263
1264 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1265 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Paul Bakker380da532012-04-18 16:10:25 +00001267 memset( pad_1, 0x36, 48 );
1268 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001270 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1271 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1272 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001274 mbedtls_md5_starts_ret( &md5 );
1275 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1276 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1277 mbedtls_md5_update_ret( &md5, hash, 16 );
1278 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001280 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1281 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1282 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001283
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001284 mbedtls_sha1_starts_ret( &sha1 );
1285 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1286 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1287 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1288 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001292
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001293 mbedtls_md5_free( &md5 );
1294 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001295
Paul Bakker380da532012-04-18 16:10:25 +00001296 return;
1297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1301void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001302{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001303 mbedtls_md5_context md5;
1304 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001307
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001308 mbedtls_md5_init( &md5 );
1309 mbedtls_sha1_init( &sha1 );
1310
1311 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1312 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001313
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001314 mbedtls_md5_finish_ret( &md5, hash );
1315 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001319
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001320 mbedtls_md5_free( &md5 );
1321 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001322
Paul Bakker380da532012-04-18 16:10:25 +00001323 return;
1324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1328#if defined(MBEDTLS_SHA256_C)
1329void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001330{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001331 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001332
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001333 mbedtls_sha256_init( &sha256 );
1334
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001336
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001337 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001338 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001342
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001343 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001344
Paul Bakker380da532012-04-18 16:10:25 +00001345 return;
1346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SHA512_C)
1350void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001351{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001352 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001353
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001354 mbedtls_sha512_init( &sha512 );
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001357
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001358 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001359 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001364 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001365
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 return;
1367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#endif /* MBEDTLS_SHA512_C */
1369#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1372int 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 +02001373{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001374 unsigned char *p = ssl->handshake->premaster;
1375 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001376 const unsigned char *psk = ssl->conf->psk;
1377 size_t psk_len = ssl->conf->psk_len;
1378
1379 /* If the psk callback was called, use its result */
1380 if( ssl->handshake->psk != NULL )
1381 {
1382 psk = ssl->handshake->psk;
1383 psk_len = ssl->handshake->psk_len;
1384 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001385
1386 /*
1387 * PMS = struct {
1388 * opaque other_secret<0..2^16-1>;
1389 * opaque psk<0..2^16-1>;
1390 * };
1391 * with "other_secret" depending on the particular key exchange
1392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1394 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001395 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001396 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001398
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001399 *(p++) = (unsigned char)( psk_len >> 8 );
1400 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001401
1402 if( end < p || (size_t)( end - p ) < psk_len )
1403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1404
1405 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001406 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001407 }
1408 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1410#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1411 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001412 {
1413 /*
1414 * other_secret already set by the ClientKeyExchange message,
1415 * and is 48 bytes long
1416 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001417 if( end - p < 2 )
1418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1419
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001420 *p++ = 0;
1421 *p++ = 48;
1422 p += 48;
1423 }
1424 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1426#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1427 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001428 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001429 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001430 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001431
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001432 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001434 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001435 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001438 return( ret );
1439 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001440 *(p++) = (unsigned char)( len >> 8 );
1441 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001442 p += len;
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001445 }
1446 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1448#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1449 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001450 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001451 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001452 size_t zlen;
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001455 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001456 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001459 return( ret );
1460 }
1461
1462 *(p++) = (unsigned char)( zlen >> 8 );
1463 *(p++) = (unsigned char)( zlen );
1464 p += zlen;
1465
Janos Follath3fbdada2018-08-15 10:26:53 +01001466 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1467 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001468 }
1469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001474 }
1475
1476 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001477 if( end - p < 2 )
1478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001479
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001480 *(p++) = (unsigned char)( psk_len >> 8 );
1481 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001482
1483 if( end < p || (size_t)( end - p ) < psk_len )
1484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1485
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001486 memcpy( p, psk, psk_len );
1487 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001488
1489 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1490
1491 return( 0 );
1492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001496/*
1497 * SSLv3.0 MAC functions
1498 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001499#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001500static void ssl_mac( mbedtls_md_context_t *md_ctx,
1501 const unsigned char *secret,
1502 const unsigned char *buf, size_t len,
1503 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001504 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505{
1506 unsigned char header[11];
1507 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001508 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1510 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001511
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001512 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001514 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001515 else
Paul Bakker68884e32013-01-07 18:20:04 +01001516 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
1518 memcpy( header, ctr, 8 );
1519 header[ 8] = (unsigned char) type;
1520 header[ 9] = (unsigned char)( len >> 8 );
1521 header[10] = (unsigned char)( len );
1522
Paul Bakker68884e32013-01-07 18:20:04 +01001523 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_md_starts( md_ctx );
1525 mbedtls_md_update( md_ctx, secret, md_size );
1526 mbedtls_md_update( md_ctx, padding, padlen );
1527 mbedtls_md_update( md_ctx, header, 11 );
1528 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001529 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Paul Bakker68884e32013-01-07 18:20:04 +01001531 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_md_starts( md_ctx );
1533 mbedtls_md_update( md_ctx, secret, md_size );
1534 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001535 mbedtls_md_update( md_ctx, out, md_size );
1536 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001539
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001540/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001541 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001542#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001543 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1544 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1545 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1546/* This function makes sure every byte in the memory region is accessed
1547 * (in ascending addresses order) */
1548static void ssl_read_memory( unsigned char *p, size_t len )
1549{
1550 unsigned char acc = 0;
1551 volatile unsigned char force;
1552
1553 for( ; len != 0; p++, len-- )
1554 acc ^= *p;
1555
1556 force = acc;
1557 (void) force;
1558}
1559#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1560
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001561/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001563 */
Hanno Becker3307b532017-12-27 21:37:21 +00001564
Hanno Beckera5a2b082019-05-15 14:03:01 +01001565#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001566/* This functions transforms a DTLS plaintext fragment and a record content
1567 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001568 *
1569 * struct {
1570 * opaque content[DTLSPlaintext.length];
1571 * ContentType real_type;
1572 * uint8 zeros[length_of_padding];
1573 * } DTLSInnerPlaintext;
1574 *
1575 * Input:
1576 * - `content`: The beginning of the buffer holding the
1577 * plaintext to be wrapped.
1578 * - `*content_size`: The length of the plaintext in Bytes.
1579 * - `max_len`: The number of Bytes available starting from
1580 * `content`. This must be `>= *content_size`.
1581 * - `rec_type`: The desired record content type.
1582 *
1583 * Output:
1584 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1585 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1586 *
1587 * Returns:
1588 * - `0` on success.
1589 * - A negative error code if `max_len` didn't offer enough space
1590 * for the expansion.
1591 */
1592static int ssl_cid_build_inner_plaintext( unsigned char *content,
1593 size_t *content_size,
1594 size_t remaining,
1595 uint8_t rec_type )
1596{
1597 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001598 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1599 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1600 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001601
1602 /* Write real content type */
1603 if( remaining == 0 )
1604 return( -1 );
1605 content[ len ] = rec_type;
1606 len++;
1607 remaining--;
1608
1609 if( remaining < pad )
1610 return( -1 );
1611 memset( content + len, 0, pad );
1612 len += pad;
1613 remaining -= pad;
1614
1615 *content_size = len;
1616 return( 0 );
1617}
1618
Hanno Becker7dc25772019-05-20 15:08:01 +01001619/* This function parses a DTLSInnerPlaintext structure.
1620 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001621static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1622 size_t *content_size,
1623 uint8_t *rec_type )
1624{
1625 size_t remaining = *content_size;
1626
1627 /* Determine length of padding by skipping zeroes from the back. */
1628 do
1629 {
1630 if( remaining == 0 )
1631 return( -1 );
1632 remaining--;
1633 } while( content[ remaining ] == 0 );
1634
1635 *content_size = remaining;
1636 *rec_type = content[ remaining ];
1637
1638 return( 0 );
1639}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001640#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001641
Hanno Becker99abf512019-05-20 14:50:53 +01001642/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001643 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001644static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001645 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001646 mbedtls_record *rec )
1647{
Hanno Becker99abf512019-05-20 14:50:53 +01001648 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001649 *
1650 * additional_data = seq_num + TLSCompressed.type +
1651 * TLSCompressed.version + TLSCompressed.length;
1652 *
Hanno Becker99abf512019-05-20 14:50:53 +01001653 * For the CID extension, this is extended as follows
1654 * (quoting draft-ietf-tls-dtls-connection-id-05,
1655 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001656 *
1657 * additional_data = seq_num + DTLSPlaintext.type +
1658 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001659 * cid +
1660 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001661 * length_of_DTLSInnerPlaintext;
1662 */
1663
Hanno Becker3307b532017-12-27 21:37:21 +00001664 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1665 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001666 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001667
Hanno Beckera5a2b082019-05-15 14:03:01 +01001668#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001669 if( rec->cid_len != 0 )
1670 {
1671 memcpy( add_data + 11, rec->cid, rec->cid_len );
1672 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1673 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1674 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1675 *add_data_len = 13 + 1 + rec->cid_len;
1676 }
1677 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001678#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001679 {
1680 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1681 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1682 *add_data_len = 13;
1683 }
Hanno Becker3307b532017-12-27 21:37:21 +00001684}
1685
Hanno Becker611a83b2018-01-03 14:27:32 +00001686int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1687 mbedtls_ssl_transform *transform,
1688 mbedtls_record *rec,
1689 int (*f_rng)(void *, unsigned char *, size_t),
1690 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001693 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001694 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001695 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001696 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001697 size_t post_avail;
1698
1699 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001700#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker3307b532017-12-27 21:37:21 +00001701 ((void) ssl);
1702#endif
1703
1704 /* The PRNG is used for dynamic IV generation that's used
1705 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1706#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1707 ( defined(MBEDTLS_AES_C) || \
1708 defined(MBEDTLS_ARIA_C) || \
1709 defined(MBEDTLS_CAMELLIA_C) ) && \
1710 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1711 ((void) f_rng);
1712 ((void) p_rng);
1713#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Hanno Becker3307b532017-12-27 21:37:21 +00001717 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001718 {
Hanno Becker3307b532017-12-27 21:37:21 +00001719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1721 }
Hanno Becker505089d2019-05-01 09:45:57 +01001722 if( rec == NULL
1723 || rec->buf == NULL
1724 || rec->buf_len < rec->data_offset
1725 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001726#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001727 || rec->cid_len != 0
1728#endif
1729 )
Hanno Becker3307b532017-12-27 21:37:21 +00001730 {
1731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001733 }
1734
Hanno Becker3307b532017-12-27 21:37:21 +00001735 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001736 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001738 data, rec->data_len );
1739
1740 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1741
1742 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1743 {
1744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1745 (unsigned) rec->data_len,
1746 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1748 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001749
Hanno Beckera5a2b082019-05-15 14:03:01 +01001750#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001751 /*
1752 * Add CID information
1753 */
1754 rec->cid_len = transform->out_cid_len;
1755 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1756 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001757
1758 if( rec->cid_len != 0 )
1759 {
1760 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001761 * Wrap plaintext into DTLSInnerPlaintext structure.
1762 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001763 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001764 * Note that this changes `rec->data_len`, and hence
1765 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001766 */
1767 if( ssl_cid_build_inner_plaintext( data,
1768 &rec->data_len,
1769 post_avail,
1770 rec->type ) != 0 )
1771 {
1772 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1773 }
1774
1775 rec->type = MBEDTLS_SSL_MSG_CID;
1776 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001777#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001778
Hanno Becker92c930f2019-04-29 17:31:37 +01001779 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1780
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001782 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001784#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 if( mode == MBEDTLS_MODE_STREAM ||
1786 ( mode == MBEDTLS_MODE_CBC
1787#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001788 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001789#endif
1790 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 {
Hanno Becker3307b532017-12-27 21:37:21 +00001792 if( post_avail < transform->maclen )
1793 {
1794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1795 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1796 }
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001799 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001800 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001801 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001802 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1803 data, rec->data_len, rec->ctr, rec->type, mac );
1804 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001805 }
1806 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001807#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1809 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001810 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001811 {
Hanno Becker992b6872017-11-09 18:57:39 +00001812 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1813
Hanno Beckere83efe62019-04-29 13:52:53 +01001814 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001815
Hanno Becker3307b532017-12-27 21:37:21 +00001816 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001817 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001818 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1819 data, rec->data_len );
1820 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1821 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1822
1823 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 }
1825 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001826#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 }
1831
Hanno Becker3307b532017-12-27 21:37:21 +00001832 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1833 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001834
Hanno Becker3307b532017-12-27 21:37:21 +00001835 rec->data_len += transform->maclen;
1836 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001837 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001838 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001839#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 /*
1842 * Encrypt
1843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1845 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001847 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001848 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001850 "including %d bytes of padding",
1851 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Hanno Becker3307b532017-12-27 21:37:21 +00001853 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1854 transform->iv_enc, transform->ivlen,
1855 data, rec->data_len,
1856 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001859 return( ret );
1860 }
1861
Hanno Becker3307b532017-12-27 21:37:21 +00001862 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 }
Paul Bakker68884e32013-01-07 18:20:04 +01001868 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00001870
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001871#if defined(MBEDTLS_GCM_C) || \
1872 defined(MBEDTLS_CCM_C) || \
1873 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001875 mode == MBEDTLS_MODE_CCM ||
1876 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001877 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001878 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00001880 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001881
Hanno Becker3307b532017-12-27 21:37:21 +00001882 /* Check that there's space for both the authentication tag
1883 * and the explicit IV before and after the record content. */
1884 if( post_avail < transform->taglen ||
1885 rec->data_offset < explicit_iv_len )
1886 {
1887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1888 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1889 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001890
Paul Bakker68884e32013-01-07 18:20:04 +01001891 /*
1892 * Generate IV
1893 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001894 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1895 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001896 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001897 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00001898 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1899 explicit_iv_len );
1900 /* Prefix record content with explicit IV. */
1901 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001902 }
1903 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1904 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001905 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001906 unsigned char i;
1907
1908 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1909
1910 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001911 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001912 }
1913 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001914 {
1915 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001918 }
1919
Hanno Beckere83efe62019-04-29 13:52:53 +01001920 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01001921
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001922 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1923 iv, transform->ivlen );
1924 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00001925 data - explicit_iv_len, explicit_iv_len );
1926 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01001927 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001929 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00001930 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001931
Paul Bakker68884e32013-01-07 18:20:04 +01001932 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001933 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001934 */
Hanno Becker3307b532017-12-27 21:37:21 +00001935
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001936 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00001937 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01001938 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00001939 data, rec->data_len, /* source */
1940 data, &rec->data_len, /* destination */
1941 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001944 return( ret );
1945 }
1946
Hanno Becker3307b532017-12-27 21:37:21 +00001947 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1948 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001949
Hanno Becker3307b532017-12-27 21:37:21 +00001950 rec->data_len += transform->taglen + explicit_iv_len;
1951 rec->data_offset -= explicit_iv_len;
1952 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001953 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1957#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001958 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001961 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001962 size_t padlen, i;
1963 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001964
Hanno Becker3307b532017-12-27 21:37:21 +00001965 /* Currently we're always using minimal padding
1966 * (up to 255 bytes would be allowed). */
1967 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1968 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 padlen = 0;
1970
Hanno Becker3307b532017-12-27 21:37:21 +00001971 /* Check there's enough space in the buffer for the padding. */
1972 if( post_avail < padlen + 1 )
1973 {
1974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1975 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1976 }
1977
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001979 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Hanno Becker3307b532017-12-27 21:37:21 +00001981 rec->data_len += padlen + 1;
1982 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001985 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001986 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1987 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001988 */
Hanno Becker3307b532017-12-27 21:37:21 +00001989 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001990 {
Hanno Becker3307b532017-12-27 21:37:21 +00001991 if( f_rng == NULL )
1992 {
1993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
1994 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1995 }
1996
1997 if( rec->data_offset < transform->ivlen )
1998 {
1999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2000 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2001 }
2002
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002003 /*
2004 * Generate IV
2005 */
Hanno Becker3307b532017-12-27 21:37:21 +00002006 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002007 if( ret != 0 )
2008 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009
Hanno Becker3307b532017-12-27 21:37:21 +00002010 memcpy( data - transform->ivlen, transform->iv_enc,
2011 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002012
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002017 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002018 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002019 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Hanno Becker3307b532017-12-27 21:37:21 +00002021 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2022 transform->iv_enc,
2023 transform->ivlen,
2024 data, rec->data_len,
2025 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002028 return( ret );
2029 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002030
Hanno Becker3307b532017-12-27 21:37:21 +00002031 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002035 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002038 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002039 {
2040 /*
2041 * Save IV in SSL3 and TLS1
2042 */
Hanno Becker3307b532017-12-27 21:37:21 +00002043 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2044 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
Hanno Becker3307b532017-12-27 21:37:21 +00002046 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002047#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002048 {
2049 data -= transform->ivlen;
2050 rec->data_offset -= transform->ivlen;
2051 rec->data_len += transform->ivlen;
2052 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002055 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002056 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002057 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2058
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002059 /*
2060 * MAC(MAC_write_key, seq_num +
2061 * TLSCipherText.type +
2062 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002063 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002064 * IV + // except for TLS 1.0
2065 * ENC(content + padding + padding_length));
2066 */
Hanno Becker3307b532017-12-27 21:37:21 +00002067
2068 if( post_avail < transform->maclen)
2069 {
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2071 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2072 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002073
Hanno Beckere83efe62019-04-29 13:52:53 +01002074 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002077 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002078 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002079
Hanno Becker3307b532017-12-27 21:37:21 +00002080 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002081 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002082 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2083 data, rec->data_len );
2084 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2085 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002086
Hanno Becker3307b532017-12-27 21:37:21 +00002087 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002088
Hanno Becker3307b532017-12-27 21:37:21 +00002089 rec->data_len += transform->maclen;
2090 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002091 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002092 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002097 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002103 /* Make extra sure authentication was performed, exactly once */
2104 if( auth_done != 1 )
2105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002108 }
2109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
2112 return( 0 );
2113}
2114
Hanno Becker611a83b2018-01-03 14:27:32 +00002115int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2116 mbedtls_ssl_transform *transform,
2117 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002118{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002119 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002121 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002122#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002123 size_t padlen = 0, correct = 1;
2124#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002125 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002126 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002127 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002128
Hanno Becker611a83b2018-01-03 14:27:32 +00002129#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002130 ((void) ssl);
2131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002134 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002135 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2138 }
2139 if( rec == NULL ||
2140 rec->buf == NULL ||
2141 rec->buf_len < rec->data_offset ||
2142 rec->buf_len - rec->data_offset < rec->data_len )
2143 {
2144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002146 }
2147
Hanno Becker4c6876b2017-12-27 21:28:58 +00002148 data = rec->buf + rec->data_offset;
2149 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Hanno Beckera5a2b082019-05-15 14:03:01 +01002151#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002152 /*
2153 * Match record's CID with incoming CID.
2154 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002155 if( rec->cid_len != transform->in_cid_len ||
2156 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2157 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002158 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002159 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002160#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2163 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002164 {
2165 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002166 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2167 transform->iv_dec,
2168 transform->ivlen,
2169 data, rec->data_len,
2170 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002173 return( ret );
2174 }
2175
Hanno Becker4c6876b2017-12-27 21:28:58 +00002176 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002180 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
Paul Bakker68884e32013-01-07 18:20:04 +01002182 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184#if defined(MBEDTLS_GCM_C) || \
2185 defined(MBEDTLS_CCM_C) || \
2186 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002188 mode == MBEDTLS_MODE_CCM ||
2189 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002190 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002191 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002192 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002193
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002194 /*
2195 * Compute and update sizes
2196 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002197 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002200 "+ taglen (%d)", rec->data_len,
2201 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002203 }
Paul Bakker68884e32013-01-07 18:20:04 +01002204
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002205 /*
2206 * Prepare IV
2207 */
2208 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2209 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002210 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002211 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002212 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002213
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002214 }
2215 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2216 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002217 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002218 unsigned char i;
2219
2220 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2221
2222 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002223 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002224 }
2225 else
2226 {
2227 /* Reminder if we ever add an AEAD mode with a different size */
2228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2230 }
2231
Hanno Becker4c6876b2017-12-27 21:28:58 +00002232 data += explicit_iv_len;
2233 rec->data_offset += explicit_iv_len;
2234 rec->data_len -= explicit_iv_len + transform->taglen;
2235
Hanno Beckere83efe62019-04-29 13:52:53 +01002236 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002237 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002238 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002239
2240 memcpy( transform->iv_dec + transform->fixed_ivlen,
2241 data - explicit_iv_len, explicit_iv_len );
2242
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002243 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002244 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002245 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002246
Paul Bakker68884e32013-01-07 18:20:04 +01002247
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002248 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002249 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002250 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002251 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2252 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002253 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002254 data, rec->data_len,
2255 data, &olen,
2256 data + rec->data_len,
2257 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2262 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002263
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002264 return( ret );
2265 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002266 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002267
Hanno Becker4c6876b2017-12-27 21:28:58 +00002268 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002272 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2276#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002277 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002280 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002281
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 /*
Paul Bakker45829992013-01-03 14:52:21 +01002283 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2287 {
2288 /* The ciphertext is prefixed with the CBC IV. */
2289 minlen += transform->ivlen;
2290 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002291#endif
Paul Bakker45829992013-01-03 14:52:21 +01002292
Hanno Becker4c6876b2017-12-27 21:28:58 +00002293 /* Size considerations:
2294 *
2295 * - The CBC cipher text must not be empty and hence
2296 * at least of size transform->ivlen.
2297 *
2298 * Together with the potential IV-prefix, this explains
2299 * the first of the two checks below.
2300 *
2301 * - The record must contain a MAC, either in plain or
2302 * encrypted, depending on whether Encrypt-then-MAC
2303 * is used or not.
2304 * - If it is, the message contains the IV-prefix,
2305 * the CBC ciphertext, and the MAC.
2306 * - If it is not, the padded plaintext, and hence
2307 * the CBC ciphertext, has at least length maclen + 1
2308 * because there is at least the padding length byte.
2309 *
2310 * As the CBC ciphertext is not empty, both cases give the
2311 * lower bound minlen + maclen + 1 on the record size, which
2312 * we test for in the second check below.
2313 */
2314 if( rec->data_len < minlen + transform->ivlen ||
2315 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002318 "+ 1 ) ( + expl IV )", rec->data_len,
2319 transform->ivlen,
2320 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002322 }
2323
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002324 /*
2325 * Authenticate before decrypt if enabled
2326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002328 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329 {
Hanno Becker992b6872017-11-09 18:57:39 +00002330 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002333
Hanno Becker4c6876b2017-12-27 21:28:58 +00002334 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2335 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002336
Hanno Beckere83efe62019-04-29 13:52:53 +01002337 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002338
Hanno Beckere83efe62019-04-29 13:52:53 +01002339 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2340 add_data_len );
2341 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2342 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002343 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2344 data, rec->data_len );
2345 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2346 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002347
Hanno Becker4c6876b2017-12-27 21:28:58 +00002348 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2349 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002350 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002351 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002352
Hanno Becker4c6876b2017-12-27 21:28:58 +00002353 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2354 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002358 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002359 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002362
2363 /*
2364 * Check length sanity
2365 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002366 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002369 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002371 }
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002374 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002375 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002376 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002377 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002378 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 /* This is safe because data_len >= minlen + maclen + 1 initially,
2380 * and at this point we have at most subtracted maclen (note that
2381 * minlen == transform->ivlen here). */
2382 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002383
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 data += transform->ivlen;
2385 rec->data_offset += transform->ivlen;
2386 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002389
Hanno Becker4c6876b2017-12-27 21:28:58 +00002390 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2391 transform->iv_dec, transform->ivlen,
2392 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002395 return( ret );
2396 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002397
Hanno Becker4c6876b2017-12-27 21:28:58 +00002398 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002402 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002405 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002406 {
2407 /*
2408 * Save IV in SSL3 and TLS1
2409 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2411 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 /* Safe since data_len >= minlen + maclen + 1, so after having
2416 * subtracted at most minlen and maclen up to this point,
2417 * data_len > 0. */
2418 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002419
Hanno Becker4c6876b2017-12-27 21:28:58 +00002420 if( auth_done == 1 )
2421 {
2422 correct *= ( rec->data_len >= padlen + 1 );
2423 padlen *= ( rec->data_len >= padlen + 1 );
2424 }
2425 else
Paul Bakker45829992013-01-03 14:52:21 +01002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002428 if( rec->data_len < transform->maclen + padlen + 1 )
2429 {
2430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2431 rec->data_len,
2432 transform->maclen,
2433 padlen + 1 ) );
2434 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002435#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002436
2437 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2438 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Hanno Becker4c6876b2017-12-27 21:28:58 +00002441 padlen++;
2442
2443 /* Regardless of the validity of the padding,
2444 * we have data_len >= padlen here. */
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002447 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002449 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SSL_DEBUG_ALL)
2452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002453 "should be no more than %d",
2454 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002455#endif
Paul Bakker45829992013-01-03 14:52:21 +01002456 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
2458 }
2459 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2461#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2462 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002463 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002465 /* The padding check involves a series of up to 256
2466 * consecutive memory reads at the end of the record
2467 * plaintext buffer. In order to hide the length and
2468 * validity of the padding, always perform exactly
2469 * `min(256,plaintext_len)` reads (but take into account
2470 * only the last `padlen` bytes for the padding check). */
2471 size_t pad_count = 0;
2472 size_t real_count = 0;
2473 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002474
Hanno Becker4c6876b2017-12-27 21:28:58 +00002475 /* Index of first padding byte; it has been ensured above
2476 * that the subtraction is safe. */
2477 size_t const padding_idx = rec->data_len - padlen;
2478 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2479 size_t const start_idx = rec->data_len - num_checks;
2480 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002481
Hanno Becker4c6876b2017-12-27 21:28:58 +00002482 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002483 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002484 real_count |= ( idx >= padding_idx );
2485 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002486 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002487 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002490 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002492#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002493 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002495 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2497 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002501 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502
Hanno Becker4c6876b2017-12-27 21:28:58 +00002503 /* If the padding was found to be invalid, padlen == 0
2504 * and the subtraction is safe. If the padding was found valid,
2505 * padlen hasn't been changed and the previous assertion
2506 * data_len >= padlen still holds. */
2507 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002511 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002517#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002520#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
2522 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 * Authenticate if not done yet.
2524 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002526#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002527 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002528 {
Hanno Becker992b6872017-11-09 18:57:39 +00002529 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002530
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 /* If the initial value of padlen was such that
2532 * data_len < maclen + padlen + 1, then padlen
2533 * got reset to 1, and the initial check
2534 * data_len >= minlen + maclen + 1
2535 * guarantees that at this point we still
2536 * have at least data_len >= maclen.
2537 *
2538 * If the initial value of padlen was such that
2539 * data_len >= maclen + padlen + 1, then we have
2540 * subtracted either padlen + 1 (if the padding was correct)
2541 * or 0 (if the padding was incorrect) since then,
2542 * hence data_len >= maclen in any case.
2543 */
2544 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Beckere83efe62019-04-29 13:52:53 +01002546 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002549 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002550 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002551 ssl_mac( &transform->md_ctx_dec,
2552 transform->mac_dec,
2553 data, rec->data_len,
2554 rec->ctr, rec->type,
2555 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002556 }
2557 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2559#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2560 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002562 {
2563 /*
2564 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002565 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002566 *
2567 * Known timing attacks:
2568 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2569 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002570 * To compensate for different timings for the MAC calculation
2571 * depending on how much padding was removed (which is determined
2572 * by padlen), process extra_run more blocks through the hash
2573 * function.
2574 *
2575 * The formula in the paper is
2576 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2577 * where L1 is the size of the header plus the decrypted message
2578 * plus CBC padding and L2 is the size of the header plus the
2579 * decrypted message. This is for an underlying hash function
2580 * with 64-byte blocks.
2581 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2582 * correctly. We round down instead of up, so -56 is the correct
2583 * value for our calculations instead of -55.
2584 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002585 * Repeat the formula rather than defining a block_size variable.
2586 * This avoids requiring division by a variable at runtime
2587 * (which would be marginally less efficient and would require
2588 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002589 */
2590 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002592
2593 /*
2594 * The next two sizes are the minimum and maximum values of
2595 * in_msglen over all padlen values.
2596 *
2597 * They're independent of padlen, since we previously did
2598 * in_msglen -= padlen.
2599 *
2600 * Note that max_len + maclen is never more than the buffer
2601 * length, as we previously did in_msglen -= maclen too.
2602 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002603 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002604 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2605
Hanno Becker4c6876b2017-12-27 21:28:58 +00002606 memset( tmp, 0, sizeof( tmp ) );
2607
2608 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002609 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002610#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2611 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002612 case MBEDTLS_MD_MD5:
2613 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002614 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002615 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002616 extra_run =
2617 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2618 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002619 break;
2620#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002621#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002622 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002623 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002624 extra_run =
2625 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2626 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002627 break;
2628#endif
2629 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2632 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002633
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635
Hanno Beckere83efe62019-04-29 13:52:53 +01002636 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2637 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002638 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2639 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002640 /* Make sure we access everything even when padlen > 0. This
2641 * makes the synchronisation requirements for just-in-time
2642 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002643 ssl_read_memory( data + rec->data_len, padlen );
2644 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002645
2646 /* Call mbedtls_md_process at least once due to cache attacks
2647 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002648 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002650
Hanno Becker4c6876b2017-12-27 21:28:58 +00002651 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002652
2653 /* Make sure we access all the memory that could contain the MAC,
2654 * before we check it in the next code block. This makes the
2655 * synchronisation requirements for just-in-time Prime+Probe
2656 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002657 ssl_read_memory( data + min_len,
2658 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002659 }
2660 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2662 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002668#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002669 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2670 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002671#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Hanno Becker4c6876b2017-12-27 21:28:58 +00002673 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2674 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#if defined(MBEDTLS_SSL_DEBUG_ALL)
2677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002678#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002679 correct = 0;
2680 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002681 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002682 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002683
2684 /*
2685 * Finally check the correct flag
2686 */
2687 if( correct == 0 )
2688 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002689#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002690
2691 /* Make extra sure authentication was performed, exactly once */
2692 if( auth_done != 1 )
2693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Hanno Beckera5a2b082019-05-15 14:03:01 +01002698#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002699 if( rec->cid_len != 0 )
2700 {
2701 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2702 &rec->type );
2703 if( ret != 0 )
2704 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2705 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002706#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
2710 return( 0 );
2711}
2712
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002713#undef MAC_NONE
2714#undef MAC_PLAINTEXT
2715#undef MAC_CIPHERTEXT
2716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002718/*
2719 * Compression/decompression functions
2720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002722{
2723 int ret;
2724 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002725 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002726 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002727 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002730
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002731 if( len_pre == 0 )
2732 return( 0 );
2733
Paul Bakker2770fbd2012-07-03 13:30:23 +00002734 memcpy( msg_pre, ssl->out_msg, len_pre );
2735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002737 ssl->out_msglen ) );
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002740 ssl->out_msg, ssl->out_msglen );
2741
Paul Bakker48916f92012-09-16 19:57:18 +00002742 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2743 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2744 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002745 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746
Paul Bakker48916f92012-09-16 19:57:18 +00002747 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002748 if( ret != Z_OK )
2749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2751 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002752 }
2753
Angus Grattond8213d02016-05-25 20:56:48 +10002754 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002755 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002758 ssl->out_msglen ) );
2759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002761 ssl->out_msg, ssl->out_msglen );
2762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002764
2765 return( 0 );
2766}
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002769{
2770 int ret;
2771 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002772 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002774 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002778 if( len_pre == 0 )
2779 return( 0 );
2780
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781 memcpy( msg_pre, ssl->in_msg, len_pre );
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784 ssl->in_msglen ) );
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002787 ssl->in_msg, ssl->in_msglen );
2788
Paul Bakker48916f92012-09-16 19:57:18 +00002789 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2790 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2791 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002792 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002793 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002794
Paul Bakker48916f92012-09-16 19:57:18 +00002795 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002796 if( ret != Z_OK )
2797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2799 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002800 }
2801
Angus Grattond8213d02016-05-25 20:56:48 +10002802 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002803 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806 ssl->in_msglen ) );
2807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002809 ssl->in_msg, ssl->in_msglen );
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002812
2813 return( 0 );
2814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2818static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_PROTO_DTLS)
2821static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002822{
2823 /* If renegotiation is not enforced, retransmit until we would reach max
2824 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002825 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002826 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002827 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002828 unsigned char doublings = 1;
2829
2830 while( ratio != 0 )
2831 {
2832 ++doublings;
2833 ratio >>= 1;
2834 }
2835
2836 if( ++ssl->renego_records_seen > doublings )
2837 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002839 return( 0 );
2840 }
2841 }
2842
2843 return( ssl_write_hello_request( ssl ) );
2844}
2845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002847
Paul Bakker5121ce52009-01-03 21:22:43 +00002848/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002849 * Fill the input message buffer by appending data to it.
2850 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002851 *
2852 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2853 * available (from this read and/or a previous one). Otherwise, an error code
2854 * is returned (possibly EOF or WANT_READ).
2855 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002856 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2857 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2858 * since we always read a whole datagram at once.
2859 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002860 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002861 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864{
Paul Bakker23986e52011-04-24 08:57:21 +00002865 int ret;
2866 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002870 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002873 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002875 }
2876
Angus Grattond8213d02016-05-25 20:56:48 +10002877 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002881 }
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002886 uint32_t timeout;
2887
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002888 /* Just to be sure */
2889 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2890 {
2891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2892 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2894 }
2895
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002896 /*
2897 * The point is, we need to always read a full datagram at once, so we
2898 * sometimes read more then requested, and handle the additional data.
2899 * It could be the rest of the current record (while fetching the
2900 * header) and/or some other records in the same datagram.
2901 */
2902
2903 /*
2904 * Move to the next record in the already read datagram if applicable
2905 */
2906 if( ssl->next_record_offset != 0 )
2907 {
2908 if( ssl->in_left < ssl->next_record_offset )
2909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002912 }
2913
2914 ssl->in_left -= ssl->next_record_offset;
2915
2916 if( ssl->in_left != 0 )
2917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002919 ssl->next_record_offset ) );
2920 memmove( ssl->in_hdr,
2921 ssl->in_hdr + ssl->next_record_offset,
2922 ssl->in_left );
2923 }
2924
2925 ssl->next_record_offset = 0;
2926 }
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002930
2931 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002932 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002933 */
2934 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002937 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002938 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002939
2940 /*
2941 * A record can't be split accross datagrams. If we need to read but
2942 * are not at the beginning of a new record, the caller did something
2943 * wrong.
2944 */
2945 if( ssl->in_left != 0 )
2946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002949 }
2950
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002951 /*
2952 * Don't even try to read if time's out already.
2953 * This avoids by-passing the timer when repeatedly receiving messages
2954 * that will end up being dropped.
2955 */
2956 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002957 {
2958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002959 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002960 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002961 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002962 {
Angus Grattond8213d02016-05-25 20:56:48 +10002963 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002966 timeout = ssl->handshake->retransmit_timeout;
2967 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002968 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002971
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002972 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002973 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2974 timeout );
2975 else
2976 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002979
2980 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002982 }
2983
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002984 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002987 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002990 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002991 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002994 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002995 }
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003000 return( ret );
3001 }
3002
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003003 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003004 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003006 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003008 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003009 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003012 return( ret );
3013 }
3014
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003015 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003016 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003018 }
3019
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 if( ret < 0 )
3021 return( ret );
3022
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003023 ssl->in_left = ret;
3024 }
3025 else
3026#endif
3027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003029 ssl->in_left, nb_want ) );
3030
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003031 while( ssl->in_left < nb_want )
3032 {
3033 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003034
3035 if( ssl_check_timer( ssl ) != 0 )
3036 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3037 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003038 {
3039 if( ssl->f_recv_timeout != NULL )
3040 {
3041 ret = ssl->f_recv_timeout( ssl->p_bio,
3042 ssl->in_hdr + ssl->in_left, len,
3043 ssl->conf->read_timeout );
3044 }
3045 else
3046 {
3047 ret = ssl->f_recv( ssl->p_bio,
3048 ssl->in_hdr + ssl->in_left, len );
3049 }
3050 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003053 ssl->in_left, nb_want ) );
3054 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003055
3056 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003058
3059 if( ret < 0 )
3060 return( ret );
3061
mohammad160352aecb92018-03-28 23:41:40 -07003062 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003063 {
Darryl Green11999bb2018-03-13 15:22:58 +00003064 MBEDTLS_SSL_DEBUG_MSG( 1,
3065 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003066 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3068 }
3069
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003070 ssl->in_left += ret;
3071 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 }
3073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
3076 return( 0 );
3077}
3078
3079/*
3080 * Flush any data not yet written
3081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003083{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003084 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003085 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003089 if( ssl->f_send == NULL )
3090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003092 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003094 }
3095
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003096 /* Avoid incrementing counter if data is flushed */
3097 if( ssl->out_left == 0 )
3098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003100 return( 0 );
3101 }
3102
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 while( ssl->out_left > 0 )
3104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003106 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Hanno Becker2b1e3542018-08-06 11:19:13 +01003108 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003109 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
3113 if( ret <= 0 )
3114 return( ret );
3115
mohammad160352aecb92018-03-28 23:41:40 -07003116 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003117 {
Darryl Green11999bb2018-03-13 15:22:58 +00003118 MBEDTLS_SSL_DEBUG_MSG( 1,
3119 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003120 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3122 }
3123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 ssl->out_left -= ret;
3125 }
3126
Hanno Becker2b1e3542018-08-06 11:19:13 +01003127#if defined(MBEDTLS_SSL_PROTO_DTLS)
3128 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003129 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003130 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003131 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003132 else
3133#endif
3134 {
3135 ssl->out_hdr = ssl->out_buf + 8;
3136 }
3137 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
3141 return( 0 );
3142}
3143
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003144/*
3145 * Functions to handle the DTLS retransmission state machine
3146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003148/*
3149 * Append current handshake message to current outgoing flight
3150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3155 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3156 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157
3158 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003159 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003160 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003163 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003164 }
3165
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003166 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003167 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003170 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003171 }
3172
3173 /* Copy current handshake message with headers */
3174 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3175 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003176 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003177 msg->next = NULL;
3178
3179 /* Append to the current flight */
3180 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003181 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003182 else
3183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003185 while( cur->next != NULL )
3186 cur = cur->next;
3187 cur->next = msg;
3188 }
3189
Hanno Becker3b235902018-08-06 09:54:53 +01003190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191 return( 0 );
3192}
3193
3194/*
3195 * Free the current flight of handshake messages
3196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 mbedtls_ssl_flight_item *cur = flight;
3200 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003201
3202 while( cur != NULL )
3203 {
3204 next = cur->next;
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 mbedtls_free( cur->p );
3207 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003208
3209 cur = next;
3210 }
3211}
3212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3214static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003215#endif
3216
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003217/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003218 * Swap transform_out and out_ctr with the alternative ones
3219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003223 unsigned char tmp_out_ctr[8];
3224
3225 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003228 return;
3229 }
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003232
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003233 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003234 tmp_transform = ssl->transform_out;
3235 ssl->transform_out = ssl->handshake->alt_transform_out;
3236 ssl->handshake->alt_transform_out = tmp_transform;
3237
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003238 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003239 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3240 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003241 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003242
3243 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003244 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3247 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3252 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003253 }
3254 }
3255#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003256}
3257
3258/*
3259 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003260 */
3261int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3262{
3263 int ret = 0;
3264
3265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3266
3267 ret = mbedtls_ssl_flight_transmit( ssl );
3268
3269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3270
3271 return( ret );
3272}
3273
3274/*
3275 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003276 *
3277 * Need to remember the current message in case flush_output returns
3278 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003279 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003280 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003281int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003282{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003283 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003287 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003289
3290 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003291 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003292 ssl_swap_epochs( ssl );
3293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003295 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003296
3297 while( ssl->handshake->cur_msg != NULL )
3298 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003299 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003300 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003301
Hanno Beckere1dcb032018-08-17 16:47:58 +01003302 int const is_finished =
3303 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3304 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3305
Hanno Becker04da1892018-08-14 13:22:10 +01003306 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3307 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3308
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003309 /* Swap epochs before sending Finished: we can't do it after
3310 * sending ChangeCipherSpec, in case write returns WANT_READ.
3311 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003312 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003313 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003315 ssl_swap_epochs( ssl );
3316 }
3317
Hanno Becker67bc7c32018-08-06 11:33:50 +01003318 ret = ssl_get_remaining_payload_in_datagram( ssl );
3319 if( ret < 0 )
3320 return( ret );
3321 max_frag_len = (size_t) ret;
3322
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003323 /* CCS is copied as is, while HS messages may need fragmentation */
3324 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3325 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 if( max_frag_len == 0 )
3327 {
3328 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3329 return( ret );
3330
3331 continue;
3332 }
3333
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003334 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003335 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003336 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003338 /* Update position inside current message */
3339 ssl->handshake->cur_msg_p += cur->len;
3340 }
3341 else
3342 {
3343 const unsigned char * const p = ssl->handshake->cur_msg_p;
3344 const size_t hs_len = cur->len - 12;
3345 const size_t frag_off = p - ( cur->p + 12 );
3346 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003347 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348
Hanno Beckere1dcb032018-08-17 16:47:58 +01003349 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003350 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003351 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003353
Hanno Becker67bc7c32018-08-06 11:33:50 +01003354 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3355 return( ret );
3356
3357 continue;
3358 }
3359 max_hs_frag_len = max_frag_len - 12;
3360
3361 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3362 max_hs_frag_len : rem_len;
3363
3364 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003365 {
3366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003367 (unsigned) cur_hs_frag_len,
3368 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003369 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003370
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003371 /* Messages are stored with handshake headers as if not fragmented,
3372 * copy beginning of headers then fill fragmentation fields.
3373 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3374 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003376 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3377 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3378 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3379
Hanno Becker67bc7c32018-08-06 11:33:50 +01003380 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3381 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3382 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003383
3384 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3385
Hanno Becker3f7b9732018-08-28 09:53:25 +01003386 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003387 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3388 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003389 ssl->out_msgtype = cur->type;
3390
3391 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003392 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003393 }
3394
3395 /* If done with the current message move to the next one if any */
3396 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3397 {
3398 if( cur->next != NULL )
3399 {
3400 ssl->handshake->cur_msg = cur->next;
3401 ssl->handshake->cur_msg_p = cur->next->p + 12;
3402 }
3403 else
3404 {
3405 ssl->handshake->cur_msg = NULL;
3406 ssl->handshake->cur_msg_p = NULL;
3407 }
3408 }
3409
3410 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003411 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414 return( ret );
3415 }
3416 }
3417
Hanno Becker67bc7c32018-08-06 11:33:50 +01003418 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3419 return( ret );
3420
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003421 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3423 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003424 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003427 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3428 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003429
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003431
3432 return( 0 );
3433}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434
3435/*
3436 * To be called when the last message of an incoming flight is received.
3437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003439{
3440 /* We won't need to resend that one any more */
3441 ssl_flight_free( ssl->handshake->flight );
3442 ssl->handshake->flight = NULL;
3443 ssl->handshake->cur_msg = NULL;
3444
3445 /* The next incoming flight will start with this msg_seq */
3446 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3447
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003448 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003449 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003450
Hanno Becker0271f962018-08-16 13:23:47 +01003451 /* Clear future message buffering structure. */
3452 ssl_buffering_free( ssl );
3453
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003454 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003455 ssl_set_timer( ssl, 0 );
3456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3458 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 }
3462 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003465
3466/*
3467 * To be called when the last message of an outgoing flight is send.
3468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003470{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003471 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003472 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3475 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003478 }
3479 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003483
Paul Bakker5121ce52009-01-03 21:22:43 +00003484/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003485 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003487
3488/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003489 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003490 *
3491 * - fill in handshake headers
3492 * - update handshake checksum
3493 * - DTLS: save message for resending
3494 * - then pass to the record layer
3495 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003496 * DTLS: except for HelloRequest, messages are only queued, and will only be
3497 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003498 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003499 * Inputs:
3500 * - ssl->out_msglen: 4 + actual handshake message len
3501 * (4 is the size of handshake headers for TLS)
3502 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3503 * - ssl->out_msg + 4: the handshake message body
3504 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003505 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003506 * - ssl->out_msglen: the length of the record contents
3507 * (including handshake headers but excluding record headers)
3508 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003509 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003510int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003511{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003512 int ret;
3513 const size_t hs_len = ssl->out_msglen - 4;
3514 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3517
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003518 /*
3519 * Sanity checks
3520 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003521 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003522 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3523 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003524 /* In SSLv3, the client might send a NoCertificate alert. */
3525#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3526 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3527 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3528 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3529#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3530 {
3531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3533 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003536 /* Whenever we send anything different from a
3537 * HelloRequest we should be in a handshake - double check. */
3538 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3539 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003540 ssl->handshake == NULL )
3541 {
3542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003547 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003548 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003550 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003553 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003554#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003555
Hanno Beckerb50a2532018-08-06 11:52:54 +01003556 /* Double-check that we did not exceed the bounds
3557 * of the outgoing record buffer.
3558 * This should never fail as the various message
3559 * writing functions must obey the bounds of the
3560 * outgoing record buffer, but better be safe.
3561 *
3562 * Note: We deliberately do not check for the MTU or MFL here.
3563 */
3564 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3565 {
3566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3567 "size %u, maximum %u",
3568 (unsigned) ssl->out_msglen,
3569 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3571 }
3572
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003573 /*
3574 * Fill handshake headers
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003577 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003578 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3579 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3580 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003582 /*
3583 * DTLS has additional fields in the Handshake layer,
3584 * between the length field and the actual payload:
3585 * uint16 message_seq;
3586 * uint24 fragment_offset;
3587 * uint24 fragment_length;
3588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003590 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003591 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003592 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003593 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003594 {
3595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3596 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003597 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003598 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3600 }
3601
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003602 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003603 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003604
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003605 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003606 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003607 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003608 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3609 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3610 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003611 }
3612 else
3613 {
3614 ssl->out_msg[4] = 0;
3615 ssl->out_msg[5] = 0;
3616 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003617
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003618 /* Handshake hashes are computed without fragmentation,
3619 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003620 memset( ssl->out_msg + 6, 0x00, 3 );
3621 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003622 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003624
Hanno Becker0207e532018-08-28 10:28:28 +01003625 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003626 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3627 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 }
3629
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003630 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003632 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003633 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3634 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635 {
3636 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003639 return( ret );
3640 }
3641 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003642 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003643#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003644 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003645 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003646 {
3647 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3648 return( ret );
3649 }
3650 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003651
3652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3653
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003654 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003655}
3656
3657/*
3658 * Record layer functions
3659 */
3660
3661/*
3662 * Write current record.
3663 *
3664 * Uses:
3665 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3666 * - ssl->out_msglen: length of the record content (excl headers)
3667 * - ssl->out_msg: record content
3668 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003669int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003670{
3671 int ret, done = 0;
3672 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003673 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003674
3675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003678 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003680 {
3681 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003684 return( ret );
3685 }
3686
3687 len = ssl->out_msglen;
3688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3692 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 ret = mbedtls_ssl_hw_record_write( ssl );
3697 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3700 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003701 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003702
3703 if( ret == 0 )
3704 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003707 if( !done )
3708 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003709 unsigned i;
3710 size_t protected_record_size;
3711
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003712 /* Skip writing the record content type to after the encryption,
3713 * as it may change when using the CID extension. */
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003716 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003717
Hanno Becker19859472018-08-06 09:40:20 +01003718 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003719 ssl->out_len[0] = (unsigned char)( len >> 8 );
3720 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003721
Paul Bakker48916f92012-09-16 19:57:18 +00003722 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003723 {
Hanno Becker3307b532017-12-27 21:37:21 +00003724 mbedtls_record rec;
3725
3726 rec.buf = ssl->out_iv;
3727 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3728 ( ssl->out_iv - ssl->out_buf );
3729 rec.data_len = ssl->out_msglen;
3730 rec.data_offset = ssl->out_msg - rec.buf;
3731
3732 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3733 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3734 ssl->conf->transport, rec.ver );
3735 rec.type = ssl->out_msgtype;
3736
Hanno Beckera5a2b082019-05-15 14:03:01 +01003737#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003738 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003739 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003740#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003741
Hanno Becker611a83b2018-01-03 14:27:32 +00003742 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003743 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003746 return( ret );
3747 }
3748
Hanno Becker3307b532017-12-27 21:37:21 +00003749 if( rec.data_offset != 0 )
3750 {
3751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3752 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3753 }
3754
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003755 /* Update the record content type and CID. */
3756 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003757#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003758 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003759#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003760 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003761 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3762 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003763 }
3764
Hanno Becker43395762019-05-03 14:46:38 +01003765 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003766
3767#if defined(MBEDTLS_SSL_PROTO_DTLS)
3768 /* In case of DTLS, double-check that we don't exceed
3769 * the remaining space in the datagram. */
3770 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3771 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003772 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003773 if( ret < 0 )
3774 return( ret );
3775
3776 if( protected_record_size > (size_t) ret )
3777 {
3778 /* Should never happen */
3779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3780 }
3781 }
3782#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003783
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003784 /* Now write the potentially updated record content type. */
3785 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003788 "version = [%d:%d], msglen = %d",
3789 ssl->out_hdr[0], ssl->out_hdr[1],
3790 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003793 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003794
3795 ssl->out_left += protected_record_size;
3796 ssl->out_hdr += protected_record_size;
3797 ssl_update_out_pointers( ssl, ssl->transform_out );
3798
Hanno Becker04484622018-08-06 09:49:38 +01003799 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3800 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3801 break;
3802
3803 /* The loop goes to its end iff the counter is wrapping */
3804 if( i == ssl_ep_len( ssl ) )
3805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3807 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 }
3810
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3813 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003815 size_t remaining;
3816 ret = ssl_get_remaining_payload_in_datagram( ssl );
3817 if( ret < 0 )
3818 {
3819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3820 ret );
3821 return( ret );
3822 }
3823
3824 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003825 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003826 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003827 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003828 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003829 else
3830 {
Hanno Becker513815a2018-08-20 11:56:09 +01003831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003832 }
3833 }
3834#endif /* MBEDTLS_SSL_PROTO_DTLS */
3835
3836 if( ( flush == SSL_FORCE_FLUSH ) &&
3837 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 return( ret );
3841 }
3842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003844
3845 return( 0 );
3846}
3847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003849
3850static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3851{
3852 if( ssl->in_msglen < ssl->in_hslen ||
3853 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3854 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3855 {
3856 return( 1 );
3857 }
3858 return( 0 );
3859}
Hanno Becker44650b72018-08-16 12:51:11 +01003860
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003861static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003862{
3863 return( ( ssl->in_msg[9] << 16 ) |
3864 ( ssl->in_msg[10] << 8 ) |
3865 ssl->in_msg[11] );
3866}
3867
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003868static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003869{
3870 return( ( ssl->in_msg[6] << 16 ) |
3871 ( ssl->in_msg[7] << 8 ) |
3872 ssl->in_msg[8] );
3873}
3874
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003875static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003876{
3877 uint32_t msg_len, frag_off, frag_len;
3878
3879 msg_len = ssl_get_hs_total_len( ssl );
3880 frag_off = ssl_get_hs_frag_off( ssl );
3881 frag_len = ssl_get_hs_frag_len( ssl );
3882
3883 if( frag_off > msg_len )
3884 return( -1 );
3885
3886 if( frag_len > msg_len - frag_off )
3887 return( -1 );
3888
3889 if( frag_len + 12 > ssl->in_msglen )
3890 return( -1 );
3891
3892 return( 0 );
3893}
3894
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003895/*
3896 * Mark bits in bitmask (used for DTLS HS reassembly)
3897 */
3898static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3899{
3900 unsigned int start_bits, end_bits;
3901
3902 start_bits = 8 - ( offset % 8 );
3903 if( start_bits != 8 )
3904 {
3905 size_t first_byte_idx = offset / 8;
3906
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003907 /* Special case */
3908 if( len <= start_bits )
3909 {
3910 for( ; len != 0; len-- )
3911 mask[first_byte_idx] |= 1 << ( start_bits - len );
3912
3913 /* Avoid potential issues with offset or len becoming invalid */
3914 return;
3915 }
3916
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003917 offset += start_bits; /* Now offset % 8 == 0 */
3918 len -= start_bits;
3919
3920 for( ; start_bits != 0; start_bits-- )
3921 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3922 }
3923
3924 end_bits = len % 8;
3925 if( end_bits != 0 )
3926 {
3927 size_t last_byte_idx = ( offset + len ) / 8;
3928
3929 len -= end_bits; /* Now len % 8 == 0 */
3930
3931 for( ; end_bits != 0; end_bits-- )
3932 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3933 }
3934
3935 memset( mask + offset / 8, 0xFF, len / 8 );
3936}
3937
3938/*
3939 * Check that bitmask is full
3940 */
3941static int ssl_bitmask_check( unsigned char *mask, size_t len )
3942{
3943 size_t i;
3944
3945 for( i = 0; i < len / 8; i++ )
3946 if( mask[i] != 0xFF )
3947 return( -1 );
3948
3949 for( i = 0; i < len % 8; i++ )
3950 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3951 return( -1 );
3952
3953 return( 0 );
3954}
3955
Hanno Becker56e205e2018-08-16 09:06:12 +01003956/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003957static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003958 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003959{
Hanno Becker56e205e2018-08-16 09:06:12 +01003960 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003961
Hanno Becker56e205e2018-08-16 09:06:12 +01003962 alloc_len = 12; /* Handshake header */
3963 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003964
Hanno Beckerd07df862018-08-16 09:14:58 +01003965 if( add_bitmap )
3966 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003967
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003968 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003969}
Hanno Becker56e205e2018-08-16 09:06:12 +01003970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003972
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003973static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003974{
3975 return( ( ssl->in_msg[1] << 16 ) |
3976 ( ssl->in_msg[2] << 8 ) |
3977 ssl->in_msg[3] );
3978}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003979
Simon Butcher99000142016-10-13 17:21:01 +01003980int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003985 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003987 }
3988
Hanno Becker12555c62018-08-16 12:47:53 +01003989 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003992 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003993 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003997 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003998 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003999 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004000
Hanno Becker44650b72018-08-16 12:51:11 +01004001 if( ssl_check_hs_header( ssl ) != 0 )
4002 {
4003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4004 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4005 }
4006
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004007 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004008 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4009 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4010 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4011 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004012 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004013 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4014 {
4015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4016 recv_msg_seq,
4017 ssl->handshake->in_msg_seq ) );
4018 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4019 }
4020
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004021 /* Retransmit only on last message from previous flight, to avoid
4022 * too many retransmissions.
4023 * Besides, No sane server ever retransmits HelloVerifyRequest */
4024 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004028 "message_seq = %d, start_of_flight = %d",
4029 recv_msg_seq,
4030 ssl->handshake->in_flight_start_seq ) );
4031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004035 return( ret );
4036 }
4037 }
4038 else
4039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004041 "message_seq = %d, expected = %d",
4042 recv_msg_seq,
4043 ssl->handshake->in_msg_seq ) );
4044 }
4045
Hanno Becker90333da2017-10-10 11:27:13 +01004046 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004047 }
4048 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004049
Hanno Becker6d97ef52018-08-16 13:09:04 +01004050 /* Message reassembly is handled alongside buffering of future
4051 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004052 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004053 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004054 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004057 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004058 }
4059 }
4060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004062 /* With TLS we don't handle fragmentation (for now) */
4063 if( ssl->in_msglen < ssl->in_hslen )
4064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4066 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004067 }
4068
Simon Butcher99000142016-10-13 17:21:01 +01004069 return( 0 );
4070}
4071
4072void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4073{
Hanno Becker0271f962018-08-16 13:23:47 +01004074 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004075
Hanno Becker0271f962018-08-16 13:23:47 +01004076 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004077 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004078 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004079 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004080
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004081 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004083 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004084 ssl->handshake != NULL )
4085 {
Hanno Becker0271f962018-08-16 13:23:47 +01004086 unsigned offset;
4087 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004088
Hanno Becker0271f962018-08-16 13:23:47 +01004089 /* Increment handshake sequence number */
4090 hs->in_msg_seq++;
4091
4092 /*
4093 * Clear up handshake buffering and reassembly structure.
4094 */
4095
4096 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004097 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004098
4099 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004100 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4101 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004102 offset++, hs_buf++ )
4103 {
4104 *hs_buf = *(hs_buf + 1);
4105 }
4106
4107 /* Create a fresh last entry */
4108 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004109 }
4110#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004111}
4112
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004113/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004114 * DTLS anti-replay: RFC 6347 4.1.2.6
4115 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004116 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4117 * Bit n is set iff record number in_window_top - n has been seen.
4118 *
4119 * Usually, in_window_top is the last record number seen and the lsb of
4120 * in_window is set. The only exception is the initial state (record number 0
4121 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4124static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004125{
4126 ssl->in_window_top = 0;
4127 ssl->in_window = 0;
4128}
4129
4130static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4131{
4132 return( ( (uint64_t) buf[0] << 40 ) |
4133 ( (uint64_t) buf[1] << 32 ) |
4134 ( (uint64_t) buf[2] << 24 ) |
4135 ( (uint64_t) buf[3] << 16 ) |
4136 ( (uint64_t) buf[4] << 8 ) |
4137 ( (uint64_t) buf[5] ) );
4138}
4139
4140/*
4141 * Return 0 if sequence number is acceptable, -1 otherwise
4142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004144{
4145 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4146 uint64_t bit;
4147
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004148 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004149 return( 0 );
4150
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004151 if( rec_seqnum > ssl->in_window_top )
4152 return( 0 );
4153
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004154 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004155
4156 if( bit >= 64 )
4157 return( -1 );
4158
4159 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4160 return( -1 );
4161
4162 return( 0 );
4163}
4164
4165/*
4166 * Update replay window on new validated record
4167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004169{
4170 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4171
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004172 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004173 return;
4174
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004175 if( rec_seqnum > ssl->in_window_top )
4176 {
4177 /* Update window_top and the contents of the window */
4178 uint64_t shift = rec_seqnum - ssl->in_window_top;
4179
4180 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004181 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004182 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004183 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004184 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004185 ssl->in_window |= 1;
4186 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187
4188 ssl->in_window_top = rec_seqnum;
4189 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004190 else
4191 {
4192 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004193 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004194
4195 if( bit < 64 ) /* Always true, but be extra sure */
4196 ssl->in_window |= (uint64_t) 1 << bit;
4197 }
4198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004200
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004201#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004202/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004203static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4204
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004205/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004206 * Without any SSL context, check if a datagram looks like a ClientHello with
4207 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004208 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004209 *
4210 * - if cookie is valid, return 0
4211 * - if ClientHello looks superficially valid but cookie is not,
4212 * fill obuf and set olen, then
4213 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4214 * - otherwise return a specific error code
4215 */
4216static int ssl_check_dtls_clihlo_cookie(
4217 mbedtls_ssl_cookie_write_t *f_cookie_write,
4218 mbedtls_ssl_cookie_check_t *f_cookie_check,
4219 void *p_cookie,
4220 const unsigned char *cli_id, size_t cli_id_len,
4221 const unsigned char *in, size_t in_len,
4222 unsigned char *obuf, size_t buf_len, size_t *olen )
4223{
4224 size_t sid_len, cookie_len;
4225 unsigned char *p;
4226
4227 if( f_cookie_write == NULL || f_cookie_check == NULL )
4228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4229
4230 /*
4231 * Structure of ClientHello with record and handshake headers,
4232 * and expected values. We don't need to check a lot, more checks will be
4233 * done when actually parsing the ClientHello - skipping those checks
4234 * avoids code duplication and does not make cookie forging any easier.
4235 *
4236 * 0-0 ContentType type; copied, must be handshake
4237 * 1-2 ProtocolVersion version; copied
4238 * 3-4 uint16 epoch; copied, must be 0
4239 * 5-10 uint48 sequence_number; copied
4240 * 11-12 uint16 length; (ignored)
4241 *
4242 * 13-13 HandshakeType msg_type; (ignored)
4243 * 14-16 uint24 length; (ignored)
4244 * 17-18 uint16 message_seq; copied
4245 * 19-21 uint24 fragment_offset; copied, must be 0
4246 * 22-24 uint24 fragment_length; (ignored)
4247 *
4248 * 25-26 ProtocolVersion client_version; (ignored)
4249 * 27-58 Random random; (ignored)
4250 * 59-xx SessionID session_id; 1 byte len + sid_len content
4251 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4252 * ...
4253 *
4254 * Minimum length is 61 bytes.
4255 */
4256 if( in_len < 61 ||
4257 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4258 in[3] != 0 || in[4] != 0 ||
4259 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4260 {
4261 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4262 }
4263
4264 sid_len = in[59];
4265 if( sid_len > in_len - 61 )
4266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4267
4268 cookie_len = in[60 + sid_len];
4269 if( cookie_len > in_len - 60 )
4270 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4271
4272 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4273 cli_id, cli_id_len ) == 0 )
4274 {
4275 /* Valid cookie */
4276 return( 0 );
4277 }
4278
4279 /*
4280 * If we get here, we've got an invalid cookie, let's prepare HVR.
4281 *
4282 * 0-0 ContentType type; copied
4283 * 1-2 ProtocolVersion version; copied
4284 * 3-4 uint16 epoch; copied
4285 * 5-10 uint48 sequence_number; copied
4286 * 11-12 uint16 length; olen - 13
4287 *
4288 * 13-13 HandshakeType msg_type; hello_verify_request
4289 * 14-16 uint24 length; olen - 25
4290 * 17-18 uint16 message_seq; copied
4291 * 19-21 uint24 fragment_offset; copied
4292 * 22-24 uint24 fragment_length; olen - 25
4293 *
4294 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4295 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4296 *
4297 * Minimum length is 28.
4298 */
4299 if( buf_len < 28 )
4300 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4301
4302 /* Copy most fields and adapt others */
4303 memcpy( obuf, in, 25 );
4304 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4305 obuf[25] = 0xfe;
4306 obuf[26] = 0xff;
4307
4308 /* Generate and write actual cookie */
4309 p = obuf + 28;
4310 if( f_cookie_write( p_cookie,
4311 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4312 {
4313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4314 }
4315
4316 *olen = p - obuf;
4317
4318 /* Go back and fill length fields */
4319 obuf[27] = (unsigned char)( *olen - 28 );
4320
4321 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4322 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4323 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4324
4325 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4326 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4327
4328 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4329}
4330
4331/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004332 * Handle possible client reconnect with the same UDP quadruplet
4333 * (RFC 6347 Section 4.2.8).
4334 *
4335 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4336 * that looks like a ClientHello.
4337 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004338 * - if the input looks like a ClientHello without cookies,
4339 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004340 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004341 * - if the input looks like a ClientHello with a valid cookie,
4342 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004343 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004344 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004345 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004346 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004347 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4348 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004349 */
4350static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4351{
4352 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004353 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004354
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004355 ret = ssl_check_dtls_clihlo_cookie(
4356 ssl->conf->f_cookie_write,
4357 ssl->conf->f_cookie_check,
4358 ssl->conf->p_cookie,
4359 ssl->cli_id, ssl->cli_id_len,
4360 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004361 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004362
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004363 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4364
4365 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004366 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004367 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004368 * If the error is permanent we'll catch it later,
4369 * if it's not, then hopefully it'll work next time. */
4370 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4371
4372 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004373 }
4374
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004375 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004377 /* Got a valid cookie, partially reset context */
4378 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4379 {
4380 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4381 return( ret );
4382 }
4383
4384 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004385 }
4386
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004387 return( ret );
4388}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004389#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004390
Hanno Becker46483f12019-05-03 13:25:54 +01004391static int ssl_check_record_type( uint8_t record_type )
4392{
4393 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4394 record_type != MBEDTLS_SSL_MSG_ALERT &&
4395 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4396 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4397 {
4398 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4399 }
4400
4401 return( 0 );
4402}
4403
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004404/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004405 * ContentType type;
4406 * ProtocolVersion version;
4407 * uint16 epoch; // DTLS only
4408 * uint48 sequence_number; // DTLS only
4409 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004410 *
4411 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004412 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004413 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4414 *
4415 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004416 * 1. proceed with the record if this function returns 0
4417 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4418 * 3. return CLIENT_RECONNECT if this function return that value
4419 * 4. drop the whole datagram if this function returns anything else.
4420 * Point 2 is needed when the peer is resending, and we have already received
4421 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004425 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004426 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004427
Hanno Becker8b09b732019-05-08 12:03:28 +01004428 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004429
Paul Bakker5121ce52009-01-03 21:22:43 +00004430 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004431 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004433 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004434#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4436 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4437 ssl->conf->cid_len != 0 )
4438 {
4439 /* Shift pointers to account for record header including CID
4440 * struct {
4441 * ContentType special_type = tls12_cid;
4442 * ProtocolVersion version;
4443 * uint16 epoch;
4444 * uint48 sequence_number;
4445 * opaque cid[cid_length]; // New field
4446 * uint16 length;
4447 * opaque enc_content[DTLSCiphertext.length];
4448 * } DTLSCiphertext;
4449 */
4450
4451 /* So far, we only support static CID lengths
4452 * fixed in the configuration. */
4453 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4454 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4455 }
4456 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004457#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004458 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004461
4462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004463 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4464 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004465 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4466#endif /* MBEDTLS_SSL_PROTO_DTLS */
4467 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4468 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004471 }
4472
4473 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004474 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4477 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 }
4479
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004480 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4483 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 }
4485
Hanno Becker8b09b732019-05-08 12:03:28 +01004486 /* Now that the total length of the record header is known, ensure
4487 * that the current datagram is large enough to hold it.
4488 * This would fail, for example, if we received a datagram of
4489 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4490 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4491 if( ret != 0 )
4492 {
4493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4494 return( ret );
4495 }
4496 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4497
4498 /* Parse and validate record length
4499 * This must happen after the CID parsing because
4500 * its position in the record header depends on
4501 * the presence of a CID. */
4502
4503 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004504 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004505 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4508 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004509 }
4510
Hanno Becker8b09b732019-05-08 12:03:28 +01004511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4512 "version = [%d:%d], msglen = %d",
4513 ssl->in_msgtype,
4514 major_ver, minor_ver, ssl->in_msglen ) );
4515
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004516 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004517 * DTLS-related tests.
4518 * Check epoch before checking length constraint because
4519 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4520 * message gets duplicated before the corresponding Finished message,
4521 * the second ChangeCipherSpec should be discarded because it belongs
4522 * to an old epoch, but not because its length is shorter than
4523 * the minimum record length for packets using the new record transform.
4524 * Note that these two kinds of failures are handled differently,
4525 * as an unexpected record is silently skipped but an invalid
4526 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004527 */
4528#if defined(MBEDTLS_SSL_PROTO_DTLS)
4529 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4530 {
4531 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4532
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004533 /* Check epoch (and sequence number) with DTLS */
4534 if( rec_epoch != ssl->in_epoch )
4535 {
4536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4537 "expected %d, received %d",
4538 ssl->in_epoch, rec_epoch ) );
4539
4540#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4541 /*
4542 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4543 * access the first byte of record content (handshake type), as we
4544 * have an active transform (possibly iv_len != 0), so use the
4545 * fact that the record header len is 13 instead.
4546 */
4547 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4548 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4549 rec_epoch == 0 &&
4550 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4551 ssl->in_left > 13 &&
4552 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4553 {
4554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4555 "from the same port" ) );
4556 return( ssl_handle_possible_reconnect( ssl ) );
4557 }
4558 else
4559#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004560 {
4561 /* Consider buffering the record. */
4562 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4563 {
4564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4565 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4566 }
4567
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004569 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004570 }
4571
4572#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4573 /* Replay detection only works for the current epoch */
4574 if( rec_epoch == ssl->in_epoch &&
4575 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4576 {
4577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4578 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4579 }
4580#endif
4581 }
4582#endif /* MBEDTLS_SSL_PROTO_DTLS */
4583
Hanno Becker52c6dc62017-05-26 16:07:36 +01004584
4585 /* Check length against bounds of the current transform and version */
4586 if( ssl->transform_in == NULL )
4587 {
4588 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004589 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004590 {
4591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4592 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4593 }
4594 }
4595 else
4596 {
4597 if( ssl->in_msglen < ssl->transform_in->minlen )
4598 {
4599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4600 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4601 }
4602
4603#if defined(MBEDTLS_SSL_PROTO_SSL3)
4604 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004605 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4608 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4609 }
4610#endif
4611#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4612 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4613 /*
4614 * TLS encrypted messages can have up to 256 bytes of padding
4615 */
4616 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4617 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004618 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004619 {
4620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4621 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4622 }
4623#endif
4624 }
4625
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004626 return( 0 );
4627}
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004629/*
4630 * If applicable, decrypt (and decompress) record content
4631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004633{
4634 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004637 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4640 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 ret = mbedtls_ssl_hw_record_read( ssl );
4645 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4648 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004649 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004650
4651 if( ret == 0 )
4652 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004655 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004656 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004657 mbedtls_record rec;
4658
4659 rec.buf = ssl->in_iv;
4660 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4661 - ( ssl->in_iv - ssl->in_buf );
4662 rec.data_len = ssl->in_msglen;
4663 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004664#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004665 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004666 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004667#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004668
4669 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4670 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4671 ssl->conf->transport, rec.ver );
4672 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004673 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4674 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004677
Hanno Beckera5a2b082019-05-15 14:03:01 +01004678#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004679 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4680 ssl->conf->ignore_unexpected_cid
4681 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4682 {
Hanno Becker687e0fb2019-05-08 13:02:55 +01004683 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004684 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004685#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004686
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 return( ret );
4688 }
4689
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004690 if( ssl->in_msgtype != rec.type )
4691 {
4692 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4693 ssl->in_msgtype, rec.type ) );
4694 }
4695
4696 /* The record content type may change during decryption,
4697 * so re-read it. */
4698 ssl->in_msgtype = rec.type;
4699 /* Also update the input buffer, because unfortunately
4700 * the server-side ssl_parse_client_hello() reparses the
4701 * record header when receiving a ClientHello initiating
4702 * a renegotiation. */
4703 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004704 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004705 ssl->in_msglen = rec.data_len;
4706 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4707 ssl->in_len[1] = (unsigned char)( rec.data_len );
4708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 ssl->in_msg, ssl->in_msglen );
4711
Hanno Beckera5a2b082019-05-15 14:03:01 +01004712#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004713 /* We have already checked the record content type
4714 * in ssl_parse_record_header(), failing or silently
4715 * dropping the record in the case of an unknown type.
4716 *
4717 * Since with the use of CIDs, the record content type
4718 * might change during decryption, re-check the record
4719 * content type, but treat a failure as fatal this time. */
4720 if( ssl_check_record_type( ssl->in_msgtype ) )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4723 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4724 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004725#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004726
Angus Grattond8213d02016-05-25 20:56:48 +10004727 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4730 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004731 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004732 else if( ssl->in_msglen == 0 )
4733 {
4734#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4735 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4736 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4737 {
4738 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4740 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4741 }
4742#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4743
4744 ssl->nb_zero++;
4745
4746 /*
4747 * Three or more empty messages may be a DoS attack
4748 * (excessive CPU consumption).
4749 */
4750 if( ssl->nb_zero > 3 )
4751 {
4752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004753 "messages, possible DoS attack" ) );
4754 /* Treat the records as if they were not properly authenticated,
4755 * thereby failing the connection if we see more than allowed
4756 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004757 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4758 }
4759 }
4760 else
4761 ssl->nb_zero = 0;
4762
4763#if defined(MBEDTLS_SSL_PROTO_DTLS)
4764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4765 {
4766 ; /* in_ctr read from peer, not maintained internally */
4767 }
4768 else
4769#endif
4770 {
4771 unsigned i;
4772 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4773 if( ++ssl->in_ctr[i - 1] != 0 )
4774 break;
4775
4776 /* The loop goes to its end iff the counter is wrapping */
4777 if( i == ssl_ep_len( ssl ) )
4778 {
4779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4780 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4781 }
4782 }
4783
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 }
4785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004787 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004789 {
4790 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004793 return( ret );
4794 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004799 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004802 }
4803#endif
4804
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004805 return( 0 );
4806}
4807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004809
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004810/*
4811 * Read a record.
4812 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004813 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4814 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4815 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004816 */
Hanno Becker1097b342018-08-15 14:09:41 +01004817
4818/* Helper functions for mbedtls_ssl_read_record(). */
4819static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004820static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4821static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004822
Hanno Becker327c93b2018-08-15 13:56:18 +01004823int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004824 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004825{
4826 int ret;
4827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004829
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004830 if( ssl->keep_current_message == 0 )
4831 {
4832 do {
Simon Butcher99000142016-10-13 17:21:01 +01004833
Hanno Becker26994592018-08-15 14:14:59 +01004834 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004835 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004836 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004837
Hanno Beckere74d5562018-08-15 14:26:08 +01004838 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004839 {
Hanno Becker40f50842018-08-15 14:48:01 +01004840#if defined(MBEDTLS_SSL_PROTO_DTLS)
4841 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004842
Hanno Becker40f50842018-08-15 14:48:01 +01004843 /* We only check for buffered messages if the
4844 * current datagram is fully consumed. */
4845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004846 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004847 {
Hanno Becker40f50842018-08-15 14:48:01 +01004848 if( ssl_load_buffered_message( ssl ) == 0 )
4849 have_buffered = 1;
4850 }
4851
4852 if( have_buffered == 0 )
4853#endif /* MBEDTLS_SSL_PROTO_DTLS */
4854 {
4855 ret = ssl_get_next_record( ssl );
4856 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4857 continue;
4858
4859 if( ret != 0 )
4860 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004861 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004862 return( ret );
4863 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004864 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004865 }
4866
4867 ret = mbedtls_ssl_handle_message_type( ssl );
4868
Hanno Becker40f50842018-08-15 14:48:01 +01004869#if defined(MBEDTLS_SSL_PROTO_DTLS)
4870 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4871 {
4872 /* Buffer future message */
4873 ret = ssl_buffer_message( ssl );
4874 if( ret != 0 )
4875 return( ret );
4876
4877 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4878 }
4879#endif /* MBEDTLS_SSL_PROTO_DTLS */
4880
Hanno Becker90333da2017-10-10 11:27:13 +01004881 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4882 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004883
4884 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004885 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004886 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004887 return( ret );
4888 }
4889
Hanno Becker327c93b2018-08-15 13:56:18 +01004890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004891 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004892 {
4893 mbedtls_ssl_update_handshake_status( ssl );
4894 }
Simon Butcher99000142016-10-13 17:21:01 +01004895 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004896 else
Simon Butcher99000142016-10-13 17:21:01 +01004897 {
Hanno Becker02f59072018-08-15 14:00:24 +01004898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004899 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004900 }
4901
4902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4903
4904 return( 0 );
4905}
4906
Hanno Becker40f50842018-08-15 14:48:01 +01004907#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004908static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004909{
Hanno Becker40f50842018-08-15 14:48:01 +01004910 if( ssl->in_left > ssl->next_record_offset )
4911 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004912
Hanno Becker40f50842018-08-15 14:48:01 +01004913 return( 0 );
4914}
4915
4916static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4917{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004918 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004919 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004920 int ret = 0;
4921
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004922 if( hs == NULL )
4923 return( -1 );
4924
Hanno Beckere00ae372018-08-20 09:39:42 +01004925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4926
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004927 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4928 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4929 {
4930 /* Check if we have seen a ChangeCipherSpec before.
4931 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004932 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004933 {
4934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4935 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004936 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004937 }
4938
Hanno Becker39b8bc92018-08-28 17:17:13 +01004939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004940 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4941 ssl->in_msglen = 1;
4942 ssl->in_msg[0] = 1;
4943
4944 /* As long as they are equal, the exact value doesn't matter. */
4945 ssl->in_left = 0;
4946 ssl->next_record_offset = 0;
4947
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004948 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004949 goto exit;
4950 }
Hanno Becker37f95322018-08-16 13:55:32 +01004951
Hanno Beckerb8f50142018-08-28 10:01:34 +01004952#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004953 /* Debug only */
4954 {
4955 unsigned offset;
4956 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4957 {
4958 hs_buf = &hs->buffering.hs[offset];
4959 if( hs_buf->is_valid == 1 )
4960 {
4961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4962 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004963 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004964 }
4965 }
4966 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004967#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004968
4969 /* Check if we have buffered and/or fully reassembled the
4970 * next handshake message. */
4971 hs_buf = &hs->buffering.hs[0];
4972 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4973 {
4974 /* Synthesize a record containing the buffered HS message. */
4975 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4976 ( hs_buf->data[2] << 8 ) |
4977 hs_buf->data[3];
4978
4979 /* Double-check that we haven't accidentally buffered
4980 * a message that doesn't fit into the input buffer. */
4981 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4982 {
4983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4985 }
4986
4987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4988 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4989 hs_buf->data, msg_len + 12 );
4990
4991 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4992 ssl->in_hslen = msg_len + 12;
4993 ssl->in_msglen = msg_len + 12;
4994 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4995
4996 ret = 0;
4997 goto exit;
4998 }
4999 else
5000 {
5001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5002 hs->in_msg_seq ) );
5003 }
5004
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005005 ret = -1;
5006
5007exit:
5008
5009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5010 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005011}
5012
Hanno Beckera02b0b42018-08-21 17:20:27 +01005013static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5014 size_t desired )
5015{
5016 int offset;
5017 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5019 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005020
Hanno Becker01315ea2018-08-21 17:22:17 +01005021 /* Get rid of future records epoch first, if such exist. */
5022 ssl_free_buffered_record( ssl );
5023
5024 /* Check if we have enough space available now. */
5025 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5026 hs->buffering.total_bytes_buffered ) )
5027 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005029 return( 0 );
5030 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005031
Hanno Becker4f432ad2018-08-28 10:02:32 +01005032 /* We don't have enough space to buffer the next expected handshake
5033 * message. Remove buffers used for future messages to gain space,
5034 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005035 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5036 offset >= 0; offset-- )
5037 {
5038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5039 offset ) );
5040
Hanno Beckerb309b922018-08-23 13:18:05 +01005041 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005042
5043 /* Check if we have enough space available now. */
5044 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5045 hs->buffering.total_bytes_buffered ) )
5046 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005048 return( 0 );
5049 }
5050 }
5051
5052 return( -1 );
5053}
5054
Hanno Becker40f50842018-08-15 14:48:01 +01005055static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5056{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005057 int ret = 0;
5058 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5059
5060 if( hs == NULL )
5061 return( 0 );
5062
5063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5064
5065 switch( ssl->in_msgtype )
5066 {
5067 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005069
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005070 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005071 break;
5072
5073 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005074 {
5075 unsigned recv_msg_seq_offset;
5076 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5077 mbedtls_ssl_hs_buffer *hs_buf;
5078 size_t msg_len = ssl->in_hslen - 12;
5079
5080 /* We should never receive an old handshake
5081 * message - double-check nonetheless. */
5082 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5083 {
5084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5086 }
5087
5088 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5089 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5090 {
5091 /* Silently ignore -- message too far in the future */
5092 MBEDTLS_SSL_DEBUG_MSG( 2,
5093 ( "Ignore future HS message with sequence number %u, "
5094 "buffering window %u - %u",
5095 recv_msg_seq, ssl->handshake->in_msg_seq,
5096 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5097
5098 goto exit;
5099 }
5100
5101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5102 recv_msg_seq, recv_msg_seq_offset ) );
5103
5104 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5105
5106 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005107 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005108 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005109 size_t reassembly_buf_sz;
5110
Hanno Becker37f95322018-08-16 13:55:32 +01005111 hs_buf->is_fragmented =
5112 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5113
5114 /* We copy the message back into the input buffer
5115 * after reassembly, so check that it's not too large.
5116 * This is an implementation-specific limitation
5117 * and not one from the standard, hence it is not
5118 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005119 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005120 {
5121 /* Ignore message */
5122 goto exit;
5123 }
5124
Hanno Beckere0b150f2018-08-21 15:51:03 +01005125 /* Check if we have enough space to buffer the message. */
5126 if( hs->buffering.total_bytes_buffered >
5127 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5128 {
5129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5131 }
5132
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005133 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5134 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005135
5136 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5137 hs->buffering.total_bytes_buffered ) )
5138 {
5139 if( recv_msg_seq_offset > 0 )
5140 {
5141 /* If we can't buffer a future message because
5142 * of space limitations -- ignore. */
5143 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",
5144 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5145 (unsigned) hs->buffering.total_bytes_buffered ) );
5146 goto exit;
5147 }
Hanno Beckere1801392018-08-21 16:51:05 +01005148 else
5149 {
5150 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",
5151 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5152 (unsigned) hs->buffering.total_bytes_buffered ) );
5153 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005154
Hanno Beckera02b0b42018-08-21 17:20:27 +01005155 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005156 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005157 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",
5158 (unsigned) msg_len,
5159 (unsigned) reassembly_buf_sz,
5160 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005161 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005162 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5163 goto exit;
5164 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005165 }
5166
5167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5168 msg_len ) );
5169
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005170 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5171 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005172 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005173 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005174 goto exit;
5175 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005176 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005177
5178 /* Prepare final header: copy msg_type, length and message_seq,
5179 * then add standardised fragment_offset and fragment_length */
5180 memcpy( hs_buf->data, ssl->in_msg, 6 );
5181 memset( hs_buf->data + 6, 0, 3 );
5182 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5183
5184 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005185
5186 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005187 }
5188 else
5189 {
5190 /* Make sure msg_type and length are consistent */
5191 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5192 {
5193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5194 /* Ignore */
5195 goto exit;
5196 }
5197 }
5198
Hanno Becker4422bbb2018-08-20 09:40:19 +01005199 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005200 {
5201 size_t frag_len, frag_off;
5202 unsigned char * const msg = hs_buf->data + 12;
5203
5204 /*
5205 * Check and copy current fragment
5206 */
5207
5208 /* Validation of header fields already done in
5209 * mbedtls_ssl_prepare_handshake_record(). */
5210 frag_off = ssl_get_hs_frag_off( ssl );
5211 frag_len = ssl_get_hs_frag_len( ssl );
5212
5213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5214 frag_off, frag_len ) );
5215 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5216
5217 if( hs_buf->is_fragmented )
5218 {
5219 unsigned char * const bitmask = msg + msg_len;
5220 ssl_bitmask_set( bitmask, frag_off, frag_len );
5221 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5222 msg_len ) == 0 );
5223 }
5224 else
5225 {
5226 hs_buf->is_complete = 1;
5227 }
5228
5229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5230 hs_buf->is_complete ? "" : "not yet " ) );
5231 }
5232
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005233 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005234 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005235
5236 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005237 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005238 break;
5239 }
5240
5241exit:
5242
5243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5244 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005245}
5246#endif /* MBEDTLS_SSL_PROTO_DTLS */
5247
Hanno Becker1097b342018-08-15 14:09:41 +01005248static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005249{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005250 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005251 * Consume last content-layer message and potentially
5252 * update in_msglen which keeps track of the contents'
5253 * consumption state.
5254 *
5255 * (1) Handshake messages:
5256 * Remove last handshake message, move content
5257 * and adapt in_msglen.
5258 *
5259 * (2) Alert messages:
5260 * Consume whole record content, in_msglen = 0.
5261 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005262 * (3) Change cipher spec:
5263 * Consume whole record content, in_msglen = 0.
5264 *
5265 * (4) Application data:
5266 * Don't do anything - the record layer provides
5267 * the application data as a stream transport
5268 * and consumes through mbedtls_ssl_read only.
5269 *
5270 */
5271
5272 /* Case (1): Handshake messages */
5273 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005274 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005275 /* Hard assertion to be sure that no application data
5276 * is in flight, as corrupting ssl->in_msglen during
5277 * ssl->in_offt != NULL is fatal. */
5278 if( ssl->in_offt != NULL )
5279 {
5280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5282 }
5283
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005284 /*
5285 * Get next Handshake message in the current record
5286 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005287
Hanno Becker4a810fb2017-05-24 16:27:30 +01005288 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005289 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005290 * current handshake content: If DTLS handshake
5291 * fragmentation is used, that's the fragment
5292 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005293 * size here is faulty and should be changed at
5294 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005295 * (2) While it doesn't seem to cause problems, one
5296 * has to be very careful not to assume that in_hslen
5297 * is always <= in_msglen in a sensible communication.
5298 * Again, it's wrong for DTLS handshake fragmentation.
5299 * The following check is therefore mandatory, and
5300 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005301 * Additionally, ssl->in_hslen might be arbitrarily out of
5302 * bounds after handling a DTLS message with an unexpected
5303 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005304 */
5305 if( ssl->in_hslen < ssl->in_msglen )
5306 {
5307 ssl->in_msglen -= ssl->in_hslen;
5308 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5309 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005310
Hanno Becker4a810fb2017-05-24 16:27:30 +01005311 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5312 ssl->in_msg, ssl->in_msglen );
5313 }
5314 else
5315 {
5316 ssl->in_msglen = 0;
5317 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005318
Hanno Becker4a810fb2017-05-24 16:27:30 +01005319 ssl->in_hslen = 0;
5320 }
5321 /* Case (4): Application data */
5322 else if( ssl->in_offt != NULL )
5323 {
5324 return( 0 );
5325 }
5326 /* Everything else (CCS & Alerts) */
5327 else
5328 {
5329 ssl->in_msglen = 0;
5330 }
5331
Hanno Becker1097b342018-08-15 14:09:41 +01005332 return( 0 );
5333}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005334
Hanno Beckere74d5562018-08-15 14:26:08 +01005335static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5336{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005337 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005338 return( 1 );
5339
5340 return( 0 );
5341}
5342
Hanno Becker5f066e72018-08-16 14:56:31 +01005343#if defined(MBEDTLS_SSL_PROTO_DTLS)
5344
5345static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5346{
5347 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5348 if( hs == NULL )
5349 return;
5350
Hanno Becker01315ea2018-08-21 17:22:17 +01005351 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005352 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005353 hs->buffering.total_bytes_buffered -=
5354 hs->buffering.future_record.len;
5355
5356 mbedtls_free( hs->buffering.future_record.data );
5357 hs->buffering.future_record.data = NULL;
5358 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005359}
5360
5361static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5362{
5363 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5364 unsigned char * rec;
5365 size_t rec_len;
5366 unsigned rec_epoch;
5367
5368 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5369 return( 0 );
5370
5371 if( hs == NULL )
5372 return( 0 );
5373
Hanno Becker5f066e72018-08-16 14:56:31 +01005374 rec = hs->buffering.future_record.data;
5375 rec_len = hs->buffering.future_record.len;
5376 rec_epoch = hs->buffering.future_record.epoch;
5377
5378 if( rec == NULL )
5379 return( 0 );
5380
Hanno Becker4cb782d2018-08-20 11:19:05 +01005381 /* Only consider loading future records if the
5382 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005383 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005384 return( 0 );
5385
Hanno Becker5f066e72018-08-16 14:56:31 +01005386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5387
5388 if( rec_epoch != ssl->in_epoch )
5389 {
5390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5391 goto exit;
5392 }
5393
5394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5395
5396 /* Double-check that the record is not too large */
5397 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5398 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5399 {
5400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5402 }
5403
5404 memcpy( ssl->in_hdr, rec, rec_len );
5405 ssl->in_left = rec_len;
5406 ssl->next_record_offset = 0;
5407
5408 ssl_free_buffered_record( ssl );
5409
5410exit:
5411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5412 return( 0 );
5413}
5414
5415static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5416{
5417 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5418 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005419 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005420
5421 /* Don't buffer future records outside handshakes. */
5422 if( hs == NULL )
5423 return( 0 );
5424
5425 /* Only buffer handshake records (we are only interested
5426 * in Finished messages). */
5427 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5428 return( 0 );
5429
5430 /* Don't buffer more than one future epoch record. */
5431 if( hs->buffering.future_record.data != NULL )
5432 return( 0 );
5433
Hanno Becker01315ea2018-08-21 17:22:17 +01005434 /* Don't buffer record if there's not enough buffering space remaining. */
5435 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5436 hs->buffering.total_bytes_buffered ) )
5437 {
5438 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",
5439 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5440 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005441 return( 0 );
5442 }
5443
Hanno Becker5f066e72018-08-16 14:56:31 +01005444 /* Buffer record */
5445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5446 ssl->in_epoch + 1 ) );
5447 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5448 rec_hdr_len + ssl->in_msglen );
5449
5450 /* ssl_parse_record_header() only considers records
5451 * of the next epoch as candidates for buffering. */
5452 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005453 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005454
5455 hs->buffering.future_record.data =
5456 mbedtls_calloc( 1, hs->buffering.future_record.len );
5457 if( hs->buffering.future_record.data == NULL )
5458 {
5459 /* If we run out of RAM trying to buffer a
5460 * record from the next epoch, just ignore. */
5461 return( 0 );
5462 }
5463
Hanno Becker01315ea2018-08-21 17:22:17 +01005464 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005465
Hanno Becker01315ea2018-08-21 17:22:17 +01005466 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005467 return( 0 );
5468}
5469
5470#endif /* MBEDTLS_SSL_PROTO_DTLS */
5471
Hanno Beckere74d5562018-08-15 14:26:08 +01005472static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005473{
5474 int ret;
5475
Hanno Becker5f066e72018-08-16 14:56:31 +01005476#if defined(MBEDTLS_SSL_PROTO_DTLS)
5477 /* We might have buffered a future record; if so,
5478 * and if the epoch matches now, load it.
5479 * On success, this call will set ssl->in_left to
5480 * the length of the buffered record, so that
5481 * the calls to ssl_fetch_input() below will
5482 * essentially be no-ops. */
5483 ret = ssl_load_buffered_record( ssl );
5484 if( ret != 0 )
5485 return( ret );
5486#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005487
Hanno Becker8b09b732019-05-08 12:03:28 +01005488 /* Reset in pointers to default state for TLS/DTLS records,
5489 * assuming no CID and no offset between record content and
5490 * record plaintext. */
5491 ssl_update_in_pointers( ssl );
5492
5493 /* Ensure that we have enough space available for the default form
5494 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5495 * with no space for CIDs counted in). */
5496 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5497 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005500 return( ret );
5501 }
5502
5503 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005506 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5507 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005508 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005509 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5510 {
5511 ret = ssl_buffer_future_record( ssl );
5512 if( ret != 0 )
5513 return( ret );
5514
5515 /* Fall through to handling of unexpected records */
5516 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5517 }
5518
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005519 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5520 {
5521 /* Skip unexpected record (but not whole datagram) */
5522 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005523 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005524
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5526 "(header)" ) );
5527 }
5528 else
5529 {
5530 /* Skip invalid record and the rest of the datagram */
5531 ssl->next_record_offset = 0;
5532 ssl->in_left = 0;
5533
5534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5535 "(header)" ) );
5536 }
5537
5538 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005539 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005540 }
5541#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005542 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005543 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005544
5545 /*
5546 * Read and optionally decrypt the message contents
5547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005549 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005552 return( ret );
5553 }
5554
5555 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005557 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005558 {
Hanno Becker43395762019-05-03 14:46:38 +01005559 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005560 if( ssl->next_record_offset < ssl->in_left )
5561 {
5562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5563 }
5564 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005565 else
5566#endif
5567 ssl->in_left = 0;
5568
5569 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005573 {
5574 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005575 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005576 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005577 /* Except when waiting for Finished as a bad mac here
5578 * probably means something went wrong in the handshake
5579 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5580 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5581 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5582 {
5583#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5584 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5585 {
5586 mbedtls_ssl_send_alert_message( ssl,
5587 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5588 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5589 }
5590#endif
5591 return( ret );
5592 }
5593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005595 if( ssl->conf->badmac_limit != 0 &&
5596 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5599 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005600 }
5601#endif
5602
Hanno Becker4a810fb2017-05-24 16:27:30 +01005603 /* As above, invalid records cause
5604 * dismissal of the whole datagram. */
5605
5606 ssl->next_record_offset = 0;
5607 ssl->in_left = 0;
5608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005610 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005611 }
5612
5613 return( ret );
5614 }
5615 else
5616#endif
5617 {
5618 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5620 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 mbedtls_ssl_send_alert_message( ssl,
5623 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5624 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005625 }
5626#endif
5627 return( ret );
5628 }
5629 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630
Simon Butcher99000142016-10-13 17:21:01 +01005631 return( 0 );
5632}
5633
5634int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5635{
5636 int ret;
5637
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005638 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005639 * Handle particular types of records
5640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 {
Simon Butcher99000142016-10-13 17:21:01 +01005643 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5644 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005645 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 }
5648
Hanno Beckere678eaa2018-08-21 14:57:46 +01005649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005650 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005651 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005652 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5654 ssl->in_msglen ) );
5655 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005656 }
5657
Hanno Beckere678eaa2018-08-21 14:57:46 +01005658 if( ssl->in_msg[0] != 1 )
5659 {
5660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5661 ssl->in_msg[0] ) );
5662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5663 }
5664
5665#if defined(MBEDTLS_SSL_PROTO_DTLS)
5666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5667 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5668 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5669 {
5670 if( ssl->handshake == NULL )
5671 {
5672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5673 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5674 }
5675
5676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5677 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5678 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005679#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005680 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005683 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005684 if( ssl->in_msglen != 2 )
5685 {
5686 /* Note: Standard allows for more than one 2 byte alert
5687 to be packed in a single message, but Mbed TLS doesn't
5688 currently support this. */
5689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5690 ssl->in_msglen ) );
5691 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5692 }
5693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005695 ssl->in_msg[0], ssl->in_msg[1] ) );
5696
5697 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005698 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005703 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005705 }
5706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5708 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5711 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005713
5714#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5715 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5716 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5717 {
Hanno Becker90333da2017-10-10 11:27:13 +01005718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005719 /* Will be handled when trying to parse ServerHello */
5720 return( 0 );
5721 }
5722#endif
5723
5724#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5725 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5726 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5727 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5728 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5729 {
5730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5731 /* Will be handled in mbedtls_ssl_parse_certificate() */
5732 return( 0 );
5733 }
5734#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5735
5736 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005737 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 }
5739
Hanno Beckerc76c6192017-06-06 10:03:17 +01005740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005742 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005743 /* Drop unexpected ApplicationData records,
5744 * except at the beginning of renegotiations */
5745 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5746 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5747#if defined(MBEDTLS_SSL_RENEGOTIATION)
5748 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5749 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005750#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005751 )
5752 {
5753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5754 return( MBEDTLS_ERR_SSL_NON_FATAL );
5755 }
5756
5757 if( ssl->handshake != NULL &&
5758 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5759 {
5760 ssl_handshake_wrapup_free_hs_transform( ssl );
5761 }
5762 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005763#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005764
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 return( 0 );
5766}
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005769{
5770 int ret;
5771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5773 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5774 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005775 {
5776 return( ret );
5777 }
5778
5779 return( 0 );
5780}
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005783 unsigned char level,
5784 unsigned char message )
5785{
5786 int ret;
5787
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005788 if( ssl == NULL || ssl->conf == NULL )
5789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005795 ssl->out_msglen = 2;
5796 ssl->out_msg[0] = level;
5797 ssl->out_msg[1] = message;
5798
Hanno Becker67bc7c32018-08-06 11:33:50 +01005799 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005802 return( ret );
5803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005805
5806 return( 0 );
5807}
5808
Paul Bakker5121ce52009-01-03 21:22:43 +00005809/*
5810 * Handshake functions
5811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5813 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5814 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5815 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5816 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5817 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5818 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005819/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005821{
Hanno Becker8759e162017-12-27 21:34:08 +00005822 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5827 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005828 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5829 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005832 ssl->state++;
5833 return( 0 );
5834 }
5835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005838}
5839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005841{
Hanno Becker8759e162017-12-27 21:34:08 +00005842 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5847 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005848 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5849 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005852 ssl->state++;
5853 return( 0 );
5854 }
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005858}
Gilles Peskinef9828522017-05-03 12:28:43 +02005859
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005860#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005861/* Some certificate support -> implement write and parse */
5862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005866 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005868 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5873 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005874 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5875 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005878 ssl->state++;
5879 return( 0 );
5880 }
5881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005883 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 {
5885 if( ssl->client_auth == 0 )
5886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005888 ssl->state++;
5889 return( 0 );
5890 }
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 /*
5894 * If using SSLv3 and got no cert, send an Alert message
5895 * (otherwise an empty Certificate message will be sent).
5896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5898 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 {
5900 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5902 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5903 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005906 goto write_msg;
5907 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910#endif /* MBEDTLS_SSL_CLI_C */
5911#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005912 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5917 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005918 }
5919 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005923
5924 /*
5925 * 0 . 0 handshake type
5926 * 1 . 3 handshake length
5927 * 4 . 6 length of all certs
5928 * 7 . 9 length of cert. 1
5929 * 10 . n-1 peer certificate
5930 * n . n+2 length of cert. 2
5931 * n+3 . ... upper level cert, etc.
5932 */
5933 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005935
Paul Bakker29087132010-03-21 21:03:34 +00005936 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 {
5938 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005939 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005942 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005944 }
5945
5946 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5947 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5948 ssl->out_msg[i + 2] = (unsigned char)( n );
5949
5950 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5951 i += n; crt = crt->next;
5952 }
5953
5954 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5955 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5956 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5957
5958 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5960 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005961
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005962#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005963write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005964#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005965
5966 ssl->state++;
5967
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005968 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005969 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005971 return( ret );
5972 }
5973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005975
Paul Bakkered27a042013-04-18 22:46:23 +02005976 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005977}
5978
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005979/*
5980 * Once the certificate message is read, parse it into a cert chain and
5981 * perform basic checks, but leave actual verification to the caller
5982 */
5983static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005984{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005985 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005986 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005987 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989#if defined(MBEDTLS_SSL_SRV_C)
5990#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005991 /*
5992 * Check if the client sent an empty certificate
5993 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005994 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005997 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5999 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6000 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006003
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006004 /* The client was asked for a certificate but didn't send
6005 one. The client should know what's going on, so we
6006 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006007 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006008 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006009 }
6010 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6014 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006015 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6019 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6020 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6021 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006024
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006025 /* The client was asked for a certificate but didn't send
6026 one. The client should know what's going on, so we
6027 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006028 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006029 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 }
6031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6033 MBEDTLS_SSL_PROTO_TLS1_2 */
6034#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6040 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6045 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006048 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6049 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006051 }
6052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006054
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006058 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006059
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006060 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006064 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6065 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 }
6068
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006069 /* In case we tried to reuse a session but it failed */
6070 if( ssl->session_negotiate->peer_cert != NULL )
6071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6073 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006074 }
6075
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006076 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6082 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 }
6085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006087
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006088 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006089
6090 while( i < ssl->in_hslen )
6091 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006092 if ( i + 3 > ssl->in_hslen ) {
6093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6094 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6095 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6096 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006098 if( ssl->in_msg[i] != 0 )
6099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006101 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6102 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006104 }
6105
6106 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6107 | (unsigned int) ssl->in_msg[i + 2];
6108 i += 3;
6109
6110 if( n < 128 || i + n > ssl->in_hslen )
6111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006113 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6114 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 }
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006119 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006120 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006121 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006122 case 0: /*ok*/
6123 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6124 /* Ignore certificate with an unknown algorithm: maybe a
6125 prior certificate was already trusted. */
6126 break;
6127
6128 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6129 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6130 goto crt_parse_der_failed;
6131
6132 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6133 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6134 goto crt_parse_der_failed;
6135
6136 default:
6137 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6138 crt_parse_der_failed:
6139 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141 return( ret );
6142 }
6143
6144 i += n;
6145 }
6146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006148
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006149 /*
6150 * On client, make sure the server cert doesn't change during renego to
6151 * avoid "triple handshake" attack: https://secure-resumption.com/
6152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006154 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006156 {
6157 if( ssl->session->peer_cert == NULL )
6158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006160 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6161 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006163 }
6164
6165 if( ssl->session->peer_cert->raw.len !=
6166 ssl->session_negotiate->peer_cert->raw.len ||
6167 memcmp( ssl->session->peer_cert->raw.p,
6168 ssl->session_negotiate->peer_cert->raw.p,
6169 ssl->session->peer_cert->raw.len ) != 0 )
6170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006172 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6173 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006175 }
6176 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006178
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006179 return( 0 );
6180}
6181
6182int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6183{
6184 int ret;
6185 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006186 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006187#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6188 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6189 ? ssl->handshake->sni_authmode
6190 : ssl->conf->authmode;
6191#else
6192 const int authmode = ssl->conf->authmode;
6193#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006194 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006195
6196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6197
6198 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6199 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6200 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6201 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6202 {
6203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6204 ssl->state++;
6205 return( 0 );
6206 }
6207
6208#if defined(MBEDTLS_SSL_SRV_C)
6209 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6210 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6211 {
6212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6213 ssl->state++;
6214 return( 0 );
6215 }
6216
6217 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6218 authmode == MBEDTLS_SSL_VERIFY_NONE )
6219 {
6220 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006222
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006223 ssl->state++;
6224 return( 0 );
6225 }
6226#endif
6227
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006228#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6229 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006230 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006231 {
6232 goto crt_verify;
6233 }
6234#endif
6235
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006236 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006237 {
6238 /* mbedtls_ssl_read_record may have sent an alert already. We
6239 let it decide whether to alert. */
6240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6241 return( ret );
6242 }
6243
6244 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6245 {
6246#if defined(MBEDTLS_SSL_SRV_C)
6247 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6248 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6249 {
6250 ret = 0;
6251 }
6252#endif
6253
6254 ssl->state++;
6255 return( ret );
6256 }
6257
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006258#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6259 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006260 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006261
6262crt_verify:
6263 if( ssl->handshake->ecrs_enabled)
6264 rs_ctx = &ssl->handshake->ecrs_ctx;
6265#endif
6266
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006267 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006268 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006269 mbedtls_x509_crt *ca_chain;
6270 mbedtls_x509_crl *ca_crl;
6271
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006272#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006273 if( ssl->handshake->sni_ca_chain != NULL )
6274 {
6275 ca_chain = ssl->handshake->sni_ca_chain;
6276 ca_crl = ssl->handshake->sni_ca_crl;
6277 }
6278 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006279#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006280 {
6281 ca_chain = ssl->conf->ca_chain;
6282 ca_crl = ssl->conf->ca_crl;
6283 }
6284
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006285 /*
6286 * Main check: verify certificate
6287 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006288 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006289 ssl->session_negotiate->peer_cert,
6290 ca_chain, ca_crl,
6291 ssl->conf->cert_profile,
6292 ssl->hostname,
6293 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006294 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006295
6296 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006299 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006300
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006301#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6302 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006303 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006304#endif
6305
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006306 /*
6307 * Secondary checks: always done, but change 'ret' only if it was 0
6308 */
6309
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006310#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006313
6314 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006316 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006317 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006318 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006321 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006323 }
6324 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006325#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006328 ciphersuite_info,
6329 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006330 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006333 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006335 }
6336
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006337 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6338 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6339 * with details encoded in the verification flags. All other kinds
6340 * of error codes, including those from the user provided f_vrfy
6341 * functions, are treated as fatal and lead to a failure of
6342 * ssl_parse_certificate even if verification was optional. */
6343 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6344 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6345 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6346 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006348 }
6349
6350 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6353 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6354 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006355
6356 if( ret != 0 )
6357 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006358 uint8_t alert;
6359
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006360 /* The certificate may have been rejected for several reasons.
6361 Pick one and send the corresponding alert. Which alert to send
6362 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006363 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6364 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6365 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6366 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6367 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6368 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6369 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6370 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6371 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6372 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6373 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6374 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6375 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6376 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6377 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6378 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6379 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6380 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6381 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6382 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006383 else
6384 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6386 alert );
6387 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006388
Hanno Beckere6706e62017-05-15 16:05:15 +01006389#if defined(MBEDTLS_DEBUG_C)
6390 if( ssl->session_negotiate->verify_result != 0 )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6393 ssl->session_negotiate->verify_result ) );
6394 }
6395 else
6396 {
6397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6398 }
6399#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006400 }
6401
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006402 ssl->state++;
6403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006405
6406 return( ret );
6407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6409 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6410 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6411 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6412 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6413 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6414 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006417{
6418 int ret;
6419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 ssl->out_msglen = 1;
6424 ssl->out_msg[0] = 1;
6425
Paul Bakker5121ce52009-01-03 21:22:43 +00006426 ssl->state++;
6427
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006428 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006431 return( ret );
6432 }
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006435
6436 return( 0 );
6437}
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006440{
6441 int ret;
6442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006444
Hanno Becker327c93b2018-08-15 13:56:18 +01006445 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006448 return( ret );
6449 }
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006454 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6455 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 }
6458
Hanno Beckere678eaa2018-08-21 14:57:46 +01006459 /* CCS records are only accepted if they have length 1 and content '1',
6460 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006461
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006462 /*
6463 * Switch to our negotiated transform and session parameters for inbound
6464 * data.
6465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006467 ssl->transform_in = ssl->transform_negotiate;
6468 ssl->session_in = ssl->session_negotiate;
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006471 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006474 ssl_dtls_replay_reset( ssl );
6475#endif
6476
6477 /* Increment epoch */
6478 if( ++ssl->in_epoch == 0 )
6479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006481 /* This is highly unlikely to happen for legitimate reasons, so
6482 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006484 }
6485 }
6486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006488 memset( ssl->in_ctr, 0, 8 );
6489
Hanno Beckerf5970a02019-05-08 09:38:41 +01006490 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6493 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006498 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6499 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006501 }
6502 }
6503#endif
6504
Paul Bakker5121ce52009-01-03 21:22:43 +00006505 ssl->state++;
6506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006508
6509 return( 0 );
6510}
6511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6513 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006514{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006515 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6518 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6519 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006520 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006521 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6524#if defined(MBEDTLS_SHA512_C)
6525 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006526 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6527 else
6528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#if defined(MBEDTLS_SHA256_C)
6530 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006531 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006532 else
6533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006537 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006538 }
Paul Bakker380da532012-04-18 16:10:25 +00006539}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6544 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006545 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6546 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6549#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006550 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006553 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006556}
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006559 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006560{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6562 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006563 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6564 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006565#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6567#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006568 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006571 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006574}
6575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6577 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6578static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006579 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006580{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006581 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6582 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006583}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006584#endif
Paul Bakker380da532012-04-18 16:10:25 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6587#if defined(MBEDTLS_SHA256_C)
6588static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006589 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006590{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006591 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006592}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006593#endif
Paul Bakker380da532012-04-18 16:10:25 +00006594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_SHA512_C)
6596static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006597 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006598{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006599 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006600}
Paul Bakker769075d2012-11-24 11:26:46 +01006601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006605static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006607{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006608 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006609 mbedtls_md5_context md5;
6610 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006611
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 unsigned char padbuf[48];
6613 unsigned char md5sum[16];
6614 unsigned char sha1sum[20];
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006617 if( !session )
6618 session = ssl->session;
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006621
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006622 mbedtls_md5_init( &md5 );
6623 mbedtls_sha1_init( &sha1 );
6624
6625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006627
6628 /*
6629 * SSLv3:
6630 * hash =
6631 * MD5( master + pad2 +
6632 * MD5( handshake + sender + master + pad1 ) )
6633 * + SHA1( master + pad2 +
6634 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 */
6636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006638 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6639 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006640#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006643 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6644 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006645#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006648 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006649
Paul Bakker1ef83d62012-04-11 12:09:53 +00006650 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006652 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6653 mbedtls_md5_update_ret( &md5, session->master, 48 );
6654 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6655 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006657 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6658 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6659 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6660 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661
Paul Bakker1ef83d62012-04-11 12:09:53 +00006662 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006664 mbedtls_md5_starts_ret( &md5 );
6665 mbedtls_md5_update_ret( &md5, session->master, 48 );
6666 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6667 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6668 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006670 mbedtls_sha1_starts_ret( &sha1 );
6671 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6672 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6673 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6674 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006678 mbedtls_md5_free( &md5 );
6679 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006680
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006681 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6682 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6683 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006690static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006692{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006693 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006694 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006695 mbedtls_md5_context md5;
6696 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006697 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006700 if( !session )
6701 session = ssl->session;
6702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006704
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006705 mbedtls_md5_init( &md5 );
6706 mbedtls_sha1_init( &sha1 );
6707
6708 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6709 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006710
Paul Bakker1ef83d62012-04-11 12:09:53 +00006711 /*
6712 * TLSv1:
6713 * hash = PRF( master, finished_label,
6714 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6715 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006718 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6719 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006720#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006723 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6724 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006725#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006728 ? "client finished"
6729 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006730
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006731 mbedtls_md5_finish_ret( &md5, padbuf );
6732 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006733
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006734 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006735 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006738
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006739 mbedtls_md5_free( &md5 );
6740 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006741
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006742 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006745}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6749#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006750static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006752{
6753 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006754 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006755 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006756 unsigned char padbuf[32];
6757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006759 if( !session )
6760 session = ssl->session;
6761
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006762 mbedtls_sha256_init( &sha256 );
6763
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006765
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006766 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006767
6768 /*
6769 * TLSv1.2:
6770 * hash = PRF( master, finished_label,
6771 * Hash( handshake ) )[0.11]
6772 */
6773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774#if !defined(MBEDTLS_SHA256_ALT)
6775 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006776 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006777#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006780 ? "client finished"
6781 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006782
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006783 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006784
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006785 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006786 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006789
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006790 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006791
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006792 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006799static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006801{
6802 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006803 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006804 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006805 unsigned char padbuf[48];
6806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006808 if( !session )
6809 session = ssl->session;
6810
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006811 mbedtls_sha512_init( &sha512 );
6812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006814
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006815 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006816
6817 /*
6818 * TLSv1.2:
6819 * hash = PRF( master, finished_label,
6820 * Hash( handshake ) )[0.11]
6821 */
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006824 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6825 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006826#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006829 ? "client finished"
6830 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006831
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006832 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006833
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006834 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006835 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006838
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006839 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006840
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006841 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845#endif /* MBEDTLS_SHA512_C */
6846#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006849{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006851
6852 /*
6853 * Free our handshake params
6854 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006855 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006857 ssl->handshake = NULL;
6858
6859 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006860 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006861 */
6862 if( ssl->transform )
6863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 mbedtls_ssl_transform_free( ssl->transform );
6865 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006866 }
6867 ssl->transform = ssl->transform_negotiate;
6868 ssl->transform_negotiate = NULL;
6869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006871}
6872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006874{
6875 int resume = ssl->handshake->resume;
6876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879#if defined(MBEDTLS_SSL_RENEGOTIATION)
6880 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006883 ssl->renego_records_seen = 0;
6884 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006885#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006886
6887 /*
6888 * Free the previous session and switch in the current one
6889 */
Paul Bakker0a597072012-09-25 21:55:46 +00006890 if( ssl->session )
6891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006893 /* RFC 7366 3.1: keep the EtM state */
6894 ssl->session_negotiate->encrypt_then_mac =
6895 ssl->session->encrypt_then_mac;
6896#endif
6897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 mbedtls_ssl_session_free( ssl->session );
6899 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006900 }
6901 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006902 ssl->session_negotiate = NULL;
6903
Paul Bakker0a597072012-09-25 21:55:46 +00006904 /*
6905 * Add cache entry
6906 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006907 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006908 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006909 resume == 0 )
6910 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006911 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006913 }
Paul Bakker0a597072012-09-25 21:55:46 +00006914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006916 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006917 ssl->handshake->flight != NULL )
6918 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006919 /* Cancel handshake timer */
6920 ssl_set_timer( ssl, 0 );
6921
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006922 /* Keep last flight around in case we need to resend it:
6923 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006925 }
6926 else
6927#endif
6928 ssl_handshake_wrapup_free_hs_transform( ssl );
6929
Paul Bakker48916f92012-09-16 19:57:18 +00006930 ssl->state++;
6931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006933}
6934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006936{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006937 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006940
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006941 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006942
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006943 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006945 /*
6946 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6947 * may define some other value. Currently (early 2016), no defined
6948 * ciphersuite does this (and this is unlikely to change as activity has
6949 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006954 ssl->verify_data_len = hash_len;
6955 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006956#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006957
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6960 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006961
6962 /*
6963 * In case of session resuming, invert the client and server
6964 * ChangeCipherSpec messages order.
6965 */
Paul Bakker0a597072012-09-25 21:55:46 +00006966 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006973 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 }
6977 else
6978 ssl->state++;
6979
Paul Bakker48916f92012-09-16 19:57:18 +00006980 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006981 * Switch to our negotiated transform and session parameters for outbound
6982 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006988 {
6989 unsigned char i;
6990
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006991 /* Remember current epoch settings for resending */
6992 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006993 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006994
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006995 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006996 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006997
6998 /* Increment epoch */
6999 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007000 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007001 break;
7002
7003 /* The loop goes to its end iff the counter is wrapping */
7004 if( i == 0 )
7005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7007 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007008 }
7009 }
7010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007012 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007014 ssl->transform_out = ssl->transform_negotiate;
7015 ssl->session_out = ssl->session_negotiate;
7016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7018 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7023 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007024 }
7025 }
7026#endif
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007031#endif
7032
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007033 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007034 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007036 return( ret );
7037 }
7038
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007039#if defined(MBEDTLS_SSL_PROTO_DTLS)
7040 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7041 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7042 {
7043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7044 return( ret );
7045 }
7046#endif
7047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007049
7050 return( 0 );
7051}
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007054#define SSL_MAX_HASH_LEN 36
7055#else
7056#define SSL_MAX_HASH_LEN 12
7057#endif
7058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007060{
Paul Bakker23986e52011-04-24 08:57:21 +00007061 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007062 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007063 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007067 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068
Hanno Becker327c93b2018-08-15 13:56:18 +01007069 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072 return( ret );
7073 }
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007078 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7079 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007081 }
7082
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007083 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_PROTO_SSL3)
7085 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007086 hash_len = 36;
7087 else
7088#endif
7089 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7092 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007095 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7096 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007098 }
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007101 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7105 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 }
7108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007110 ssl->verify_data_len = hash_len;
7111 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007112#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007113
Paul Bakker0a597072012-09-25 21:55:46 +00007114 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007121 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007123#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007124 }
7125 else
7126 ssl->state++;
7127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007129 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007131#endif
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007134
7135 return( 0 );
7136}
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7143 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7144 mbedtls_md5_init( &handshake->fin_md5 );
7145 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007146 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7147 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7150#if defined(MBEDTLS_SHA256_C)
7151 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007152 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#if defined(MBEDTLS_SHA512_C)
7155 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007156 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007159
7160 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007161
7162#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7163 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7164 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7165#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if defined(MBEDTLS_DHM_C)
7168 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if defined(MBEDTLS_ECDH_C)
7171 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007172#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007173#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007174 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007175#if defined(MBEDTLS_SSL_CLI_C)
7176 handshake->ecjpake_cache = NULL;
7177 handshake->ecjpake_cache_len = 0;
7178#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007179#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007180
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007181#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007182 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007183#endif
7184
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007185#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7186 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7187#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007188}
7189
Hanno Becker611a83b2018-01-03 14:27:32 +00007190void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007191{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7195 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007196
Hanno Becker92231322018-01-03 15:32:51 +00007197#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 mbedtls_md_init( &transform->md_ctx_enc );
7199 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007200#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007201}
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007206}
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007209{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007210 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007211 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007213 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007215 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007216 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007217
7218 /*
7219 * Either the pointers are now NULL or cleared properly and can be freed.
7220 * Now allocate missing structures.
7221 */
7222 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007223 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007224 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007225 }
Paul Bakker48916f92012-09-16 19:57:18 +00007226
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007227 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007228 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007229 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007230 }
Paul Bakker48916f92012-09-16 19:57:18 +00007231
Paul Bakker82788fb2014-10-20 13:59:19 +02007232 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007233 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007234 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007235 }
Paul Bakker48916f92012-09-16 19:57:18 +00007236
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007237 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007238 if( ssl->handshake == NULL ||
7239 ssl->transform_negotiate == NULL ||
7240 ssl->session_negotiate == NULL )
7241 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 mbedtls_free( ssl->handshake );
7245 mbedtls_free( ssl->transform_negotiate );
7246 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007247
7248 ssl->handshake = NULL;
7249 ssl->transform_negotiate = NULL;
7250 ssl->session_negotiate = NULL;
7251
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007253 }
7254
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007255 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007257 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007258 ssl_handshake_params_init( ssl->handshake );
7259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007261 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7262 {
7263 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007264
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7266 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7267 else
7268 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007269
7270 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007271 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007272#endif
7273
Paul Bakker48916f92012-09-16 19:57:18 +00007274 return( 0 );
7275}
7276
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007277#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007278/* Dummy cookie callbacks for defaults */
7279static int ssl_cookie_write_dummy( void *ctx,
7280 unsigned char **p, unsigned char *end,
7281 const unsigned char *cli_id, size_t cli_id_len )
7282{
7283 ((void) ctx);
7284 ((void) p);
7285 ((void) end);
7286 ((void) cli_id);
7287 ((void) cli_id_len);
7288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007290}
7291
7292static int ssl_cookie_check_dummy( void *ctx,
7293 const unsigned char *cookie, size_t cookie_len,
7294 const unsigned char *cli_id, size_t cli_id_len )
7295{
7296 ((void) ctx);
7297 ((void) cookie);
7298 ((void) cookie_len);
7299 ((void) cli_id);
7300 ((void) cli_id_len);
7301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007303}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007304#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007305
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007306/* Once ssl->out_hdr as the address of the beginning of the
7307 * next outgoing record is set, deduce the other pointers.
7308 *
7309 * Note: For TLS, we save the implicit record sequence number
7310 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7311 * and the caller has to make sure there's space for this.
7312 */
7313
7314static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7315 mbedtls_ssl_transform *transform )
7316{
7317#if defined(MBEDTLS_SSL_PROTO_DTLS)
7318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7319 {
7320 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007321#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007322 ssl->out_cid = ssl->out_ctr + 8;
7323 ssl->out_len = ssl->out_cid;
7324 if( transform != NULL )
7325 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007326#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007327 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007328#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007329 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007330 }
7331 else
7332#endif
7333 {
7334 ssl->out_ctr = ssl->out_hdr - 8;
7335 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007336#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007337 ssl->out_cid = ssl->out_len;
7338#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007339 ssl->out_iv = ssl->out_hdr + 5;
7340 }
7341
7342 /* Adjust out_msg to make space for explicit IV, if used. */
7343 if( transform != NULL &&
7344 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7345 {
7346 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7347 }
7348 else
7349 ssl->out_msg = ssl->out_iv;
7350}
7351
7352/* Once ssl->in_hdr as the address of the beginning of the
7353 * next incoming record is set, deduce the other pointers.
7354 *
7355 * Note: For TLS, we save the implicit record sequence number
7356 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7357 * and the caller has to make sure there's space for this.
7358 */
7359
Hanno Beckerf5970a02019-05-08 09:38:41 +01007360static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007361{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007362 /* This function sets the pointers to match the case
7363 * of unprotected TLS/DTLS records, with both ssl->in_iv
7364 * and ssl->in_msg pointing to the beginning of the record
7365 * content.
7366 *
7367 * When decrypting a protected record, ssl->in_msg
7368 * will be shifted to point to the beginning of the
7369 * record plaintext.
7370 */
7371
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007372#if defined(MBEDTLS_SSL_PROTO_DTLS)
7373 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7374 {
Hanno Becker70e79282019-05-03 14:34:53 +01007375 /* This sets the header pointers to match records
7376 * without CID. When we receive a record containing
7377 * a CID, the fields are shifted accordingly in
7378 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007379 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007380#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007381 ssl->in_cid = ssl->in_ctr + 8;
7382 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007383#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007384 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007385#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007386 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007387 }
7388 else
7389#endif
7390 {
7391 ssl->in_ctr = ssl->in_hdr - 8;
7392 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007393#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007394 ssl->in_cid = ssl->in_len;
7395#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007396 ssl->in_iv = ssl->in_hdr + 5;
7397 }
7398
Hanno Beckerf5970a02019-05-08 09:38:41 +01007399 /* This will be adjusted at record decryption time. */
7400 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007401}
7402
Paul Bakker5121ce52009-01-03 21:22:43 +00007403/*
7404 * Initialize an SSL context
7405 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007406void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7407{
7408 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7409}
7410
7411/*
7412 * Setup an SSL context
7413 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007414
7415static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7416{
7417 /* Set the incoming and outgoing record pointers. */
7418#if defined(MBEDTLS_SSL_PROTO_DTLS)
7419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7420 {
7421 ssl->out_hdr = ssl->out_buf;
7422 ssl->in_hdr = ssl->in_buf;
7423 }
7424 else
7425#endif /* MBEDTLS_SSL_PROTO_DTLS */
7426 {
7427 ssl->out_hdr = ssl->out_buf + 8;
7428 ssl->in_hdr = ssl->in_buf + 8;
7429 }
7430
7431 /* Derive other internal pointers. */
7432 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007433 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007434}
7435
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007436int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007437 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007438{
Paul Bakker48916f92012-09-16 19:57:18 +00007439 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007440
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007441 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007442
7443 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007444 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007445 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007446
7447 /* Set to NULL in case of an error condition */
7448 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007449
Angus Grattond8213d02016-05-25 20:56:48 +10007450 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7451 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 {
Angus Grattond8213d02016-05-25 20:56:48 +10007453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007454 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007455 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007456 }
7457
7458 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7459 if( ssl->out_buf == NULL )
7460 {
7461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007462 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007463 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007464 }
7465
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007466 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007467
Paul Bakker48916f92012-09-16 19:57:18 +00007468 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007469 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007470
7471 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007472
7473error:
7474 mbedtls_free( ssl->in_buf );
7475 mbedtls_free( ssl->out_buf );
7476
7477 ssl->conf = NULL;
7478
7479 ssl->in_buf = NULL;
7480 ssl->out_buf = NULL;
7481
7482 ssl->in_hdr = NULL;
7483 ssl->in_ctr = NULL;
7484 ssl->in_len = NULL;
7485 ssl->in_iv = NULL;
7486 ssl->in_msg = NULL;
7487
7488 ssl->out_hdr = NULL;
7489 ssl->out_ctr = NULL;
7490 ssl->out_len = NULL;
7491 ssl->out_iv = NULL;
7492 ssl->out_msg = NULL;
7493
k-stachowiak9f7798e2018-07-31 16:52:32 +02007494 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007495}
7496
7497/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007498 * Reset an initialized and used SSL context for re-use while retaining
7499 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007500 *
7501 * If partial is non-zero, keep data in the input buffer and client ID.
7502 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007503 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007504static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007505{
Paul Bakker48916f92012-09-16 19:57:18 +00007506 int ret;
7507
Hanno Becker7e772132018-08-10 12:38:21 +01007508#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7509 !defined(MBEDTLS_SSL_SRV_C)
7510 ((void) partial);
7511#endif
7512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007514
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007515 /* Cancel any possibly running timer */
7516 ssl_set_timer( ssl, 0 );
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SSL_RENEGOTIATION)
7519 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007520 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007521
7522 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7524 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007527
Paul Bakker7eb013f2011-10-06 12:37:39 +00007528 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007529 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007530
7531 ssl->in_msgtype = 0;
7532 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007534 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007535 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007538 ssl_dtls_replay_reset( ssl );
7539#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007540
7541 ssl->in_hslen = 0;
7542 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007543
7544 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007545
7546 ssl->out_msgtype = 0;
7547 ssl->out_msglen = 0;
7548 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7550 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007551 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007552#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007553
Hanno Becker19859472018-08-06 09:40:20 +01007554 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7555
Paul Bakker48916f92012-09-16 19:57:18 +00007556 ssl->transform_in = NULL;
7557 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007558
Hanno Becker78640902018-08-13 16:35:15 +01007559 ssl->session_in = NULL;
7560 ssl->session_out = NULL;
7561
Angus Grattond8213d02016-05-25 20:56:48 +10007562 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007563
7564#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007565 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007566#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7567 {
7568 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007569 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007570 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7573 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7576 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7579 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007580 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007581 }
7582#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007583
Paul Bakker48916f92012-09-16 19:57:18 +00007584 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 mbedtls_ssl_transform_free( ssl->transform );
7587 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007588 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007589 }
Paul Bakker48916f92012-09-16 19:57:18 +00007590
Paul Bakkerc0463502013-02-14 11:19:38 +01007591 if( ssl->session )
7592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 mbedtls_ssl_session_free( ssl->session );
7594 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007595 ssl->session = NULL;
7596 }
7597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007599 ssl->alpn_chosen = NULL;
7600#endif
7601
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007602#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007603#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007604 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007605#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007606 {
7607 mbedtls_free( ssl->cli_id );
7608 ssl->cli_id = NULL;
7609 ssl->cli_id_len = 0;
7610 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007611#endif
7612
Paul Bakker48916f92012-09-16 19:57:18 +00007613 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7614 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007615
7616 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007617}
7618
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007619/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007620 * Reset an initialized and used SSL context for re-use while retaining
7621 * all application-set variables, function pointers and data.
7622 */
7623int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7624{
7625 return( ssl_session_reset_int( ssl, 0 ) );
7626}
7627
7628/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007629 * SSL set accessors
7630 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007631void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007633 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007634}
7635
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007636void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007638 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007639}
7640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007642void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007644 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007645}
7646#endif
7647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007649void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007651 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007652}
7653#endif
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007656
Hanno Becker1841b0a2018-08-24 11:13:57 +01007657void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7658 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007659{
7660 ssl->disable_datagram_packing = !allow_packing;
7661}
7662
7663void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7664 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007665{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007666 conf->hs_timeout_min = min;
7667 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007668}
7669#endif
7670
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007671void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007672{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007673 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007674}
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007677void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007678 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007679 void *p_vrfy )
7680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007681 conf->f_vrfy = f_vrfy;
7682 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007685
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007686void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007687 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007688 void *p_rng )
7689{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007690 conf->f_rng = f_rng;
7691 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007692}
7693
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007694void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007695 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007696 void *p_dbg )
7697{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007698 conf->f_dbg = f_dbg;
7699 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007700}
7701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007703 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007704 mbedtls_ssl_send_t *f_send,
7705 mbedtls_ssl_recv_t *f_recv,
7706 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007707{
7708 ssl->p_bio = p_bio;
7709 ssl->f_send = f_send;
7710 ssl->f_recv = f_recv;
7711 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007712}
7713
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007714#if defined(MBEDTLS_SSL_PROTO_DTLS)
7715void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7716{
7717 ssl->mtu = mtu;
7718}
7719#endif
7720
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007721void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007722{
7723 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007724}
7725
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007726void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7727 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007728 mbedtls_ssl_set_timer_t *f_set_timer,
7729 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007730{
7731 ssl->p_timer = p_timer;
7732 ssl->f_set_timer = f_set_timer;
7733 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007734
7735 /* Make sure we start with no timer running */
7736 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007737}
7738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007740void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007741 void *p_cache,
7742 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7743 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007744{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007745 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007746 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007747 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_CLI_C)
7752int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007753{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007754 int ret;
7755
7756 if( ssl == NULL ||
7757 session == NULL ||
7758 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007759 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007762 }
7763
7764 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7765 return( ret );
7766
Paul Bakker0a597072012-09-25 21:55:46 +00007767 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007768
7769 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007772
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007773void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007774 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007775{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007776 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7777 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7778 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7779 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007780}
7781
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007782void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007783 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007784 int major, int minor )
7785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007787 return;
7788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007790 return;
7791
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007792 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007793}
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007796void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007797 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007798{
7799 conf->cert_profile = profile;
7800}
7801
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007802/* Append a new keycert entry to a (possibly empty) list */
7803static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7804 mbedtls_x509_crt *cert,
7805 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007806{
niisato8ee24222018-06-25 19:05:48 +09007807 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007808
niisato8ee24222018-06-25 19:05:48 +09007809 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7810 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007811 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007812
niisato8ee24222018-06-25 19:05:48 +09007813 new_cert->cert = cert;
7814 new_cert->key = key;
7815 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007816
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007817 /* Update head is the list was null, else add to the end */
7818 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007819 {
niisato8ee24222018-06-25 19:05:48 +09007820 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007821 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007822 else
7823 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007824 mbedtls_ssl_key_cert *cur = *head;
7825 while( cur->next != NULL )
7826 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007827 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007828 }
7829
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007830 return( 0 );
7831}
7832
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007833int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007834 mbedtls_x509_crt *own_cert,
7835 mbedtls_pk_context *pk_key )
7836{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007837 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007838}
7839
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007840void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007841 mbedtls_x509_crt *ca_chain,
7842 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007844 conf->ca_chain = ca_chain;
7845 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007848
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007849#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7850int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7851 mbedtls_x509_crt *own_cert,
7852 mbedtls_pk_context *pk_key )
7853{
7854 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7855 own_cert, pk_key ) );
7856}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007857
7858void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7859 mbedtls_x509_crt *ca_chain,
7860 mbedtls_x509_crl *ca_crl )
7861{
7862 ssl->handshake->sni_ca_chain = ca_chain;
7863 ssl->handshake->sni_ca_crl = ca_crl;
7864}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007865
7866void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7867 int authmode )
7868{
7869 ssl->handshake->sni_authmode = authmode;
7870}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007871#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7872
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007873#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007874/*
7875 * Set EC J-PAKE password for current handshake
7876 */
7877int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7878 const unsigned char *pw,
7879 size_t pw_len )
7880{
7881 mbedtls_ecjpake_role role;
7882
Janos Follath8eb64132016-06-03 15:40:57 +01007883 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7885
7886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7887 role = MBEDTLS_ECJPAKE_SERVER;
7888 else
7889 role = MBEDTLS_ECJPAKE_CLIENT;
7890
7891 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7892 role,
7893 MBEDTLS_MD_SHA256,
7894 MBEDTLS_ECP_DP_SECP256R1,
7895 pw, pw_len ) );
7896}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007897#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007900int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007901 const unsigned char *psk, size_t psk_len,
7902 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007903{
Paul Bakker6db455e2013-09-18 17:29:31 +02007904 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007909
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007910 /* Identity len will be encoded on two bytes */
7911 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007912 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007913 {
7914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7915 }
7916
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007917 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007918 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007919 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007920
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007921 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007922 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007923 conf->psk_len = 0;
7924 }
7925 if( conf->psk_identity != NULL )
7926 {
7927 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007928 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007929 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007930 }
7931
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007932 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7933 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007934 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007935 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007936 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007937 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007938 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007939 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007940 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007941
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007942 conf->psk_len = psk_len;
7943 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007944
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007945 memcpy( conf->psk, psk, conf->psk_len );
7946 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007947
7948 return( 0 );
7949}
7950
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007951int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7952 const unsigned char *psk, size_t psk_len )
7953{
7954 if( psk == NULL || ssl->handshake == NULL )
7955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7956
7957 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7959
7960 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007961 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007962 mbedtls_platform_zeroize( ssl->handshake->psk,
7963 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007964 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007965 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007966 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007967
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007968 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007969 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007970
7971 ssl->handshake->psk_len = psk_len;
7972 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7973
7974 return( 0 );
7975}
7976
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007977void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007979 size_t),
7980 void *p_psk )
7981{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007982 conf->f_psk = f_psk;
7983 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007986
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007987#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007988
7989#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007990int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007991{
7992 int ret;
7993
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007994 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7995 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7996 {
7997 mbedtls_mpi_free( &conf->dhm_P );
7998 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007999 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008001
8002 return( 0 );
8003}
Hanno Becker470a8c42017-10-04 15:28:46 +01008004#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008005
Hanno Beckera90658f2017-10-04 15:29:08 +01008006int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8007 const unsigned char *dhm_P, size_t P_len,
8008 const unsigned char *dhm_G, size_t G_len )
8009{
8010 int ret;
8011
8012 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8013 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8014 {
8015 mbedtls_mpi_free( &conf->dhm_P );
8016 mbedtls_mpi_free( &conf->dhm_G );
8017 return( ret );
8018 }
8019
8020 return( 0 );
8021}
Paul Bakker5121ce52009-01-03 21:22:43 +00008022
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008023int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008024{
8025 int ret;
8026
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008027 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8028 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8029 {
8030 mbedtls_mpi_free( &conf->dhm_P );
8031 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008032 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008033 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008034
8035 return( 0 );
8036}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008037#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008038
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008039#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8040/*
8041 * Set the minimum length for Diffie-Hellman parameters
8042 */
8043void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8044 unsigned int bitlen )
8045{
8046 conf->dhm_min_bitlen = bitlen;
8047}
8048#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8049
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008050#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008051/*
8052 * Set allowed/preferred hashes for handshake signatures
8053 */
8054void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8055 const int *hashes )
8056{
8057 conf->sig_hashes = hashes;
8058}
Hanno Becker947194e2017-04-07 13:25:49 +01008059#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008060
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008061#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008062/*
8063 * Set the allowed elliptic curves
8064 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008065void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008066 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008067{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008068 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008069}
Hanno Becker947194e2017-04-07 13:25:49 +01008070#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008071
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008072#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008074{
Hanno Becker947194e2017-04-07 13:25:49 +01008075 /* Initialize to suppress unnecessary compiler warning */
8076 size_t hostname_len = 0;
8077
8078 /* Check if new hostname is valid before
8079 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008080 if( hostname != NULL )
8081 {
8082 hostname_len = strlen( hostname );
8083
8084 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8086 }
8087
8088 /* Now it's clear that we will overwrite the old hostname,
8089 * so we can free it safely */
8090
8091 if( ssl->hostname != NULL )
8092 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008093 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008094 mbedtls_free( ssl->hostname );
8095 }
8096
8097 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008098
Paul Bakker5121ce52009-01-03 21:22:43 +00008099 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008100 {
8101 ssl->hostname = NULL;
8102 }
8103 else
8104 {
8105 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008106 if( ssl->hostname == NULL )
8107 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008108
Hanno Becker947194e2017-04-07 13:25:49 +01008109 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008110
Hanno Becker947194e2017-04-07 13:25:49 +01008111 ssl->hostname[hostname_len] = '\0';
8112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008113
8114 return( 0 );
8115}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008116#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008117
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008118#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008119void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008121 const unsigned char *, size_t),
8122 void *p_sni )
8123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008124 conf->f_sni = f_sni;
8125 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008130int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008131{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008132 size_t cur_len, tot_len;
8133 const char **p;
8134
8135 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008136 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8137 * MUST NOT be truncated."
8138 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008139 */
8140 tot_len = 0;
8141 for( p = protos; *p != NULL; p++ )
8142 {
8143 cur_len = strlen( *p );
8144 tot_len += cur_len;
8145
8146 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008148 }
8149
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008150 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008151
8152 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008153}
8154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008156{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008157 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008160
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008161void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008162{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008163 conf->max_major_ver = major;
8164 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008165}
8166
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008167void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008169 conf->min_major_ver = major;
8170 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008171}
8172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008174void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008175{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008176 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008177}
8178#endif
8179
Janos Follath088ce432017-04-10 12:42:31 +01008180#if defined(MBEDTLS_SSL_SRV_C)
8181void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8182 char cert_req_ca_list )
8183{
8184 conf->cert_req_ca_list = cert_req_ca_list;
8185}
8186#endif
8187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008189void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008191 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008192}
8193#endif
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008196void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008197{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008198 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008199}
8200#endif
8201
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008202#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008203void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008205 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008206}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008207#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008210int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008213 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008216 }
8217
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008218 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008219
8220 return( 0 );
8221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008225void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008226{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008227 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008232void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008233{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008234 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008235}
8236#endif
8237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008238void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008240 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008241}
8242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008244void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008246 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008247}
8248
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008249void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008250{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008251 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008252}
8253
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008254void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008255 const unsigned char period[8] )
8256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008257 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008262#if defined(MBEDTLS_SSL_CLI_C)
8263void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008264{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008265 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008266}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008267#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008268
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008270void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8271 mbedtls_ssl_ticket_write_t *f_ticket_write,
8272 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8273 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008274{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008275 conf->f_ticket_write = f_ticket_write;
8276 conf->f_ticket_parse = f_ticket_parse;
8277 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008278}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008281
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008282#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8283void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8284 mbedtls_ssl_export_keys_t *f_export_keys,
8285 void *p_export_keys )
8286{
8287 conf->f_export_keys = f_export_keys;
8288 conf->p_export_keys = p_export_keys;
8289}
8290#endif
8291
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008292#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008293void mbedtls_ssl_conf_async_private_cb(
8294 mbedtls_ssl_config *conf,
8295 mbedtls_ssl_async_sign_t *f_async_sign,
8296 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8297 mbedtls_ssl_async_resume_t *f_async_resume,
8298 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008299 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008300{
8301 conf->f_async_sign_start = f_async_sign;
8302 conf->f_async_decrypt_start = f_async_decrypt;
8303 conf->f_async_resume = f_async_resume;
8304 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008305 conf->p_async_config_data = async_config_data;
8306}
8307
Gilles Peskine8f97af72018-04-26 11:46:10 +02008308void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8309{
8310 return( conf->p_async_config_data );
8311}
8312
Gilles Peskine1febfef2018-04-30 11:54:39 +02008313void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008314{
8315 if( ssl->handshake == NULL )
8316 return( NULL );
8317 else
8318 return( ssl->handshake->user_async_ctx );
8319}
8320
Gilles Peskine1febfef2018-04-30 11:54:39 +02008321void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008322 void *ctx )
8323{
8324 if( ssl->handshake != NULL )
8325 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008326}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008327#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008328
Paul Bakker5121ce52009-01-03 21:22:43 +00008329/*
8330 * SSL get accessors
8331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008333{
8334 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8335}
8336
Hanno Becker8b170a02017-10-10 11:51:19 +01008337int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8338{
8339 /*
8340 * Case A: We're currently holding back
8341 * a message for further processing.
8342 */
8343
8344 if( ssl->keep_current_message == 1 )
8345 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008347 return( 1 );
8348 }
8349
8350 /*
8351 * Case B: Further records are pending in the current datagram.
8352 */
8353
8354#if defined(MBEDTLS_SSL_PROTO_DTLS)
8355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8356 ssl->in_left > ssl->next_record_offset )
8357 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008359 return( 1 );
8360 }
8361#endif /* MBEDTLS_SSL_PROTO_DTLS */
8362
8363 /*
8364 * Case C: A handshake message is being processed.
8365 */
8366
Hanno Becker8b170a02017-10-10 11:51:19 +01008367 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8368 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008370 return( 1 );
8371 }
8372
8373 /*
8374 * Case D: An application data message is being processed
8375 */
8376 if( ssl->in_offt != NULL )
8377 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008379 return( 1 );
8380 }
8381
8382 /*
8383 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008384 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008385 * we implement support for multiple alerts in single records.
8386 */
8387
8388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8389 return( 0 );
8390}
8391
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008392uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008393{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008394 if( ssl->session != NULL )
8395 return( ssl->session->verify_result );
8396
8397 if( ssl->session_negotiate != NULL )
8398 return( ssl->session_negotiate->verify_result );
8399
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008400 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008401}
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008404{
Paul Bakker926c8e42013-03-06 10:23:34 +01008405 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008406 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008409}
8410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008415 {
8416 switch( ssl->minor_ver )
8417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008419 return( "DTLSv1.0" );
8420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008422 return( "DTLSv1.2" );
8423
8424 default:
8425 return( "unknown (DTLS)" );
8426 }
8427 }
8428#endif
8429
Paul Bakker43ca69c2011-01-15 17:35:19 +00008430 switch( ssl->minor_ver )
8431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008433 return( "SSLv3.0" );
8434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008436 return( "TLSv1.0" );
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008439 return( "TLSv1.1" );
8440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008442 return( "TLSv1.2" );
8443
Paul Bakker43ca69c2011-01-15 17:35:19 +00008444 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008445 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008446 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008447}
8448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008450{
Hanno Becker3136ede2018-08-17 15:28:19 +01008451 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008453 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008454
Hanno Becker43395762019-05-03 14:46:38 +01008455 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8456
Hanno Becker78640902018-08-13 16:35:15 +01008457 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008458 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460#if defined(MBEDTLS_ZLIB_SUPPORT)
8461 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8462 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008463#endif
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 case MBEDTLS_MODE_GCM:
8468 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008469 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008471 transform_expansion = transform->minlen;
8472 break;
8473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008475
8476 block_size = mbedtls_cipher_get_block_size(
8477 &transform->cipher_ctx_enc );
8478
Hanno Becker3136ede2018-08-17 15:28:19 +01008479 /* Expansion due to the addition of the MAC. */
8480 transform_expansion += transform->maclen;
8481
8482 /* Expansion due to the addition of CBC padding;
8483 * Theoretically up to 256 bytes, but we never use
8484 * more than the block size of the underlying cipher. */
8485 transform_expansion += block_size;
8486
8487 /* For TLS 1.1 or higher, an explicit IV is added
8488 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008489#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8490 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008491 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008492#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008493
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008494 break;
8495
8496 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008499 }
8500
Hanno Beckera5a2b082019-05-15 14:03:01 +01008501#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008502 if( transform->out_cid_len != 0 )
8503 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008505
Hanno Becker43395762019-05-03 14:46:38 +01008506 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008507}
8508
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008509#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8510size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8511{
8512 size_t max_len;
8513
8514 /*
8515 * Assume mfl_code is correct since it was checked when set
8516 */
Angus Grattond8213d02016-05-25 20:56:48 +10008517 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008518
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008519 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008520 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008521 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008522 {
Angus Grattond8213d02016-05-25 20:56:48 +10008523 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008524 }
8525
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008526 /* During a handshake, use the value being negotiated */
8527 if( ssl->session_negotiate != NULL &&
8528 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8529 {
8530 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8531 }
8532
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008533 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008534}
8535#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8536
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008537#if defined(MBEDTLS_SSL_PROTO_DTLS)
8538static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8539{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008540 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8542 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8543 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8544 return ( 0 );
8545
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008546 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8547 return( ssl->mtu );
8548
8549 if( ssl->mtu == 0 )
8550 return( ssl->handshake->mtu );
8551
8552 return( ssl->mtu < ssl->handshake->mtu ?
8553 ssl->mtu : ssl->handshake->mtu );
8554}
8555#endif /* MBEDTLS_SSL_PROTO_DTLS */
8556
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008557int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8558{
8559 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8560
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008561#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8562 !defined(MBEDTLS_SSL_PROTO_DTLS)
8563 (void) ssl;
8564#endif
8565
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008566#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8567 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8568
8569 if( max_len > mfl )
8570 max_len = mfl;
8571#endif
8572
8573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008574 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008575 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008576 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008577 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8578 const size_t overhead = (size_t) ret;
8579
8580 if( ret < 0 )
8581 return( ret );
8582
8583 if( mtu <= overhead )
8584 {
8585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8586 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8587 }
8588
8589 if( max_len > mtu - overhead )
8590 max_len = mtu - overhead;
8591 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008592#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008593
Hanno Becker0defedb2018-08-10 12:35:02 +01008594#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8595 !defined(MBEDTLS_SSL_PROTO_DTLS)
8596 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008597#endif
8598
8599 return( (int) max_len );
8600}
8601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602#if defined(MBEDTLS_X509_CRT_PARSE_C)
8603const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008604{
8605 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008606 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008607
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008608 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612#if defined(MBEDTLS_SSL_CLI_C)
8613int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008614{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008615 if( ssl == NULL ||
8616 dst == NULL ||
8617 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008618 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008621 }
8622
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008623 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008626
Paul Bakker5121ce52009-01-03 21:22:43 +00008627/*
Paul Bakker1961b702013-01-25 14:49:24 +01008628 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008633
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008634 if( ssl == NULL || ssl->conf == NULL )
8635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008638 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008644#endif
8645
Paul Bakker1961b702013-01-25 14:49:24 +01008646 return( ret );
8647}
8648
8649/*
8650 * Perform the SSL handshake
8651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008653{
8654 int ret = 0;
8655
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008656 if( ssl == NULL || ssl->conf == NULL )
8657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008664
8665 if( ret != 0 )
8666 break;
8667 }
8668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008670
8671 return( ret );
8672}
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674#if defined(MBEDTLS_SSL_RENEGOTIATION)
8675#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008676/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008677 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008680{
8681 int ret;
8682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008684
8685 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8687 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008688
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008689 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008690 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008692 return( ret );
8693 }
8694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008696
8697 return( 0 );
8698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008700
8701/*
8702 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 * - any side: calling mbedtls_ssl_renegotiate(),
8704 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8705 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008706 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008707 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008711{
8712 int ret;
8713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008715
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008716 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8717 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008718
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008719 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8720 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008722 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008724 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008726 ssl->handshake->out_msg_seq = 1;
8727 else
8728 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008729 }
8730#endif
8731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8733 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008738 return( ret );
8739 }
8740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008742
8743 return( 0 );
8744}
8745
8746/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008747 * Renegotiate current connection on client,
8748 * or request renegotiation on server
8749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008753
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008754 if( ssl == NULL || ssl->conf == NULL )
8755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008758 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008759 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008765
8766 /* Did we already try/start sending HelloRequest? */
8767 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008769
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008770 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008771 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008775 /*
8776 * On client, either start the renegotiation process or,
8777 * if already in progress, continue the handshake
8778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008783
8784 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008787 return( ret );
8788 }
8789 }
8790 else
8791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008795 return( ret );
8796 }
8797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008799
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008800 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008801}
8802
8803/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008804 * Check record counters and renegotiate if they're above the limit.
8805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008807{
Andres AG2196c7f2016-12-15 17:01:16 +00008808 size_t ep_len = ssl_ep_len( ssl );
8809 int in_ctr_cmp;
8810 int out_ctr_cmp;
8811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8813 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008814 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008815 {
8816 return( 0 );
8817 }
8818
Andres AG2196c7f2016-12-15 17:01:16 +00008819 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8820 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008821 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008822 ssl->conf->renego_period + ep_len, 8 - ep_len );
8823
8824 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008825 {
8826 return( 0 );
8827 }
8828
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008833
8834/*
8835 * Receive application data decrypted from the SSL layer
8836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008838{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008839 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008840 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008841
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008842 if( ssl == NULL || ssl->conf == NULL )
8843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008848 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008851 return( ret );
8852
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008853 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008855 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008856 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008857 return( ret );
8858 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008859 }
8860#endif
8861
Hanno Becker4a810fb2017-05-24 16:27:30 +01008862 /*
8863 * Check if renegotiation is necessary and/or handshake is
8864 * in process. If yes, perform/continue, and fall through
8865 * if an unexpected packet is received while the client
8866 * is waiting for the ServerHello.
8867 *
8868 * (There is no equivalent to the last condition on
8869 * the server-side as it is not treated as within
8870 * a handshake while waiting for the ClientHello
8871 * after a renegotiation request.)
8872 */
8873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008875 ret = ssl_check_ctr_renegotiate( ssl );
8876 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8877 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008880 return( ret );
8881 }
8882#endif
8883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008884 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008887 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8888 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008891 return( ret );
8892 }
8893 }
8894
Hanno Beckere41158b2017-10-23 13:30:32 +01008895 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008896 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008897 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008898 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008899 if( ssl->f_get_timer != NULL &&
8900 ssl->f_get_timer( ssl->p_timer ) == -1 )
8901 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008902 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008903 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008904
Hanno Becker327c93b2018-08-15 13:56:18 +01008905 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008906 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008907 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8908 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008909
Hanno Becker4a810fb2017-05-24 16:27:30 +01008910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8911 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008912 }
8913
8914 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008916 {
8917 /*
8918 * OpenSSL sends empty messages to randomize the IV
8919 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008920 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008923 return( 0 );
8924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008926 return( ret );
8927 }
8928 }
8929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008933
Hanno Becker4a810fb2017-05-24 16:27:30 +01008934 /*
8935 * - For client-side, expect SERVER_HELLO_REQUEST.
8936 * - For server-side, expect CLIENT_HELLO.
8937 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8938 */
8939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008941 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008943 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008946
8947 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008950 {
8951 continue;
8952 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008955 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008956#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008957
Hanno Becker4a810fb2017-05-24 16:27:30 +01008958#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008959 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008963
8964 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008967 {
8968 continue;
8969 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008972 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008973#endif /* MBEDTLS_SSL_SRV_C */
8974
Hanno Becker21df7f92017-10-17 11:03:26 +01008975#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008976 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008977 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8978 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8979 ssl->conf->allow_legacy_renegotiation ==
8980 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8981 {
8982 /*
8983 * Accept renegotiation request
8984 */
Paul Bakker48916f92012-09-16 19:57:18 +00008985
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008986 /* DTLS clients need to know renego is server-initiated */
8987#if defined(MBEDTLS_SSL_PROTO_DTLS)
8988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8989 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8990 {
8991 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8992 }
8993#endif
8994 ret = ssl_start_renegotiation( ssl );
8995 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8996 ret != 0 )
8997 {
8998 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8999 return( ret );
9000 }
9001 }
9002 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009003#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009004 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009005 /*
9006 * Refuse renegotiation
9007 */
9008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#if defined(MBEDTLS_SSL_PROTO_SSL3)
9012 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009013 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009014 /* SSLv3 does not have a "no_renegotiation" warning, so
9015 we send a fatal alert and abort the connection. */
9016 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9017 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9018 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009019 }
9020 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9022#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9023 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9024 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9027 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9028 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009029 {
9030 return( ret );
9031 }
Paul Bakker48916f92012-09-16 19:57:18 +00009032 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9035 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009039 }
Paul Bakker48916f92012-09-16 19:57:18 +00009040 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009041
Hanno Becker90333da2017-10-10 11:27:13 +01009042 /* At this point, we don't know whether the renegotiation has been
9043 * completed or not. The cases to consider are the following:
9044 * 1) The renegotiation is complete. In this case, no new record
9045 * has been read yet.
9046 * 2) The renegotiation is incomplete because the client received
9047 * an application data record while awaiting the ServerHello.
9048 * 3) The renegotiation is incomplete because the client received
9049 * a non-handshake, non-application data message while awaiting
9050 * the ServerHello.
9051 * In each of these case, looping will be the proper action:
9052 * - For 1), the next iteration will read a new record and check
9053 * if it's application data.
9054 * - For 2), the loop condition isn't satisfied as application data
9055 * is present, hence continue is the same as break
9056 * - For 3), the loop condition is satisfied and read_record
9057 * will re-deliver the message that was held back by the client
9058 * when expecting the ServerHello.
9059 */
9060 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009061 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009062#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009064 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009065 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009066 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009067 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009070 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009072 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009073 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009081 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009082 }
9083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009088 }
9089
9090 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009091
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009092 /* We're going to return something now, cancel timer,
9093 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009095 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009096
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009097#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009098 /* If we requested renego but received AppData, resend HelloRequest.
9099 * Do it now, after setting in_offt, to avoid taking this branch
9100 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009102 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009104 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009105 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009108 return( ret );
9109 }
9110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009113 }
9114
9115 n = ( len < ssl->in_msglen )
9116 ? len : ssl->in_msglen;
9117
9118 memcpy( buf, ssl->in_offt, n );
9119 ssl->in_msglen -= n;
9120
9121 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009122 {
9123 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009124 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009125 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009126 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009127 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009128 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009129 /* more data available */
9130 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009134
Paul Bakker23986e52011-04-24 08:57:21 +00009135 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009136}
9137
9138/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009139 * Send application data to be encrypted by the SSL layer, taking care of max
9140 * fragment length and buffer size.
9141 *
9142 * According to RFC 5246 Section 6.2.1:
9143 *
9144 * Zero-length fragments of Application data MAY be sent as they are
9145 * potentially useful as a traffic analysis countermeasure.
9146 *
9147 * Therefore, it is possible that the input message length is 0 and the
9148 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009149 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009150static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009151 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009152{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009153 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9154 const size_t max_len = (size_t) ret;
9155
9156 if( ret < 0 )
9157 {
9158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9159 return( ret );
9160 }
9161
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009162 if( len > max_len )
9163 {
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é-Gonnard37e08e12014-10-13 17:55:52 +02009166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009168 "maximum fragment length: %d > %d",
9169 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009171 }
9172 else
9173#endif
9174 len = max_len;
9175 }
Paul Bakker887bd502011-06-08 13:10:54 +00009176
Paul Bakker5121ce52009-01-03 21:22:43 +00009177 if( ssl->out_left != 0 )
9178 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009179 /*
9180 * The user has previously tried to send the data and
9181 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9182 * written. In this case, we expect the high-level write function
9183 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009188 return( ret );
9189 }
9190 }
Paul Bakker887bd502011-06-08 13:10:54 +00009191 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009192 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009193 /*
9194 * The user is trying to send a message the first time, so we need to
9195 * copy the data into the internal buffers and setup the data structure
9196 * to keep track of partial writes
9197 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009198 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009200 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009201
Hanno Becker67bc7c32018-08-06 11:33:50 +01009202 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009205 return( ret );
9206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009207 }
9208
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009209 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009210}
9211
9212/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009213 * Write application data, doing 1/n-1 splitting if necessary.
9214 *
9215 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009216 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009217 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009220static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009221 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009222{
9223 int ret;
9224
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009225 if( ssl->conf->cbc_record_splitting ==
9226 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009227 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9229 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9230 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009231 {
9232 return( ssl_write_real( ssl, buf, len ) );
9233 }
9234
9235 if( ssl->split_done == 0 )
9236 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009237 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009238 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009239 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009240 }
9241
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009242 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9243 return( ret );
9244 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009245
9246 return( ret + 1 );
9247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009249
9250/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009251 * Write application data (public-facing wrapper)
9252 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009253int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009254{
9255 int ret;
9256
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009258
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009259 if( ssl == NULL || ssl->conf == NULL )
9260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9261
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009262#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009263 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9264 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009265 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009266 return( ret );
9267 }
9268#endif
9269
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009270 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009271 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009272 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009273 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009275 return( ret );
9276 }
9277 }
9278
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009279#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009280 ret = ssl_write_split( ssl, buf, len );
9281#else
9282 ret = ssl_write_real( ssl, buf, len );
9283#endif
9284
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009286
9287 return( ret );
9288}
9289
9290/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009291 * Notify the peer that the connection is being closed
9292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009294{
9295 int ret;
9296
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009297 if( ssl == NULL || ssl->conf == NULL )
9298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009301
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009302 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9308 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9309 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009312 return( ret );
9313 }
9314 }
9315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009317
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009318 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009319}
9320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009322{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009323 if( transform == NULL )
9324 return;
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009327 deflateEnd( &transform->ctx_deflate );
9328 inflateEnd( &transform->ctx_inflate );
9329#endif
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9332 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009333
Hanno Becker92231322018-01-03 15:32:51 +00009334#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 mbedtls_md_free( &transform->md_ctx_enc );
9336 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009337#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009338
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009339 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009340}
9341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#if defined(MBEDTLS_X509_CRT_PARSE_C)
9343static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009346
9347 while( cur != NULL )
9348 {
9349 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009351 cur = next;
9352 }
9353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009355
Hanno Becker0271f962018-08-16 13:23:47 +01009356#if defined(MBEDTLS_SSL_PROTO_DTLS)
9357
9358static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9359{
9360 unsigned offset;
9361 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9362
9363 if( hs == NULL )
9364 return;
9365
Hanno Becker283f5ef2018-08-24 09:34:47 +01009366 ssl_free_buffered_record( ssl );
9367
Hanno Becker0271f962018-08-16 13:23:47 +01009368 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009369 ssl_buffering_free_slot( ssl, offset );
9370}
9371
9372static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9373 uint8_t slot )
9374{
9375 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9376 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009377
9378 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9379 return;
9380
Hanno Beckere605b192018-08-21 15:59:07 +01009381 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009382 {
Hanno Beckere605b192018-08-21 15:59:07 +01009383 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009384 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009385 mbedtls_free( hs_buf->data );
9386 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009387 }
9388}
9389
9390#endif /* MBEDTLS_SSL_PROTO_DTLS */
9391
Gilles Peskine9b562d52018-04-25 20:32:43 +02009392void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009393{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009394 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9395
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009396 if( handshake == NULL )
9397 return;
9398
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009399#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9400 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9401 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009402 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009403 handshake->async_in_progress = 0;
9404 }
9405#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9406
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009407#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9408 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9409 mbedtls_md5_free( &handshake->fin_md5 );
9410 mbedtls_sha1_free( &handshake->fin_sha1 );
9411#endif
9412#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9413#if defined(MBEDTLS_SHA256_C)
9414 mbedtls_sha256_free( &handshake->fin_sha256 );
9415#endif
9416#if defined(MBEDTLS_SHA512_C)
9417 mbedtls_sha512_free( &handshake->fin_sha512 );
9418#endif
9419#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421#if defined(MBEDTLS_DHM_C)
9422 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424#if defined(MBEDTLS_ECDH_C)
9425 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009426#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009427#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009428 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009429#if defined(MBEDTLS_SSL_CLI_C)
9430 mbedtls_free( handshake->ecjpake_cache );
9431 handshake->ecjpake_cache = NULL;
9432 handshake->ecjpake_cache_len = 0;
9433#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009434#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009435
Janos Follath4ae5c292016-02-10 11:27:43 +00009436#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9437 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009438 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009440#endif
9441
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009442#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9443 if( handshake->psk != NULL )
9444 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009445 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009446 mbedtls_free( handshake->psk );
9447 }
9448#endif
9449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9451 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009452 /*
9453 * Free only the linked list wrapper, not the keys themselves
9454 * since the belong to the SNI callback
9455 */
9456 if( handshake->sni_key_cert != NULL )
9457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009459
9460 while( cur != NULL )
9461 {
9462 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009464 cur = next;
9465 }
9466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009468
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009469#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009470 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009471#endif
9472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#if defined(MBEDTLS_SSL_PROTO_DTLS)
9474 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009475 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009476 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009477#endif
9478
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009479 mbedtls_platform_zeroize( handshake,
9480 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009481}
9482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009484{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009485 if( session == NULL )
9486 return;
9487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009489 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 mbedtls_x509_crt_free( session->peer_cert );
9492 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009493 }
Paul Bakkered27a042013-04-18 22:46:23 +02009494#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009495
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009496#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009498#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009500 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009501}
9502
Paul Bakker5121ce52009-01-03 21:22:43 +00009503/*
9504 * Free an SSL context
9505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009507{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009508 if( ssl == NULL )
9509 return;
9510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009512
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009513 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009514 {
Angus Grattond8213d02016-05-25 20:56:48 +10009515 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009517 }
9518
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009519 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009520 {
Angus Grattond8213d02016-05-25 20:56:48 +10009521 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009523 }
9524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009526 if( ssl->compress_buf != NULL )
9527 {
Angus Grattond8213d02016-05-25 20:56:48 +10009528 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009530 }
9531#endif
9532
Paul Bakker48916f92012-09-16 19:57:18 +00009533 if( ssl->transform )
9534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 mbedtls_ssl_transform_free( ssl->transform );
9536 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009537 }
9538
9539 if( ssl->handshake )
9540 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009541 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9543 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 mbedtls_free( ssl->handshake );
9546 mbedtls_free( ssl->transform_negotiate );
9547 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009548 }
9549
Paul Bakkerc0463502013-02-14 11:19:38 +01009550 if( ssl->session )
9551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 mbedtls_ssl_session_free( ssl->session );
9553 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009554 }
9555
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009556#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009557 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009558 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009559 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009561 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9565 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9568 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009569 }
9570#endif
9571
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009572#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009574#endif
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009577
Paul Bakker86f04f42013-02-14 11:20:09 +01009578 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009579 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009580}
9581
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009582/*
9583 * Initialze mbedtls_ssl_config
9584 */
9585void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9586{
9587 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9588}
9589
Simon Butcherc97b6972015-12-27 23:48:17 +00009590#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009591static int ssl_preset_default_hashes[] = {
9592#if defined(MBEDTLS_SHA512_C)
9593 MBEDTLS_MD_SHA512,
9594 MBEDTLS_MD_SHA384,
9595#endif
9596#if defined(MBEDTLS_SHA256_C)
9597 MBEDTLS_MD_SHA256,
9598 MBEDTLS_MD_SHA224,
9599#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009600#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009601 MBEDTLS_MD_SHA1,
9602#endif
9603 MBEDTLS_MD_NONE
9604};
Simon Butcherc97b6972015-12-27 23:48:17 +00009605#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009606
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009607static int ssl_preset_suiteb_ciphersuites[] = {
9608 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9609 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9610 0
9611};
9612
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009613#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009614static int ssl_preset_suiteb_hashes[] = {
9615 MBEDTLS_MD_SHA256,
9616 MBEDTLS_MD_SHA384,
9617 MBEDTLS_MD_NONE
9618};
9619#endif
9620
9621#if defined(MBEDTLS_ECP_C)
9622static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9623 MBEDTLS_ECP_DP_SECP256R1,
9624 MBEDTLS_ECP_DP_SECP384R1,
9625 MBEDTLS_ECP_DP_NONE
9626};
9627#endif
9628
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009629/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009630 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009631 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009632int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009633 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009634{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009635#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009636 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009637#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009638
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009639 /* Use the functions here so that they are covered in tests,
9640 * but otherwise access member directly for efficiency */
9641 mbedtls_ssl_conf_endpoint( conf, endpoint );
9642 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009643
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009644 /*
9645 * Things that are common to all presets
9646 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009647#if defined(MBEDTLS_SSL_CLI_C)
9648 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9649 {
9650 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9651#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9652 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9653#endif
9654 }
9655#endif
9656
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009657#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009658 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009659#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009660
9661#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9662 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9663#endif
9664
9665#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9666 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9667#endif
9668
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009669#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9670 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9671#endif
9672
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009673#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009674 conf->f_cookie_write = ssl_cookie_write_dummy;
9675 conf->f_cookie_check = ssl_cookie_check_dummy;
9676#endif
9677
9678#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9679 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9680#endif
9681
Janos Follath088ce432017-04-10 12:42:31 +01009682#if defined(MBEDTLS_SSL_SRV_C)
9683 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9684#endif
9685
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009686#if defined(MBEDTLS_SSL_PROTO_DTLS)
9687 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9688 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9689#endif
9690
9691#if defined(MBEDTLS_SSL_RENEGOTIATION)
9692 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009693 memset( conf->renego_period, 0x00, 2 );
9694 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009695#endif
9696
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009697#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9698 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9699 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009700 const unsigned char dhm_p[] =
9701 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9702 const unsigned char dhm_g[] =
9703 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9704
Hanno Beckera90658f2017-10-04 15:29:08 +01009705 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9706 dhm_p, sizeof( dhm_p ),
9707 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009708 {
9709 return( ret );
9710 }
9711 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009712#endif
9713
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009714 /*
9715 * Preset-specific defaults
9716 */
9717 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009718 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009719 /*
9720 * NSA Suite B
9721 */
9722 case MBEDTLS_SSL_PRESET_SUITEB:
9723 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9724 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9725 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9726 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9727
9728 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9729 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9730 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9731 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9732 ssl_preset_suiteb_ciphersuites;
9733
9734#if defined(MBEDTLS_X509_CRT_PARSE_C)
9735 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009736#endif
9737
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009738#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009739 conf->sig_hashes = ssl_preset_suiteb_hashes;
9740#endif
9741
9742#if defined(MBEDTLS_ECP_C)
9743 conf->curve_list = ssl_preset_suiteb_curves;
9744#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009745 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009746
9747 /*
9748 * Default
9749 */
9750 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009751 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9752 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9753 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9754 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9755 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9756 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9757 MBEDTLS_SSL_MIN_MINOR_VERSION :
9758 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009759 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9760 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9761
9762#if defined(MBEDTLS_SSL_PROTO_DTLS)
9763 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9764 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9765#endif
9766
9767 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9768 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9769 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9770 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9771 mbedtls_ssl_list_ciphersuites();
9772
9773#if defined(MBEDTLS_X509_CRT_PARSE_C)
9774 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9775#endif
9776
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009777#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009778 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009779#endif
9780
9781#if defined(MBEDTLS_ECP_C)
9782 conf->curve_list = mbedtls_ecp_grp_id_list();
9783#endif
9784
9785#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9786 conf->dhm_min_bitlen = 1024;
9787#endif
9788 }
9789
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009790 return( 0 );
9791}
9792
9793/*
9794 * Free mbedtls_ssl_config
9795 */
9796void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9797{
9798#if defined(MBEDTLS_DHM_C)
9799 mbedtls_mpi_free( &conf->dhm_P );
9800 mbedtls_mpi_free( &conf->dhm_G );
9801#endif
9802
9803#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9804 if( conf->psk != NULL )
9805 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009806 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009807 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009808 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009809 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009810 }
9811
9812 if( conf->psk_identity != NULL )
9813 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009814 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009815 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009816 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009817 conf->psk_identity_len = 0;
9818 }
9819#endif
9820
9821#if defined(MBEDTLS_X509_CRT_PARSE_C)
9822 ssl_key_cert_free( conf->key_cert );
9823#endif
9824
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009825 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009826}
9827
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009828#if defined(MBEDTLS_PK_C) && \
9829 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009830/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#if defined(MBEDTLS_RSA_C)
9836 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9837 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839#if defined(MBEDTLS_ECDSA_C)
9840 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9841 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009844}
9845
Hanno Becker7e5437a2017-04-28 17:15:26 +01009846unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9847{
9848 switch( type ) {
9849 case MBEDTLS_PK_RSA:
9850 return( MBEDTLS_SSL_SIG_RSA );
9851 case MBEDTLS_PK_ECDSA:
9852 case MBEDTLS_PK_ECKEY:
9853 return( MBEDTLS_SSL_SIG_ECDSA );
9854 default:
9855 return( MBEDTLS_SSL_SIG_ANON );
9856 }
9857}
9858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009860{
9861 switch( sig )
9862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863#if defined(MBEDTLS_RSA_C)
9864 case MBEDTLS_SSL_SIG_RSA:
9865 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867#if defined(MBEDTLS_ECDSA_C)
9868 case MBEDTLS_SSL_SIG_ECDSA:
9869 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009870#endif
9871 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009873 }
9874}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009875#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009876
Hanno Becker7e5437a2017-04-28 17:15:26 +01009877#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9878 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9879
9880/* Find an entry in a signature-hash set matching a given hash algorithm. */
9881mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9882 mbedtls_pk_type_t sig_alg )
9883{
9884 switch( sig_alg )
9885 {
9886 case MBEDTLS_PK_RSA:
9887 return( set->rsa );
9888 case MBEDTLS_PK_ECDSA:
9889 return( set->ecdsa );
9890 default:
9891 return( MBEDTLS_MD_NONE );
9892 }
9893}
9894
9895/* Add a signature-hash-pair to a signature-hash set */
9896void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9897 mbedtls_pk_type_t sig_alg,
9898 mbedtls_md_type_t md_alg )
9899{
9900 switch( sig_alg )
9901 {
9902 case MBEDTLS_PK_RSA:
9903 if( set->rsa == MBEDTLS_MD_NONE )
9904 set->rsa = md_alg;
9905 break;
9906
9907 case MBEDTLS_PK_ECDSA:
9908 if( set->ecdsa == MBEDTLS_MD_NONE )
9909 set->ecdsa = md_alg;
9910 break;
9911
9912 default:
9913 break;
9914 }
9915}
9916
9917/* Allow exactly one hash algorithm for each signature. */
9918void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9919 mbedtls_md_type_t md_alg )
9920{
9921 set->rsa = md_alg;
9922 set->ecdsa = md_alg;
9923}
9924
9925#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9926 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9927
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009928/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009929 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009932{
9933 switch( hash )
9934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935#if defined(MBEDTLS_MD5_C)
9936 case MBEDTLS_SSL_HASH_MD5:
9937 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939#if defined(MBEDTLS_SHA1_C)
9940 case MBEDTLS_SSL_HASH_SHA1:
9941 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_SHA256_C)
9944 case MBEDTLS_SSL_HASH_SHA224:
9945 return( MBEDTLS_MD_SHA224 );
9946 case MBEDTLS_SSL_HASH_SHA256:
9947 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949#if defined(MBEDTLS_SHA512_C)
9950 case MBEDTLS_SSL_HASH_SHA384:
9951 return( MBEDTLS_MD_SHA384 );
9952 case MBEDTLS_SSL_HASH_SHA512:
9953 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009954#endif
9955 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009957 }
9958}
9959
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009960/*
9961 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9962 */
9963unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9964{
9965 switch( md )
9966 {
9967#if defined(MBEDTLS_MD5_C)
9968 case MBEDTLS_MD_MD5:
9969 return( MBEDTLS_SSL_HASH_MD5 );
9970#endif
9971#if defined(MBEDTLS_SHA1_C)
9972 case MBEDTLS_MD_SHA1:
9973 return( MBEDTLS_SSL_HASH_SHA1 );
9974#endif
9975#if defined(MBEDTLS_SHA256_C)
9976 case MBEDTLS_MD_SHA224:
9977 return( MBEDTLS_SSL_HASH_SHA224 );
9978 case MBEDTLS_MD_SHA256:
9979 return( MBEDTLS_SSL_HASH_SHA256 );
9980#endif
9981#if defined(MBEDTLS_SHA512_C)
9982 case MBEDTLS_MD_SHA384:
9983 return( MBEDTLS_SSL_HASH_SHA384 );
9984 case MBEDTLS_MD_SHA512:
9985 return( MBEDTLS_SSL_HASH_SHA512 );
9986#endif
9987 default:
9988 return( MBEDTLS_SSL_HASH_NONE );
9989 }
9990}
9991
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009992#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009993/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009994 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009995 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009996 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009997int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010000
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010001 if( ssl->conf->curve_list == NULL )
10002 return( -1 );
10003
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010004 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010005 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010006 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010007
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010008 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010009}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010010#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010011
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010012#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010013/*
10014 * Check if a hash proposed by the peer is in our list.
10015 * Return 0 if we're willing to use it, -1 otherwise.
10016 */
10017int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10018 mbedtls_md_type_t md )
10019{
10020 const int *cur;
10021
10022 if( ssl->conf->sig_hashes == NULL )
10023 return( -1 );
10024
10025 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10026 if( *cur == (int) md )
10027 return( 0 );
10028
10029 return( -1 );
10030}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010031#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033#if defined(MBEDTLS_X509_CRT_PARSE_C)
10034int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10035 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010036 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010037 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010038{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010039 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010041 int usage = 0;
10042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010044 const char *ext_oid;
10045 size_t ext_len;
10046#endif
10047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10049 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010050 ((void) cert);
10051 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010052 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010053#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10056 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010057 {
10058 /* Server part of the key exchange */
10059 switch( ciphersuite->key_exchange )
10060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 case MBEDTLS_KEY_EXCHANGE_RSA:
10062 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010063 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010064 break;
10065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10067 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10068 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10069 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010070 break;
10071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10073 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010074 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010075 break;
10076
10077 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 case MBEDTLS_KEY_EXCHANGE_NONE:
10079 case MBEDTLS_KEY_EXCHANGE_PSK:
10080 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10081 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010082 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010083 usage = 0;
10084 }
10085 }
10086 else
10087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10089 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010090 }
10091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010093 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010094 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010095 ret = -1;
10096 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010097#else
10098 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10102 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010104 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10105 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010106 }
10107 else
10108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10110 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010111 }
10112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010114 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010115 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010116 ret = -1;
10117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010119
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010120 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010123
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010124/*
10125 * Convert version numbers to/from wire format
10126 * and, for DTLS, to/from TLS equivalent.
10127 *
10128 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010129 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010130 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10131 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010134 unsigned char ver[2] )
10135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136#if defined(MBEDTLS_SSL_PROTO_DTLS)
10137 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010140 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10141
10142 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10143 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10144 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010145 else
10146#else
10147 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010148#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010149 {
10150 ver[0] = (unsigned char) major;
10151 ver[1] = (unsigned char) minor;
10152 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010153}
10154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010156 const unsigned char ver[2] )
10157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158#if defined(MBEDTLS_SSL_PROTO_DTLS)
10159 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010160 {
10161 *major = 255 - ver[0] + 2;
10162 *minor = 255 - ver[1] + 1;
10163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010165 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10166 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010167 else
10168#else
10169 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010170#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010171 {
10172 *major = ver[0];
10173 *minor = ver[1];
10174 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010175}
10176
Simon Butcher99000142016-10-13 17:21:01 +010010177int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10178{
10179#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10180 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10181 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10182
10183 switch( md )
10184 {
10185#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10186#if defined(MBEDTLS_MD5_C)
10187 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010188 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010189#endif
10190#if defined(MBEDTLS_SHA1_C)
10191 case MBEDTLS_SSL_HASH_SHA1:
10192 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10193 break;
10194#endif
10195#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10196#if defined(MBEDTLS_SHA512_C)
10197 case MBEDTLS_SSL_HASH_SHA384:
10198 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10199 break;
10200#endif
10201#if defined(MBEDTLS_SHA256_C)
10202 case MBEDTLS_SSL_HASH_SHA256:
10203 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10204 break;
10205#endif
10206 default:
10207 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10208 }
10209
10210 return 0;
10211#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10212 (void) ssl;
10213 (void) md;
10214
10215 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10217}
10218
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010219#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10220 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10221int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10222 unsigned char *output,
10223 unsigned char *data, size_t data_len )
10224{
10225 int ret = 0;
10226 mbedtls_md5_context mbedtls_md5;
10227 mbedtls_sha1_context mbedtls_sha1;
10228
10229 mbedtls_md5_init( &mbedtls_md5 );
10230 mbedtls_sha1_init( &mbedtls_sha1 );
10231
10232 /*
10233 * digitally-signed struct {
10234 * opaque md5_hash[16];
10235 * opaque sha_hash[20];
10236 * };
10237 *
10238 * md5_hash
10239 * MD5(ClientHello.random + ServerHello.random
10240 * + ServerParams);
10241 * sha_hash
10242 * SHA(ClientHello.random + ServerHello.random
10243 * + ServerParams);
10244 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010245 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010246 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010248 goto exit;
10249 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010250 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010251 ssl->handshake->randbytes, 64 ) ) != 0 )
10252 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010254 goto exit;
10255 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010256 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010257 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010259 goto exit;
10260 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010261 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010262 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010264 goto exit;
10265 }
10266
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010267 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010268 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010269 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010270 goto exit;
10271 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010272 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010273 ssl->handshake->randbytes, 64 ) ) != 0 )
10274 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010276 goto exit;
10277 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010278 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010279 data_len ) ) != 0 )
10280 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010282 goto exit;
10283 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010284 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010285 output + 16 ) ) != 0 )
10286 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010288 goto exit;
10289 }
10290
10291exit:
10292 mbedtls_md5_free( &mbedtls_md5 );
10293 mbedtls_sha1_free( &mbedtls_sha1 );
10294
10295 if( ret != 0 )
10296 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10297 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10298
10299 return( ret );
10300
10301}
10302#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10303 MBEDTLS_SSL_PROTO_TLS1_1 */
10304
10305#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10306 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10307int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010308 unsigned char *hash, size_t *hashlen,
10309 unsigned char *data, size_t data_len,
10310 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010311{
10312 int ret = 0;
10313 mbedtls_md_context_t ctx;
10314 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010315 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010316
10317 mbedtls_md_init( &ctx );
10318
10319 /*
10320 * digitally-signed struct {
10321 * opaque client_random[32];
10322 * opaque server_random[32];
10323 * ServerDHParams params;
10324 * };
10325 */
10326 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10327 {
10328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10329 goto exit;
10330 }
10331 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10332 {
10333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10334 goto exit;
10335 }
10336 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10337 {
10338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10339 goto exit;
10340 }
10341 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10342 {
10343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10344 goto exit;
10345 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010346 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010347 {
10348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10349 goto exit;
10350 }
10351
10352exit:
10353 mbedtls_md_free( &ctx );
10354
10355 if( ret != 0 )
10356 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10357 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10358
10359 return( ret );
10360}
10361#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10362 MBEDTLS_SSL_PROTO_TLS1_2 */
10363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010364#endif /* MBEDTLS_SSL_TLS_C */