blob: 4cfef3f574f6af8aa00427a759adf40bda0eb980 [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"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010067#else
68 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069#endif
70 return( 0 );
71}
72
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020073/*
74 * Start a timer.
75 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020079 if( ssl->f_set_timer == NULL )
80 return;
81
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
83 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084}
85
86/*
87 * Return -1 is timer is expired, 0 if it isn't.
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020090{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020092 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020093
94 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020095 {
96 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020098 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020099
100 return( 0 );
101}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100103static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100105static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
Hanno Beckera5a2b082019-05-15 14:03:01 +0100112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100113/* Top-level Connection ID API */
114
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100115int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
116 size_t len,
117 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100118{
119 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
121
Hanno Becker791ec6b2019-05-14 11:45:26 +0100122 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
123 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
124 {
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126 }
127
128 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100129 conf->cid_len = len;
130 return( 0 );
131}
132
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100133int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
134 int enable,
135 unsigned char const *own_cid,
136 size_t own_cid_len )
137{
Hanno Becker78c43022019-05-03 14:38:32 +0100138 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
140
Hanno Becker07489862019-04-25 16:01:49 +0100141 ssl->negotiate_cid = enable;
142 if( enable == MBEDTLS_SSL_CID_DISABLED )
143 {
144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
145 return( 0 );
146 }
147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100148 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100149
Hanno Beckereec2be92019-05-03 13:06:44 +0100150 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100151 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
153 (unsigned) own_cid_len,
154 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
156 }
157
158 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100159 /* Truncation is not an issue here because
160 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
161 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100162
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100163 return( 0 );
164}
165
166int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
167 int *enabled,
168 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
169 size_t *peer_cid_len )
170{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100171 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100172
Hanno Becker78c43022019-05-03 14:38:32 +0100173 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
174 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
175 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100177 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100178
Hanno Beckercb063f52019-05-03 12:54:52 +0100179 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
180 * were used, but client and server requested the empty CID.
181 * This is indistinguishable from not using the CID extension
182 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183 if( ssl->transform_in->in_cid_len == 0 &&
184 ssl->transform_in->out_cid_len == 0 )
185 {
186 return( 0 );
187 }
188
Hanno Becker633d6042019-05-22 16:50:35 +0100189 if( peer_cid_len != NULL )
190 {
191 *peer_cid_len = ssl->transform_in->out_cid_len;
192 if( peer_cid != NULL )
193 {
194 memcpy( peer_cid, ssl->transform_in->out_cid,
195 ssl->transform_in->out_cid_len );
196 }
197 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100198
199 *enabled = MBEDTLS_SSL_CID_ENABLED;
200
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100201 return( 0 );
202}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100204
Hanno Beckerd5847772018-08-28 10:09:23 +0100205/* Forward declarations for functions related to message buffering. */
206static void ssl_buffering_free( mbedtls_ssl_context *ssl );
207static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
208 uint8_t slot );
209static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
210static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
211static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
212static int ssl_buffer_message( mbedtls_ssl_context *ssl );
213static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100214static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100215
Hanno Beckera67dee22018-08-22 10:05:20 +0100216static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100217static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100218{
Hanno Becker11682cc2018-08-22 14:41:02 +0100219 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100220
221 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100222 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223
224 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
225}
226
Hanno Becker67bc7c32018-08-06 11:33:50 +0100227static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
228{
Hanno Becker11682cc2018-08-22 14:41:02 +0100229 size_t const bytes_written = ssl->out_left;
230 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231
232 /* Double-check that the write-index hasn't gone
233 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235 {
236 /* Should never happen... */
237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
238 }
239
240 return( (int) ( mtu - bytes_written ) );
241}
242
243static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
244{
245 int ret;
246 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400247 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100248
249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
250 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
251
252 if( max_len > mfl )
253 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100254
255 /* By the standard (RFC 6066 Sect. 4), the MFL extension
256 * only limits the maximum record payload size, so in theory
257 * we would be allowed to pack multiple records of payload size
258 * MFL into a single datagram. However, this would mean that there's
259 * no way to explicitly communicate MTU restrictions to the peer.
260 *
261 * The following reduction of max_len makes sure that we never
262 * write datagrams larger than MFL + Record Expansion Overhead.
263 */
264 if( max_len <= ssl->out_left )
265 return( 0 );
266
267 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100268#endif
269
270 ret = ssl_get_remaining_space_in_datagram( ssl );
271 if( ret < 0 )
272 return( ret );
273 remaining = (size_t) ret;
274
275 ret = mbedtls_ssl_get_record_expansion( ssl );
276 if( ret < 0 )
277 return( ret );
278 expansion = (size_t) ret;
279
280 if( remaining <= expansion )
281 return( 0 );
282
283 remaining -= expansion;
284 if( remaining >= max_len )
285 remaining = max_len;
286
287 return( (int) remaining );
288}
289
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290/*
291 * Double the retransmit timeout value, within the allowed range,
292 * returning -1 if the maximum value has already been reached.
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295{
296 uint32_t new_timeout;
297
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200298 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299 return( -1 );
300
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200301 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
302 * in the following way: after the initial transmission and a first
303 * retransmission, back off to a temporary estimated MTU of 508 bytes.
304 * This value is guaranteed to be deliverable (if not guaranteed to be
305 * delivered) of any compliant IPv4 (and IPv6) network, and should work
306 * on most non-IP stacks too. */
307 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400308 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200309 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
311 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200312
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313 new_timeout = 2 * ssl->handshake->retransmit_timeout;
314
315 /* Avoid arithmetic overflow and range overflow */
316 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200317 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200319 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200320 }
321
322 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324 ssl->handshake->retransmit_timeout ) );
325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200330{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200331 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333 ssl->handshake->retransmit_timeout ) );
334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200338/*
339 * Convert max_fragment_length codes to length.
340 * RFC 6066 says:
341 * enum{
342 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
343 * } MaxFragmentLength;
344 * and we add 0 -> extension unused
345 */
Angus Grattond8213d02016-05-25 20:56:48 +1000346static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200347{
Angus Grattond8213d02016-05-25 20:56:48 +1000348 switch( mfl )
349 {
350 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
351 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
352 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
353 return 512;
354 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
355 return 1024;
356 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
357 return 2048;
358 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
359 return 4096;
360 default:
361 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
362 }
363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200365
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200366#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_ssl_session_free( dst );
370 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373 if( src->peer_cert != NULL )
374 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200375 int ret;
376
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200377 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200378 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200384 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387 dst->peer_cert = NULL;
388 return( ret );
389 }
390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 if( src->ticket != NULL )
395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200396 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399
400 memcpy( dst->ticket, src->ticket, src->ticket_len );
401 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
404 return( 0 );
405}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200406#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
409int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200410 const unsigned char *key_enc, const unsigned char *key_dec,
411 size_t keylen,
412 const unsigned char *iv_enc, const unsigned char *iv_dec,
413 size_t ivlen,
414 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200415 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
417int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
420int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
421#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423/*
424 * Key material generation
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427static int ssl3_prf( const unsigned char *secret, size_t slen,
428 const char *label,
429 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000430 unsigned char *dstbuf, size_t dlen )
431{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100432 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000433 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200434 mbedtls_md5_context md5;
435 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000436 unsigned char padding[16];
437 unsigned char sha1sum[20];
438 ((void)label);
439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200440 mbedtls_md5_init( &md5 );
441 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200442
Paul Bakker5f70b252012-09-13 14:23:06 +0000443 /*
444 * SSLv3:
445 * block =
446 * MD5( secret + SHA1( 'A' + secret + random ) ) +
447 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
448 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
449 * ...
450 */
451 for( i = 0; i < dlen / 16; i++ )
452 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200453 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100455 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100456 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000465
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000474 }
475
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200477 mbedtls_md5_free( &md5 );
478 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000479
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500480 mbedtls_platform_zeroize( padding, sizeof( padding ) );
481 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000482
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200488static int tls1_prf( const unsigned char *secret, size_t slen,
489 const char *label,
490 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000491 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000492{
Paul Bakker23986e52011-04-24 08:57:21 +0000493 size_t nb, hs;
494 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200495 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 unsigned char tmp[128];
497 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 const mbedtls_md_info_t *md_info;
499 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100500 int ret;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507 hs = ( slen + 1 ) / 2;
508 S1 = secret;
509 S2 = secret + slen - hs;
510
511 nb = strlen( label );
512 memcpy( tmp + 20, label, nb );
513 memcpy( tmp + 20 + nb, random, rlen );
514 nb += rlen;
515
516 /*
517 * First compute P_md5(secret,label+random)[0..dlen]
518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100523 return( ret );
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
526 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
527 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 for( i = 0; i < dlen; i += 16 )
530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_md_hmac_reset ( &md_ctx );
532 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
533 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_reset ( &md_ctx );
536 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
537 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
540
541 for( j = 0; j < k; j++ )
542 dstbuf[i + j] = h_i[j];
543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100546
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 /*
548 * XOR out with P_sha1(secret,label+random)[0..dlen]
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554 return( ret );
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
557 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
558 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 for( i = 0; i < dlen; i += 20 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_md_hmac_reset ( &md_ctx );
563 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
564 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_hmac_reset ( &md_ctx );
567 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
568 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
571
572 for( j = 0; j < k; j++ )
573 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
574 }
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500578 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
579 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 return( 0 );
582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
586static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100587 const unsigned char *secret, size_t slen,
588 const char *label,
589 const unsigned char *random, size_t rlen,
590 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591{
592 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100593 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
596 const mbedtls_md_info_t *md_info;
597 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598 int ret;
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
607 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000609
610 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611 memcpy( tmp + md_len, label, nb );
612 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000613 nb += rlen;
614
615 /*
616 * Compute P_<hash>(secret, label + random)[0..dlen]
617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 return( ret );
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
622 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
623 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100624
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_md_hmac_reset ( &md_ctx );
628 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
629 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_md_hmac_reset ( &md_ctx );
632 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
633 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100635 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
637 for( j = 0; j < k; j++ )
638 dstbuf[i + j] = h_i[j];
639 }
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100642
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500643 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
644 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000645
646 return( 0 );
647}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650static int tls_prf_sha256( const unsigned char *secret, size_t slen,
651 const char *label,
652 const unsigned char *random, size_t rlen,
653 unsigned char *dstbuf, size_t dlen )
654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 label, random, rlen, dstbuf, dlen ) );
657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200661static int tls_prf_sha384( const unsigned char *secret, size_t slen,
662 const char *label,
663 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 unsigned char *dstbuf, size_t dlen )
665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100667 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_SHA512_C */
670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
676static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677#endif
Paul Bakker380da532012-04-18 16:10:25 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200680static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200685static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
690#if defined(MBEDTLS_SHA256_C)
691static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200692static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_SHA512_C)
697static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200698static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200703/* Type for the TLS PRF */
704typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
705 const unsigned char *, size_t,
706 unsigned char *, size_t);
707
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200708/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200709 * Populate a transform structure with session keys and all the other
710 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200711 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200712 * Parameters:
713 * - [in/out]: transform: structure to populate
714 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200715 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200716 * - [in] ciphersuite
717 * - [in] master
718 * - [in] encrypt_then_mac
719 * - [in] trunc_hmac
720 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200721 * - [in] tls_prf: pointer to PRF to use for key derivation
722 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200723 * - [in] minor_ver: SSL/TLS minor version
724 * - [in] endpoint: client or server
725 * - [in] ssl: optionally used for:
726 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
727 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
728 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200731 int ciphersuite,
732 const unsigned char master[48],
733#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
734 int encrypt_then_mac,
735#endif
736#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
737 int trunc_hmac,
738#endif
739#if defined(MBEDTLS_ZLIB_SUPPORT)
740 int compression,
741#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 ssl_tls_prf_t tls_prf,
743 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 int minor_ver,
745 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200746 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000747{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200748 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 unsigned char keyblk[256];
750 unsigned char *key1;
751 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100752 unsigned char *mac_enc;
753 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000754 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200755 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000756 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000757 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 const mbedtls_cipher_info_t *cipher_info;
759 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100760
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200761#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
762 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
763 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200764 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000766#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000767
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200768 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200770 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000771#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200772 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200774 /*
775 * Get various info structures
776 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200777 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200778 if( ciphersuite_info == NULL )
779 {
780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200781 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
783 }
784
Hanno Becker8759e162017-12-27 21:34:08 +0000785 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100786 if( cipher_info == NULL )
787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000789 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100791 }
792
Hanno Becker8759e162017-12-27 21:34:08 +0000793 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100794 if( md_info == NULL )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000797 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 }
800
Hanno Beckera5a2b082019-05-15 14:03:01 +0100801#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100802 /* Copy own and peer's CID if the use of the CID
803 * extension has been negotiated. */
804 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
805 {
806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100807
Hanno Becker4932f9f2019-05-03 15:23:51 +0100808 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100809 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100810 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100811 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100812
813 transform->out_cid_len = ssl->handshake->peer_cid_len;
814 memcpy( transform->out_cid, ssl->handshake->peer_cid,
815 ssl->handshake->peer_cid_len );
816 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
817 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100818 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100819#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200822 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000823 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200824 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100825 if( ret != 0 )
826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100828 return( ret );
829 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200832 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200833 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200834 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 /*
838 * Determine the appropriate key, IV and MAC length.
839 */
Paul Bakker68884e32013-01-07 18:20:04 +0100840
Hanno Beckere7f2df02017-12-27 08:17:40 +0000841 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842
Hanno Beckerf1229442018-01-03 15:32:31 +0000843#if defined(MBEDTLS_GCM_C) || \
844 defined(MBEDTLS_CCM_C) || \
845 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200847 cipher_info->mode == MBEDTLS_MODE_CCM ||
848 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 {
Hanno Becker8759e162017-12-27 21:34:08 +0000850 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200851
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200852 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000853 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000854 transform->taglen =
855 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200856
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200857 /* All modes haves 96-bit IVs;
858 * GCM and CCM has 4 implicit and 8 explicit bytes
859 * ChachaPoly has all 12 bytes implicit
860 */
Paul Bakker68884e32013-01-07 18:20:04 +0100861 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
863 transform->fixed_ivlen = 12;
864 else
865 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 /* Minimum length of encrypted record */
868 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000869 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100870 }
871 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000872#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
873#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
874 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
875 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100876 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
879 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200882 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200885 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000886 mac_key_len = mbedtls_md_get_size( md_info );
887 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890 /*
891 * If HMAC is to be truncated, we shall keep the leftmost bytes,
892 * (rfc 6066 page 13 or rfc 2104 section 4),
893 * so we only need to adjust the length here.
894 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200895 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000898
899#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
900 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000901 * HMAC implementation which also truncates the key
902 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000903 mac_key_len = transform->maclen;
904#endif
905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200907
908 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100909 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200911 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200913 transform->minlen = transform->maclen;
914 else
Paul Bakker68884e32013-01-07 18:20:04 +0100915 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 /*
917 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100918 * 1. if EtM is in use: one block plus MAC
919 * otherwise: * first multiple of blocklen greater than maclen
920 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200923 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100924 {
925 transform->minlen = transform->maclen
926 + cipher_info->block_size;
927 }
928 else
929#endif
930 {
931 transform->minlen = transform->maclen
932 + cipher_info->block_size
933 - transform->maclen % cipher_info->block_size;
934 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200937 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
938 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200939 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100940 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200943 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
944 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 {
946 transform->minlen += transform->ivlen;
947 }
948 else
949#endif
950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200953 }
Paul Bakker68884e32013-01-07 18:20:04 +0100954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000956 else
957#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
958 {
959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
961 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Hanno Beckere7f2df02017-12-27 08:17:40 +0000963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
964 (unsigned) keylen,
965 (unsigned) transform->minlen,
966 (unsigned) transform->ivlen,
967 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
969 /*
970 * Finally setup the cipher contexts, IVs and MAC secrets.
971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200973 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000975 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000976 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Paul Bakker68884e32013-01-07 18:20:04 +0100978 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000981 /*
982 * This is not used in TLS v1.1.
983 */
Paul Bakker48916f92012-09-16 19:57:18 +0000984 iv_copy_len = ( transform->fixed_ivlen ) ?
985 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000986 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
987 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000988 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 }
990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_SSL_CLI_C */
992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000995 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000996 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1007 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Hanno Becker92231322018-01-03 15:32:51 +00001017#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001019 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001020 {
Hanno Becker92231322018-01-03 15:32:51 +00001021 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001025 }
1026
Hanno Becker81c7b182017-11-09 18:39:33 +00001027 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1028 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
1030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1032#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1033 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001034 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001035 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001036 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1037 For AEAD-based ciphersuites, there is nothing to do here. */
1038 if( mac_key_len != 0 )
1039 {
1040 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1041 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1042 }
Paul Bakker68884e32013-01-07 18:20:04 +01001043 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001044 else
1045#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001049 }
Hanno Becker92231322018-01-03 15:32:51 +00001050#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1053 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001054 {
1055 int ret = 0;
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001058
Hanno Beckere7f2df02017-12-27 08:17:40 +00001059 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001060 transform->iv_enc, transform->iv_dec,
1061 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001062 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001063 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001067 }
1068 }
Hanno Becker92231322018-01-03 15:32:51 +00001069#else
1070 ((void) mac_dec);
1071 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001073
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001074#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1075 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001076 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001077 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001078 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001080 iv_copy_len );
1081 }
1082#endif
1083
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001084 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001085 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088 return( ret );
1089 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001090
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001091 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001092 cipher_info ) ) != 0 )
1093 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095 return( ret );
1096 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001099 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001103 return( ret );
1104 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001107 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001111 return( ret );
1112 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_CIPHER_MODE_CBC)
1115 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1118 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001122 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1125 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128 return( ret );
1129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001133 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001135 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001137 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001140
Paul Bakker48916f92012-09-16 19:57:18 +00001141 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1142 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001144 if( deflateInit( &transform->ctx_deflate,
1145 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001146 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1149 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001150 }
1151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001153
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( 0 );
1155}
1156
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001157/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001158 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001159 *
1160 * Inputs:
1161 * - SSL/TLS minor version
1162 * - hash associated with the ciphersuite (only used by TLS 1.2)
1163 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001164 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001165 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1166 */
1167static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1168 int minor_ver,
1169 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001170{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001171#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1172 (void) hash;
1173#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001174
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001175#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001176 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001177 {
1178 handshake->tls_prf = ssl3_prf;
1179 handshake->calc_verify = ssl_calc_verify_ssl;
1180 handshake->calc_finished = ssl_calc_finished_ssl;
1181 }
1182 else
1183#endif
1184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001186 {
1187 handshake->tls_prf = tls1_prf;
1188 handshake->calc_verify = ssl_calc_verify_tls;
1189 handshake->calc_finished = ssl_calc_finished_tls;
1190 }
1191 else
1192#endif
1193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1196 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = tls_prf_sha384;
1199 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1200 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls_prf_sha256;
1208 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1209 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1210 }
1211 else
1212#endif
1213#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1214 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1216 }
1217
1218 return( 0 );
1219}
1220
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001221/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001222 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001223 *
1224 * Parameters:
1225 * [in/out] handshake
1226 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1227 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001228 * [out] master
1229 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001230 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001231static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001232 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001233 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001234{
1235 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236
1237#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001238 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001239 (void) ssl;
1240#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001243 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1245 return( 0 );
1246 }
1247
1248 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1249 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250
1251#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001253 {
1254 unsigned char session_hash[48];
1255 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001256
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001257 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001258
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001259 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1260 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001261
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001262 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263 "extended master secret",
1264 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001265 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001266 }
1267 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001269 {
1270 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1271 "master secret",
1272 handshake->randbytes, 64,
1273 master, 48 );
1274 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001275 if( ret != 0 )
1276 {
1277 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1278 return( ret );
1279 }
1280
1281 mbedtls_platform_zeroize( handshake->premaster,
1282 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283
1284 return( 0 );
1285}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001286
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001287int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1288{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001289 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001290 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1291 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001292
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1294
1295 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001296 ret = ssl_set_handshake_prfs( ssl->handshake,
1297 ssl->minor_ver,
1298 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001299 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001300 {
1301 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001302 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001303 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001305 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001306 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001307 ssl->session_negotiate->master,
1308 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001309 if( ret != 0 )
1310 {
1311 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1312 return( ret );
1313 }
1314
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001315 /* Swap the client and server random values:
1316 * - MS derivation wanted client+server (RFC 5246 8.1)
1317 * - key derivation wants server+client (RFC 5246 6.3) */
1318 {
1319 unsigned char tmp[64];
1320 memcpy( tmp, ssl->handshake->randbytes, 64 );
1321 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1322 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1323 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1324 }
1325
1326 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001327 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001328 ssl->session_negotiate->ciphersuite,
1329 ssl->session_negotiate->master,
1330#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1331 ssl->session_negotiate->encrypt_then_mac,
1332#endif
1333#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1334 ssl->session_negotiate->trunc_hmac,
1335#endif
1336#if defined(MBEDTLS_ZLIB_SUPPORT)
1337 ssl->session_negotiate->compression,
1338#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 ssl->handshake->tls_prf,
1340 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001341 ssl->minor_ver,
1342 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001343 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001344 if( ret != 0 )
1345 {
1346 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1347 return( ret );
1348 }
1349
1350 /* We no longer need Server/ClientHello.random values */
1351 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1352 sizeof( ssl->handshake->randbytes ) );
1353
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001354 /* Allocate compression buffer */
1355#if defined(MBEDTLS_ZLIB_SUPPORT)
1356 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1357 ssl->compress_buf == NULL )
1358 {
1359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1360 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1361 if( ssl->compress_buf == NULL )
1362 {
1363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001364 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001365 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1366 }
1367 }
1368#endif
1369
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1371
1372 return( 0 );
1373}
1374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001376void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1377 unsigned char hash[36],
1378 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001380 mbedtls_md5_context md5;
1381 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 unsigned char pad_1[48];
1383 unsigned char pad_2[48];
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001387 mbedtls_md5_init( &md5 );
1388 mbedtls_sha1_init( &sha1 );
1389
1390 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1391 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Paul Bakker380da532012-04-18 16:10:25 +00001393 memset( pad_1, 0x36, 48 );
1394 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001396 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1397 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1398 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_starts_ret( &md5 );
1401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1403 mbedtls_md5_update_ret( &md5, hash, 16 );
1404 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001406 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1407 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1408 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_starts_ret( &sha1 );
1411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1413 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1414 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001415
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001416 *hlen = 36;
1417
1418 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001421 mbedtls_md5_free( &md5 );
1422 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001423
Paul Bakker380da532012-04-18 16:10:25 +00001424 return;
1425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001429void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1430 unsigned char hash[36],
1431 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001432{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001433 mbedtls_md5_context md5;
1434 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001438 mbedtls_md5_init( &md5 );
1439 mbedtls_sha1_init( &sha1 );
1440
1441 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1442 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001444 mbedtls_md5_finish_ret( &md5, hash );
1445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001447 *hlen = 36;
1448
1449 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001452 mbedtls_md5_free( &md5 );
1453 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001454
Paul Bakker380da532012-04-18 16:10:25 +00001455 return;
1456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1460#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001461void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1462 unsigned char hash[32],
1463 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001464{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001465 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001467 mbedtls_sha256_init( &sha256 );
1468
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001471 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001472 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001474 *hlen = 32;
1475
1476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001480
Paul Bakker380da532012-04-18 16:10:25 +00001481 return;
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001486void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1487 unsigned char hash[48],
1488 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001489{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha512_init( &sha512 );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001496 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001497 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001499 *hlen = 48;
1500
1501 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001504 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001505
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 return;
1507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SHA512_C */
1509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1512int 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 +02001513{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001514 unsigned char *p = ssl->handshake->premaster;
1515 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001516 const unsigned char *psk = ssl->conf->psk;
1517 size_t psk_len = ssl->conf->psk_len;
1518
1519 /* If the psk callback was called, use its result */
1520 if( ssl->handshake->psk != NULL )
1521 {
1522 psk = ssl->handshake->psk;
1523 psk_len = ssl->handshake->psk_len;
1524 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001525
1526 /*
1527 * PMS = struct {
1528 * opaque other_secret<0..2^16-1>;
1529 * opaque psk<0..2^16-1>;
1530 * };
1531 * with "other_secret" depending on the particular key exchange
1532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1534 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001536 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001538
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001539 *(p++) = (unsigned char)( psk_len >> 8 );
1540 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541
1542 if( end < p || (size_t)( end - p ) < psk_len )
1543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1544
1545 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001546 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 }
1548 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1550#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1551 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001552 {
1553 /*
1554 * other_secret already set by the ClientKeyExchange message,
1555 * and is 48 bytes long
1556 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001557 if( end - p < 2 )
1558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1559
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001560 *p++ = 0;
1561 *p++ = 48;
1562 p += 48;
1563 }
1564 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1566#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1567 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001569 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001570 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001571
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001572 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001574 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001575 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001578 return( ret );
1579 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001580 *(p++) = (unsigned char)( len >> 8 );
1581 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001582 p += len;
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001585 }
1586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1588#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1589 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001591 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592 size_t zlen;
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 return( ret );
1600 }
1601
1602 *(p++) = (unsigned char)( zlen >> 8 );
1603 *(p++) = (unsigned char)( zlen );
1604 p += zlen;
1605
Janos Follath3fbdada2018-08-15 10:26:53 +01001606 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1607 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 }
1609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001614 }
1615
1616 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001617 if( end - p < 2 )
1618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001619
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001620 *(p++) = (unsigned char)( psk_len >> 8 );
1621 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622
1623 if( end < p || (size_t)( end - p ) < psk_len )
1624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1625
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001626 memcpy( p, psk, psk_len );
1627 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628
1629 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1630
1631 return( 0 );
1632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001636/*
1637 * SSLv3.0 MAC functions
1638 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001639#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001640static void ssl_mac( mbedtls_md_context_t *md_ctx,
1641 const unsigned char *secret,
1642 const unsigned char *buf, size_t len,
1643 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
1646 unsigned char header[11];
1647 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001648 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1650 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001651
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001652 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001654 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001655 else
Paul Bakker68884e32013-01-07 18:20:04 +01001656 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658 memcpy( header, ctr, 8 );
1659 header[ 8] = (unsigned char) type;
1660 header[ 9] = (unsigned char)( len >> 8 );
1661 header[10] = (unsigned char)( len );
1662
Paul Bakker68884e32013-01-07 18:20:04 +01001663 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_md_starts( md_ctx );
1665 mbedtls_md_update( md_ctx, secret, md_size );
1666 mbedtls_md_update( md_ctx, padding, padlen );
1667 mbedtls_md_update( md_ctx, header, 11 );
1668 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001669 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Paul Bakker68884e32013-01-07 18:20:04 +01001671 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 mbedtls_md_starts( md_ctx );
1673 mbedtls_md_update( md_ctx, secret, md_size );
1674 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001675 mbedtls_md_update( md_ctx, out, md_size );
1676 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001679
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001680/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001681 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001682#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001683 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1684 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1685 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1686/* This function makes sure every byte in the memory region is accessed
1687 * (in ascending addresses order) */
1688static void ssl_read_memory( unsigned char *p, size_t len )
1689{
1690 unsigned char acc = 0;
1691 volatile unsigned char force;
1692
1693 for( ; len != 0; p++, len-- )
1694 acc ^= *p;
1695
1696 force = acc;
1697 (void) force;
1698}
1699#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1700
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001701/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001703 */
Hanno Becker3307b532017-12-27 21:37:21 +00001704
Hanno Beckera5a2b082019-05-15 14:03:01 +01001705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001706/* This functions transforms a DTLS plaintext fragment and a record content
1707 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001708 *
1709 * struct {
1710 * opaque content[DTLSPlaintext.length];
1711 * ContentType real_type;
1712 * uint8 zeros[length_of_padding];
1713 * } DTLSInnerPlaintext;
1714 *
1715 * Input:
1716 * - `content`: The beginning of the buffer holding the
1717 * plaintext to be wrapped.
1718 * - `*content_size`: The length of the plaintext in Bytes.
1719 * - `max_len`: The number of Bytes available starting from
1720 * `content`. This must be `>= *content_size`.
1721 * - `rec_type`: The desired record content type.
1722 *
1723 * Output:
1724 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1725 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1726 *
1727 * Returns:
1728 * - `0` on success.
1729 * - A negative error code if `max_len` didn't offer enough space
1730 * for the expansion.
1731 */
1732static int ssl_cid_build_inner_plaintext( unsigned char *content,
1733 size_t *content_size,
1734 size_t remaining,
1735 uint8_t rec_type )
1736{
1737 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001738 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1739 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1740 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001741
1742 /* Write real content type */
1743 if( remaining == 0 )
1744 return( -1 );
1745 content[ len ] = rec_type;
1746 len++;
1747 remaining--;
1748
1749 if( remaining < pad )
1750 return( -1 );
1751 memset( content + len, 0, pad );
1752 len += pad;
1753 remaining -= pad;
1754
1755 *content_size = len;
1756 return( 0 );
1757}
1758
Hanno Becker7dc25772019-05-20 15:08:01 +01001759/* This function parses a DTLSInnerPlaintext structure.
1760 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001761static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1762 size_t *content_size,
1763 uint8_t *rec_type )
1764{
1765 size_t remaining = *content_size;
1766
1767 /* Determine length of padding by skipping zeroes from the back. */
1768 do
1769 {
1770 if( remaining == 0 )
1771 return( -1 );
1772 remaining--;
1773 } while( content[ remaining ] == 0 );
1774
1775 *content_size = remaining;
1776 *rec_type = content[ remaining ];
1777
1778 return( 0 );
1779}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001780#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781
Hanno Becker99abf512019-05-20 14:50:53 +01001782/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001783 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001784static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001785 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001786 mbedtls_record *rec )
1787{
Hanno Becker99abf512019-05-20 14:50:53 +01001788 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001789 *
1790 * additional_data = seq_num + TLSCompressed.type +
1791 * TLSCompressed.version + TLSCompressed.length;
1792 *
Hanno Becker99abf512019-05-20 14:50:53 +01001793 * For the CID extension, this is extended as follows
1794 * (quoting draft-ietf-tls-dtls-connection-id-05,
1795 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001796 *
1797 * additional_data = seq_num + DTLSPlaintext.type +
1798 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001799 * cid +
1800 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 * length_of_DTLSInnerPlaintext;
1802 */
1803
Hanno Becker3307b532017-12-27 21:37:21 +00001804 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1805 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001806 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001807
Hanno Beckera5a2b082019-05-15 14:03:01 +01001808#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001809 if( rec->cid_len != 0 )
1810 {
1811 memcpy( add_data + 11, rec->cid, rec->cid_len );
1812 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1813 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1814 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1815 *add_data_len = 13 + 1 + rec->cid_len;
1816 }
1817 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001818#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001819 {
1820 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1821 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1822 *add_data_len = 13;
1823 }
Hanno Becker3307b532017-12-27 21:37:21 +00001824}
1825
Hanno Becker611a83b2018-01-03 14:27:32 +00001826int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1827 mbedtls_ssl_transform *transform,
1828 mbedtls_record *rec,
1829 int (*f_rng)(void *, unsigned char *, size_t),
1830 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001833 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001834 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001835 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001836 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001837 size_t post_avail;
1838
1839 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001840#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001841 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001842 ((void) ssl);
1843#endif
1844
1845 /* The PRNG is used for dynamic IV generation that's used
1846 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1847#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1848 ( defined(MBEDTLS_AES_C) || \
1849 defined(MBEDTLS_ARIA_C) || \
1850 defined(MBEDTLS_CAMELLIA_C) ) && \
1851 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1852 ((void) f_rng);
1853 ((void) p_rng);
1854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Hanno Becker3307b532017-12-27 21:37:21 +00001858 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859 {
Hanno Becker3307b532017-12-27 21:37:21 +00001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1862 }
Hanno Becker505089d2019-05-01 09:45:57 +01001863 if( rec == NULL
1864 || rec->buf == NULL
1865 || rec->buf_len < rec->data_offset
1866 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001868 || rec->cid_len != 0
1869#endif
1870 )
Hanno Becker3307b532017-12-27 21:37:21 +00001871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 }
1875
Hanno Becker3307b532017-12-27 21:37:21 +00001876 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001877 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001879 data, rec->data_len );
1880
1881 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1882
1883 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1884 {
1885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1886 (unsigned) rec->data_len,
1887 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1889 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001890
Hanno Beckera5a2b082019-05-15 14:03:01 +01001891#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001892 /*
1893 * Add CID information
1894 */
1895 rec->cid_len = transform->out_cid_len;
1896 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1897 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001898
1899 if( rec->cid_len != 0 )
1900 {
1901 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001902 * Wrap plaintext into DTLSInnerPlaintext structure.
1903 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001904 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001905 * Note that this changes `rec->data_len`, and hence
1906 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001907 */
1908 if( ssl_cid_build_inner_plaintext( data,
1909 &rec->data_len,
1910 post_avail,
1911 rec->type ) != 0 )
1912 {
1913 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1914 }
1915
1916 rec->type = MBEDTLS_SSL_MSG_CID;
1917 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001918#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001919
Hanno Becker92c930f2019-04-29 17:31:37 +01001920 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001923 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001925#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 if( mode == MBEDTLS_MODE_STREAM ||
1927 ( mode == MBEDTLS_MODE_CBC
1928#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001929 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001930#endif
1931 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 {
Hanno Becker3307b532017-12-27 21:37:21 +00001933 if( post_avail < transform->maclen )
1934 {
1935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1936 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1937 }
1938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001940 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001941 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001942 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001943 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1944 data, rec->data_len, rec->ctr, rec->type, mac );
1945 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 }
1947 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1950 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001951 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001952 {
Hanno Becker992b6872017-11-09 18:57:39 +00001953 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1954
Hanno Beckere83efe62019-04-29 13:52:53 +01001955 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001956
Hanno Becker3307b532017-12-27 21:37:21 +00001957 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001958 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001959 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1960 data, rec->data_len );
1961 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1962 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1963
1964 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001965 }
1966 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001967#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001971 }
1972
Hanno Becker3307b532017-12-27 21:37:21 +00001973 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1974 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975
Hanno Becker3307b532017-12-27 21:37:21 +00001976 rec->data_len += transform->maclen;
1977 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001978 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001979 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001980#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001982 /*
1983 * Encrypt
1984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1986 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001988 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001989 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001991 "including %d bytes of padding",
1992 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
Hanno Becker3307b532017-12-27 21:37:21 +00001994 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1995 transform->iv_enc, transform->ivlen,
1996 data, rec->data_len,
1997 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002000 return( ret );
2001 }
2002
Hanno Becker3307b532017-12-27 21:37:21 +00002003 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
Paul Bakker68884e32013-01-07 18:20:04 +01002009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002011
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002012#if defined(MBEDTLS_GCM_C) || \
2013 defined(MBEDTLS_CCM_C) || \
2014 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002016 mode == MBEDTLS_MODE_CCM ||
2017 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002018 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002019 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002021 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 /* Check that there's space for both the authentication tag
2024 * and the explicit IV before and after the record content. */
2025 if( post_avail < transform->taglen ||
2026 rec->data_offset < explicit_iv_len )
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2029 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2030 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002031
Paul Bakker68884e32013-01-07 18:20:04 +01002032 /*
2033 * Generate IV
2034 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002035 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2036 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002037 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002038 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002039 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2040 explicit_iv_len );
2041 /* Prefix record content with explicit IV. */
2042 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 }
2044 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2045 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002046 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 unsigned char i;
2048
2049 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2050
2051 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002052 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002053 }
2054 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002055 {
2056 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002059 }
2060
Hanno Beckere83efe62019-04-29 13:52:53 +01002061 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002062
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2064 iv, transform->ivlen );
2065 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002066 data - explicit_iv_len, explicit_iv_len );
2067 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002068 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002070 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002072
Paul Bakker68884e32013-01-07 18:20:04 +01002073 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002074 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002075 */
Hanno Becker3307b532017-12-27 21:37:21 +00002076
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002077 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002078 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002079 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002080 data, rec->data_len, /* source */
2081 data, &rec->data_len, /* destination */
2082 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002085 return( ret );
2086 }
2087
Hanno Becker3307b532017-12-27 21:37:21 +00002088 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2089 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len += transform->taglen + explicit_iv_len;
2092 rec->data_offset -= explicit_iv_len;
2093 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002094 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2098#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002099 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002102 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002103 size_t padlen, i;
2104 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002105
Hanno Becker3307b532017-12-27 21:37:21 +00002106 /* Currently we're always using minimal padding
2107 * (up to 255 bytes would be allowed). */
2108 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2109 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 padlen = 0;
2111
Hanno Becker3307b532017-12-27 21:37:21 +00002112 /* Check there's enough space in the buffer for the padding. */
2113 if( post_avail < padlen + 1 )
2114 {
2115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2116 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2117 }
2118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002120 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Hanno Becker3307b532017-12-27 21:37:21 +00002122 rec->data_len += padlen + 1;
2123 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002127 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2128 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129 */
Hanno Becker3307b532017-12-27 21:37:21 +00002130 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 {
Hanno Becker3307b532017-12-27 21:37:21 +00002132 if( f_rng == NULL )
2133 {
2134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2136 }
2137
2138 if( rec->data_offset < transform->ivlen )
2139 {
2140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2142 }
2143
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144 /*
2145 * Generate IV
2146 */
Hanno Becker3307b532017-12-27 21:37:21 +00002147 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002148 if( ret != 0 )
2149 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150
Hanno Becker3307b532017-12-27 21:37:21 +00002151 memcpy( data - transform->ivlen, transform->iv_enc,
2152 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002159 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002160 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Hanno Becker3307b532017-12-27 21:37:21 +00002162 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2163 transform->iv_enc,
2164 transform->ivlen,
2165 data, rec->data_len,
2166 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002169 return( ret );
2170 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002171
Hanno Becker3307b532017-12-27 21:37:21 +00002172 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002176 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002179 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002180 {
2181 /*
2182 * Save IV in SSL3 and TLS1
2183 */
Hanno Becker3307b532017-12-27 21:37:21 +00002184 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2185 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
Hanno Becker3307b532017-12-27 21:37:21 +00002187 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002188#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002189 {
2190 data -= transform->ivlen;
2191 rec->data_offset -= transform->ivlen;
2192 rec->data_len += transform->ivlen;
2193 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002196 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002197 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002198 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2199
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002200 /*
2201 * MAC(MAC_write_key, seq_num +
2202 * TLSCipherText.type +
2203 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002204 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 * IV + // except for TLS 1.0
2206 * ENC(content + padding + padding_length));
2207 */
Hanno Becker3307b532017-12-27 21:37:21 +00002208
2209 if( post_avail < transform->maclen)
2210 {
2211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Hanno Beckere83efe62019-04-29 13:52:53 +01002215 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002218 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002219 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002220
Hanno Becker3307b532017-12-27 21:37:21 +00002221 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002222 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2224 data, rec->data_len );
2225 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2226 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002227
Hanno Becker3307b532017-12-27 21:37:21 +00002228 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002229
Hanno Becker3307b532017-12-27 21:37:21 +00002230 rec->data_len += transform->maclen;
2231 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002232 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002238 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002244 /* Make extra sure authentication was performed, exactly once */
2245 if( auth_done != 1 )
2246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 }
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
2253 return( 0 );
2254}
2255
Hanno Becker611a83b2018-01-03 14:27:32 +00002256int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2257 mbedtls_ssl_transform *transform,
2258 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002260 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002262 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002263#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002264 size_t padlen = 0, correct = 1;
2265#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002266 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002267 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002268 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002269
Hanno Becker611a83b2018-01-03 14:27:32 +00002270#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002271 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002272 ((void) ssl);
2273#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002276 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002277 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2280 }
2281 if( rec == NULL ||
2282 rec->buf == NULL ||
2283 rec->buf_len < rec->data_offset ||
2284 rec->buf_len - rec->data_offset < rec->data_len )
2285 {
2286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002288 }
2289
Hanno Becker4c6876b2017-12-27 21:28:58 +00002290 data = rec->buf + rec->data_offset;
2291 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Hanno Beckera5a2b082019-05-15 14:03:01 +01002293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002294 /*
2295 * Match record's CID with incoming CID.
2296 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002297 if( rec->cid_len != transform->in_cid_len ||
2298 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2299 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002300 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002301 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002302#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2305 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002306 {
2307 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002308 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2309 transform->iv_dec,
2310 transform->ivlen,
2311 data, rec->data_len,
2312 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002315 return( ret );
2316 }
2317
Hanno Becker4c6876b2017-12-27 21:28:58 +00002318 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 }
Paul Bakker68884e32013-01-07 18:20:04 +01002324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002326#if defined(MBEDTLS_GCM_C) || \
2327 defined(MBEDTLS_CCM_C) || \
2328 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 mode == MBEDTLS_MODE_CCM ||
2331 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002332 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002333 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002335
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 /*
2337 * Compute and update sizes
2338 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002342 "+ taglen (%d)", rec->data_len,
2343 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 }
Paul Bakker68884e32013-01-07 18:20:04 +01002346
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 /*
2348 * Prepare IV
2349 */
2350 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2351 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002352 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002354 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 }
2357 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2358 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002359 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 unsigned char i;
2361
2362 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2363
2364 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002365 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002366 }
2367 else
2368 {
2369 /* Reminder if we ever add an AEAD mode with a different size */
2370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2372 }
2373
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 data += explicit_iv_len;
2375 rec->data_offset += explicit_iv_len;
2376 rec->data_len -= explicit_iv_len + transform->taglen;
2377
Hanno Beckere83efe62019-04-29 13:52:53 +01002378 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002381
2382 memcpy( transform->iv_dec + transform->fixed_ivlen,
2383 data - explicit_iv_len, explicit_iv_len );
2384
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002387 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002388
Paul Bakker68884e32013-01-07 18:20:04 +01002389
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002390 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002391 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002392 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002393 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2394 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002395 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002396 data, rec->data_len,
2397 data, &olen,
2398 data + rec->data_len,
2399 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2404 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002405
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002406 return( ret );
2407 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002408 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002409
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002414 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2418#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002419 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002422 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 /*
Paul Bakker45829992013-01-03 14:52:21 +01002425 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002428 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2429 {
2430 /* The ciphertext is prefixed with the CBC IV. */
2431 minlen += transform->ivlen;
2432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#endif
Paul Bakker45829992013-01-03 14:52:21 +01002434
Hanno Becker4c6876b2017-12-27 21:28:58 +00002435 /* Size considerations:
2436 *
2437 * - The CBC cipher text must not be empty and hence
2438 * at least of size transform->ivlen.
2439 *
2440 * Together with the potential IV-prefix, this explains
2441 * the first of the two checks below.
2442 *
2443 * - The record must contain a MAC, either in plain or
2444 * encrypted, depending on whether Encrypt-then-MAC
2445 * is used or not.
2446 * - If it is, the message contains the IV-prefix,
2447 * the CBC ciphertext, and the MAC.
2448 * - If it is not, the padded plaintext, and hence
2449 * the CBC ciphertext, has at least length maclen + 1
2450 * because there is at least the padding length byte.
2451 *
2452 * As the CBC ciphertext is not empty, both cases give the
2453 * lower bound minlen + maclen + 1 on the record size, which
2454 * we test for in the second check below.
2455 */
2456 if( rec->data_len < minlen + transform->ivlen ||
2457 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002460 "+ 1 ) ( + expl IV )", rec->data_len,
2461 transform->ivlen,
2462 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002464 }
2465
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002466 /*
2467 * Authenticate before decrypt if enabled
2468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002470 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 {
Hanno Becker992b6872017-11-09 18:57:39 +00002472 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475
Hanno Becker4c6876b2017-12-27 21:28:58 +00002476 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2477 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Hanno Beckere83efe62019-04-29 13:52:53 +01002479 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002480
Hanno Beckere83efe62019-04-29 13:52:53 +01002481 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2482 add_data_len );
2483 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2484 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002485 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2486 data, rec->data_len );
2487 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2488 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002489
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2491 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002492 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002493 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2496 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504
2505 /*
2506 * Check length sanity
2507 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002508 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002516 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002517 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 /* This is safe because data_len >= minlen + maclen + 1 initially,
2522 * and at this point we have at most subtracted maclen (note that
2523 * minlen == transform->ivlen here). */
2524 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 data += transform->ivlen;
2527 rec->data_offset += transform->ivlen;
2528 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002531
Hanno Becker4c6876b2017-12-27 21:28:58 +00002532 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2533 transform->iv_dec, transform->ivlen,
2534 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002537 return( ret );
2538 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002539
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002548 {
2549 /*
2550 * Save IV in SSL3 and TLS1
2551 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2553 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 /* Safe since data_len >= minlen + maclen + 1, so after having
2558 * subtracted at most minlen and maclen up to this point,
2559 * data_len > 0. */
2560 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 if( auth_done == 1 )
2563 {
2564 correct *= ( rec->data_len >= padlen + 1 );
2565 padlen *= ( rec->data_len >= padlen + 1 );
2566 }
2567 else
Paul Bakker45829992013-01-03 14:52:21 +01002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002570 if( rec->data_len < transform->maclen + padlen + 1 )
2571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2573 rec->data_len,
2574 transform->maclen,
2575 padlen + 1 ) );
2576 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002577#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578
2579 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2580 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 padlen++;
2584
2585 /* Regardless of the validity of the padding,
2586 * we have data_len >= padlen here. */
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_DEBUG_ALL)
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002595 "should be no more than %d",
2596 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Paul Bakker45829992013-01-03 14:52:21 +01002598 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600 }
2601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2603#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2604 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002605 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607 /* The padding check involves a series of up to 256
2608 * consecutive memory reads at the end of the record
2609 * plaintext buffer. In order to hide the length and
2610 * validity of the padding, always perform exactly
2611 * `min(256,plaintext_len)` reads (but take into account
2612 * only the last `padlen` bytes for the padding check). */
2613 size_t pad_count = 0;
2614 size_t real_count = 0;
2615 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002616
Hanno Becker4c6876b2017-12-27 21:28:58 +00002617 /* Index of first padding byte; it has been ensured above
2618 * that the subtraction is safe. */
2619 size_t const padding_idx = rec->data_len - padlen;
2620 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2621 size_t const start_idx = rec->data_len - num_checks;
2622 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002623
Hanno Becker4c6876b2017-12-27 21:28:58 +00002624 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002625 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 real_count |= ( idx >= padding_idx );
2627 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002628 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002632 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002634#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2639 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002643 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002644
Hanno Becker4c6876b2017-12-27 21:28:58 +00002645 /* If the padding was found to be invalid, padlen == 0
2646 * and the subtraction is safe. If the padding was found valid,
2647 * padlen hasn't been changed and the previous assertion
2648 * data_len >= padlen still holds. */
2649 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002653 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002659#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002661 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002662#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002665 * Authenticate if not done yet.
2666 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002668#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002669 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002670 {
Hanno Becker992b6872017-11-09 18:57:39 +00002671 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002672
Hanno Becker4c6876b2017-12-27 21:28:58 +00002673 /* If the initial value of padlen was such that
2674 * data_len < maclen + padlen + 1, then padlen
2675 * got reset to 1, and the initial check
2676 * data_len >= minlen + maclen + 1
2677 * guarantees that at this point we still
2678 * have at least data_len >= maclen.
2679 *
2680 * If the initial value of padlen was such that
2681 * data_len >= maclen + padlen + 1, then we have
2682 * subtracted either padlen + 1 (if the padding was correct)
2683 * or 0 (if the padding was incorrect) since then,
2684 * hence data_len >= maclen in any case.
2685 */
2686 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Hanno Beckere83efe62019-04-29 13:52:53 +01002688 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002691 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002692 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 ssl_mac( &transform->md_ctx_dec,
2694 transform->mac_dec,
2695 data, rec->data_len,
2696 rec->ctr, rec->type,
2697 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002698 }
2699 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2701#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2702 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002703 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002704 {
2705 /*
2706 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002707 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002708 *
2709 * Known timing attacks:
2710 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2711 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * To compensate for different timings for the MAC calculation
2713 * depending on how much padding was removed (which is determined
2714 * by padlen), process extra_run more blocks through the hash
2715 * function.
2716 *
2717 * The formula in the paper is
2718 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2719 * where L1 is the size of the header plus the decrypted message
2720 * plus CBC padding and L2 is the size of the header plus the
2721 * decrypted message. This is for an underlying hash function
2722 * with 64-byte blocks.
2723 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2724 * correctly. We round down instead of up, so -56 is the correct
2725 * value for our calculations instead of -55.
2726 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002727 * Repeat the formula rather than defining a block_size variable.
2728 * This avoids requiring division by a variable at runtime
2729 * (which would be marginally less efficient and would require
2730 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002731 */
2732 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002733 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002734
2735 /*
2736 * The next two sizes are the minimum and maximum values of
2737 * in_msglen over all padlen values.
2738 *
2739 * They're independent of padlen, since we previously did
2740 * in_msglen -= padlen.
2741 *
2742 * Note that max_len + maclen is never more than the buffer
2743 * length, as we previously did in_msglen -= maclen too.
2744 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002745 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002746 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2747
Hanno Becker4c6876b2017-12-27 21:28:58 +00002748 memset( tmp, 0, sizeof( tmp ) );
2749
2750 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002751 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002752#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2753 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002754 case MBEDTLS_MD_MD5:
2755 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002757 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002758 extra_run =
2759 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2760 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 break;
2762#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002763#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002764 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002765 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002766 extra_run =
2767 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2768 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 break;
2770#endif
2771 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2774 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002775
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002776 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002777
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2779 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002780 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2781 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002782 /* Make sure we access everything even when padlen > 0. This
2783 * makes the synchronisation requirements for just-in-time
2784 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 ssl_read_memory( data + rec->data_len, padlen );
2786 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787
2788 /* Call mbedtls_md_process at least once due to cache attacks
2789 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002790 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002791 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002792
Hanno Becker4c6876b2017-12-27 21:28:58 +00002793 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002794
2795 /* Make sure we access all the memory that could contain the MAC,
2796 * before we check it in the next code block. This makes the
2797 * synchronisation requirements for just-in-time Prime+Probe
2798 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002799 ssl_read_memory( data + min_len,
2800 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002801 }
2802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2804 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002810#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2812 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002813#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Hanno Becker4c6876b2017-12-27 21:28:58 +00002815 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2816 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_DEBUG_ALL)
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002820#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 correct = 0;
2822 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002823 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002824 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002825
2826 /*
2827 * Finally check the correct flag
2828 */
2829 if( correct == 0 )
2830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002831#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002832
2833 /* Make extra sure authentication was performed, exactly once */
2834 if( auth_done != 1 )
2835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Hanno Beckera5a2b082019-05-15 14:03:01 +01002840#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002841 if( rec->cid_len != 0 )
2842 {
2843 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2844 &rec->type );
2845 if( ret != 0 )
2846 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2847 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002848#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
2852 return( 0 );
2853}
2854
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002855#undef MAC_NONE
2856#undef MAC_PLAINTEXT
2857#undef MAC_CIPHERTEXT
2858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002860/*
2861 * Compression/decompression functions
2862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002864{
2865 int ret;
2866 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002867 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002868 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002869 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002872
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002873 if( len_pre == 0 )
2874 return( 0 );
2875
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876 memcpy( msg_pre, ssl->out_msg, len_pre );
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002879 ssl->out_msglen ) );
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002882 ssl->out_msg, ssl->out_msglen );
2883
Paul Bakker48916f92012-09-16 19:57:18 +00002884 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2885 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2886 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002887 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890 if( ret != Z_OK )
2891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2893 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002894 }
2895
Angus Grattond8213d02016-05-25 20:56:48 +10002896 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002897 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msglen ) );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 ssl->out_msg, ssl->out_msglen );
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002906
2907 return( 0 );
2908}
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911{
2912 int ret;
2913 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002914 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002916 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002920 if( len_pre == 0 )
2921 return( 0 );
2922
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 memcpy( msg_pre, ssl->in_msg, len_pre );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 ssl->in_msglen ) );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->in_msg, ssl->in_msglen );
2930
Paul Bakker48916f92012-09-16 19:57:18 +00002931 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2932 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2933 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002934 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002935 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936
Paul Bakker48916f92012-09-16 19:57:18 +00002937 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938 if( ret != Z_OK )
2939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2941 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942 }
2943
Angus Grattond8213d02016-05-25 20:56:48 +10002944 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002945 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948 ssl->in_msglen ) );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951 ssl->in_msg, ssl->in_msglen );
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002954
2955 return( 0 );
2956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2960static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_PROTO_DTLS)
2963static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002964{
2965 /* If renegotiation is not enforced, retransmit until we would reach max
2966 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002967 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002968 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002969 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002970 unsigned char doublings = 1;
2971
2972 while( ratio != 0 )
2973 {
2974 ++doublings;
2975 ratio >>= 1;
2976 }
2977
2978 if( ++ssl->renego_records_seen > doublings )
2979 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981 return( 0 );
2982 }
2983 }
2984
2985 return( ssl_write_hello_request( ssl ) );
2986}
2987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002989
Paul Bakker5121ce52009-01-03 21:22:43 +00002990/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002991 * Fill the input message buffer by appending data to it.
2992 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002993 *
2994 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2995 * available (from this read and/or a previous one). Otherwise, an error code
2996 * is returned (possibly EOF or WANT_READ).
2997 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002998 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2999 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3000 * since we always read a whole datagram at once.
3001 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003002 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003006{
Paul Bakker23986e52011-04-24 08:57:21 +00003007 int ret;
3008 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003012 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003015 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 }
3018
Angus Grattond8213d02016-05-25 20:56:48 +10003019 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003023 }
3024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003026 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003028 uint32_t timeout;
3029
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003030 /* Just to be sure */
3031 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3032 {
3033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3034 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3036 }
3037
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003038 /*
3039 * The point is, we need to always read a full datagram at once, so we
3040 * sometimes read more then requested, and handle the additional data.
3041 * It could be the rest of the current record (while fetching the
3042 * header) and/or some other records in the same datagram.
3043 */
3044
3045 /*
3046 * Move to the next record in the already read datagram if applicable
3047 */
3048 if( ssl->next_record_offset != 0 )
3049 {
3050 if( ssl->in_left < ssl->next_record_offset )
3051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003054 }
3055
3056 ssl->in_left -= ssl->next_record_offset;
3057
3058 if( ssl->in_left != 0 )
3059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003061 ssl->next_record_offset ) );
3062 memmove( ssl->in_hdr,
3063 ssl->in_hdr + ssl->next_record_offset,
3064 ssl->in_left );
3065 }
3066
3067 ssl->next_record_offset = 0;
3068 }
3069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003072
3073 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003075 */
3076 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003079 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003080 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003081
3082 /*
3083 * A record can't be split accross datagrams. If we need to read but
3084 * are not at the beginning of a new record, the caller did something
3085 * wrong.
3086 */
3087 if( ssl->in_left != 0 )
3088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003091 }
3092
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003093 /*
3094 * Don't even try to read if time's out already.
3095 * This avoids by-passing the timer when repeatedly receiving messages
3096 * that will end up being dropped.
3097 */
3098 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003099 {
3100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003101 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003102 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003103 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003104 {
Angus Grattond8213d02016-05-25 20:56:48 +10003105 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003108 timeout = ssl->handshake->retransmit_timeout;
3109 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003110 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003114 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003115 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3116 timeout );
3117 else
3118 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003121
3122 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003124 }
3125
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003126 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003132 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003133 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003136 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003137 }
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 return( ret );
3143 }
3144
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003145 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003148 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003150 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003151 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003154 return( ret );
3155 }
3156
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003157 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003160 }
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 if( ret < 0 )
3163 return( ret );
3164
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003165 ssl->in_left = ret;
3166 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003167 MBEDTLS_SSL_TRANSPORT_ELSE
3168#endif /* MBEDTLS_SSL_PROTO_DTLS */
3169#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003172 ssl->in_left, nb_want ) );
3173
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003174 while( ssl->in_left < nb_want )
3175 {
3176 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003177
3178 if( ssl_check_timer( ssl ) != 0 )
3179 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3180 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003181 {
3182 if( ssl->f_recv_timeout != NULL )
3183 {
3184 ret = ssl->f_recv_timeout( ssl->p_bio,
3185 ssl->in_hdr + ssl->in_left, len,
3186 ssl->conf->read_timeout );
3187 }
3188 else
3189 {
3190 ret = ssl->f_recv( ssl->p_bio,
3191 ssl->in_hdr + ssl->in_left, len );
3192 }
3193 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003196 ssl->in_left, nb_want ) );
3197 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003198
3199 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003201
3202 if( ret < 0 )
3203 return( ret );
3204
mohammad160352aecb92018-03-28 23:41:40 -07003205 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003206 {
Darryl Green11999bb2018-03-13 15:22:58 +00003207 MBEDTLS_SSL_DEBUG_MSG( 1,
3208 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003209 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003210 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3211 }
3212
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003213 ssl->in_left += ret;
3214 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003216#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
3220 return( 0 );
3221}
3222
3223/*
3224 * Flush any data not yet written
3225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003227{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003228 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003229 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003233 if( ssl->f_send == NULL )
3234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003236 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003238 }
3239
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003240 /* Avoid incrementing counter if data is flushed */
3241 if( ssl->out_left == 0 )
3242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003244 return( 0 );
3245 }
3246
Paul Bakker5121ce52009-01-03 21:22:43 +00003247 while( ssl->out_left > 0 )
3248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003250 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Hanno Becker2b1e3542018-08-06 11:19:13 +01003252 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003253 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
3257 if( ret <= 0 )
3258 return( ret );
3259
mohammad160352aecb92018-03-28 23:41:40 -07003260 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003261 {
Darryl Green11999bb2018-03-13 15:22:58 +00003262 MBEDTLS_SSL_DEBUG_MSG( 1,
3263 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003264 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3266 }
3267
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 ssl->out_left -= ret;
3269 }
3270
Hanno Becker2b1e3542018-08-06 11:19:13 +01003271#if defined(MBEDTLS_SSL_PROTO_DTLS)
3272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003273 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003274 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003275 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003276 else
3277#endif
3278 {
3279 ssl->out_hdr = ssl->out_buf + 8;
3280 }
3281 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
3285 return( 0 );
3286}
3287
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003288/*
3289 * Functions to handle the DTLS retransmission state machine
3290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003292/*
3293 * Append current handshake message to current outgoing flight
3294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3299 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3300 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003301
3302 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003303 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003304 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003307 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308 }
3309
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003310 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315 }
3316
3317 /* Copy current handshake message with headers */
3318 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3319 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003320 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321 msg->next = NULL;
3322
3323 /* Append to the current flight */
3324 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003325 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326 else
3327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003329 while( cur->next != NULL )
3330 cur = cur->next;
3331 cur->next = msg;
3332 }
3333
Hanno Becker3b235902018-08-06 09:54:53 +01003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335 return( 0 );
3336}
3337
3338/*
3339 * Free the current flight of handshake messages
3340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 mbedtls_ssl_flight_item *cur = flight;
3344 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003345
3346 while( cur != NULL )
3347 {
3348 next = cur->next;
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_free( cur->p );
3351 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352
3353 cur = next;
3354 }
3355}
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3358static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003359#endif
3360
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003361/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003362 * Swap transform_out and out_ctr with the alternative ones
3363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003367 unsigned char tmp_out_ctr[8];
3368
3369 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372 return;
3373 }
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003376
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003377 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003378 tmp_transform = ssl->transform_out;
3379 ssl->transform_out = ssl->handshake->alt_transform_out;
3380 ssl->handshake->alt_transform_out = tmp_transform;
3381
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003382 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003383 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3384 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003386
3387 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003388 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3391 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3396 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003397 }
3398 }
3399#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400}
3401
3402/*
3403 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003404 */
3405int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3406{
3407 int ret = 0;
3408
3409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3410
3411 ret = mbedtls_ssl_flight_transmit( ssl );
3412
3413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3414
3415 return( ret );
3416}
3417
3418/*
3419 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003420 *
3421 * Need to remember the current message in case flush_output returns
3422 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003423 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003424 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003425int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003426{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003427 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433
3434 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003435 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436 ssl_swap_epochs( ssl );
3437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003439 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003440
3441 while( ssl->handshake->cur_msg != NULL )
3442 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003443 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003444 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003445
Hanno Beckere1dcb032018-08-17 16:47:58 +01003446 int const is_finished =
3447 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3448 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3449
Hanno Becker04da1892018-08-14 13:22:10 +01003450 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3451 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3452
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003453 /* Swap epochs before sending Finished: we can't do it after
3454 * sending ChangeCipherSpec, in case write returns WANT_READ.
3455 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003456 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003457 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003459 ssl_swap_epochs( ssl );
3460 }
3461
Hanno Becker67bc7c32018-08-06 11:33:50 +01003462 ret = ssl_get_remaining_payload_in_datagram( ssl );
3463 if( ret < 0 )
3464 return( ret );
3465 max_frag_len = (size_t) ret;
3466
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003467 /* CCS is copied as is, while HS messages may need fragmentation */
3468 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3469 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003470 if( max_frag_len == 0 )
3471 {
3472 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3473 return( ret );
3474
3475 continue;
3476 }
3477
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003478 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003479 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003480 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003481
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003482 /* Update position inside current message */
3483 ssl->handshake->cur_msg_p += cur->len;
3484 }
3485 else
3486 {
3487 const unsigned char * const p = ssl->handshake->cur_msg_p;
3488 const size_t hs_len = cur->len - 12;
3489 const size_t frag_off = p - ( cur->p + 12 );
3490 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003491 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003492
Hanno Beckere1dcb032018-08-17 16:47:58 +01003493 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003494 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003495 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003496 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003497
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3499 return( ret );
3500
3501 continue;
3502 }
3503 max_hs_frag_len = max_frag_len - 12;
3504
3505 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3506 max_hs_frag_len : rem_len;
3507
3508 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003509 {
3510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003511 (unsigned) cur_hs_frag_len,
3512 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003513 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003514
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003515 /* Messages are stored with handshake headers as if not fragmented,
3516 * copy beginning of headers then fill fragmentation fields.
3517 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3518 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003520 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3521 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3522 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3523
Hanno Becker67bc7c32018-08-06 11:33:50 +01003524 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3525 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3526 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003527
3528 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3529
Hanno Becker3f7b9732018-08-28 09:53:25 +01003530 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003531 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3532 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003533 ssl->out_msgtype = cur->type;
3534
3535 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003536 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003537 }
3538
3539 /* If done with the current message move to the next one if any */
3540 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3541 {
3542 if( cur->next != NULL )
3543 {
3544 ssl->handshake->cur_msg = cur->next;
3545 ssl->handshake->cur_msg_p = cur->next->p + 12;
3546 }
3547 else
3548 {
3549 ssl->handshake->cur_msg = NULL;
3550 ssl->handshake->cur_msg_p = NULL;
3551 }
3552 }
3553
3554 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003555 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003558 return( ret );
3559 }
3560 }
3561
Hanno Becker67bc7c32018-08-06 11:33:50 +01003562 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3563 return( ret );
3564
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003565 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3567 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003568 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003571 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3572 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003573
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575
3576 return( 0 );
3577}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003578
3579/*
3580 * To be called when the last message of an incoming flight is received.
3581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003583{
3584 /* We won't need to resend that one any more */
3585 ssl_flight_free( ssl->handshake->flight );
3586 ssl->handshake->flight = NULL;
3587 ssl->handshake->cur_msg = NULL;
3588
3589 /* The next incoming flight will start with this msg_seq */
3590 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3591
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003592 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003593 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003594
Hanno Becker0271f962018-08-16 13:23:47 +01003595 /* Clear future message buffering structure. */
3596 ssl_buffering_free( ssl );
3597
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003598 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003599 ssl_set_timer( ssl, 0 );
3600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3602 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605 }
3606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003608}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003609
3610/*
3611 * To be called when the last message of an outgoing flight is send.
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003614{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003615 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003616 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3619 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003622 }
3623 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003627
Paul Bakker5121ce52009-01-03 21:22:43 +00003628/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003629 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003631
3632/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003633 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003634 *
3635 * - fill in handshake headers
3636 * - update handshake checksum
3637 * - DTLS: save message for resending
3638 * - then pass to the record layer
3639 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003640 * DTLS: except for HelloRequest, messages are only queued, and will only be
3641 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003642 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003643 * Inputs:
3644 * - ssl->out_msglen: 4 + actual handshake message len
3645 * (4 is the size of handshake headers for TLS)
3646 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3647 * - ssl->out_msg + 4: the handshake message body
3648 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003649 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003650 * - ssl->out_msglen: the length of the record contents
3651 * (including handshake headers but excluding record headers)
3652 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003653 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003654int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003655{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003656 int ret;
3657 const size_t hs_len = ssl->out_msglen - 4;
3658 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3661
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003662 /*
3663 * Sanity checks
3664 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003665 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003666 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3667 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003668 /* In SSLv3, the client might send a NoCertificate alert. */
3669#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3670 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3671 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3672 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3673#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3674 {
3675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3677 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003680 /* Whenever we send anything different from a
3681 * HelloRequest we should be in a handshake - double check. */
3682 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3683 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 ssl->handshake == NULL )
3685 {
3686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003691 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003692 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003694 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003699
Hanno Beckerb50a2532018-08-06 11:52:54 +01003700 /* Double-check that we did not exceed the bounds
3701 * of the outgoing record buffer.
3702 * This should never fail as the various message
3703 * writing functions must obey the bounds of the
3704 * outgoing record buffer, but better be safe.
3705 *
3706 * Note: We deliberately do not check for the MTU or MFL here.
3707 */
3708 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3709 {
3710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3711 "size %u, maximum %u",
3712 (unsigned) ssl->out_msglen,
3713 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3714 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3715 }
3716
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003717 /*
3718 * Fill handshake headers
3719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003722 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3723 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3724 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003726 /*
3727 * DTLS has additional fields in the Handshake layer,
3728 * between the length field and the actual payload:
3729 * uint16 message_seq;
3730 * uint24 fragment_offset;
3731 * uint24 fragment_length;
3732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003735 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003736 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003737 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3740 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003741 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003742 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3744 }
3745
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003746 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003747 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003748
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003749 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003750 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003751 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003752 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3753 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3754 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003755 }
3756 else
3757 {
3758 ssl->out_msg[4] = 0;
3759 ssl->out_msg[5] = 0;
3760 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003761
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003762 /* Handshake hashes are computed without fragmentation,
3763 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003764 memset( ssl->out_msg + 6, 0x00, 3 );
3765 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003768
Hanno Becker0207e532018-08-28 10:28:28 +01003769 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003770 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3771 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 }
3773
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003774 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003777 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3778 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003779 {
3780 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003783 return( ret );
3784 }
3785 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003786 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003787#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003788 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003789 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003790 {
3791 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3792 return( ret );
3793 }
3794 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003795
3796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3797
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003798 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003799}
3800
3801/*
3802 * Record layer functions
3803 */
3804
3805/*
3806 * Write current record.
3807 *
3808 * Uses:
3809 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3810 * - ssl->out_msglen: length of the record content (excl headers)
3811 * - ssl->out_msg: record content
3812 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003813int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003814{
3815 int ret, done = 0;
3816 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003817 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003818
3819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003822 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003824 {
3825 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003828 return( ret );
3829 }
3830
3831 len = ssl->out_msglen;
3832 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3836 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 ret = mbedtls_ssl_hw_record_write( ssl );
3841 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3844 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003845 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003846
3847 if( ret == 0 )
3848 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003851 if( !done )
3852 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003853 unsigned i;
3854 size_t protected_record_size;
3855
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003856 /* Skip writing the record content type to after the encryption,
3857 * as it may change when using the CID extension. */
3858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003860 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003861
Hanno Becker19859472018-08-06 09:40:20 +01003862 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003863 ssl->out_len[0] = (unsigned char)( len >> 8 );
3864 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003865
Paul Bakker48916f92012-09-16 19:57:18 +00003866 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003867 {
Hanno Becker3307b532017-12-27 21:37:21 +00003868 mbedtls_record rec;
3869
3870 rec.buf = ssl->out_iv;
3871 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3872 ( ssl->out_iv - ssl->out_buf );
3873 rec.data_len = ssl->out_msglen;
3874 rec.data_offset = ssl->out_msg - rec.buf;
3875
3876 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3877 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3878 ssl->conf->transport, rec.ver );
3879 rec.type = ssl->out_msgtype;
3880
Hanno Beckera5a2b082019-05-15 14:03:01 +01003881#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003882 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003883 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003884#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003885
Hanno Becker611a83b2018-01-03 14:27:32 +00003886 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003887 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003890 return( ret );
3891 }
3892
Hanno Becker3307b532017-12-27 21:37:21 +00003893 if( rec.data_offset != 0 )
3894 {
3895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3897 }
3898
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003899 /* Update the record content type and CID. */
3900 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003901#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003902 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003903#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003904 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003905 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3906 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003907 }
3908
Hanno Becker43395762019-05-03 14:46:38 +01003909 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003910
3911#if defined(MBEDTLS_SSL_PROTO_DTLS)
3912 /* In case of DTLS, double-check that we don't exceed
3913 * the remaining space in the datagram. */
3914 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3915 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003916 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003917 if( ret < 0 )
3918 return( ret );
3919
3920 if( protected_record_size > (size_t) ret )
3921 {
3922 /* Should never happen */
3923 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3924 }
3925 }
3926#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003927
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003928 /* Now write the potentially updated record content type. */
3929 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003932 "version = [%d:%d], msglen = %d",
3933 ssl->out_hdr[0], ssl->out_hdr[1],
3934 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003937 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938
3939 ssl->out_left += protected_record_size;
3940 ssl->out_hdr += protected_record_size;
3941 ssl_update_out_pointers( ssl, ssl->transform_out );
3942
Hanno Becker04484622018-08-06 09:49:38 +01003943 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3944 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3945 break;
3946
3947 /* The loop goes to its end iff the counter is wrapping */
3948 if( i == ssl_ep_len( ssl ) )
3949 {
3950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3951 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 }
3954
Hanno Becker67bc7c32018-08-06 11:33:50 +01003955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3957 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003959 size_t remaining;
3960 ret = ssl_get_remaining_payload_in_datagram( ssl );
3961 if( ret < 0 )
3962 {
3963 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3964 ret );
3965 return( ret );
3966 }
3967
3968 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003969 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003970 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003971 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003972 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003973 else
3974 {
Hanno Becker513815a2018-08-20 11:56:09 +01003975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003976 }
3977 }
3978#endif /* MBEDTLS_SSL_PROTO_DTLS */
3979
3980 if( ( flush == SSL_FORCE_FLUSH ) &&
3981 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003984 return( ret );
3985 }
3986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003988
3989 return( 0 );
3990}
3991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003993
3994static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3995{
3996 if( ssl->in_msglen < ssl->in_hslen ||
3997 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3998 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3999 {
4000 return( 1 );
4001 }
4002 return( 0 );
4003}
Hanno Becker44650b72018-08-16 12:51:11 +01004004
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004005static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004006{
4007 return( ( ssl->in_msg[9] << 16 ) |
4008 ( ssl->in_msg[10] << 8 ) |
4009 ssl->in_msg[11] );
4010}
4011
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004012static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004013{
4014 return( ( ssl->in_msg[6] << 16 ) |
4015 ( ssl->in_msg[7] << 8 ) |
4016 ssl->in_msg[8] );
4017}
4018
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004019static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004020{
4021 uint32_t msg_len, frag_off, frag_len;
4022
4023 msg_len = ssl_get_hs_total_len( ssl );
4024 frag_off = ssl_get_hs_frag_off( ssl );
4025 frag_len = ssl_get_hs_frag_len( ssl );
4026
4027 if( frag_off > msg_len )
4028 return( -1 );
4029
4030 if( frag_len > msg_len - frag_off )
4031 return( -1 );
4032
4033 if( frag_len + 12 > ssl->in_msglen )
4034 return( -1 );
4035
4036 return( 0 );
4037}
4038
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004039/*
4040 * Mark bits in bitmask (used for DTLS HS reassembly)
4041 */
4042static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4043{
4044 unsigned int start_bits, end_bits;
4045
4046 start_bits = 8 - ( offset % 8 );
4047 if( start_bits != 8 )
4048 {
4049 size_t first_byte_idx = offset / 8;
4050
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004051 /* Special case */
4052 if( len <= start_bits )
4053 {
4054 for( ; len != 0; len-- )
4055 mask[first_byte_idx] |= 1 << ( start_bits - len );
4056
4057 /* Avoid potential issues with offset or len becoming invalid */
4058 return;
4059 }
4060
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004061 offset += start_bits; /* Now offset % 8 == 0 */
4062 len -= start_bits;
4063
4064 for( ; start_bits != 0; start_bits-- )
4065 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4066 }
4067
4068 end_bits = len % 8;
4069 if( end_bits != 0 )
4070 {
4071 size_t last_byte_idx = ( offset + len ) / 8;
4072
4073 len -= end_bits; /* Now len % 8 == 0 */
4074
4075 for( ; end_bits != 0; end_bits-- )
4076 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4077 }
4078
4079 memset( mask + offset / 8, 0xFF, len / 8 );
4080}
4081
4082/*
4083 * Check that bitmask is full
4084 */
4085static int ssl_bitmask_check( unsigned char *mask, size_t len )
4086{
4087 size_t i;
4088
4089 for( i = 0; i < len / 8; i++ )
4090 if( mask[i] != 0xFF )
4091 return( -1 );
4092
4093 for( i = 0; i < len % 8; i++ )
4094 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4095 return( -1 );
4096
4097 return( 0 );
4098}
4099
Hanno Becker56e205e2018-08-16 09:06:12 +01004100/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004101static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004102 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004103{
Hanno Becker56e205e2018-08-16 09:06:12 +01004104 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004105
Hanno Becker56e205e2018-08-16 09:06:12 +01004106 alloc_len = 12; /* Handshake header */
4107 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004108
Hanno Beckerd07df862018-08-16 09:14:58 +01004109 if( add_bitmap )
4110 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004111
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004112 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004113}
Hanno Becker56e205e2018-08-16 09:06:12 +01004114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004116
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004117static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004118{
4119 return( ( ssl->in_msg[1] << 16 ) |
4120 ( ssl->in_msg[2] << 8 ) |
4121 ssl->in_msg[3] );
4122}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004123
Simon Butcher99000142016-10-13 17:21:01 +01004124int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004129 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004131 }
4132
Hanno Becker12555c62018-08-16 12:47:53 +01004133 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004136 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004137 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004141 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004142 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004143 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004144
Hanno Becker44650b72018-08-16 12:51:11 +01004145 if( ssl_check_hs_header( ssl ) != 0 )
4146 {
4147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4148 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4149 }
4150
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004151 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004152 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4153 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4154 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4155 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004156 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004157 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4158 {
4159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4160 recv_msg_seq,
4161 ssl->handshake->in_msg_seq ) );
4162 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4163 }
4164
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004165 /* Retransmit only on last message from previous flight, to avoid
4166 * too many retransmissions.
4167 * Besides, No sane server ever retransmits HelloVerifyRequest */
4168 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004172 "message_seq = %d, start_of_flight = %d",
4173 recv_msg_seq,
4174 ssl->handshake->in_flight_start_seq ) );
4175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004179 return( ret );
4180 }
4181 }
4182 else
4183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004185 "message_seq = %d, expected = %d",
4186 recv_msg_seq,
4187 ssl->handshake->in_msg_seq ) );
4188 }
4189
Hanno Becker90333da2017-10-10 11:27:13 +01004190 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004191 }
4192 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004193
Hanno Becker6d97ef52018-08-16 13:09:04 +01004194 /* Message reassembly is handled alongside buffering of future
4195 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004196 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004197 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004198 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004201 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004202 }
4203 }
4204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004206 /* With TLS we don't handle fragmentation (for now) */
4207 if( ssl->in_msglen < ssl->in_hslen )
4208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004211 }
4212
Simon Butcher99000142016-10-13 17:21:01 +01004213 return( 0 );
4214}
4215
4216void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4217{
Hanno Becker0271f962018-08-16 13:23:47 +01004218 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004219
Hanno Becker0271f962018-08-16 13:23:47 +01004220 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004221 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004222 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004223 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004224
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004225 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004228 ssl->handshake != NULL )
4229 {
Hanno Becker0271f962018-08-16 13:23:47 +01004230 unsigned offset;
4231 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004232
Hanno Becker0271f962018-08-16 13:23:47 +01004233 /* Increment handshake sequence number */
4234 hs->in_msg_seq++;
4235
4236 /*
4237 * Clear up handshake buffering and reassembly structure.
4238 */
4239
4240 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004241 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004242
4243 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004244 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4245 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004246 offset++, hs_buf++ )
4247 {
4248 *hs_buf = *(hs_buf + 1);
4249 }
4250
4251 /* Create a fresh last entry */
4252 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004253 }
4254#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004255}
4256
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004257/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004258 * DTLS anti-replay: RFC 6347 4.1.2.6
4259 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004260 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4261 * Bit n is set iff record number in_window_top - n has been seen.
4262 *
4263 * Usually, in_window_top is the last record number seen and the lsb of
4264 * in_window is set. The only exception is the initial state (record number 0
4265 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4268static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004269{
4270 ssl->in_window_top = 0;
4271 ssl->in_window = 0;
4272}
4273
4274static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4275{
4276 return( ( (uint64_t) buf[0] << 40 ) |
4277 ( (uint64_t) buf[1] << 32 ) |
4278 ( (uint64_t) buf[2] << 24 ) |
4279 ( (uint64_t) buf[3] << 16 ) |
4280 ( (uint64_t) buf[4] << 8 ) |
4281 ( (uint64_t) buf[5] ) );
4282}
4283
4284/*
4285 * Return 0 if sequence number is acceptable, -1 otherwise
4286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004288{
4289 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4290 uint64_t bit;
4291
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004292 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004293 return( 0 );
4294
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004295 if( rec_seqnum > ssl->in_window_top )
4296 return( 0 );
4297
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004298 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299
4300 if( bit >= 64 )
4301 return( -1 );
4302
4303 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4304 return( -1 );
4305
4306 return( 0 );
4307}
4308
4309/*
4310 * Update replay window on new validated record
4311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004313{
4314 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4315
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004316 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004317 return;
4318
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004319 if( rec_seqnum > ssl->in_window_top )
4320 {
4321 /* Update window_top and the contents of the window */
4322 uint64_t shift = rec_seqnum - ssl->in_window_top;
4323
4324 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004325 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004327 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004329 ssl->in_window |= 1;
4330 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004331
4332 ssl->in_window_top = rec_seqnum;
4333 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004334 else
4335 {
4336 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004337 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004338
4339 if( bit < 64 ) /* Always true, but be extra sure */
4340 ssl->in_window |= (uint64_t) 1 << bit;
4341 }
4342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004344
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004345#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004346/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004347static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4348
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004349/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004350 * Without any SSL context, check if a datagram looks like a ClientHello with
4351 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004352 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004353 *
4354 * - if cookie is valid, return 0
4355 * - if ClientHello looks superficially valid but cookie is not,
4356 * fill obuf and set olen, then
4357 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4358 * - otherwise return a specific error code
4359 */
4360static int ssl_check_dtls_clihlo_cookie(
4361 mbedtls_ssl_cookie_write_t *f_cookie_write,
4362 mbedtls_ssl_cookie_check_t *f_cookie_check,
4363 void *p_cookie,
4364 const unsigned char *cli_id, size_t cli_id_len,
4365 const unsigned char *in, size_t in_len,
4366 unsigned char *obuf, size_t buf_len, size_t *olen )
4367{
4368 size_t sid_len, cookie_len;
4369 unsigned char *p;
4370
4371 if( f_cookie_write == NULL || f_cookie_check == NULL )
4372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4373
4374 /*
4375 * Structure of ClientHello with record and handshake headers,
4376 * and expected values. We don't need to check a lot, more checks will be
4377 * done when actually parsing the ClientHello - skipping those checks
4378 * avoids code duplication and does not make cookie forging any easier.
4379 *
4380 * 0-0 ContentType type; copied, must be handshake
4381 * 1-2 ProtocolVersion version; copied
4382 * 3-4 uint16 epoch; copied, must be 0
4383 * 5-10 uint48 sequence_number; copied
4384 * 11-12 uint16 length; (ignored)
4385 *
4386 * 13-13 HandshakeType msg_type; (ignored)
4387 * 14-16 uint24 length; (ignored)
4388 * 17-18 uint16 message_seq; copied
4389 * 19-21 uint24 fragment_offset; copied, must be 0
4390 * 22-24 uint24 fragment_length; (ignored)
4391 *
4392 * 25-26 ProtocolVersion client_version; (ignored)
4393 * 27-58 Random random; (ignored)
4394 * 59-xx SessionID session_id; 1 byte len + sid_len content
4395 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4396 * ...
4397 *
4398 * Minimum length is 61 bytes.
4399 */
4400 if( in_len < 61 ||
4401 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4402 in[3] != 0 || in[4] != 0 ||
4403 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4404 {
4405 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4406 }
4407
4408 sid_len = in[59];
4409 if( sid_len > in_len - 61 )
4410 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4411
4412 cookie_len = in[60 + sid_len];
4413 if( cookie_len > in_len - 60 )
4414 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4415
4416 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4417 cli_id, cli_id_len ) == 0 )
4418 {
4419 /* Valid cookie */
4420 return( 0 );
4421 }
4422
4423 /*
4424 * If we get here, we've got an invalid cookie, let's prepare HVR.
4425 *
4426 * 0-0 ContentType type; copied
4427 * 1-2 ProtocolVersion version; copied
4428 * 3-4 uint16 epoch; copied
4429 * 5-10 uint48 sequence_number; copied
4430 * 11-12 uint16 length; olen - 13
4431 *
4432 * 13-13 HandshakeType msg_type; hello_verify_request
4433 * 14-16 uint24 length; olen - 25
4434 * 17-18 uint16 message_seq; copied
4435 * 19-21 uint24 fragment_offset; copied
4436 * 22-24 uint24 fragment_length; olen - 25
4437 *
4438 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4439 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4440 *
4441 * Minimum length is 28.
4442 */
4443 if( buf_len < 28 )
4444 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4445
4446 /* Copy most fields and adapt others */
4447 memcpy( obuf, in, 25 );
4448 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4449 obuf[25] = 0xfe;
4450 obuf[26] = 0xff;
4451
4452 /* Generate and write actual cookie */
4453 p = obuf + 28;
4454 if( f_cookie_write( p_cookie,
4455 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4456 {
4457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4458 }
4459
4460 *olen = p - obuf;
4461
4462 /* Go back and fill length fields */
4463 obuf[27] = (unsigned char)( *olen - 28 );
4464
4465 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4466 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4467 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4468
4469 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4470 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4471
4472 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4473}
4474
4475/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004476 * Handle possible client reconnect with the same UDP quadruplet
4477 * (RFC 6347 Section 4.2.8).
4478 *
4479 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4480 * that looks like a ClientHello.
4481 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004482 * - if the input looks like a ClientHello without cookies,
4483 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004484 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004485 * - if the input looks like a ClientHello with a valid cookie,
4486 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004487 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004488 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004489 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004490 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004491 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4492 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004493 */
4494static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4495{
4496 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004497 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004498
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004499 ret = ssl_check_dtls_clihlo_cookie(
4500 ssl->conf->f_cookie_write,
4501 ssl->conf->f_cookie_check,
4502 ssl->conf->p_cookie,
4503 ssl->cli_id, ssl->cli_id_len,
4504 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004505 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004506
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004507 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4508
4509 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004510 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004511 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004512 * If the error is permanent we'll catch it later,
4513 * if it's not, then hopefully it'll work next time. */
4514 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4515
4516 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517 }
4518
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004519 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004521 /* Got a valid cookie, partially reset context */
4522 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4523 {
4524 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4525 return( ret );
4526 }
4527
4528 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004529 }
4530
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531 return( ret );
4532}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004533#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004534
Hanno Becker46483f12019-05-03 13:25:54 +01004535static int ssl_check_record_type( uint8_t record_type )
4536{
4537 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4538 record_type != MBEDTLS_SSL_MSG_ALERT &&
4539 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4540 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4541 {
4542 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4543 }
4544
4545 return( 0 );
4546}
4547
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004548/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004549 * ContentType type;
4550 * ProtocolVersion version;
4551 * uint16 epoch; // DTLS only
4552 * uint48 sequence_number; // DTLS only
4553 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004554 *
4555 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004556 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004557 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4558 *
4559 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004560 * 1. proceed with the record if this function returns 0
4561 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4562 * 3. return CLIENT_RECONNECT if this function return that value
4563 * 4. drop the whole datagram if this function returns anything else.
4564 * Point 2 is needed when the peer is resending, and we have already received
4565 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004568{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004569 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004570 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004571
Hanno Becker8b09b732019-05-08 12:03:28 +01004572 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004573
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004575 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004577 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004578#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004579 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4580 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4581 ssl->conf->cid_len != 0 )
4582 {
4583 /* Shift pointers to account for record header including CID
4584 * struct {
4585 * ContentType special_type = tls12_cid;
4586 * ProtocolVersion version;
4587 * uint16 epoch;
4588 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004589 * opaque cid[cid_length]; // Additional field compared to
4590 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004591 * uint16 length;
4592 * opaque enc_content[DTLSCiphertext.length];
4593 * } DTLSCiphertext;
4594 */
4595
4596 /* So far, we only support static CID lengths
4597 * fixed in the configuration. */
4598 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4599 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4600 }
4601 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004602#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004603 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004606
4607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004608 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4609 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004610 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4611#endif /* MBEDTLS_SSL_PROTO_DTLS */
4612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4613 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004616 }
4617
4618 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004619 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4622 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 }
4624
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004625 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4628 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 }
4630
Hanno Becker8b09b732019-05-08 12:03:28 +01004631 /* Now that the total length of the record header is known, ensure
4632 * that the current datagram is large enough to hold it.
4633 * This would fail, for example, if we received a datagram of
4634 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4635 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4636 if( ret != 0 )
4637 {
4638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4639 return( ret );
4640 }
4641 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4642
4643 /* Parse and validate record length
4644 * This must happen after the CID parsing because
4645 * its position in the record header depends on
4646 * the presence of a CID. */
4647
4648 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004649 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004650 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4653 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004654 }
4655
Hanno Becker8b09b732019-05-08 12:03:28 +01004656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004657 "version = [%d:%d], msglen = %d",
4658 ssl->in_msgtype,
4659 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004660
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004661 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004662 * DTLS-related tests.
4663 * Check epoch before checking length constraint because
4664 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4665 * message gets duplicated before the corresponding Finished message,
4666 * the second ChangeCipherSpec should be discarded because it belongs
4667 * to an old epoch, but not because its length is shorter than
4668 * the minimum record length for packets using the new record transform.
4669 * Note that these two kinds of failures are handled differently,
4670 * as an unexpected record is silently skipped but an invalid
4671 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004672 */
4673#if defined(MBEDTLS_SSL_PROTO_DTLS)
4674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4675 {
4676 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4677
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004678 /* Check epoch (and sequence number) with DTLS */
4679 if( rec_epoch != ssl->in_epoch )
4680 {
4681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4682 "expected %d, received %d",
4683 ssl->in_epoch, rec_epoch ) );
4684
4685#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4686 /*
4687 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4688 * access the first byte of record content (handshake type), as we
4689 * have an active transform (possibly iv_len != 0), so use the
4690 * fact that the record header len is 13 instead.
4691 */
4692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4693 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4694 rec_epoch == 0 &&
4695 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4696 ssl->in_left > 13 &&
4697 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4698 {
4699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4700 "from the same port" ) );
4701 return( ssl_handle_possible_reconnect( ssl ) );
4702 }
4703 else
4704#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004705 {
4706 /* Consider buffering the record. */
4707 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4710 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4711 }
4712
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004713 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004714 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004715 }
4716
4717#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4718 /* Replay detection only works for the current epoch */
4719 if( rec_epoch == ssl->in_epoch &&
4720 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4723 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4724 }
4725#endif
4726 }
4727#endif /* MBEDTLS_SSL_PROTO_DTLS */
4728
Hanno Becker52c6dc62017-05-26 16:07:36 +01004729
4730 /* Check length against bounds of the current transform and version */
4731 if( ssl->transform_in == NULL )
4732 {
4733 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004734 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004735 {
4736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4737 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4738 }
4739 }
4740 else
4741 {
4742 if( ssl->in_msglen < ssl->transform_in->minlen )
4743 {
4744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4745 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4746 }
4747
4748#if defined(MBEDTLS_SSL_PROTO_SSL3)
4749 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004750 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004751 {
4752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4753 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4754 }
4755#endif
4756#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4757 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4758 /*
4759 * TLS encrypted messages can have up to 256 bytes of padding
4760 */
4761 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4762 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004763 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764 {
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4767 }
4768#endif
4769 }
4770
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004771 return( 0 );
4772}
Paul Bakker5121ce52009-01-03 21:22:43 +00004773
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004774/*
4775 * If applicable, decrypt (and decompress) record content
4776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004778{
4779 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004782 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4785 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 ret = mbedtls_ssl_hw_record_read( ssl );
4790 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4793 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004794 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004795
4796 if( ret == 0 )
4797 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004800 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004801 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004802 mbedtls_record rec;
4803
4804 rec.buf = ssl->in_iv;
4805 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4806 - ( ssl->in_iv - ssl->in_buf );
4807 rec.data_len = ssl->in_msglen;
4808 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004809#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004810 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004811 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004812#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004813
4814 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4815 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4816 ssl->conf->transport, rec.ver );
4817 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004818 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4819 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004822
Hanno Beckera5a2b082019-05-15 14:03:01 +01004823#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004824 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4825 ssl->conf->ignore_unexpected_cid
4826 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4827 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004829 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004830 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004831#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004832
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 return( ret );
4834 }
4835
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004836 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004837 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004838 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4839 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004841
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004842 /* The record content type may change during decryption,
4843 * so re-read it. */
4844 ssl->in_msgtype = rec.type;
4845 /* Also update the input buffer, because unfortunately
4846 * the server-side ssl_parse_client_hello() reparses the
4847 * record header when receiving a ClientHello initiating
4848 * a renegotiation. */
4849 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004850 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004851 ssl->in_msglen = rec.data_len;
4852 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4853 ssl->in_len[1] = (unsigned char)( rec.data_len );
4854
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4856 ssl->in_msg, ssl->in_msglen );
4857
Hanno Beckera5a2b082019-05-15 14:03:01 +01004858#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004859 /* We have already checked the record content type
4860 * in ssl_parse_record_header(), failing or silently
4861 * dropping the record in the case of an unknown type.
4862 *
4863 * Since with the use of CIDs, the record content type
4864 * might change during decryption, re-check the record
4865 * content type, but treat a failure as fatal this time. */
4866 if( ssl_check_record_type( ssl->in_msgtype ) )
4867 {
4868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4870 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004871#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004872
Angus Grattond8213d02016-05-25 20:56:48 +10004873 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4876 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004877 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004878 else if( ssl->in_msglen == 0 )
4879 {
4880#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4881 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4882 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4883 {
4884 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4886 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4887 }
4888#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4889
4890 ssl->nb_zero++;
4891
4892 /*
4893 * Three or more empty messages may be a DoS attack
4894 * (excessive CPU consumption).
4895 */
4896 if( ssl->nb_zero > 3 )
4897 {
4898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004899 "messages, possible DoS attack" ) );
4900 /* Treat the records as if they were not properly authenticated,
4901 * thereby failing the connection if we see more than allowed
4902 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004903 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4904 }
4905 }
4906 else
4907 ssl->nb_zero = 0;
4908
4909#if defined(MBEDTLS_SSL_PROTO_DTLS)
4910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4911 {
4912 ; /* in_ctr read from peer, not maintained internally */
4913 }
4914 else
4915#endif
4916 {
4917 unsigned i;
4918 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4919 if( ++ssl->in_ctr[i - 1] != 0 )
4920 break;
4921
4922 /* The loop goes to its end iff the counter is wrapping */
4923 if( i == ssl_ep_len( ssl ) )
4924 {
4925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4926 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4927 }
4928 }
4929
Paul Bakker5121ce52009-01-03 21:22:43 +00004930 }
4931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004933 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004935 {
4936 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004939 return( ret );
4940 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004945 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004948 }
4949#endif
4950
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004951 return( 0 );
4952}
4953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004955
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004956/*
4957 * Read a record.
4958 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004959 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4960 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4961 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004962 */
Hanno Becker1097b342018-08-15 14:09:41 +01004963
4964/* Helper functions for mbedtls_ssl_read_record(). */
4965static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004966static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4967static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004968
Hanno Becker327c93b2018-08-15 13:56:18 +01004969int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004970 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004971{
4972 int ret;
4973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004975
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004976 if( ssl->keep_current_message == 0 )
4977 {
4978 do {
Simon Butcher99000142016-10-13 17:21:01 +01004979
Hanno Becker26994592018-08-15 14:14:59 +01004980 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004981 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004982 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004983
Hanno Beckere74d5562018-08-15 14:26:08 +01004984 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004985 {
Hanno Becker40f50842018-08-15 14:48:01 +01004986#if defined(MBEDTLS_SSL_PROTO_DTLS)
4987 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004988
Hanno Becker40f50842018-08-15 14:48:01 +01004989 /* We only check for buffered messages if the
4990 * current datagram is fully consumed. */
4991 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004992 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004993 {
Hanno Becker40f50842018-08-15 14:48:01 +01004994 if( ssl_load_buffered_message( ssl ) == 0 )
4995 have_buffered = 1;
4996 }
4997
4998 if( have_buffered == 0 )
4999#endif /* MBEDTLS_SSL_PROTO_DTLS */
5000 {
5001 ret = ssl_get_next_record( ssl );
5002 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5003 continue;
5004
5005 if( ret != 0 )
5006 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005007 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005008 return( ret );
5009 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005010 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005011 }
5012
5013 ret = mbedtls_ssl_handle_message_type( ssl );
5014
Hanno Becker40f50842018-08-15 14:48:01 +01005015#if defined(MBEDTLS_SSL_PROTO_DTLS)
5016 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5017 {
5018 /* Buffer future message */
5019 ret = ssl_buffer_message( ssl );
5020 if( ret != 0 )
5021 return( ret );
5022
5023 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5024 }
5025#endif /* MBEDTLS_SSL_PROTO_DTLS */
5026
Hanno Becker90333da2017-10-10 11:27:13 +01005027 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5028 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005029
5030 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005031 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005032 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005033 return( ret );
5034 }
5035
Hanno Becker327c93b2018-08-15 13:56:18 +01005036 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005037 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005038 {
5039 mbedtls_ssl_update_handshake_status( ssl );
5040 }
Simon Butcher99000142016-10-13 17:21:01 +01005041 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005042 else
Simon Butcher99000142016-10-13 17:21:01 +01005043 {
Hanno Becker02f59072018-08-15 14:00:24 +01005044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005045 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005046 }
5047
5048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5049
5050 return( 0 );
5051}
5052
Hanno Becker40f50842018-08-15 14:48:01 +01005053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005054static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005055{
Hanno Becker40f50842018-08-15 14:48:01 +01005056 if( ssl->in_left > ssl->next_record_offset )
5057 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005058
Hanno Becker40f50842018-08-15 14:48:01 +01005059 return( 0 );
5060}
5061
5062static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5063{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005064 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005065 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005066 int ret = 0;
5067
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005068 if( hs == NULL )
5069 return( -1 );
5070
Hanno Beckere00ae372018-08-20 09:39:42 +01005071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5072
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005073 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5074 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5075 {
5076 /* Check if we have seen a ChangeCipherSpec before.
5077 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005078 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005079 {
5080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5081 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005082 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005083 }
5084
Hanno Becker39b8bc92018-08-28 17:17:13 +01005085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005086 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5087 ssl->in_msglen = 1;
5088 ssl->in_msg[0] = 1;
5089
5090 /* As long as they are equal, the exact value doesn't matter. */
5091 ssl->in_left = 0;
5092 ssl->next_record_offset = 0;
5093
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005094 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095 goto exit;
5096 }
Hanno Becker37f95322018-08-16 13:55:32 +01005097
Hanno Beckerb8f50142018-08-28 10:01:34 +01005098#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005099 /* Debug only */
5100 {
5101 unsigned offset;
5102 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5103 {
5104 hs_buf = &hs->buffering.hs[offset];
5105 if( hs_buf->is_valid == 1 )
5106 {
5107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5108 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005109 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005110 }
5111 }
5112 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005113#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005114
5115 /* Check if we have buffered and/or fully reassembled the
5116 * next handshake message. */
5117 hs_buf = &hs->buffering.hs[0];
5118 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5119 {
5120 /* Synthesize a record containing the buffered HS message. */
5121 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5122 ( hs_buf->data[2] << 8 ) |
5123 hs_buf->data[3];
5124
5125 /* Double-check that we haven't accidentally buffered
5126 * a message that doesn't fit into the input buffer. */
5127 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5128 {
5129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5131 }
5132
5133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5134 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5135 hs_buf->data, msg_len + 12 );
5136
5137 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5138 ssl->in_hslen = msg_len + 12;
5139 ssl->in_msglen = msg_len + 12;
5140 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5141
5142 ret = 0;
5143 goto exit;
5144 }
5145 else
5146 {
5147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5148 hs->in_msg_seq ) );
5149 }
5150
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005151 ret = -1;
5152
5153exit:
5154
5155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5156 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005157}
5158
Hanno Beckera02b0b42018-08-21 17:20:27 +01005159static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5160 size_t desired )
5161{
5162 int offset;
5163 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5165 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005166
Hanno Becker01315ea2018-08-21 17:22:17 +01005167 /* Get rid of future records epoch first, if such exist. */
5168 ssl_free_buffered_record( ssl );
5169
5170 /* Check if we have enough space available now. */
5171 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5172 hs->buffering.total_bytes_buffered ) )
5173 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005175 return( 0 );
5176 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005177
Hanno Becker4f432ad2018-08-28 10:02:32 +01005178 /* We don't have enough space to buffer the next expected handshake
5179 * message. Remove buffers used for future messages to gain space,
5180 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005181 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5182 offset >= 0; offset-- )
5183 {
5184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5185 offset ) );
5186
Hanno Beckerb309b922018-08-23 13:18:05 +01005187 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005188
5189 /* Check if we have enough space available now. */
5190 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5191 hs->buffering.total_bytes_buffered ) )
5192 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005194 return( 0 );
5195 }
5196 }
5197
5198 return( -1 );
5199}
5200
Hanno Becker40f50842018-08-15 14:48:01 +01005201static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5202{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005203 int ret = 0;
5204 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5205
5206 if( hs == NULL )
5207 return( 0 );
5208
5209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5210
5211 switch( ssl->in_msgtype )
5212 {
5213 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005215
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005216 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005217 break;
5218
5219 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005220 {
5221 unsigned recv_msg_seq_offset;
5222 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5223 mbedtls_ssl_hs_buffer *hs_buf;
5224 size_t msg_len = ssl->in_hslen - 12;
5225
5226 /* We should never receive an old handshake
5227 * message - double-check nonetheless. */
5228 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5229 {
5230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5232 }
5233
5234 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5235 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5236 {
5237 /* Silently ignore -- message too far in the future */
5238 MBEDTLS_SSL_DEBUG_MSG( 2,
5239 ( "Ignore future HS message with sequence number %u, "
5240 "buffering window %u - %u",
5241 recv_msg_seq, ssl->handshake->in_msg_seq,
5242 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5243
5244 goto exit;
5245 }
5246
5247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5248 recv_msg_seq, recv_msg_seq_offset ) );
5249
5250 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5251
5252 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005253 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005254 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005255 size_t reassembly_buf_sz;
5256
Hanno Becker37f95322018-08-16 13:55:32 +01005257 hs_buf->is_fragmented =
5258 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5259
5260 /* We copy the message back into the input buffer
5261 * after reassembly, so check that it's not too large.
5262 * This is an implementation-specific limitation
5263 * and not one from the standard, hence it is not
5264 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005265 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005266 {
5267 /* Ignore message */
5268 goto exit;
5269 }
5270
Hanno Beckere0b150f2018-08-21 15:51:03 +01005271 /* Check if we have enough space to buffer the message. */
5272 if( hs->buffering.total_bytes_buffered >
5273 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5274 {
5275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5277 }
5278
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005279 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5280 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005281
5282 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5283 hs->buffering.total_bytes_buffered ) )
5284 {
5285 if( recv_msg_seq_offset > 0 )
5286 {
5287 /* If we can't buffer a future message because
5288 * of space limitations -- ignore. */
5289 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",
5290 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5291 (unsigned) hs->buffering.total_bytes_buffered ) );
5292 goto exit;
5293 }
Hanno Beckere1801392018-08-21 16:51:05 +01005294 else
5295 {
5296 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",
5297 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5298 (unsigned) hs->buffering.total_bytes_buffered ) );
5299 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005300
Hanno Beckera02b0b42018-08-21 17:20:27 +01005301 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005302 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005303 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",
5304 (unsigned) msg_len,
5305 (unsigned) reassembly_buf_sz,
5306 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005307 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005308 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5309 goto exit;
5310 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005311 }
5312
5313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5314 msg_len ) );
5315
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005316 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5317 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005318 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005319 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005320 goto exit;
5321 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005322 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005323
5324 /* Prepare final header: copy msg_type, length and message_seq,
5325 * then add standardised fragment_offset and fragment_length */
5326 memcpy( hs_buf->data, ssl->in_msg, 6 );
5327 memset( hs_buf->data + 6, 0, 3 );
5328 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5329
5330 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005331
5332 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005333 }
5334 else
5335 {
5336 /* Make sure msg_type and length are consistent */
5337 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5338 {
5339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5340 /* Ignore */
5341 goto exit;
5342 }
5343 }
5344
Hanno Becker4422bbb2018-08-20 09:40:19 +01005345 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005346 {
5347 size_t frag_len, frag_off;
5348 unsigned char * const msg = hs_buf->data + 12;
5349
5350 /*
5351 * Check and copy current fragment
5352 */
5353
5354 /* Validation of header fields already done in
5355 * mbedtls_ssl_prepare_handshake_record(). */
5356 frag_off = ssl_get_hs_frag_off( ssl );
5357 frag_len = ssl_get_hs_frag_len( ssl );
5358
5359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5360 frag_off, frag_len ) );
5361 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5362
5363 if( hs_buf->is_fragmented )
5364 {
5365 unsigned char * const bitmask = msg + msg_len;
5366 ssl_bitmask_set( bitmask, frag_off, frag_len );
5367 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5368 msg_len ) == 0 );
5369 }
5370 else
5371 {
5372 hs_buf->is_complete = 1;
5373 }
5374
5375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5376 hs_buf->is_complete ? "" : "not yet " ) );
5377 }
5378
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005379 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005380 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005381
5382 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005383 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005384 break;
5385 }
5386
5387exit:
5388
5389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5390 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005391}
5392#endif /* MBEDTLS_SSL_PROTO_DTLS */
5393
Hanno Becker1097b342018-08-15 14:09:41 +01005394static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005395{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005396 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005397 * Consume last content-layer message and potentially
5398 * update in_msglen which keeps track of the contents'
5399 * consumption state.
5400 *
5401 * (1) Handshake messages:
5402 * Remove last handshake message, move content
5403 * and adapt in_msglen.
5404 *
5405 * (2) Alert messages:
5406 * Consume whole record content, in_msglen = 0.
5407 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005408 * (3) Change cipher spec:
5409 * Consume whole record content, in_msglen = 0.
5410 *
5411 * (4) Application data:
5412 * Don't do anything - the record layer provides
5413 * the application data as a stream transport
5414 * and consumes through mbedtls_ssl_read only.
5415 *
5416 */
5417
5418 /* Case (1): Handshake messages */
5419 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005420 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005421 /* Hard assertion to be sure that no application data
5422 * is in flight, as corrupting ssl->in_msglen during
5423 * ssl->in_offt != NULL is fatal. */
5424 if( ssl->in_offt != NULL )
5425 {
5426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5428 }
5429
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005430 /*
5431 * Get next Handshake message in the current record
5432 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005433
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005435 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005436 * current handshake content: If DTLS handshake
5437 * fragmentation is used, that's the fragment
5438 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005439 * size here is faulty and should be changed at
5440 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005441 * (2) While it doesn't seem to cause problems, one
5442 * has to be very careful not to assume that in_hslen
5443 * is always <= in_msglen in a sensible communication.
5444 * Again, it's wrong for DTLS handshake fragmentation.
5445 * The following check is therefore mandatory, and
5446 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005447 * Additionally, ssl->in_hslen might be arbitrarily out of
5448 * bounds after handling a DTLS message with an unexpected
5449 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005450 */
5451 if( ssl->in_hslen < ssl->in_msglen )
5452 {
5453 ssl->in_msglen -= ssl->in_hslen;
5454 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5455 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005456
Hanno Becker4a810fb2017-05-24 16:27:30 +01005457 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5458 ssl->in_msg, ssl->in_msglen );
5459 }
5460 else
5461 {
5462 ssl->in_msglen = 0;
5463 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005464
Hanno Becker4a810fb2017-05-24 16:27:30 +01005465 ssl->in_hslen = 0;
5466 }
5467 /* Case (4): Application data */
5468 else if( ssl->in_offt != NULL )
5469 {
5470 return( 0 );
5471 }
5472 /* Everything else (CCS & Alerts) */
5473 else
5474 {
5475 ssl->in_msglen = 0;
5476 }
5477
Hanno Becker1097b342018-08-15 14:09:41 +01005478 return( 0 );
5479}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005480
Hanno Beckere74d5562018-08-15 14:26:08 +01005481static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5482{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005483 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005484 return( 1 );
5485
5486 return( 0 );
5487}
5488
Hanno Becker5f066e72018-08-16 14:56:31 +01005489#if defined(MBEDTLS_SSL_PROTO_DTLS)
5490
5491static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5492{
5493 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5494 if( hs == NULL )
5495 return;
5496
Hanno Becker01315ea2018-08-21 17:22:17 +01005497 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005498 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005499 hs->buffering.total_bytes_buffered -=
5500 hs->buffering.future_record.len;
5501
5502 mbedtls_free( hs->buffering.future_record.data );
5503 hs->buffering.future_record.data = NULL;
5504 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005505}
5506
5507static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5508{
5509 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5510 unsigned char * rec;
5511 size_t rec_len;
5512 unsigned rec_epoch;
5513
5514 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5515 return( 0 );
5516
5517 if( hs == NULL )
5518 return( 0 );
5519
Hanno Becker5f066e72018-08-16 14:56:31 +01005520 rec = hs->buffering.future_record.data;
5521 rec_len = hs->buffering.future_record.len;
5522 rec_epoch = hs->buffering.future_record.epoch;
5523
5524 if( rec == NULL )
5525 return( 0 );
5526
Hanno Becker4cb782d2018-08-20 11:19:05 +01005527 /* Only consider loading future records if the
5528 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005529 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005530 return( 0 );
5531
Hanno Becker5f066e72018-08-16 14:56:31 +01005532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5533
5534 if( rec_epoch != ssl->in_epoch )
5535 {
5536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5537 goto exit;
5538 }
5539
5540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5541
5542 /* Double-check that the record is not too large */
5543 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5544 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5548 }
5549
5550 memcpy( ssl->in_hdr, rec, rec_len );
5551 ssl->in_left = rec_len;
5552 ssl->next_record_offset = 0;
5553
5554 ssl_free_buffered_record( ssl );
5555
5556exit:
5557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5558 return( 0 );
5559}
5560
5561static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5562{
5563 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5564 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005565 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005566
5567 /* Don't buffer future records outside handshakes. */
5568 if( hs == NULL )
5569 return( 0 );
5570
5571 /* Only buffer handshake records (we are only interested
5572 * in Finished messages). */
5573 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5574 return( 0 );
5575
5576 /* Don't buffer more than one future epoch record. */
5577 if( hs->buffering.future_record.data != NULL )
5578 return( 0 );
5579
Hanno Becker01315ea2018-08-21 17:22:17 +01005580 /* Don't buffer record if there's not enough buffering space remaining. */
5581 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5582 hs->buffering.total_bytes_buffered ) )
5583 {
5584 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",
5585 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5586 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005587 return( 0 );
5588 }
5589
Hanno Becker5f066e72018-08-16 14:56:31 +01005590 /* Buffer record */
5591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5592 ssl->in_epoch + 1 ) );
5593 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5594 rec_hdr_len + ssl->in_msglen );
5595
5596 /* ssl_parse_record_header() only considers records
5597 * of the next epoch as candidates for buffering. */
5598 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005599 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005600
5601 hs->buffering.future_record.data =
5602 mbedtls_calloc( 1, hs->buffering.future_record.len );
5603 if( hs->buffering.future_record.data == NULL )
5604 {
5605 /* If we run out of RAM trying to buffer a
5606 * record from the next epoch, just ignore. */
5607 return( 0 );
5608 }
5609
Hanno Becker01315ea2018-08-21 17:22:17 +01005610 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005611
Hanno Becker01315ea2018-08-21 17:22:17 +01005612 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005613 return( 0 );
5614}
5615
5616#endif /* MBEDTLS_SSL_PROTO_DTLS */
5617
Hanno Beckere74d5562018-08-15 14:26:08 +01005618static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005619{
5620 int ret;
5621
Hanno Becker5f066e72018-08-16 14:56:31 +01005622#if defined(MBEDTLS_SSL_PROTO_DTLS)
5623 /* We might have buffered a future record; if so,
5624 * and if the epoch matches now, load it.
5625 * On success, this call will set ssl->in_left to
5626 * the length of the buffered record, so that
5627 * the calls to ssl_fetch_input() below will
5628 * essentially be no-ops. */
5629 ret = ssl_load_buffered_record( ssl );
5630 if( ret != 0 )
5631 return( ret );
5632#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005633
Hanno Becker8b09b732019-05-08 12:03:28 +01005634 /* Reset in pointers to default state for TLS/DTLS records,
5635 * assuming no CID and no offset between record content and
5636 * record plaintext. */
5637 ssl_update_in_pointers( ssl );
5638
5639 /* Ensure that we have enough space available for the default form
5640 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5641 * with no space for CIDs counted in). */
5642 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5643 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005646 return( ret );
5647 }
5648
5649 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005652 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5653 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005654 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005655 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5656 {
5657 ret = ssl_buffer_future_record( ssl );
5658 if( ret != 0 )
5659 return( ret );
5660
5661 /* Fall through to handling of unexpected records */
5662 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5663 }
5664
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005665 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5666 {
5667 /* Skip unexpected record (but not whole datagram) */
5668 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005669 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005670
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5672 "(header)" ) );
5673 }
5674 else
5675 {
5676 /* Skip invalid record and the rest of the datagram */
5677 ssl->next_record_offset = 0;
5678 ssl->in_left = 0;
5679
5680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5681 "(header)" ) );
5682 }
5683
5684 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005685 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005686 }
5687#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005688 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005689 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005690
5691 /*
5692 * Read and optionally decrypt the message contents
5693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005695 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005698 return( ret );
5699 }
5700
5701 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005703 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005704 {
Hanno Becker43395762019-05-03 14:46:38 +01005705 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005706 if( ssl->next_record_offset < ssl->in_left )
5707 {
5708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5709 }
5710 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005711 else
5712#endif
5713 ssl->in_left = 0;
5714
5715 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005718 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005719 {
5720 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005721 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005722 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005723 /* Except when waiting for Finished as a bad mac here
5724 * probably means something went wrong in the handshake
5725 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5726 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5727 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5728 {
5729#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5730 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5731 {
5732 mbedtls_ssl_send_alert_message( ssl,
5733 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5734 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5735 }
5736#endif
5737 return( ret );
5738 }
5739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005741 if( ssl->conf->badmac_limit != 0 &&
5742 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5745 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005746 }
5747#endif
5748
Hanno Becker4a810fb2017-05-24 16:27:30 +01005749 /* As above, invalid records cause
5750 * dismissal of the whole datagram. */
5751
5752 ssl->next_record_offset = 0;
5753 ssl->in_left = 0;
5754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005756 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005757 }
5758
5759 return( ret );
5760 }
5761 else
5762#endif
5763 {
5764 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5766 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 mbedtls_ssl_send_alert_message( ssl,
5769 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5770 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005771 }
5772#endif
5773 return( ret );
5774 }
5775 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005776
Simon Butcher99000142016-10-13 17:21:01 +01005777 return( 0 );
5778}
5779
5780int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5781{
5782 int ret;
5783
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005784 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005785 * Handle particular types of records
5786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005788 {
Simon Butcher99000142016-10-13 17:21:01 +01005789 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5790 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005791 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005793 }
5794
Hanno Beckere678eaa2018-08-21 14:57:46 +01005795 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005796 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005797 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005798 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5800 ssl->in_msglen ) );
5801 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005802 }
5803
Hanno Beckere678eaa2018-08-21 14:57:46 +01005804 if( ssl->in_msg[0] != 1 )
5805 {
5806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5807 ssl->in_msg[0] ) );
5808 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5809 }
5810
5811#if defined(MBEDTLS_SSL_PROTO_DTLS)
5812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5813 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5814 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5815 {
5816 if( ssl->handshake == NULL )
5817 {
5818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5819 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5820 }
5821
5822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5823 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5824 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005825#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005826 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005830 if( ssl->in_msglen != 2 )
5831 {
5832 /* Note: Standard allows for more than one 2 byte alert
5833 to be packed in a single message, but Mbed TLS doesn't
5834 currently support this. */
5835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5836 ssl->in_msglen ) );
5837 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5838 }
5839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005841 ssl->in_msg[0], ssl->in_msg[1] ) );
5842
5843 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005844 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005849 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005851 }
5852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5854 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5857 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005859
5860#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5861 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5862 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5863 {
Hanno Becker90333da2017-10-10 11:27:13 +01005864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005865 /* Will be handled when trying to parse ServerHello */
5866 return( 0 );
5867 }
5868#endif
5869
5870#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5871 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5872 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5873 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5874 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5875 {
5876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5877 /* Will be handled in mbedtls_ssl_parse_certificate() */
5878 return( 0 );
5879 }
5880#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5881
5882 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005883 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 }
5885
Hanno Beckerc76c6192017-06-06 10:03:17 +01005886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005887 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005888 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005889 /* Drop unexpected ApplicationData records,
5890 * except at the beginning of renegotiations */
5891 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5892 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5893#if defined(MBEDTLS_SSL_RENEGOTIATION)
5894 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5895 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005896#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005897 )
5898 {
5899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5900 return( MBEDTLS_ERR_SSL_NON_FATAL );
5901 }
5902
5903 if( ssl->handshake != NULL &&
5904 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5905 {
5906 ssl_handshake_wrapup_free_hs_transform( ssl );
5907 }
5908 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005910
Paul Bakker5121ce52009-01-03 21:22:43 +00005911 return( 0 );
5912}
5913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005915{
5916 int ret;
5917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5919 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5920 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005921 {
5922 return( ret );
5923 }
5924
5925 return( 0 );
5926}
5927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005929 unsigned char level,
5930 unsigned char message )
5931{
5932 int ret;
5933
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005934 if( ssl == NULL || ssl->conf == NULL )
5935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005941 ssl->out_msglen = 2;
5942 ssl->out_msg[0] = level;
5943 ssl->out_msg[1] = message;
5944
Hanno Becker67bc7c32018-08-06 11:33:50 +01005945 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005948 return( ret );
5949 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005951
5952 return( 0 );
5953}
5954
Paul Bakker5121ce52009-01-03 21:22:43 +00005955/*
5956 * Handshake functions
5957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5959 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5960 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5961 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5962 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5963 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5964 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005965/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005967{
Hanno Becker8759e162017-12-27 21:34:08 +00005968 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5973 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005974 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5975 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005978 ssl->state++;
5979 return( 0 );
5980 }
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005984}
5985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005987{
Hanno Becker8759e162017-12-27 21:34:08 +00005988 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5993 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005994 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5995 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005998 ssl->state++;
5999 return( 0 );
6000 }
6001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6003 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006004}
Gilles Peskinef9828522017-05-03 12:28:43 +02006005
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006006#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006007/* Some certificate support -> implement write and parse */
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006012 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006014 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6019 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006020 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6021 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006024 ssl->state++;
6025 return( 0 );
6026 }
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006029 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 {
6031 if( ssl->client_auth == 0 )
6032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006034 ssl->state++;
6035 return( 0 );
6036 }
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 /*
6040 * If using SSLv3 and got no cert, send an Alert message
6041 * (otherwise an empty Certificate message will be sent).
6042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6044 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 {
6046 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6048 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6049 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 goto write_msg;
6053 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#endif /* MBEDTLS_SSL_CLI_C */
6057#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006058 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6063 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 }
6065 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006069
6070 /*
6071 * 0 . 0 handshake type
6072 * 1 . 3 handshake length
6073 * 4 . 6 length of all certs
6074 * 7 . 9 length of cert. 1
6075 * 10 . n-1 peer certificate
6076 * n . n+2 length of cert. 2
6077 * n+3 . ... upper level cert, etc.
6078 */
6079 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Paul Bakker29087132010-03-21 21:03:34 +00006082 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006083 {
6084 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006085 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006088 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 }
6091
6092 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6093 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6094 ssl->out_msg[i + 2] = (unsigned char)( n );
6095
6096 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6097 i += n; crt = crt->next;
6098 }
6099
6100 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6101 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6102 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6103
6104 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6106 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006107
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006108#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006109write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006111
6112 ssl->state++;
6113
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006114 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 return( ret );
6118 }
6119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Paul Bakkered27a042013-04-18 22:46:23 +02006122 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123}
6124
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006125/*
6126 * Once the certificate message is read, parse it into a cert chain and
6127 * perform basic checks, but leave actual verification to the caller
6128 */
6129static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006131 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006132 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006133 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135#if defined(MBEDTLS_SSL_SRV_C)
6136#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 /*
6138 * Check if the client sent an empty certificate
6139 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006140 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006142 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006143 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6145 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6146 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006149
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006150 /* The client was asked for a certificate but didn't send
6151 one. The client should know what's going on, so we
6152 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006153 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006154 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 }
6156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6160 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006161 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6165 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6166 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6167 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006171 /* The client was asked for a certificate but didn't send
6172 one. The client should know what's going on, so we
6173 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006174 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006175 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006176 }
6177 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6179 MBEDTLS_SSL_PROTO_TLS1_2 */
6180#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6186 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006188 }
6189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6191 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006194 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6195 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006197 }
6198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006200
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006204 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006205
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006206 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006210 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6211 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006213 }
6214
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006215 /* In case we tried to reuse a session but it failed */
6216 if( ssl->session_negotiate->peer_cert != NULL )
6217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6219 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006220 }
6221
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006222 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006227 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6228 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006229 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 }
6231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006234 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006235
6236 while( i < ssl->in_hslen )
6237 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006238 if ( i + 3 > ssl->in_hslen ) {
6239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6241 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6242 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 if( ssl->in_msg[i] != 0 )
6245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006247 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 }
6251
6252 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6253 | (unsigned int) ssl->in_msg[i + 2];
6254 i += 3;
6255
6256 if( n < 128 || i + n > ssl->in_hslen )
6257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006259 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6260 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 }
6263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006265 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006266 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006267 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006268 case 0: /*ok*/
6269 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6270 /* Ignore certificate with an unknown algorithm: maybe a
6271 prior certificate was already trusted. */
6272 break;
6273
6274 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6275 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6276 goto crt_parse_der_failed;
6277
6278 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6279 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6280 goto crt_parse_der_failed;
6281
6282 default:
6283 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6284 crt_parse_der_failed:
6285 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 return( ret );
6288 }
6289
6290 i += n;
6291 }
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006294
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006295 /*
6296 * On client, make sure the server cert doesn't change during renego to
6297 * avoid "triple handshake" attack: https://secure-resumption.com/
6298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006300 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006302 {
6303 if( ssl->session->peer_cert == NULL )
6304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006306 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6307 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006309 }
6310
6311 if( ssl->session->peer_cert->raw.len !=
6312 ssl->session_negotiate->peer_cert->raw.len ||
6313 memcmp( ssl->session->peer_cert->raw.p,
6314 ssl->session_negotiate->peer_cert->raw.p,
6315 ssl->session->peer_cert->raw.len ) != 0 )
6316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006321 }
6322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006324
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006325 return( 0 );
6326}
6327
6328int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6329{
6330 int ret;
6331 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006332 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006333#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6334 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6335 ? ssl->handshake->sni_authmode
6336 : ssl->conf->authmode;
6337#else
6338 const int authmode = ssl->conf->authmode;
6339#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006340 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006341
6342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6343
6344 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6346 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6347 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6348 {
6349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6350 ssl->state++;
6351 return( 0 );
6352 }
6353
6354#if defined(MBEDTLS_SSL_SRV_C)
6355 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6356 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6357 {
6358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6359 ssl->state++;
6360 return( 0 );
6361 }
6362
6363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6364 authmode == MBEDTLS_SSL_VERIFY_NONE )
6365 {
6366 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006368
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006369 ssl->state++;
6370 return( 0 );
6371 }
6372#endif
6373
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006374#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6375 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006376 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006377 {
6378 goto crt_verify;
6379 }
6380#endif
6381
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006382 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006383 {
6384 /* mbedtls_ssl_read_record may have sent an alert already. We
6385 let it decide whether to alert. */
6386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6387 return( ret );
6388 }
6389
6390 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6391 {
6392#if defined(MBEDTLS_SSL_SRV_C)
6393 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6394 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6395 {
6396 ret = 0;
6397 }
6398#endif
6399
6400 ssl->state++;
6401 return( ret );
6402 }
6403
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006404#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6405 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006406 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006407
6408crt_verify:
6409 if( ssl->handshake->ecrs_enabled)
6410 rs_ctx = &ssl->handshake->ecrs_ctx;
6411#endif
6412
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006413 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006415 mbedtls_x509_crt *ca_chain;
6416 mbedtls_x509_crl *ca_crl;
6417
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006419 if( ssl->handshake->sni_ca_chain != NULL )
6420 {
6421 ca_chain = ssl->handshake->sni_ca_chain;
6422 ca_crl = ssl->handshake->sni_ca_crl;
6423 }
6424 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006425#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006426 {
6427 ca_chain = ssl->conf->ca_chain;
6428 ca_crl = ssl->conf->ca_crl;
6429 }
6430
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006431 /*
6432 * Main check: verify certificate
6433 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006434 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006435 ssl->session_negotiate->peer_cert,
6436 ca_chain, ca_crl,
6437 ssl->conf->cert_profile,
6438 ssl->hostname,
6439 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006440 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006441
6442 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006445 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006446
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006447#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6448 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006449 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006450#endif
6451
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006452 /*
6453 * Secondary checks: always done, but change 'ret' only if it was 0
6454 */
6455
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006456#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006459
6460 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006462 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006463 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006464 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006467 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006469 }
6470 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006471#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006474 ciphersuite_info,
6475 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006476 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006479 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006481 }
6482
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006483 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6484 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6485 * with details encoded in the verification flags. All other kinds
6486 * of error codes, including those from the user provided f_vrfy
6487 * functions, are treated as fatal and lead to a failure of
6488 * ssl_parse_certificate even if verification was optional. */
6489 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6490 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6491 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6492 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006494 }
6495
6496 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6497 {
6498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6499 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6500 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006501
6502 if( ret != 0 )
6503 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006504 uint8_t alert;
6505
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006506 /* The certificate may have been rejected for several reasons.
6507 Pick one and send the corresponding alert. Which alert to send
6508 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006509 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6510 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6511 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6512 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6513 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6514 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6515 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6516 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6517 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6519 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6520 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6522 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6524 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6526 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6528 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006529 else
6530 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006531 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6532 alert );
6533 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006534
Hanno Beckere6706e62017-05-15 16:05:15 +01006535#if defined(MBEDTLS_DEBUG_C)
6536 if( ssl->session_negotiate->verify_result != 0 )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6539 ssl->session_negotiate->verify_result ) );
6540 }
6541 else
6542 {
6543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6544 }
6545#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 }
6547
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006548 ssl->state++;
6549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551
6552 return( ret );
6553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6555 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6556 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6557 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6558 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6559 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6560 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006563{
6564 int ret;
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 ssl->out_msglen = 1;
6570 ssl->out_msg[0] = 1;
6571
Paul Bakker5121ce52009-01-03 21:22:43 +00006572 ssl->state++;
6573
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006574 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 return( ret );
6578 }
6579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
6582 return( 0 );
6583}
6584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006586{
6587 int ret;
6588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006590
Hanno Becker327c93b2018-08-15 13:56:18 +01006591 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 return( ret );
6595 }
6596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6601 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 }
6604
Hanno Beckere678eaa2018-08-21 14:57:46 +01006605 /* CCS records are only accepted if they have length 1 and content '1',
6606 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006608 /*
6609 * Switch to our negotiated transform and session parameters for inbound
6610 * data.
6611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006613 ssl->transform_in = ssl->transform_negotiate;
6614 ssl->session_in = ssl->session_negotiate;
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006620 ssl_dtls_replay_reset( ssl );
6621#endif
6622
6623 /* Increment epoch */
6624 if( ++ssl->in_epoch == 0 )
6625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006627 /* This is highly unlikely to happen for legitimate reasons, so
6628 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006630 }
6631 }
6632 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006634 memset( ssl->in_ctr, 0, 8 );
6635
Hanno Beckerf5970a02019-05-08 09:38:41 +01006636 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6639 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006644 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6645 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006647 }
6648 }
6649#endif
6650
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 ssl->state++;
6652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
6655 return( 0 );
6656}
6657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6659 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006660{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006661 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6664 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6665 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006666 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006667 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6670#if defined(MBEDTLS_SHA512_C)
6671 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006672 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6673 else
6674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SHA256_C)
6676 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006677 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006678 else
6679#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006683 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006684 }
Paul Bakker380da532012-04-18 16:10:25 +00006685}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006688{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6690 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006691 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6692 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6695#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006696 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006699 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006705 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6708 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006709 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6710 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6713#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006714 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006717 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006720}
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6723 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6724static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006725 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006726{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006727 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6728 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006729}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006730#endif
Paul Bakker380da532012-04-18 16:10:25 +00006731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6733#if defined(MBEDTLS_SHA256_C)
6734static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006735 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006736{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006737 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006738}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006739#endif
Paul Bakker380da532012-04-18 16:10:25 +00006740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741#if defined(MBEDTLS_SHA512_C)
6742static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006743 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006744{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006745 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006746}
Paul Bakker769075d2012-11-24 11:26:46 +01006747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006751static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006753{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006754 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006755 mbedtls_md5_context md5;
6756 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006757
Paul Bakker5121ce52009-01-03 21:22:43 +00006758 unsigned char padbuf[48];
6759 unsigned char md5sum[16];
6760 unsigned char sha1sum[20];
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006763 if( !session )
6764 session = ssl->session;
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006767
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006768 mbedtls_md5_init( &md5 );
6769 mbedtls_sha1_init( &sha1 );
6770
6771 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6772 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006773
6774 /*
6775 * SSLv3:
6776 * hash =
6777 * MD5( master + pad2 +
6778 * MD5( handshake + sender + master + pad1 ) )
6779 * + SHA1( master + pad2 +
6780 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 */
6782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006784 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6785 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006786#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006789 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6790 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006794 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Paul Bakker1ef83d62012-04-11 12:09:53 +00006796 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006797
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006798 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6799 mbedtls_md5_update_ret( &md5, session->master, 48 );
6800 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6801 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006802
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006803 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6804 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6805 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6806 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006807
Paul Bakker1ef83d62012-04-11 12:09:53 +00006808 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006810 mbedtls_md5_starts_ret( &md5 );
6811 mbedtls_md5_update_ret( &md5, session->master, 48 );
6812 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6813 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6814 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006816 mbedtls_sha1_starts_ret( &sha1 );
6817 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6818 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6819 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6820 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006824 mbedtls_md5_free( &md5 );
6825 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006827 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6828 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6829 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006836static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006838{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006840 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006841 mbedtls_md5_context md5;
6842 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006843 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006846 if( !session )
6847 session = ssl->session;
6848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006850
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006851 mbedtls_md5_init( &md5 );
6852 mbedtls_sha1_init( &sha1 );
6853
6854 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6855 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Paul Bakker1ef83d62012-04-11 12:09:53 +00006857 /*
6858 * TLSv1:
6859 * hash = PRF( master, finished_label,
6860 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6861 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006864 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6865 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006866#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006869 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6870 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006871#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006874 ? "client finished"
6875 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006876
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006877 mbedtls_md5_finish_ret( &md5, padbuf );
6878 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006879
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006880 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006881 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006884
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006885 mbedtls_md5_free( &md5 );
6886 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006888 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6895#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006896static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006898{
6899 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006900 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006901 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006902 unsigned char padbuf[32];
6903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006905 if( !session )
6906 session = ssl->session;
6907
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006908 mbedtls_sha256_init( &sha256 );
6909
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006912 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006913
6914 /*
6915 * TLSv1.2:
6916 * hash = PRF( master, finished_label,
6917 * Hash( handshake ) )[0.11]
6918 */
6919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if !defined(MBEDTLS_SHA256_ALT)
6921 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006922 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006923#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006926 ? "client finished"
6927 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006929 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006930
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006931 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006932 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006935
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006936 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006937
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006938 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006941}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006945static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006947{
6948 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006949 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006950 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006951 unsigned char padbuf[48];
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006954 if( !session )
6955 session = ssl->session;
6956
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006957 mbedtls_sha512_init( &sha512 );
6958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006960
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006961 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006962
6963 /*
6964 * TLSv1.2:
6965 * hash = PRF( master, finished_label,
6966 * Hash( handshake ) )[0.11]
6967 */
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006970 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6971 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006972#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006975 ? "client finished"
6976 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006977
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006978 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006979
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006980 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006981 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006984
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006985 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006987 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991#endif /* MBEDTLS_SHA512_C */
6992#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006997
6998 /*
6999 * Free our handshake params
7000 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007001 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007003 ssl->handshake = NULL;
7004
7005 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007006 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007007 */
7008 if( ssl->transform )
7009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 mbedtls_ssl_transform_free( ssl->transform );
7011 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007012 }
7013 ssl->transform = ssl->transform_negotiate;
7014 ssl->transform_negotiate = NULL;
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007017}
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007020{
7021 int resume = ssl->handshake->resume;
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_SSL_RENEGOTIATION)
7026 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007029 ssl->renego_records_seen = 0;
7030 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007031#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007032
7033 /*
7034 * Free the previous session and switch in the current one
7035 */
Paul Bakker0a597072012-09-25 21:55:46 +00007036 if( ssl->session )
7037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007039 /* RFC 7366 3.1: keep the EtM state */
7040 ssl->session_negotiate->encrypt_then_mac =
7041 ssl->session->encrypt_then_mac;
7042#endif
7043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 mbedtls_ssl_session_free( ssl->session );
7045 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007046 }
7047 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007048 ssl->session_negotiate = NULL;
7049
Paul Bakker0a597072012-09-25 21:55:46 +00007050 /*
7051 * Add cache entry
7052 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007053 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007054 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007055 resume == 0 )
7056 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007057 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007059 }
Paul Bakker0a597072012-09-25 21:55:46 +00007060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007062 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007063 ssl->handshake->flight != NULL )
7064 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007065 /* Cancel handshake timer */
7066 ssl_set_timer( ssl, 0 );
7067
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007068 /* Keep last flight around in case we need to resend it:
7069 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007071 }
7072 else
7073#endif
7074 ssl_handshake_wrapup_free_hs_transform( ssl );
7075
Paul Bakker48916f92012-09-16 19:57:18 +00007076 ssl->state++;
7077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007079}
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007082{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007083 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007086
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007087 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007088
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007089 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007090
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007091 /*
7092 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7093 * may define some other value. Currently (early 2016), no defined
7094 * ciphersuite does this (and this is unlikely to change as activity has
7095 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007100 ssl->verify_data_len = hash_len;
7101 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007102#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007103
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7106 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007107
7108 /*
7109 * In case of session resuming, invert the client and server
7110 * ChangeCipherSpec messages order.
7111 */
Paul Bakker0a597072012-09-25 21:55:46 +00007112 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007117#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007119 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 }
7123 else
7124 ssl->state++;
7125
Paul Bakker48916f92012-09-16 19:57:18 +00007126 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007127 * Switch to our negotiated transform and session parameters for outbound
7128 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007133 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007134 {
7135 unsigned char i;
7136
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007137 /* Remember current epoch settings for resending */
7138 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007139 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007140
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007141 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007142 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007143
7144 /* Increment epoch */
7145 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007146 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007147 break;
7148
7149 /* The loop goes to its end iff the counter is wrapping */
7150 if( i == 0 )
7151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7153 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007154 }
7155 }
7156 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007158 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007159
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007160 ssl->transform_out = ssl->transform_negotiate;
7161 ssl->session_out = ssl->session_negotiate;
7162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7164 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7169 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007170 }
7171 }
7172#endif
7173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007177#endif
7178
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007179 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007180 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007182 return( ret );
7183 }
7184
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007185#if defined(MBEDTLS_SSL_PROTO_DTLS)
7186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7187 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7188 {
7189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7190 return( ret );
7191 }
7192#endif
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
7196 return( 0 );
7197}
7198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007200#define SSL_MAX_HASH_LEN 36
7201#else
7202#define SSL_MAX_HASH_LEN 12
7203#endif
7204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007206{
Paul Bakker23986e52011-04-24 08:57:21 +00007207 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007208 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007209 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007212
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007213 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007214
Hanno Becker327c93b2018-08-15 13:56:18 +01007215 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007218 return( ret );
7219 }
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007224 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7225 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007227 }
7228
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007229 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if defined(MBEDTLS_SSL_PROTO_SSL3)
7231 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007232 hash_len = 36;
7233 else
7234#endif
7235 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7238 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007241 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7242 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007244 }
7245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007247 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007250 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7251 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007253 }
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007256 ssl->verify_data_len = hash_len;
7257 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007258#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007259
Paul Bakker0a597072012-09-25 21:55:46 +00007260 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007263 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007267 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007270 }
7271 else
7272 ssl->state++;
7273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007277#endif
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007280
7281 return( 0 );
7282}
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7289 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7290 mbedtls_md5_init( &handshake->fin_md5 );
7291 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007292 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7293 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7296#if defined(MBEDTLS_SHA256_C)
7297 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007298 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300#if defined(MBEDTLS_SHA512_C)
7301 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007302 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007305
7306 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007307
7308#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7309 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7310 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7311#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313#if defined(MBEDTLS_DHM_C)
7314 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_ECDH_C)
7317 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007318#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007319#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007320 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007321#if defined(MBEDTLS_SSL_CLI_C)
7322 handshake->ecjpake_cache = NULL;
7323 handshake->ecjpake_cache_len = 0;
7324#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007325#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007326
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007327#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007328 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007329#endif
7330
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007331#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7332 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7333#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007334}
7335
Hanno Becker611a83b2018-01-03 14:27:32 +00007336void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7341 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007342
Hanno Becker92231322018-01-03 15:32:51 +00007343#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 mbedtls_md_init( &transform->md_ctx_enc );
7345 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007346#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007347}
7348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007352}
7353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007355{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007356 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007357 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007359 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007361 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007362 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007363
7364 /*
7365 * Either the pointers are now NULL or cleared properly and can be freed.
7366 * Now allocate missing structures.
7367 */
7368 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007369 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007370 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007371 }
Paul Bakker48916f92012-09-16 19:57:18 +00007372
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007373 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007374 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007375 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007376 }
Paul Bakker48916f92012-09-16 19:57:18 +00007377
Paul Bakker82788fb2014-10-20 13:59:19 +02007378 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007379 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007380 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007381 }
Paul Bakker48916f92012-09-16 19:57:18 +00007382
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007383 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007384 if( ssl->handshake == NULL ||
7385 ssl->transform_negotiate == NULL ||
7386 ssl->session_negotiate == NULL )
7387 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 mbedtls_free( ssl->handshake );
7391 mbedtls_free( ssl->transform_negotiate );
7392 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007393
7394 ssl->handshake = NULL;
7395 ssl->transform_negotiate = NULL;
7396 ssl->session_negotiate = NULL;
7397
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007399 }
7400
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007401 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007403 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007404 ssl_handshake_params_init( ssl->handshake );
7405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7408 {
7409 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007410
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7412 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7413 else
7414 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007415
7416 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007417 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007418#endif
7419
Paul Bakker48916f92012-09-16 19:57:18 +00007420 return( 0 );
7421}
7422
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007423#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007424/* Dummy cookie callbacks for defaults */
7425static int ssl_cookie_write_dummy( void *ctx,
7426 unsigned char **p, unsigned char *end,
7427 const unsigned char *cli_id, size_t cli_id_len )
7428{
7429 ((void) ctx);
7430 ((void) p);
7431 ((void) end);
7432 ((void) cli_id);
7433 ((void) cli_id_len);
7434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007436}
7437
7438static int ssl_cookie_check_dummy( void *ctx,
7439 const unsigned char *cookie, size_t cookie_len,
7440 const unsigned char *cli_id, size_t cli_id_len )
7441{
7442 ((void) ctx);
7443 ((void) cookie);
7444 ((void) cookie_len);
7445 ((void) cli_id);
7446 ((void) cli_id_len);
7447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007449}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007450#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007451
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007452/* Once ssl->out_hdr as the address of the beginning of the
7453 * next outgoing record is set, deduce the other pointers.
7454 *
7455 * Note: For TLS, we save the implicit record sequence number
7456 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7457 * and the caller has to make sure there's space for this.
7458 */
7459
7460static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7461 mbedtls_ssl_transform *transform )
7462{
7463#if defined(MBEDTLS_SSL_PROTO_DTLS)
7464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7465 {
7466 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007467#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007468 ssl->out_cid = ssl->out_ctr + 8;
7469 ssl->out_len = ssl->out_cid;
7470 if( transform != NULL )
7471 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007472#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007473 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007475 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007476 }
7477 else
7478#endif
7479 {
7480 ssl->out_ctr = ssl->out_hdr - 8;
7481 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007482#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007483 ssl->out_cid = ssl->out_len;
7484#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007485 ssl->out_iv = ssl->out_hdr + 5;
7486 }
7487
7488 /* Adjust out_msg to make space for explicit IV, if used. */
7489 if( transform != NULL &&
7490 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7491 {
7492 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7493 }
7494 else
7495 ssl->out_msg = ssl->out_iv;
7496}
7497
7498/* Once ssl->in_hdr as the address of the beginning of the
7499 * next incoming record is set, deduce the other pointers.
7500 *
7501 * Note: For TLS, we save the implicit record sequence number
7502 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7503 * and the caller has to make sure there's space for this.
7504 */
7505
Hanno Beckerf5970a02019-05-08 09:38:41 +01007506static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007507{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007508 /* This function sets the pointers to match the case
7509 * of unprotected TLS/DTLS records, with both ssl->in_iv
7510 * and ssl->in_msg pointing to the beginning of the record
7511 * content.
7512 *
7513 * When decrypting a protected record, ssl->in_msg
7514 * will be shifted to point to the beginning of the
7515 * record plaintext.
7516 */
7517
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007518#if defined(MBEDTLS_SSL_PROTO_DTLS)
7519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7520 {
Hanno Becker70e79282019-05-03 14:34:53 +01007521 /* This sets the header pointers to match records
7522 * without CID. When we receive a record containing
7523 * a CID, the fields are shifted accordingly in
7524 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007525 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007526#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007527 ssl->in_cid = ssl->in_ctr + 8;
7528 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007529#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007530 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007531#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007532 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007533 }
7534 else
7535#endif
7536 {
7537 ssl->in_ctr = ssl->in_hdr - 8;
7538 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007539#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007540 ssl->in_cid = ssl->in_len;
7541#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007542 ssl->in_iv = ssl->in_hdr + 5;
7543 }
7544
Hanno Beckerf5970a02019-05-08 09:38:41 +01007545 /* This will be adjusted at record decryption time. */
7546 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007547}
7548
Paul Bakker5121ce52009-01-03 21:22:43 +00007549/*
7550 * Initialize an SSL context
7551 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007552void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7553{
7554 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7555}
7556
7557/*
7558 * Setup an SSL context
7559 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007560
7561static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7562{
7563 /* Set the incoming and outgoing record pointers. */
7564#if defined(MBEDTLS_SSL_PROTO_DTLS)
7565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7566 {
7567 ssl->out_hdr = ssl->out_buf;
7568 ssl->in_hdr = ssl->in_buf;
7569 }
7570 else
7571#endif /* MBEDTLS_SSL_PROTO_DTLS */
7572 {
7573 ssl->out_hdr = ssl->out_buf + 8;
7574 ssl->in_hdr = ssl->in_buf + 8;
7575 }
7576
7577 /* Derive other internal pointers. */
7578 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007579 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007580}
7581
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007582int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007583 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007584{
Paul Bakker48916f92012-09-16 19:57:18 +00007585 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007586
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007587 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007588
7589 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007590 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007591 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007592
7593 /* Set to NULL in case of an error condition */
7594 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007595
Angus Grattond8213d02016-05-25 20:56:48 +10007596 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7597 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007598 {
Angus Grattond8213d02016-05-25 20:56:48 +10007599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007600 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007601 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007602 }
7603
7604 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7605 if( ssl->out_buf == NULL )
7606 {
7607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007608 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007609 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007610 }
7611
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007612 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007613
Paul Bakker48916f92012-09-16 19:57:18 +00007614 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007615 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007616
7617 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007618
7619error:
7620 mbedtls_free( ssl->in_buf );
7621 mbedtls_free( ssl->out_buf );
7622
7623 ssl->conf = NULL;
7624
7625 ssl->in_buf = NULL;
7626 ssl->out_buf = NULL;
7627
7628 ssl->in_hdr = NULL;
7629 ssl->in_ctr = NULL;
7630 ssl->in_len = NULL;
7631 ssl->in_iv = NULL;
7632 ssl->in_msg = NULL;
7633
7634 ssl->out_hdr = NULL;
7635 ssl->out_ctr = NULL;
7636 ssl->out_len = NULL;
7637 ssl->out_iv = NULL;
7638 ssl->out_msg = NULL;
7639
k-stachowiak9f7798e2018-07-31 16:52:32 +02007640 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007641}
7642
7643/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007644 * Reset an initialized and used SSL context for re-use while retaining
7645 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007646 *
7647 * If partial is non-zero, keep data in the input buffer and client ID.
7648 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007649 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007650static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007651{
Paul Bakker48916f92012-09-16 19:57:18 +00007652 int ret;
7653
Hanno Becker7e772132018-08-10 12:38:21 +01007654#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7655 !defined(MBEDTLS_SSL_SRV_C)
7656 ((void) partial);
7657#endif
7658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007660
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007661 /* Cancel any possibly running timer */
7662 ssl_set_timer( ssl, 0 );
7663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664#if defined(MBEDTLS_SSL_RENEGOTIATION)
7665 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007666 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007667
7668 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7670 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007671#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007673
Paul Bakker7eb013f2011-10-06 12:37:39 +00007674 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007675 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007676
7677 ssl->in_msgtype = 0;
7678 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007680 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007681 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007684 ssl_dtls_replay_reset( ssl );
7685#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007686
7687 ssl->in_hslen = 0;
7688 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007689
7690 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007691
7692 ssl->out_msgtype = 0;
7693 ssl->out_msglen = 0;
7694 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7696 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007697 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007698#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007699
Hanno Becker19859472018-08-06 09:40:20 +01007700 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7701
Paul Bakker48916f92012-09-16 19:57:18 +00007702 ssl->transform_in = NULL;
7703 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007704
Hanno Becker78640902018-08-13 16:35:15 +01007705 ssl->session_in = NULL;
7706 ssl->session_out = NULL;
7707
Angus Grattond8213d02016-05-25 20:56:48 +10007708 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007709
7710#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007711 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007712#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7713 {
7714 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007715 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007716 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7719 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7722 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007726 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007727 }
7728#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007729
Paul Bakker48916f92012-09-16 19:57:18 +00007730 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 mbedtls_ssl_transform_free( ssl->transform );
7733 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007734 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007735 }
Paul Bakker48916f92012-09-16 19:57:18 +00007736
Paul Bakkerc0463502013-02-14 11:19:38 +01007737 if( ssl->session )
7738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 mbedtls_ssl_session_free( ssl->session );
7740 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007741 ssl->session = NULL;
7742 }
7743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007745 ssl->alpn_chosen = NULL;
7746#endif
7747
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007748#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007749#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007750 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007751#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007752 {
7753 mbedtls_free( ssl->cli_id );
7754 ssl->cli_id = NULL;
7755 ssl->cli_id_len = 0;
7756 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007757#endif
7758
Paul Bakker48916f92012-09-16 19:57:18 +00007759 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7760 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007761
7762 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007763}
7764
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007765/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007766 * Reset an initialized and used SSL context for re-use while retaining
7767 * all application-set variables, function pointers and data.
7768 */
7769int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7770{
7771 return( ssl_session_reset_int( ssl, 0 ) );
7772}
7773
7774/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007775 * SSL set accessors
7776 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007777void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007778{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007779 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007780}
7781
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007782void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007783{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007784 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007785}
7786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007788void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007789{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007790 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007791}
7792#endif
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007795void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007796{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007797 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007798}
7799#endif
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007802
Hanno Becker1841b0a2018-08-24 11:13:57 +01007803void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7804 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007805{
7806 ssl->disable_datagram_packing = !allow_packing;
7807}
7808
7809void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7810 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007811{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007812 conf->hs_timeout_min = min;
7813 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007814}
7815#endif
7816
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007817void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007818{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007819 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007820}
7821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007823void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007824 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007825 void *p_vrfy )
7826{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007827 conf->f_vrfy = f_vrfy;
7828 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007831
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007832void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007833 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007834 void *p_rng )
7835{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007836 conf->f_rng = f_rng;
7837 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007838}
7839
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007840void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007841 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007842 void *p_dbg )
7843{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007844 conf->f_dbg = f_dbg;
7845 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007846}
7847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007849 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007850 mbedtls_ssl_send_t *f_send,
7851 mbedtls_ssl_recv_t *f_recv,
7852 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007853{
7854 ssl->p_bio = p_bio;
7855 ssl->f_send = f_send;
7856 ssl->f_recv = f_recv;
7857 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007858}
7859
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007860#if defined(MBEDTLS_SSL_PROTO_DTLS)
7861void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7862{
7863 ssl->mtu = mtu;
7864}
7865#endif
7866
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007867void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007868{
7869 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007870}
7871
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007872void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7873 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007874 mbedtls_ssl_set_timer_t *f_set_timer,
7875 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007876{
7877 ssl->p_timer = p_timer;
7878 ssl->f_set_timer = f_set_timer;
7879 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007880
7881 /* Make sure we start with no timer running */
7882 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007883}
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007886void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007887 void *p_cache,
7888 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7889 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007890{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007891 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007892 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007893 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897#if defined(MBEDTLS_SSL_CLI_C)
7898int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007899{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007900 int ret;
7901
7902 if( ssl == NULL ||
7903 session == NULL ||
7904 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007905 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007908 }
7909
7910 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7911 return( ret );
7912
Paul Bakker0a597072012-09-25 21:55:46 +00007913 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007914
7915 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007918
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007919void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007920 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7923 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7924 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007926}
7927
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007928void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007929 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007930 int major, int minor )
7931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007933 return;
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007936 return;
7937
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007938 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007939}
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007942void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007943 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007944{
7945 conf->cert_profile = profile;
7946}
7947
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007948/* Append a new keycert entry to a (possibly empty) list */
7949static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7950 mbedtls_x509_crt *cert,
7951 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007952{
niisato8ee24222018-06-25 19:05:48 +09007953 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007954
niisato8ee24222018-06-25 19:05:48 +09007955 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7956 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007957 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007958
niisato8ee24222018-06-25 19:05:48 +09007959 new_cert->cert = cert;
7960 new_cert->key = key;
7961 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007962
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007963 /* Update head is the list was null, else add to the end */
7964 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007965 {
niisato8ee24222018-06-25 19:05:48 +09007966 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007967 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007968 else
7969 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007970 mbedtls_ssl_key_cert *cur = *head;
7971 while( cur->next != NULL )
7972 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007973 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007974 }
7975
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007976 return( 0 );
7977}
7978
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007979int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007980 mbedtls_x509_crt *own_cert,
7981 mbedtls_pk_context *pk_key )
7982{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007983 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007984}
7985
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007986void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007987 mbedtls_x509_crt *ca_chain,
7988 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007989{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007990 conf->ca_chain = ca_chain;
7991 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007994
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007995#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7996int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7997 mbedtls_x509_crt *own_cert,
7998 mbedtls_pk_context *pk_key )
7999{
8000 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8001 own_cert, pk_key ) );
8002}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008003
8004void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8005 mbedtls_x509_crt *ca_chain,
8006 mbedtls_x509_crl *ca_crl )
8007{
8008 ssl->handshake->sni_ca_chain = ca_chain;
8009 ssl->handshake->sni_ca_crl = ca_crl;
8010}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008011
8012void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8013 int authmode )
8014{
8015 ssl->handshake->sni_authmode = authmode;
8016}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008017#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8018
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008019#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008020/*
8021 * Set EC J-PAKE password for current handshake
8022 */
8023int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8024 const unsigned char *pw,
8025 size_t pw_len )
8026{
8027 mbedtls_ecjpake_role role;
8028
Janos Follath8eb64132016-06-03 15:40:57 +01008029 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8031
8032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8033 role = MBEDTLS_ECJPAKE_SERVER;
8034 else
8035 role = MBEDTLS_ECJPAKE_CLIENT;
8036
8037 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8038 role,
8039 MBEDTLS_MD_SHA256,
8040 MBEDTLS_ECP_DP_SECP256R1,
8041 pw, pw_len ) );
8042}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008043#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008046int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008047 const unsigned char *psk, size_t psk_len,
8048 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008049{
Paul Bakker6db455e2013-09-18 17:29:31 +02008050 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008055
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008056 /* Identity len will be encoded on two bytes */
8057 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008058 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008059 {
8060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8061 }
8062
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008063 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008064 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008065 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008066
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008067 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008068 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008069 conf->psk_len = 0;
8070 }
8071 if( conf->psk_identity != NULL )
8072 {
8073 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008074 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008075 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008076 }
8077
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008078 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8079 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008080 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008081 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008082 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008083 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008084 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008085 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008086 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008087
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008088 conf->psk_len = psk_len;
8089 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008090
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008091 memcpy( conf->psk, psk, conf->psk_len );
8092 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008093
8094 return( 0 );
8095}
8096
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008097int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8098 const unsigned char *psk, size_t psk_len )
8099{
8100 if( psk == NULL || ssl->handshake == NULL )
8101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8102
8103 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8105
8106 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008107 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008108 mbedtls_platform_zeroize( ssl->handshake->psk,
8109 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008110 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008111 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008112 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008113
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008114 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008115 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008116
8117 ssl->handshake->psk_len = psk_len;
8118 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8119
8120 return( 0 );
8121}
8122
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008123void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008125 size_t),
8126 void *p_psk )
8127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008128 conf->f_psk = f_psk;
8129 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008132
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008133#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008134
8135#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008136int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008137{
8138 int ret;
8139
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008140 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8141 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8142 {
8143 mbedtls_mpi_free( &conf->dhm_P );
8144 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008145 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008147
8148 return( 0 );
8149}
Hanno Becker470a8c42017-10-04 15:28:46 +01008150#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008151
Hanno Beckera90658f2017-10-04 15:29:08 +01008152int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8153 const unsigned char *dhm_P, size_t P_len,
8154 const unsigned char *dhm_G, size_t G_len )
8155{
8156 int ret;
8157
8158 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8159 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8160 {
8161 mbedtls_mpi_free( &conf->dhm_P );
8162 mbedtls_mpi_free( &conf->dhm_G );
8163 return( ret );
8164 }
8165
8166 return( 0 );
8167}
Paul Bakker5121ce52009-01-03 21:22:43 +00008168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008169int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008170{
8171 int ret;
8172
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008173 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8174 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8175 {
8176 mbedtls_mpi_free( &conf->dhm_P );
8177 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008178 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008179 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008180
8181 return( 0 );
8182}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008183#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008184
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008185#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8186/*
8187 * Set the minimum length for Diffie-Hellman parameters
8188 */
8189void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8190 unsigned int bitlen )
8191{
8192 conf->dhm_min_bitlen = bitlen;
8193}
8194#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8195
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008196#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008197/*
8198 * Set allowed/preferred hashes for handshake signatures
8199 */
8200void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8201 const int *hashes )
8202{
8203 conf->sig_hashes = hashes;
8204}
Hanno Becker947194e2017-04-07 13:25:49 +01008205#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008206
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008207#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008208/*
8209 * Set the allowed elliptic curves
8210 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008211void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008212 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008213{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008214 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008215}
Hanno Becker947194e2017-04-07 13:25:49 +01008216#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008217
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008218#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008220{
Hanno Becker947194e2017-04-07 13:25:49 +01008221 /* Initialize to suppress unnecessary compiler warning */
8222 size_t hostname_len = 0;
8223
8224 /* Check if new hostname is valid before
8225 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008226 if( hostname != NULL )
8227 {
8228 hostname_len = strlen( hostname );
8229
8230 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8232 }
8233
8234 /* Now it's clear that we will overwrite the old hostname,
8235 * so we can free it safely */
8236
8237 if( ssl->hostname != NULL )
8238 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008239 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008240 mbedtls_free( ssl->hostname );
8241 }
8242
8243 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008244
Paul Bakker5121ce52009-01-03 21:22:43 +00008245 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008246 {
8247 ssl->hostname = NULL;
8248 }
8249 else
8250 {
8251 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008252 if( ssl->hostname == NULL )
8253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008254
Hanno Becker947194e2017-04-07 13:25:49 +01008255 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008256
Hanno Becker947194e2017-04-07 13:25:49 +01008257 ssl->hostname[hostname_len] = '\0';
8258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008259
8260 return( 0 );
8261}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008262#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008263
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008264#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008265void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008267 const unsigned char *, size_t),
8268 void *p_sni )
8269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008270 conf->f_sni = f_sni;
8271 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008276int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008277{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008278 size_t cur_len, tot_len;
8279 const char **p;
8280
8281 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008282 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8283 * MUST NOT be truncated."
8284 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008285 */
8286 tot_len = 0;
8287 for( p = protos; *p != NULL; p++ )
8288 {
8289 cur_len = strlen( *p );
8290 tot_len += cur_len;
8291
8292 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008294 }
8295
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008296 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008297
8298 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008299}
8300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008302{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008303 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008306
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008307void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008309 conf->max_major_ver = major;
8310 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008311}
8312
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008313void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008314{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008315 conf->min_major_ver = major;
8316 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008317}
8318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008320void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008321{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008322 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008323}
8324#endif
8325
Janos Follath088ce432017-04-10 12:42:31 +01008326#if defined(MBEDTLS_SSL_SRV_C)
8327void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8328 char cert_req_ca_list )
8329{
8330 conf->cert_req_ca_list = cert_req_ca_list;
8331}
8332#endif
8333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008335void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008336{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008337 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008338}
8339#endif
8340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008342void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008344 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008345}
8346#endif
8347
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008348#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008349void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008351 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008352}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008353#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008356int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008359 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008362 }
8363
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008364 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008365
8366 return( 0 );
8367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008371void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008373 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008378void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008379{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008380 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008381}
8382#endif
8383
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008384void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008386 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008387}
8388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008390void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008392 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008393}
8394
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008395void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008396{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008397 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008398}
8399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008400void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008401 const unsigned char period[8] )
8402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008403 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008408#if defined(MBEDTLS_SSL_CLI_C)
8409void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008410{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008411 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008412}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008413#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008415#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008416void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8417 mbedtls_ssl_ticket_write_t *f_ticket_write,
8418 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8419 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008420{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008421 conf->f_ticket_write = f_ticket_write;
8422 conf->f_ticket_parse = f_ticket_parse;
8423 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008424}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008427
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008428#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8429void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8430 mbedtls_ssl_export_keys_t *f_export_keys,
8431 void *p_export_keys )
8432{
8433 conf->f_export_keys = f_export_keys;
8434 conf->p_export_keys = p_export_keys;
8435}
8436#endif
8437
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008438#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008439void mbedtls_ssl_conf_async_private_cb(
8440 mbedtls_ssl_config *conf,
8441 mbedtls_ssl_async_sign_t *f_async_sign,
8442 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8443 mbedtls_ssl_async_resume_t *f_async_resume,
8444 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008445 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008446{
8447 conf->f_async_sign_start = f_async_sign;
8448 conf->f_async_decrypt_start = f_async_decrypt;
8449 conf->f_async_resume = f_async_resume;
8450 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008451 conf->p_async_config_data = async_config_data;
8452}
8453
Gilles Peskine8f97af72018-04-26 11:46:10 +02008454void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8455{
8456 return( conf->p_async_config_data );
8457}
8458
Gilles Peskine1febfef2018-04-30 11:54:39 +02008459void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008460{
8461 if( ssl->handshake == NULL )
8462 return( NULL );
8463 else
8464 return( ssl->handshake->user_async_ctx );
8465}
8466
Gilles Peskine1febfef2018-04-30 11:54:39 +02008467void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008468 void *ctx )
8469{
8470 if( ssl->handshake != NULL )
8471 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008472}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008473#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008474
Paul Bakker5121ce52009-01-03 21:22:43 +00008475/*
8476 * SSL get accessors
8477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008479{
8480 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8481}
8482
Hanno Becker8b170a02017-10-10 11:51:19 +01008483int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8484{
8485 /*
8486 * Case A: We're currently holding back
8487 * a message for further processing.
8488 */
8489
8490 if( ssl->keep_current_message == 1 )
8491 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008493 return( 1 );
8494 }
8495
8496 /*
8497 * Case B: Further records are pending in the current datagram.
8498 */
8499
8500#if defined(MBEDTLS_SSL_PROTO_DTLS)
8501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8502 ssl->in_left > ssl->next_record_offset )
8503 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008505 return( 1 );
8506 }
8507#endif /* MBEDTLS_SSL_PROTO_DTLS */
8508
8509 /*
8510 * Case C: A handshake message is being processed.
8511 */
8512
Hanno Becker8b170a02017-10-10 11:51:19 +01008513 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8514 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008516 return( 1 );
8517 }
8518
8519 /*
8520 * Case D: An application data message is being processed
8521 */
8522 if( ssl->in_offt != NULL )
8523 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008524 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008525 return( 1 );
8526 }
8527
8528 /*
8529 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008530 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008531 * we implement support for multiple alerts in single records.
8532 */
8533
8534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8535 return( 0 );
8536}
8537
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008538uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008539{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008540 if( ssl->session != NULL )
8541 return( ssl->session->verify_result );
8542
8543 if( ssl->session_negotiate != NULL )
8544 return( ssl->session_negotiate->verify_result );
8545
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008546 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008547}
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008550{
Paul Bakker926c8e42013-03-06 10:23:34 +01008551 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008552 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008555}
8556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008561 {
8562 switch( ssl->minor_ver )
8563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008565 return( "DTLSv1.0" );
8566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008568 return( "DTLSv1.2" );
8569
8570 default:
8571 return( "unknown (DTLS)" );
8572 }
8573 }
8574#endif
8575
Paul Bakker43ca69c2011-01-15 17:35:19 +00008576 switch( ssl->minor_ver )
8577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008579 return( "SSLv3.0" );
8580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008582 return( "TLSv1.0" );
8583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008585 return( "TLSv1.1" );
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008588 return( "TLSv1.2" );
8589
Paul Bakker43ca69c2011-01-15 17:35:19 +00008590 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008591 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008592 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008593}
8594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008596{
Hanno Becker3136ede2018-08-17 15:28:19 +01008597 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008599 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008600
Hanno Becker43395762019-05-03 14:46:38 +01008601 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8602
Hanno Becker78640902018-08-13 16:35:15 +01008603 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008604 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606#if defined(MBEDTLS_ZLIB_SUPPORT)
8607 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8608 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008609#endif
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 case MBEDTLS_MODE_GCM:
8614 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008615 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008617 transform_expansion = transform->minlen;
8618 break;
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008621
8622 block_size = mbedtls_cipher_get_block_size(
8623 &transform->cipher_ctx_enc );
8624
Hanno Becker3136ede2018-08-17 15:28:19 +01008625 /* Expansion due to the addition of the MAC. */
8626 transform_expansion += transform->maclen;
8627
8628 /* Expansion due to the addition of CBC padding;
8629 * Theoretically up to 256 bytes, but we never use
8630 * more than the block size of the underlying cipher. */
8631 transform_expansion += block_size;
8632
8633 /* For TLS 1.1 or higher, an explicit IV is added
8634 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008635#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8636 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008637 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008638#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008639
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008640 break;
8641
8642 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008645 }
8646
Hanno Beckera5a2b082019-05-15 14:03:01 +01008647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008648 if( transform->out_cid_len != 0 )
8649 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008650#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008651
Hanno Becker43395762019-05-03 14:46:38 +01008652 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008653}
8654
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008655#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8656size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8657{
8658 size_t max_len;
8659
8660 /*
8661 * Assume mfl_code is correct since it was checked when set
8662 */
Angus Grattond8213d02016-05-25 20:56:48 +10008663 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008664
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008665 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008666 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008667 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008668 {
Angus Grattond8213d02016-05-25 20:56:48 +10008669 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008670 }
8671
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008672 /* During a handshake, use the value being negotiated */
8673 if( ssl->session_negotiate != NULL &&
8674 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8675 {
8676 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8677 }
8678
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008679 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008680}
8681#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8682
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008683#if defined(MBEDTLS_SSL_PROTO_DTLS)
8684static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8685{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008686 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8688 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8689 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8690 return ( 0 );
8691
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008692 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8693 return( ssl->mtu );
8694
8695 if( ssl->mtu == 0 )
8696 return( ssl->handshake->mtu );
8697
8698 return( ssl->mtu < ssl->handshake->mtu ?
8699 ssl->mtu : ssl->handshake->mtu );
8700}
8701#endif /* MBEDTLS_SSL_PROTO_DTLS */
8702
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008703int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8704{
8705 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8706
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008707#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8708 !defined(MBEDTLS_SSL_PROTO_DTLS)
8709 (void) ssl;
8710#endif
8711
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008712#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8713 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8714
8715 if( max_len > mfl )
8716 max_len = mfl;
8717#endif
8718
8719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008720 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008721 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008722 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008723 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8724 const size_t overhead = (size_t) ret;
8725
8726 if( ret < 0 )
8727 return( ret );
8728
8729 if( mtu <= overhead )
8730 {
8731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8732 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8733 }
8734
8735 if( max_len > mtu - overhead )
8736 max_len = mtu - overhead;
8737 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008738#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008739
Hanno Becker0defedb2018-08-10 12:35:02 +01008740#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8741 !defined(MBEDTLS_SSL_PROTO_DTLS)
8742 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008743#endif
8744
8745 return( (int) max_len );
8746}
8747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748#if defined(MBEDTLS_X509_CRT_PARSE_C)
8749const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008750{
8751 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008752 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008753
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008754 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758#if defined(MBEDTLS_SSL_CLI_C)
8759int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008760{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008761 if( ssl == NULL ||
8762 dst == NULL ||
8763 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008764 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008767 }
8768
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008769 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008772
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008773const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8774{
8775 if( ssl == NULL )
8776 return( NULL );
8777
8778 return( ssl->session );
8779}
8780
Paul Bakker5121ce52009-01-03 21:22:43 +00008781/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008782 * Define ticket header determining Mbed TLS version
8783 * and structure of the ticket.
8784 */
8785
Hanno Becker41527622019-05-16 12:50:45 +01008786/*
Hanno Becker26829e92019-05-28 14:30:45 +01008787 * Define bitflag determining compile-time settings influencing
8788 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008789 */
8790
Hanno Becker26829e92019-05-28 14:30:45 +01008791#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008792#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008793#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008794#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008795#endif /* MBEDTLS_HAVE_TIME */
8796
8797#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008798#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008799#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008800#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008801#endif /* MBEDTLS_X509_CRT_PARSE_C */
8802
8803#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008804#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008805#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008806#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008807#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8808
8809#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008810#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008811#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008812#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008813#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8814
8815#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008816#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008817#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008818#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008819#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8820
8821#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008822#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008823#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008824#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008825#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8826
Hanno Becker41527622019-05-16 12:50:45 +01008827#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8828#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8829#else
8830#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8831#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8832
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008833#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8834#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8835#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8836#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8837#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8838#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8839#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008840
Hanno Becker26829e92019-05-28 14:30:45 +01008841#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008842 ( (uint16_t) ( \
8843 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8844 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8845 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8846 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8847 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8848 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008849 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008850
Hanno Becker557fe9f2019-05-16 12:41:07 +01008851static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008852 MBEDTLS_VERSION_MAJOR,
8853 MBEDTLS_VERSION_MINOR,
8854 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008855 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8856 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008857};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008858
8859/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008860 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008861 * (in the presentation language of TLS, RFC 8446 section 3)
8862 *
Hanno Becker26829e92019-05-28 14:30:45 +01008863 * opaque mbedtls_version[3]; // major, minor, patch
8864 * opaque session_format[2]; // version-specific 16-bit field determining
8865 * // the format of the remaining
8866 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008867 *
8868 * Note: When updating the format, remember to keep
8869 * these version+format bytes.
8870 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008871 * // In this version, `session_format` determines
8872 * // the setting of those compile-time
8873 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008874 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008875 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008876 * uint8 ciphersuite[2]; // defined by the standard
8877 * uint8 compression; // 0 or 1
8878 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008879 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008880 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008881 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008882 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8883 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008884 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008885 * uint8 mfl_code; // up to 255 according to standard
8886 * uint8 trunc_hmac; // 0 or 1
8887 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008888 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008889 * The order is the same as in the definition of the structure, except
8890 * verify_result is put before peer_cert so that all mandatory fields come
8891 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008892 */
8893int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8894 unsigned char *buf,
8895 size_t buf_len,
8896 size_t *olen )
8897{
8898 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008899 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008900#if defined(MBEDTLS_HAVE_TIME)
8901 uint64_t start;
8902#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008903#if defined(MBEDTLS_X509_CRT_PARSE_C)
8904 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008905#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008906
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008907 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008908 * Add version identifier
8909 */
8910
8911 used += sizeof( ssl_serialized_session_header );
8912
8913 if( used <= buf_len )
8914 {
8915 memcpy( p, ssl_serialized_session_header,
8916 sizeof( ssl_serialized_session_header ) );
8917 p += sizeof( ssl_serialized_session_header );
8918 }
8919
8920 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008921 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008922 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008923#if defined(MBEDTLS_HAVE_TIME)
8924 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008925
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008926 if( used <= buf_len )
8927 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008928 start = (uint64_t) session->start;
8929
8930 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8931 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8932 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8933 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8934 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
8935 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
8936 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
8937 *p++ = (unsigned char)( ( start ) & 0xFF );
8938 }
8939#endif /* MBEDTLS_HAVE_TIME */
8940
8941 /*
8942 * Basic mandatory fields
8943 */
8944 used += 2 /* ciphersuite */
8945 + 1 /* compression */
8946 + 1 /* id_len */
8947 + sizeof( session->id )
8948 + sizeof( session->master )
8949 + 4; /* verify_result */
8950
8951 if( used <= buf_len )
8952 {
8953 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
8954 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
8955
8956 *p++ = (unsigned char)( session->compression & 0xFF );
8957
8958 *p++ = (unsigned char)( session->id_len & 0xFF );
8959 memcpy( p, session->id, 32 );
8960 p += 32;
8961
8962 memcpy( p, session->master, 48 );
8963 p += 48;
8964
8965 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
8966 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
8967 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
8968 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008969 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008970
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008971 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008972 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008973 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008974#if defined(MBEDTLS_X509_CRT_PARSE_C)
8975 if( session->peer_cert == NULL )
8976 cert_len = 0;
8977 else
8978 cert_len = session->peer_cert->raw.len;
8979
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008980 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008981
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008982 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008983 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008984 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
8985 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
8986 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
8987
8988 if( session->peer_cert != NULL )
8989 {
8990 memcpy( p, session->peer_cert->raw.p, cert_len );
8991 p += cert_len;
8992 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008993 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008994#endif /* MBEDTLS_X509_CRT_PARSE_C */
8995
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008996 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008997 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008998 */
8999#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009000 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009001
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009002 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009003 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009004 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9005 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9006 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9007
9008 if( session->ticket != NULL )
9009 {
9010 memcpy( p, session->ticket, session->ticket_len );
9011 p += session->ticket_len;
9012 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009013
9014 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9015 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9016 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9017 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009018 }
9019#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9020
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009021 /*
9022 * Misc extension-related info
9023 */
9024#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9025 used += 1;
9026
9027 if( used <= buf_len )
9028 *p++ = session->mfl_code;
9029#endif
9030
9031#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9032 used += 1;
9033
9034 if( used <= buf_len )
9035 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9036#endif
9037
9038#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9039 used += 1;
9040
9041 if( used <= buf_len )
9042 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9043#endif
9044
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009045 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009046 *olen = used;
9047
9048 if( used > buf_len )
9049 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009050
9051 return( 0 );
9052}
9053
9054/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009055 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009056 *
9057 * This internal version is wrapped by a public function that cleans up in
9058 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009059 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009060static int ssl_session_load( mbedtls_ssl_session *session,
9061 const unsigned char *buf,
9062 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009063{
9064 const unsigned char *p = buf;
9065 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009066#if defined(MBEDTLS_HAVE_TIME)
9067 uint64_t start;
9068#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009069#if defined(MBEDTLS_X509_CRT_PARSE_C)
9070 size_t cert_len;
9071#endif /* MBEDTLS_X509_CRT_PARSE_C */
9072
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009073 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009074 * Check version identifier
9075 */
9076
9077 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009079
9080 if( memcmp( p, ssl_serialized_session_header,
9081 sizeof( ssl_serialized_session_header ) ) != 0 )
9082 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009083 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009084 }
9085 p += sizeof( ssl_serialized_session_header );
9086
9087 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009088 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009089 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009090#if defined(MBEDTLS_HAVE_TIME)
9091 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9093
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009094 start = ( (uint64_t) p[0] << 56 ) |
9095 ( (uint64_t) p[1] << 48 ) |
9096 ( (uint64_t) p[2] << 40 ) |
9097 ( (uint64_t) p[3] << 32 ) |
9098 ( (uint64_t) p[4] << 24 ) |
9099 ( (uint64_t) p[5] << 16 ) |
9100 ( (uint64_t) p[6] << 8 ) |
9101 ( (uint64_t) p[7] );
9102 p += 8;
9103
9104 session->start = (time_t) start;
9105#endif /* MBEDTLS_HAVE_TIME */
9106
9107 /*
9108 * Basic mandatory fields
9109 */
9110 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9112
9113 session->ciphersuite = ( p[0] << 8 ) | p[1];
9114 p += 2;
9115
9116 session->compression = *p++;
9117
9118 session->id_len = *p++;
9119 memcpy( session->id, p, 32 );
9120 p += 32;
9121
9122 memcpy( session->master, p, 48 );
9123 p += 48;
9124
9125 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9126 ( (uint32_t) p[1] << 16 ) |
9127 ( (uint32_t) p[2] << 8 ) |
9128 ( (uint32_t) p[3] );
9129 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009130
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009131 /* Immediately clear invalid pointer values that have been read, in case
9132 * we exit early before we replaced them with valid ones. */
9133#if defined(MBEDTLS_X509_CRT_PARSE_C)
9134 session->peer_cert = NULL;
9135#endif
9136#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9137 session->ticket = NULL;
9138#endif
9139
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009140 /*
9141 * Peer certificate
9142 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009143#if defined(MBEDTLS_X509_CRT_PARSE_C)
9144 if( 3 > (size_t)( end - p ) )
9145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9146
9147 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9148 p += 3;
9149
9150 if( cert_len == 0 )
9151 {
9152 session->peer_cert = NULL;
9153 }
9154 else
9155 {
9156 int ret;
9157
9158 if( cert_len > (size_t)( end - p ) )
9159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9160
9161 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9162
9163 if( session->peer_cert == NULL )
9164 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9165
9166 mbedtls_x509_crt_init( session->peer_cert );
9167
9168 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9169 p, cert_len ) ) != 0 )
9170 {
9171 mbedtls_x509_crt_free( session->peer_cert );
9172 mbedtls_free( session->peer_cert );
9173 session->peer_cert = NULL;
9174 return( ret );
9175 }
9176
9177 p += cert_len;
9178 }
9179#endif /* MBEDTLS_X509_CRT_PARSE_C */
9180
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009181 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009182 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009183 */
9184#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9185 if( 3 > (size_t)( end - p ) )
9186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9187
9188 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9189 p += 3;
9190
9191 if( session->ticket_len != 0 )
9192 {
9193 if( session->ticket_len > (size_t)( end - p ) )
9194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9195
9196 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9197 if( session->ticket == NULL )
9198 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9199
9200 memcpy( session->ticket, p, session->ticket_len );
9201 p += session->ticket_len;
9202 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009203
9204 if( 4 > (size_t)( end - p ) )
9205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9206
9207 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9208 ( (uint32_t) p[1] << 16 ) |
9209 ( (uint32_t) p[2] << 8 ) |
9210 ( (uint32_t) p[3] );
9211 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009212#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9213
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009214 /*
9215 * Misc extension-related info
9216 */
9217#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9218 if( 1 > (size_t)( end - p ) )
9219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9220
9221 session->mfl_code = *p++;
9222#endif
9223
9224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9225 if( 1 > (size_t)( end - p ) )
9226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9227
9228 session->trunc_hmac = *p++;
9229#endif
9230
9231#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9232 if( 1 > (size_t)( end - p ) )
9233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9234
9235 session->encrypt_then_mac = *p++;
9236#endif
9237
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009238 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009239 if( p != end )
9240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9241
9242 return( 0 );
9243}
9244
9245/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009246 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009247 */
9248int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9249 const unsigned char *buf,
9250 size_t len )
9251{
9252 int ret = ssl_session_load( session, buf, len );
9253
9254 if( ret != 0 )
9255 mbedtls_ssl_session_free( session );
9256
9257 return( ret );
9258}
9259
9260/*
Paul Bakker1961b702013-01-25 14:49:24 +01009261 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009266
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009267 if( ssl == NULL || ssl->conf == NULL )
9268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009275 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009277#endif
9278
Paul Bakker1961b702013-01-25 14:49:24 +01009279 return( ret );
9280}
9281
9282/*
9283 * Perform the SSL handshake
9284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009286{
9287 int ret = 0;
9288
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009289 if( ssl == NULL || ssl->conf == NULL )
9290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009297
9298 if( ret != 0 )
9299 break;
9300 }
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009303
9304 return( ret );
9305}
9306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307#if defined(MBEDTLS_SSL_RENEGOTIATION)
9308#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009309/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009310 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009313{
9314 int ret;
9315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009317
9318 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9320 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009321
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009322 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009323 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009325 return( ret );
9326 }
9327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009329
9330 return( 0 );
9331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009333
9334/*
9335 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 * - any side: calling mbedtls_ssl_renegotiate(),
9337 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9338 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009339 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009340 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009344{
9345 int ret;
9346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009348
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009349 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9350 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009351
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009352 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9353 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009357 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009359 ssl->handshake->out_msg_seq = 1;
9360 else
9361 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009362 }
9363#endif
9364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9366 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009371 return( ret );
9372 }
9373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009375
9376 return( 0 );
9377}
9378
9379/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009380 * Renegotiate current connection on client,
9381 * or request renegotiation on server
9382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009384{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009386
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009387 if( ssl == NULL || ssl->conf == NULL )
9388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009391 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009392 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009398
9399 /* Did we already try/start sending HelloRequest? */
9400 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009402
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009403 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009408 /*
9409 * On client, either start the renegotiation process or,
9410 * if already in progress, continue the handshake
9411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009416
9417 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009420 return( ret );
9421 }
9422 }
9423 else
9424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009428 return( ret );
9429 }
9430 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009432
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009433 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009434}
9435
9436/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009437 * Check record counters and renegotiate if they're above the limit.
9438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009440{
Andres AG2196c7f2016-12-15 17:01:16 +00009441 size_t ep_len = ssl_ep_len( ssl );
9442 int in_ctr_cmp;
9443 int out_ctr_cmp;
9444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9446 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009447 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009448 {
9449 return( 0 );
9450 }
9451
Andres AG2196c7f2016-12-15 17:01:16 +00009452 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9453 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009454 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009455 ssl->conf->renego_period + ep_len, 8 - ep_len );
9456
9457 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009458 {
9459 return( 0 );
9460 }
9461
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009466
9467/*
9468 * Receive application data decrypted from the SSL layer
9469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009471{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009472 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009473 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009474
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009475 if( ssl == NULL || ssl->conf == NULL )
9476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009481 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009484 return( ret );
9485
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009486 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009488 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009489 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009490 return( ret );
9491 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009492 }
9493#endif
9494
Hanno Becker4a810fb2017-05-24 16:27:30 +01009495 /*
9496 * Check if renegotiation is necessary and/or handshake is
9497 * in process. If yes, perform/continue, and fall through
9498 * if an unexpected packet is received while the client
9499 * is waiting for the ServerHello.
9500 *
9501 * (There is no equivalent to the last condition on
9502 * the server-side as it is not treated as within
9503 * a handshake while waiting for the ClientHello
9504 * after a renegotiation request.)
9505 */
9506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009508 ret = ssl_check_ctr_renegotiate( ssl );
9509 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9510 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009513 return( ret );
9514 }
9515#endif
9516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009520 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9521 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009524 return( ret );
9525 }
9526 }
9527
Hanno Beckere41158b2017-10-23 13:30:32 +01009528 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009529 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009530 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009531 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009532 if( ssl->f_get_timer != NULL &&
9533 ssl->f_get_timer( ssl->p_timer ) == -1 )
9534 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009535 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009536 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009537
Hanno Becker327c93b2018-08-15 13:56:18 +01009538 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009539 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009540 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9541 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009542
Hanno Becker4a810fb2017-05-24 16:27:30 +01009543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9544 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009545 }
9546
9547 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009549 {
9550 /*
9551 * OpenSSL sends empty messages to randomize the IV
9552 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009553 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009556 return( 0 );
9557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009559 return( ret );
9560 }
9561 }
9562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009566
Hanno Becker4a810fb2017-05-24 16:27:30 +01009567 /*
9568 * - For client-side, expect SERVER_HELLO_REQUEST.
9569 * - For server-side, expect CLIENT_HELLO.
9570 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9571 */
9572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009574 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009576 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009579
9580 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009582 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009583 {
9584 continue;
9585 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009588 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009589#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009590
Hanno Becker4a810fb2017-05-24 16:27:30 +01009591#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009592 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009596
9597 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009599 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009600 {
9601 continue;
9602 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009605 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009606#endif /* MBEDTLS_SSL_SRV_C */
9607
Hanno Becker21df7f92017-10-17 11:03:26 +01009608#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009609 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009610 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9611 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9612 ssl->conf->allow_legacy_renegotiation ==
9613 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9614 {
9615 /*
9616 * Accept renegotiation request
9617 */
Paul Bakker48916f92012-09-16 19:57:18 +00009618
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009619 /* DTLS clients need to know renego is server-initiated */
9620#if defined(MBEDTLS_SSL_PROTO_DTLS)
9621 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9622 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9623 {
9624 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9625 }
9626#endif
9627 ret = ssl_start_renegotiation( ssl );
9628 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9629 ret != 0 )
9630 {
9631 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9632 return( ret );
9633 }
9634 }
9635 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009636#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009637 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009638 /*
9639 * Refuse renegotiation
9640 */
9641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644#if defined(MBEDTLS_SSL_PROTO_SSL3)
9645 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009646 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009647 /* SSLv3 does not have a "no_renegotiation" warning, so
9648 we send a fatal alert and abort the connection. */
9649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9650 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9651 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009652 }
9653 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9655#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9656 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9657 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9660 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9661 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009662 {
9663 return( ret );
9664 }
Paul Bakker48916f92012-09-16 19:57:18 +00009665 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9668 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009672 }
Paul Bakker48916f92012-09-16 19:57:18 +00009673 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009674
Hanno Becker90333da2017-10-10 11:27:13 +01009675 /* At this point, we don't know whether the renegotiation has been
9676 * completed or not. The cases to consider are the following:
9677 * 1) The renegotiation is complete. In this case, no new record
9678 * has been read yet.
9679 * 2) The renegotiation is incomplete because the client received
9680 * an application data record while awaiting the ServerHello.
9681 * 3) The renegotiation is incomplete because the client received
9682 * a non-handshake, non-application data message while awaiting
9683 * the ServerHello.
9684 * In each of these case, looping will be the proper action:
9685 * - For 1), the next iteration will read a new record and check
9686 * if it's application data.
9687 * - For 2), the loop condition isn't satisfied as application data
9688 * is present, hence continue is the same as break
9689 * - For 3), the loop condition is satisfied and read_record
9690 * will re-deliver the message that was held back by the client
9691 * when expecting the ServerHello.
9692 */
9693 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009694 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009695#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009697 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009698 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009699 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009700 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009703 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009705 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009706 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9711 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009714 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009715 }
9716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9720 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009721 }
9722
9723 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009724
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009725 /* We're going to return something now, cancel timer,
9726 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009728 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009729
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009731 /* If we requested renego but received AppData, resend HelloRequest.
9732 * Do it now, after setting in_offt, to avoid taking this branch
9733 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009735 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009737 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009738 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009741 return( ret );
9742 }
9743 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009745#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009746 }
9747
9748 n = ( len < ssl->in_msglen )
9749 ? len : ssl->in_msglen;
9750
9751 memcpy( buf, ssl->in_offt, n );
9752 ssl->in_msglen -= n;
9753
9754 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009755 {
9756 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009757 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009758 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009759 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009760 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009761 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009762 /* more data available */
9763 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009767
Paul Bakker23986e52011-04-24 08:57:21 +00009768 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009769}
9770
9771/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009772 * Send application data to be encrypted by the SSL layer, taking care of max
9773 * fragment length and buffer size.
9774 *
9775 * According to RFC 5246 Section 6.2.1:
9776 *
9777 * Zero-length fragments of Application data MAY be sent as they are
9778 * potentially useful as a traffic analysis countermeasure.
9779 *
9780 * Therefore, it is possible that the input message length is 0 and the
9781 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009782 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009783static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009784 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009785{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009786 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9787 const size_t max_len = (size_t) ret;
9788
9789 if( ret < 0 )
9790 {
9791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9792 return( ret );
9793 }
9794
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009795 if( len > max_len )
9796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009798 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009801 "maximum fragment length: %d > %d",
9802 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009804 }
9805 else
9806#endif
9807 len = max_len;
9808 }
Paul Bakker887bd502011-06-08 13:10:54 +00009809
Paul Bakker5121ce52009-01-03 21:22:43 +00009810 if( ssl->out_left != 0 )
9811 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009812 /*
9813 * The user has previously tried to send the data and
9814 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9815 * written. In this case, we expect the high-level write function
9816 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 return( ret );
9822 }
9823 }
Paul Bakker887bd502011-06-08 13:10:54 +00009824 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009825 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009826 /*
9827 * The user is trying to send a message the first time, so we need to
9828 * copy the data into the internal buffers and setup the data structure
9829 * to keep track of partial writes
9830 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009831 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009833 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009834
Hanno Becker67bc7c32018-08-06 11:33:50 +01009835 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009838 return( ret );
9839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009840 }
9841
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009842 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009843}
9844
9845/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009846 * Write application data, doing 1/n-1 splitting if necessary.
9847 *
9848 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009849 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009850 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009853static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009854 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009855{
9856 int ret;
9857
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009858 if( ssl->conf->cbc_record_splitting ==
9859 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009860 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9862 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9863 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009864 {
9865 return( ssl_write_real( ssl, buf, len ) );
9866 }
9867
9868 if( ssl->split_done == 0 )
9869 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009870 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009871 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009872 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009873 }
9874
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009875 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9876 return( ret );
9877 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009878
9879 return( ret + 1 );
9880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009882
9883/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009884 * Write application data (public-facing wrapper)
9885 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009886int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009887{
9888 int ret;
9889
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009891
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009892 if( ssl == NULL || ssl->conf == NULL )
9893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9894
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009895#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009896 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9897 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009898 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009899 return( ret );
9900 }
9901#endif
9902
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009903 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009904 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009905 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009906 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009908 return( ret );
9909 }
9910 }
9911
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009912#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009913 ret = ssl_write_split( ssl, buf, len );
9914#else
9915 ret = ssl_write_real( ssl, buf, len );
9916#endif
9917
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009919
9920 return( ret );
9921}
9922
9923/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009924 * Notify the peer that the connection is being closed
9925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009927{
9928 int ret;
9929
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009930 if( ssl == NULL || ssl->conf == NULL )
9931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009934
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009935 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9941 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9942 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009945 return( ret );
9946 }
9947 }
9948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009950
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009951 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009952}
9953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009955{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009956 if( transform == NULL )
9957 return;
9958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009960 deflateEnd( &transform->ctx_deflate );
9961 inflateEnd( &transform->ctx_inflate );
9962#endif
9963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9965 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009966
Hanno Becker92231322018-01-03 15:32:51 +00009967#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968 mbedtls_md_free( &transform->md_ctx_enc );
9969 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009970#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009971
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009972 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009973}
9974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975#if defined(MBEDTLS_X509_CRT_PARSE_C)
9976static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009977{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009979
9980 while( cur != NULL )
9981 {
9982 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009984 cur = next;
9985 }
9986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009988
Hanno Becker0271f962018-08-16 13:23:47 +01009989#if defined(MBEDTLS_SSL_PROTO_DTLS)
9990
9991static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9992{
9993 unsigned offset;
9994 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9995
9996 if( hs == NULL )
9997 return;
9998
Hanno Becker283f5ef2018-08-24 09:34:47 +01009999 ssl_free_buffered_record( ssl );
10000
Hanno Becker0271f962018-08-16 13:23:47 +010010001 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010002 ssl_buffering_free_slot( ssl, offset );
10003}
10004
10005static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10006 uint8_t slot )
10007{
10008 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10009 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010010
10011 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10012 return;
10013
Hanno Beckere605b192018-08-21 15:59:07 +010010014 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010015 {
Hanno Beckere605b192018-08-21 15:59:07 +010010016 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010017 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010018 mbedtls_free( hs_buf->data );
10019 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010020 }
10021}
10022
10023#endif /* MBEDTLS_SSL_PROTO_DTLS */
10024
Gilles Peskine9b562d52018-04-25 20:32:43 +020010025void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010026{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010027 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10028
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010029 if( handshake == NULL )
10030 return;
10031
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010032#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10033 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10034 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010035 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010036 handshake->async_in_progress = 0;
10037 }
10038#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10039
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010040#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10041 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10042 mbedtls_md5_free( &handshake->fin_md5 );
10043 mbedtls_sha1_free( &handshake->fin_sha1 );
10044#endif
10045#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10046#if defined(MBEDTLS_SHA256_C)
10047 mbedtls_sha256_free( &handshake->fin_sha256 );
10048#endif
10049#if defined(MBEDTLS_SHA512_C)
10050 mbedtls_sha512_free( &handshake->fin_sha512 );
10051#endif
10052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if defined(MBEDTLS_DHM_C)
10055 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057#if defined(MBEDTLS_ECDH_C)
10058 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010059#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010060#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010061 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010062#if defined(MBEDTLS_SSL_CLI_C)
10063 mbedtls_free( handshake->ecjpake_cache );
10064 handshake->ecjpake_cache = NULL;
10065 handshake->ecjpake_cache_len = 0;
10066#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010067#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010068
Janos Follath4ae5c292016-02-10 11:27:43 +000010069#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10070 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010071 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010073#endif
10074
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010075#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10076 if( handshake->psk != NULL )
10077 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010078 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010079 mbedtls_free( handshake->psk );
10080 }
10081#endif
10082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10084 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010085 /*
10086 * Free only the linked list wrapper, not the keys themselves
10087 * since the belong to the SNI callback
10088 */
10089 if( handshake->sni_key_cert != NULL )
10090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010092
10093 while( cur != NULL )
10094 {
10095 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010097 cur = next;
10098 }
10099 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010101
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010102#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010103 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010104#endif
10105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106#if defined(MBEDTLS_SSL_PROTO_DTLS)
10107 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010108 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010109 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010110#endif
10111
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010112 mbedtls_platform_zeroize( handshake,
10113 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010114}
10115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010117{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010118 if( session == NULL )
10119 return;
10120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000010122 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +000010123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 mbedtls_x509_crt_free( session->peer_cert );
10125 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +000010126 }
Paul Bakkered27a042013-04-18 22:46:23 +020010127#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010128
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010129#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010131#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010132
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010133 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010134}
10135
Paul Bakker5121ce52009-01-03 21:22:43 +000010136/*
10137 * Free an SSL context
10138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010140{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010141 if( ssl == NULL )
10142 return;
10143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010145
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010146 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010147 {
Angus Grattond8213d02016-05-25 20:56:48 +100010148 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010150 }
10151
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010152 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010153 {
Angus Grattond8213d02016-05-25 20:56:48 +100010154 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010156 }
10157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010158#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010159 if( ssl->compress_buf != NULL )
10160 {
Angus Grattond8213d02016-05-25 20:56:48 +100010161 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010163 }
10164#endif
10165
Paul Bakker48916f92012-09-16 19:57:18 +000010166 if( ssl->transform )
10167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168 mbedtls_ssl_transform_free( ssl->transform );
10169 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010170 }
10171
10172 if( ssl->handshake )
10173 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010174 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10176 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178 mbedtls_free( ssl->handshake );
10179 mbedtls_free( ssl->transform_negotiate );
10180 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010181 }
10182
Paul Bakkerc0463502013-02-14 11:19:38 +010010183 if( ssl->session )
10184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 mbedtls_ssl_session_free( ssl->session );
10186 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010187 }
10188
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010189#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010190 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010191 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010192 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010194 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010195#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10198 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10201 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010202 }
10203#endif
10204
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010205#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010207#endif
10208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010210
Paul Bakker86f04f42013-02-14 11:20:09 +010010211 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010212 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010213}
10214
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010215/*
10216 * Initialze mbedtls_ssl_config
10217 */
10218void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10219{
10220 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010221
10222#if !defined(MBEDTLS_SSL_PROTO_TLS)
10223 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10224#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010225}
10226
Simon Butcherc97b6972015-12-27 23:48:17 +000010227#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010228static int ssl_preset_default_hashes[] = {
10229#if defined(MBEDTLS_SHA512_C)
10230 MBEDTLS_MD_SHA512,
10231 MBEDTLS_MD_SHA384,
10232#endif
10233#if defined(MBEDTLS_SHA256_C)
10234 MBEDTLS_MD_SHA256,
10235 MBEDTLS_MD_SHA224,
10236#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010237#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010238 MBEDTLS_MD_SHA1,
10239#endif
10240 MBEDTLS_MD_NONE
10241};
Simon Butcherc97b6972015-12-27 23:48:17 +000010242#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010243
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010244static int ssl_preset_suiteb_ciphersuites[] = {
10245 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10246 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10247 0
10248};
10249
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010250#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010251static int ssl_preset_suiteb_hashes[] = {
10252 MBEDTLS_MD_SHA256,
10253 MBEDTLS_MD_SHA384,
10254 MBEDTLS_MD_NONE
10255};
10256#endif
10257
10258#if defined(MBEDTLS_ECP_C)
10259static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10260 MBEDTLS_ECP_DP_SECP256R1,
10261 MBEDTLS_ECP_DP_SECP384R1,
10262 MBEDTLS_ECP_DP_NONE
10263};
10264#endif
10265
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010266/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010267 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010268 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010269int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010270 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010271{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010272#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010273 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010274#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010275
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010276 /* Use the functions here so that they are covered in tests,
10277 * but otherwise access member directly for efficiency */
10278 mbedtls_ssl_conf_endpoint( conf, endpoint );
10279 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010280
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010281 /*
10282 * Things that are common to all presets
10283 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010284#if defined(MBEDTLS_SSL_CLI_C)
10285 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10286 {
10287 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10288#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10289 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10290#endif
10291 }
10292#endif
10293
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010294#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010295 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010296#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010297
10298#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10299 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10300#endif
10301
10302#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10303 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10304#endif
10305
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010306#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10307 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10308#endif
10309
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010310#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010311 conf->f_cookie_write = ssl_cookie_write_dummy;
10312 conf->f_cookie_check = ssl_cookie_check_dummy;
10313#endif
10314
10315#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10316 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10317#endif
10318
Janos Follath088ce432017-04-10 12:42:31 +010010319#if defined(MBEDTLS_SSL_SRV_C)
10320 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10321#endif
10322
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010323#if defined(MBEDTLS_SSL_PROTO_DTLS)
10324 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10325 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10326#endif
10327
10328#if defined(MBEDTLS_SSL_RENEGOTIATION)
10329 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010330 memset( conf->renego_period, 0x00, 2 );
10331 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010332#endif
10333
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010334#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10335 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10336 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010337 const unsigned char dhm_p[] =
10338 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10339 const unsigned char dhm_g[] =
10340 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10341
Hanno Beckera90658f2017-10-04 15:29:08 +010010342 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10343 dhm_p, sizeof( dhm_p ),
10344 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010345 {
10346 return( ret );
10347 }
10348 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010349#endif
10350
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010351 /*
10352 * Preset-specific defaults
10353 */
10354 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010355 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010356 /*
10357 * NSA Suite B
10358 */
10359 case MBEDTLS_SSL_PRESET_SUITEB:
10360 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10361 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10362 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10363 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10364
10365 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10366 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10367 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10368 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10369 ssl_preset_suiteb_ciphersuites;
10370
10371#if defined(MBEDTLS_X509_CRT_PARSE_C)
10372 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010373#endif
10374
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010375#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010376 conf->sig_hashes = ssl_preset_suiteb_hashes;
10377#endif
10378
10379#if defined(MBEDTLS_ECP_C)
10380 conf->curve_list = ssl_preset_suiteb_curves;
10381#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010382 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010383
10384 /*
10385 * Default
10386 */
10387 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010388 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10389 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10390 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10391 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10392 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10393 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10394 MBEDTLS_SSL_MIN_MINOR_VERSION :
10395 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010396 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10397 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10398
10399#if defined(MBEDTLS_SSL_PROTO_DTLS)
10400 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10401 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10402#endif
10403
10404 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10405 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10406 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10407 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10408 mbedtls_ssl_list_ciphersuites();
10409
10410#if defined(MBEDTLS_X509_CRT_PARSE_C)
10411 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10412#endif
10413
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010414#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010415 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010416#endif
10417
10418#if defined(MBEDTLS_ECP_C)
10419 conf->curve_list = mbedtls_ecp_grp_id_list();
10420#endif
10421
10422#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10423 conf->dhm_min_bitlen = 1024;
10424#endif
10425 }
10426
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010427 return( 0 );
10428}
10429
10430/*
10431 * Free mbedtls_ssl_config
10432 */
10433void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10434{
10435#if defined(MBEDTLS_DHM_C)
10436 mbedtls_mpi_free( &conf->dhm_P );
10437 mbedtls_mpi_free( &conf->dhm_G );
10438#endif
10439
10440#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10441 if( conf->psk != NULL )
10442 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010443 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010444 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010445 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010446 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010447 }
10448
10449 if( conf->psk_identity != NULL )
10450 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010451 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010452 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010453 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010454 conf->psk_identity_len = 0;
10455 }
10456#endif
10457
10458#if defined(MBEDTLS_X509_CRT_PARSE_C)
10459 ssl_key_cert_free( conf->key_cert );
10460#endif
10461
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010462 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010463}
10464
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010465#if defined(MBEDTLS_PK_C) && \
10466 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010467/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472#if defined(MBEDTLS_RSA_C)
10473 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10474 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476#if defined(MBEDTLS_ECDSA_C)
10477 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10478 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010481}
10482
Hanno Becker7e5437a2017-04-28 17:15:26 +010010483unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10484{
10485 switch( type ) {
10486 case MBEDTLS_PK_RSA:
10487 return( MBEDTLS_SSL_SIG_RSA );
10488 case MBEDTLS_PK_ECDSA:
10489 case MBEDTLS_PK_ECKEY:
10490 return( MBEDTLS_SSL_SIG_ECDSA );
10491 default:
10492 return( MBEDTLS_SSL_SIG_ANON );
10493 }
10494}
10495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010497{
10498 switch( sig )
10499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500#if defined(MBEDTLS_RSA_C)
10501 case MBEDTLS_SSL_SIG_RSA:
10502 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504#if defined(MBEDTLS_ECDSA_C)
10505 case MBEDTLS_SSL_SIG_ECDSA:
10506 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010507#endif
10508 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010510 }
10511}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010512#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010513
Hanno Becker7e5437a2017-04-28 17:15:26 +010010514#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10515 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10516
10517/* Find an entry in a signature-hash set matching a given hash algorithm. */
10518mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10519 mbedtls_pk_type_t sig_alg )
10520{
10521 switch( sig_alg )
10522 {
10523 case MBEDTLS_PK_RSA:
10524 return( set->rsa );
10525 case MBEDTLS_PK_ECDSA:
10526 return( set->ecdsa );
10527 default:
10528 return( MBEDTLS_MD_NONE );
10529 }
10530}
10531
10532/* Add a signature-hash-pair to a signature-hash set */
10533void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10534 mbedtls_pk_type_t sig_alg,
10535 mbedtls_md_type_t md_alg )
10536{
10537 switch( sig_alg )
10538 {
10539 case MBEDTLS_PK_RSA:
10540 if( set->rsa == MBEDTLS_MD_NONE )
10541 set->rsa = md_alg;
10542 break;
10543
10544 case MBEDTLS_PK_ECDSA:
10545 if( set->ecdsa == MBEDTLS_MD_NONE )
10546 set->ecdsa = md_alg;
10547 break;
10548
10549 default:
10550 break;
10551 }
10552}
10553
10554/* Allow exactly one hash algorithm for each signature. */
10555void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10556 mbedtls_md_type_t md_alg )
10557{
10558 set->rsa = md_alg;
10559 set->ecdsa = md_alg;
10560}
10561
10562#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10563 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10564
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010565/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010566 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010569{
10570 switch( hash )
10571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572#if defined(MBEDTLS_MD5_C)
10573 case MBEDTLS_SSL_HASH_MD5:
10574 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010575#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576#if defined(MBEDTLS_SHA1_C)
10577 case MBEDTLS_SSL_HASH_SHA1:
10578 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580#if defined(MBEDTLS_SHA256_C)
10581 case MBEDTLS_SSL_HASH_SHA224:
10582 return( MBEDTLS_MD_SHA224 );
10583 case MBEDTLS_SSL_HASH_SHA256:
10584 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586#if defined(MBEDTLS_SHA512_C)
10587 case MBEDTLS_SSL_HASH_SHA384:
10588 return( MBEDTLS_MD_SHA384 );
10589 case MBEDTLS_SSL_HASH_SHA512:
10590 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010591#endif
10592 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010594 }
10595}
10596
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010597/*
10598 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10599 */
10600unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10601{
10602 switch( md )
10603 {
10604#if defined(MBEDTLS_MD5_C)
10605 case MBEDTLS_MD_MD5:
10606 return( MBEDTLS_SSL_HASH_MD5 );
10607#endif
10608#if defined(MBEDTLS_SHA1_C)
10609 case MBEDTLS_MD_SHA1:
10610 return( MBEDTLS_SSL_HASH_SHA1 );
10611#endif
10612#if defined(MBEDTLS_SHA256_C)
10613 case MBEDTLS_MD_SHA224:
10614 return( MBEDTLS_SSL_HASH_SHA224 );
10615 case MBEDTLS_MD_SHA256:
10616 return( MBEDTLS_SSL_HASH_SHA256 );
10617#endif
10618#if defined(MBEDTLS_SHA512_C)
10619 case MBEDTLS_MD_SHA384:
10620 return( MBEDTLS_SSL_HASH_SHA384 );
10621 case MBEDTLS_MD_SHA512:
10622 return( MBEDTLS_SSL_HASH_SHA512 );
10623#endif
10624 default:
10625 return( MBEDTLS_SSL_HASH_NONE );
10626 }
10627}
10628
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010629#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010630/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010631 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010632 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010633 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010634int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010637
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010638 if( ssl->conf->curve_list == NULL )
10639 return( -1 );
10640
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010641 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010642 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010643 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010644
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010645 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010646}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010647#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010648
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010649#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010650/*
10651 * Check if a hash proposed by the peer is in our list.
10652 * Return 0 if we're willing to use it, -1 otherwise.
10653 */
10654int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10655 mbedtls_md_type_t md )
10656{
10657 const int *cur;
10658
10659 if( ssl->conf->sig_hashes == NULL )
10660 return( -1 );
10661
10662 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10663 if( *cur == (int) md )
10664 return( 0 );
10665
10666 return( -1 );
10667}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010668#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670#if defined(MBEDTLS_X509_CRT_PARSE_C)
10671int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10672 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010673 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010674 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010675{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010676 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010678 int usage = 0;
10679#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010681 const char *ext_oid;
10682 size_t ext_len;
10683#endif
10684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10686 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010687 ((void) cert);
10688 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010689 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010690#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10693 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010694 {
10695 /* Server part of the key exchange */
10696 switch( ciphersuite->key_exchange )
10697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 case MBEDTLS_KEY_EXCHANGE_RSA:
10699 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010700 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010701 break;
10702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10704 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10705 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10706 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010707 break;
10708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10710 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010711 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010712 break;
10713
10714 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010715 case MBEDTLS_KEY_EXCHANGE_NONE:
10716 case MBEDTLS_KEY_EXCHANGE_PSK:
10717 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10718 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010719 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010720 usage = 0;
10721 }
10722 }
10723 else
10724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10726 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010727 }
10728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010730 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010731 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010732 ret = -1;
10733 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010734#else
10735 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10739 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10742 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010743 }
10744 else
10745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10747 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010748 }
10749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010751 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010752 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010753 ret = -1;
10754 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010756
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010757 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010760
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010761/*
10762 * Convert version numbers to/from wire format
10763 * and, for DTLS, to/from TLS equivalent.
10764 *
10765 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010766 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010767 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10768 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010771 unsigned char ver[2] )
10772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773#if defined(MBEDTLS_SSL_PROTO_DTLS)
10774 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010777 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10778
10779 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10780 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10781 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010782 else
10783#else
10784 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010785#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010786 {
10787 ver[0] = (unsigned char) major;
10788 ver[1] = (unsigned char) minor;
10789 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010790}
10791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010793 const unsigned char ver[2] )
10794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#if defined(MBEDTLS_SSL_PROTO_DTLS)
10796 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010797 {
10798 *major = 255 - ver[0] + 2;
10799 *minor = 255 - ver[1] + 1;
10800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010802 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10803 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010804 else
10805#else
10806 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010807#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010808 {
10809 *major = ver[0];
10810 *minor = ver[1];
10811 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010812}
10813
Simon Butcher99000142016-10-13 17:21:01 +010010814int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10815{
10816#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10817 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10818 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10819
10820 switch( md )
10821 {
10822#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10823#if defined(MBEDTLS_MD5_C)
10824 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010825 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010826#endif
10827#if defined(MBEDTLS_SHA1_C)
10828 case MBEDTLS_SSL_HASH_SHA1:
10829 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10830 break;
10831#endif
10832#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10833#if defined(MBEDTLS_SHA512_C)
10834 case MBEDTLS_SSL_HASH_SHA384:
10835 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10836 break;
10837#endif
10838#if defined(MBEDTLS_SHA256_C)
10839 case MBEDTLS_SSL_HASH_SHA256:
10840 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10841 break;
10842#endif
10843 default:
10844 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10845 }
10846
10847 return 0;
10848#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10849 (void) ssl;
10850 (void) md;
10851
10852 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10854}
10855
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010856#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10857 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10858int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10859 unsigned char *output,
10860 unsigned char *data, size_t data_len )
10861{
10862 int ret = 0;
10863 mbedtls_md5_context mbedtls_md5;
10864 mbedtls_sha1_context mbedtls_sha1;
10865
10866 mbedtls_md5_init( &mbedtls_md5 );
10867 mbedtls_sha1_init( &mbedtls_sha1 );
10868
10869 /*
10870 * digitally-signed struct {
10871 * opaque md5_hash[16];
10872 * opaque sha_hash[20];
10873 * };
10874 *
10875 * md5_hash
10876 * MD5(ClientHello.random + ServerHello.random
10877 * + ServerParams);
10878 * sha_hash
10879 * SHA(ClientHello.random + ServerHello.random
10880 * + ServerParams);
10881 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010882 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010883 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010885 goto exit;
10886 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010887 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010888 ssl->handshake->randbytes, 64 ) ) != 0 )
10889 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010891 goto exit;
10892 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010893 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010894 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010896 goto exit;
10897 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010898 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010899 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010901 goto exit;
10902 }
10903
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010904 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010905 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010907 goto exit;
10908 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010909 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010910 ssl->handshake->randbytes, 64 ) ) != 0 )
10911 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010913 goto exit;
10914 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010915 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010916 data_len ) ) != 0 )
10917 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010919 goto exit;
10920 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010921 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010922 output + 16 ) ) != 0 )
10923 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010925 goto exit;
10926 }
10927
10928exit:
10929 mbedtls_md5_free( &mbedtls_md5 );
10930 mbedtls_sha1_free( &mbedtls_sha1 );
10931
10932 if( ret != 0 )
10933 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10934 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10935
10936 return( ret );
10937
10938}
10939#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10940 MBEDTLS_SSL_PROTO_TLS1_1 */
10941
10942#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10943 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10944int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010945 unsigned char *hash, size_t *hashlen,
10946 unsigned char *data, size_t data_len,
10947 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010948{
10949 int ret = 0;
10950 mbedtls_md_context_t ctx;
10951 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010952 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010953
10954 mbedtls_md_init( &ctx );
10955
10956 /*
10957 * digitally-signed struct {
10958 * opaque client_random[32];
10959 * opaque server_random[32];
10960 * ServerDHParams params;
10961 * };
10962 */
10963 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10964 {
10965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10966 goto exit;
10967 }
10968 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10969 {
10970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10971 goto exit;
10972 }
10973 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10974 {
10975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10976 goto exit;
10977 }
10978 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10979 {
10980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10981 goto exit;
10982 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010983 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010984 {
10985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10986 goto exit;
10987 }
10988
10989exit:
10990 mbedtls_md_free( &ctx );
10991
10992 if( ret != 0 )
10993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10994 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10995
10996 return( ret );
10997}
10998#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10999 MBEDTLS_SSL_PROTO_TLS1_2 */
11000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001#endif /* MBEDTLS_SSL_TLS_C */