blob: b975116e00ccb21b307c261f697cffcada9c3fc9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010060/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020064 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010066#else
67 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068#endif
69 return( 0 );
70}
71
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072/*
73 * Start a timer.
74 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_set_timer == NULL )
79 return;
80
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
82 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083}
84
85/*
86 * Return -1 is timer is expired, 0 if it isn't.
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020090 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020091 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092
93 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
99 return( 0 );
100}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
103 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
Hanno Beckera5a2b082019-05-15 14:03:01 +0100111#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100112/* Top-level Connection ID API */
113
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100114int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
115 size_t len,
116 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100117{
118 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
120
Hanno Becker791ec6b2019-05-14 11:45:26 +0100121 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
122 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
123 {
124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100128 conf->cid_len = len;
129 return( 0 );
130}
131
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100132int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
133 int enable,
134 unsigned char const *own_cid,
135 size_t own_cid_len )
136{
Hanno Becker78c43022019-05-03 14:38:32 +0100137 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
139
Hanno Becker07489862019-04-25 16:01:49 +0100140 ssl->negotiate_cid = enable;
141 if( enable == MBEDTLS_SSL_CID_DISABLED )
142 {
143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
144 return( 0 );
145 }
146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100147 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100148
Hanno Beckereec2be92019-05-03 13:06:44 +0100149 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100150 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
152 (unsigned) own_cid_len,
153 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
155 }
156
157 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100158 /* Truncation is not an issue here because
159 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
160 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100161
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100162 return( 0 );
163}
164
165int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
166 int *enabled,
167 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
168 size_t *peer_cid_len )
169{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100170 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100171
Hanno Becker78c43022019-05-03 14:38:32 +0100172 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
173 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
174 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100176 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Hanno Beckercb063f52019-05-03 12:54:52 +0100178 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
179 * were used, but client and server requested the empty CID.
180 * This is indistinguishable from not using the CID extension
181 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100182 if( ssl->transform_in->in_cid_len == 0 &&
183 ssl->transform_in->out_cid_len == 0 )
184 {
185 return( 0 );
186 }
187
Hanno Becker633d6042019-05-22 16:50:35 +0100188 if( peer_cid_len != NULL )
189 {
190 *peer_cid_len = ssl->transform_in->out_cid_len;
191 if( peer_cid != NULL )
192 {
193 memcpy( peer_cid, ssl->transform_in->out_cid,
194 ssl->transform_in->out_cid_len );
195 }
196 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100197
198 *enabled = MBEDTLS_SSL_CID_ENABLED;
199
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100200 return( 0 );
201}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100202#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100203
Hanno Beckerd5847772018-08-28 10:09:23 +0100204/* Forward declarations for functions related to message buffering. */
205static void ssl_buffering_free( mbedtls_ssl_context *ssl );
206static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
207 uint8_t slot );
208static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
209static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
210static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
211static int ssl_buffer_message( mbedtls_ssl_context *ssl );
212static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100213static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100214
Hanno Beckera67dee22018-08-22 10:05:20 +0100215static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100216static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100217{
Hanno Becker11682cc2018-08-22 14:41:02 +0100218 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100219
220 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100221 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100222
223 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
224}
225
Hanno Becker67bc7c32018-08-06 11:33:50 +0100226static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
227{
Hanno Becker11682cc2018-08-22 14:41:02 +0100228 size_t const bytes_written = ssl->out_left;
229 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100230
231 /* Double-check that the write-index hasn't gone
232 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100233 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100234 {
235 /* Should never happen... */
236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
237 }
238
239 return( (int) ( mtu - bytes_written ) );
240}
241
242static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
243{
244 int ret;
245 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400246 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100247
248#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
249 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
250
251 if( max_len > mfl )
252 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100253
254 /* By the standard (RFC 6066 Sect. 4), the MFL extension
255 * only limits the maximum record payload size, so in theory
256 * we would be allowed to pack multiple records of payload size
257 * MFL into a single datagram. However, this would mean that there's
258 * no way to explicitly communicate MTU restrictions to the peer.
259 *
260 * The following reduction of max_len makes sure that we never
261 * write datagrams larger than MFL + Record Expansion Overhead.
262 */
263 if( max_len <= ssl->out_left )
264 return( 0 );
265
266 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100267#endif
268
269 ret = ssl_get_remaining_space_in_datagram( ssl );
270 if( ret < 0 )
271 return( ret );
272 remaining = (size_t) ret;
273
274 ret = mbedtls_ssl_get_record_expansion( ssl );
275 if( ret < 0 )
276 return( ret );
277 expansion = (size_t) ret;
278
279 if( remaining <= expansion )
280 return( 0 );
281
282 remaining -= expansion;
283 if( remaining >= max_len )
284 remaining = max_len;
285
286 return( (int) remaining );
287}
288
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200289/*
290 * Double the retransmit timeout value, within the allowed range,
291 * returning -1 if the maximum value has already been reached.
292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200294{
295 uint32_t new_timeout;
296
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200297 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200298 return( -1 );
299
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200300 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
301 * in the following way: after the initial transmission and a first
302 * retransmission, back off to a temporary estimated MTU of 508 bytes.
303 * This value is guaranteed to be deliverable (if not guaranteed to be
304 * delivered) of any compliant IPv4 (and IPv6) network, and should work
305 * on most non-IP stacks too. */
306 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400307 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200308 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
310 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200311
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200312 new_timeout = 2 * ssl->handshake->retransmit_timeout;
313
314 /* Avoid arithmetic overflow and range overflow */
315 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200316 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200317 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200318 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319 }
320
321 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 ssl->handshake->retransmit_timeout ) );
324
325 return( 0 );
326}
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200330 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200332 ssl->handshake->retransmit_timeout ) );
333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200337/*
338 * Convert max_fragment_length codes to length.
339 * RFC 6066 says:
340 * enum{
341 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
342 * } MaxFragmentLength;
343 * and we add 0 -> extension unused
344 */
Angus Grattond8213d02016-05-25 20:56:48 +1000345static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200346{
Angus Grattond8213d02016-05-25 20:56:48 +1000347 switch( mfl )
348 {
349 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
350 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
351 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
352 return 512;
353 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
354 return 1024;
355 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
356 return 2048;
357 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
358 return 4096;
359 default:
360 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
361 }
362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200364
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200365#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_ssl_session_free( dst );
369 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372 if( src->peer_cert != NULL )
373 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200374 int ret;
375
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200376 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200378 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200383 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200386 dst->peer_cert = NULL;
387 return( ret );
388 }
389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200392#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200393 if( src->ticket != NULL )
394 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200395 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200396 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398
399 memcpy( dst->ticket, src->ticket, src->ticket_len );
400 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200401#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200402
403 return( 0 );
404}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200405#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
408int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200409 const unsigned char *key_enc, const unsigned char *key_dec,
410 size_t keylen,
411 const unsigned char *iv_enc, const unsigned char *iv_dec,
412 size_t ivlen,
413 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200414 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
416int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
417int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
420#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000421
Paul Bakker5121ce52009-01-03 21:22:43 +0000422/*
423 * Key material generation
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426static int ssl3_prf( const unsigned char *secret, size_t slen,
427 const char *label,
428 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000429 unsigned char *dstbuf, size_t dlen )
430{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100431 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000432 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200433 mbedtls_md5_context md5;
434 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char padding[16];
436 unsigned char sha1sum[20];
437 ((void)label);
438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_init( &md5 );
440 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200441
Paul Bakker5f70b252012-09-13 14:23:06 +0000442 /*
443 * SSLv3:
444 * block =
445 * MD5( secret + SHA1( 'A' + secret + random ) ) +
446 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
447 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
448 * ...
449 */
450 for( i = 0; i < dlen / 16; i++ )
451 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200452 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100456 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100457 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100458 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100459 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100467 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100468 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100470 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000473 }
474
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_free( &md5 );
477 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500479 mbedtls_platform_zeroize( padding, sizeof( padding ) );
480 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000481
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200487static int tls1_prf( const unsigned char *secret, size_t slen,
488 const char *label,
489 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000490 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 size_t nb, hs;
493 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200494 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 unsigned char tmp[128];
496 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 const mbedtls_md_info_t *md_info;
498 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100499 int ret;
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
506 hs = ( slen + 1 ) / 2;
507 S1 = secret;
508 S2 = secret + slen - hs;
509
510 nb = strlen( label );
511 memcpy( tmp + 20, label, nb );
512 memcpy( tmp + 20 + nb, random, rlen );
513 nb += rlen;
514
515 /*
516 * First compute P_md5(secret,label+random)[0..dlen]
517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100522 return( ret );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
525 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
526 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 for( i = 0; i < dlen; i += 16 )
529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_md_hmac_reset ( &md_ctx );
531 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
532 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_hmac_reset ( &md_ctx );
535 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
536 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
539
540 for( j = 0; j < k; j++ )
541 dstbuf[i + j] = h_i[j];
542 }
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 /*
547 * XOR out with P_sha1(secret,label+random)[0..dlen]
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100553 return( ret );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
556 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
557 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 for( i = 0; i < dlen; i += 20 )
560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_hmac_reset ( &md_ctx );
562 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
563 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_hmac_reset ( &md_ctx );
566 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
567 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
570
571 for( j = 0; j < k; j++ )
572 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
573 }
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500577 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
578 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 return( 0 );
581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
585static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100586 const unsigned char *secret, size_t slen,
587 const char *label,
588 const unsigned char *random, size_t rlen,
589 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000590{
591 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100592 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
595 const mbedtls_md_info_t *md_info;
596 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597 int ret;
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100605
606 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000608
609 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100610 memcpy( tmp + md_len, label, nb );
611 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612 nb += rlen;
613
614 /*
615 * Compute P_<hash>(secret, label + random)[0..dlen]
616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618 return( ret );
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
621 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
622 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100623
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100624 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_hmac_reset ( &md_ctx );
627 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
628 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_md_hmac_reset ( &md_ctx );
631 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
632 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100634 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
636 for( j = 0; j < k; j++ )
637 dstbuf[i + j] = h_i[j];
638 }
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100641
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500642 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
643 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000644
645 return( 0 );
646}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100649static int tls_prf_sha256( const unsigned char *secret, size_t slen,
650 const char *label,
651 const unsigned char *random, size_t rlen,
652 unsigned char *dstbuf, size_t dlen )
653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655 label, random, rlen, dstbuf, dlen ) );
656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660static int tls_prf_sha384( const unsigned char *secret, size_t slen,
661 const char *label,
662 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000663 unsigned char *dstbuf, size_t dlen )
664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100666 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_SHA512_C */
669#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
674 defined(MBEDTLS_SSL_PROTO_TLS1_1)
675static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200676#endif
Paul Bakker380da532012-04-18 16:10:25 +0000677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SSL_PROTO_SSL3)
679static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
680static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200681#endif
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
684static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
685static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200686#endif
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
689#if defined(MBEDTLS_SHA256_C)
690static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
691static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
692static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SHA512_C)
696static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
697static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
698static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000703{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200704 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 unsigned char keyblk[256];
707 unsigned char *key1;
708 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100709 unsigned char *mac_enc;
710 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000711 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200712 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000713 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000714 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 const mbedtls_cipher_info_t *cipher_info;
716 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_ssl_session *session = ssl->session_negotiate;
719 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
720 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Hanno Becker3307b532017-12-27 21:37:21 +0000724#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
725 transform->encrypt_then_mac = session->encrypt_then_mac;
726#endif
727 transform->minor_ver = ssl->minor_ver;
728
Hanno Becker8759e162017-12-27 21:34:08 +0000729 ciphersuite_info = handshake->ciphersuite_info;
730 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100731 if( cipher_info == NULL )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000734 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100736 }
737
Hanno Becker8759e162017-12-27 21:34:08 +0000738 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100739 if( md_info == NULL )
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000742 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100744 }
745
Hanno Beckera5a2b082019-05-15 14:03:01 +0100746#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100747 /* Copy own and peer's CID if the use of the CID
748 * extension has been negotiated. */
749 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
750 {
751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100752
Hanno Becker4932f9f2019-05-03 15:23:51 +0100753 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100754 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100755 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100756 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100757
758 transform->out_cid_len = ssl->handshake->peer_cid_len;
759 memcpy( transform->out_cid, ssl->handshake->peer_cid,
760 ssl->handshake->peer_cid_len );
761 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
762 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100763 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100764#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100765
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000767 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SSL_PROTO_SSL3)
770 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000771 {
Paul Bakker48916f92012-09-16 19:57:18 +0000772 handshake->tls_prf = ssl3_prf;
773 handshake->calc_verify = ssl_calc_verify_ssl;
774 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000775 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200776 else
777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
779 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000780 {
Paul Bakker48916f92012-09-16 19:57:18 +0000781 handshake->tls_prf = tls1_prf;
782 handshake->calc_verify = ssl_calc_verify_tls;
783 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000784 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200785 else
786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
788#if defined(MBEDTLS_SHA512_C)
789 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker8759e162017-12-27 21:34:08 +0000790 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000791 {
Paul Bakker48916f92012-09-16 19:57:18 +0000792 handshake->tls_prf = tls_prf_sha384;
793 handshake->calc_verify = ssl_calc_verify_tls_sha384;
794 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000795 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SHA256_C)
799 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800 {
Paul Bakker48916f92012-09-16 19:57:18 +0000801 handshake->tls_prf = tls_prf_sha256;
802 handshake->calc_verify = ssl_calc_verify_tls_sha256;
803 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000804 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805 else
806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200811 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812
813 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 * SSLv3:
815 * master =
816 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
817 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
818 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200819 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 * master = PRF( premaster, "master secret", randbytes )[0..47]
822 */
Paul Bakker0a597072012-09-25 21:55:46 +0000823 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000826 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
829 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200830 {
831 unsigned char session_hash[48];
832 size_t hash_len;
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200835
836 ssl->handshake->calc_verify( ssl, session_hash );
837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
839 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SHA512_C)
Hanno Becker8759e162017-12-27 21:34:08 +0000842 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200843 {
844 hash_len = 48;
845 }
846 else
847#endif
848 hash_len = 32;
849 }
850 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200852 hash_len = 36;
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200855
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100856 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
857 "extended master secret",
858 session_hash, hash_len,
859 session->master, 48 );
860 if( ret != 0 )
861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100863 return( ret );
864 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200865
866 }
867 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200868#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100869 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
870 "master secret",
871 handshake->randbytes, 64,
872 session->master, 48 );
873 if( ret != 0 )
874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100876 return( ret );
877 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200878
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500879 mbedtls_platform_zeroize( handshake->premaster,
880 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 }
882 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
885 /*
886 * Swap the client and server random values.
887 */
Paul Bakker48916f92012-09-16 19:57:18 +0000888 memcpy( tmp, handshake->randbytes, 64 );
889 memcpy( handshake->randbytes, tmp + 32, 32 );
890 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500891 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893 /*
894 * SSLv3:
895 * key block =
896 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
897 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
898 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
899 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
900 * ...
901 *
902 * TLSv1:
903 * key block = PRF( master, "key expansion", randbytes )
904 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100905 ret = handshake->tls_prf( session->master, 48, "key expansion",
906 handshake->randbytes, 64, keyblk, 256 );
907 if( ret != 0 )
908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100910 return( ret );
911 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
914 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
915 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
916 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
917 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500919 mbedtls_platform_zeroize( handshake->randbytes,
920 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
922 /*
923 * Determine the appropriate key, IV and MAC length.
924 */
Paul Bakker68884e32013-01-07 18:20:04 +0100925
Hanno Beckere7f2df02017-12-27 08:17:40 +0000926 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927
Hanno Beckerf1229442018-01-03 15:32:31 +0000928#if defined(MBEDTLS_GCM_C) || \
929 defined(MBEDTLS_CCM_C) || \
930 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200932 cipher_info->mode == MBEDTLS_MODE_CCM ||
933 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 {
Hanno Becker8759e162017-12-27 21:34:08 +0000935 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200936
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200937 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000938 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000939 transform->taglen =
940 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200941
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200942 /* All modes haves 96-bit IVs;
943 * GCM and CCM has 4 implicit and 8 explicit bytes
944 * ChachaPoly has all 12 bytes implicit
945 */
Paul Bakker68884e32013-01-07 18:20:04 +0100946 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200947 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
948 transform->fixed_ivlen = 12;
949 else
950 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200951
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200952 /* Minimum length of encrypted record */
953 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000954 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100955 }
956 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000957#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
958#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
959 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
960 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100961 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200962 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
964 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200967 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100968 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200970 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000971 mac_key_len = mbedtls_md_get_size( md_info );
972 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200975 /*
976 * If HMAC is to be truncated, we shall keep the leftmost bytes,
977 * (rfc 6066 page 13 or rfc 2104 section 4),
978 * so we only need to adjust the length here.
979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000983
984#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
985 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000986 * HMAC implementation which also truncates the key
987 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000988 mac_key_len = transform->maclen;
989#endif
990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200992
993 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100994 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200996 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200998 transform->minlen = transform->maclen;
999 else
Paul Bakker68884e32013-01-07 18:20:04 +01001000 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001001 /*
1002 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001003 * 1. if EtM is in use: one block plus MAC
1004 * otherwise: * first multiple of blocklen greater than maclen
1005 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1008 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001009 {
1010 transform->minlen = transform->maclen
1011 + cipher_info->block_size;
1012 }
1013 else
1014#endif
1015 {
1016 transform->minlen = transform->maclen
1017 + cipher_info->block_size
1018 - transform->maclen % cipher_info->block_size;
1019 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1022 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1023 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001024 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001025 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1028 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1029 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001030 {
1031 transform->minlen += transform->ivlen;
1032 }
1033 else
1034#endif
1035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001038 }
Paul Bakker68884e32013-01-07 18:20:04 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001041 else
1042#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1043 {
1044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Hanno Beckere7f2df02017-12-27 08:17:40 +00001048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1049 (unsigned) keylen,
1050 (unsigned) transform->minlen,
1051 (unsigned) transform->ivlen,
1052 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
1054 /*
1055 * Finally setup the cipher contexts, IVs and MAC secrets.
1056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001058 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001060 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001061 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Paul Bakker68884e32013-01-07 18:20:04 +01001063 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001064 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001066 /*
1067 * This is not used in TLS v1.1.
1068 */
Paul Bakker48916f92012-09-16 19:57:18 +00001069 iv_copy_len = ( transform->fixed_ivlen ) ?
1070 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001071 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1072 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001073 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 }
1075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#endif /* MBEDTLS_SSL_CLI_C */
1077#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001078 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001080 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001081 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Hanno Becker81c7b182017-11-09 18:39:33 +00001083 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001084 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001086 /*
1087 * This is not used in TLS v1.1.
1088 */
Paul Bakker48916f92012-09-16 19:57:18 +00001089 iv_copy_len = ( transform->fixed_ivlen ) ?
1090 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001091 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1092 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001093 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Hanno Becker92231322018-01-03 15:32:51 +00001102#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_SSL_PROTO_SSL3)
1104 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001105 {
Hanno Becker92231322018-01-03 15:32:51 +00001106 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001110 }
1111
Hanno Becker81c7b182017-11-09 18:39:33 +00001112 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1113 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001114 }
1115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1117#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1118 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1119 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001120 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001121 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1122 For AEAD-based ciphersuites, there is nothing to do here. */
1123 if( mac_key_len != 0 )
1124 {
1125 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1126 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1127 }
Paul Bakker68884e32013-01-07 18:20:04 +01001128 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001129 else
1130#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001134 }
Hanno Becker92231322018-01-03 15:32:51 +00001135#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1138 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001139 {
1140 int ret = 0;
1141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001143
Hanno Beckere7f2df02017-12-27 08:17:40 +00001144 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001145 transform->iv_enc, transform->iv_dec,
1146 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001147 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001148 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1151 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001152 }
1153 }
Hanno Becker92231322018-01-03 15:32:51 +00001154#else
1155 ((void) mac_dec);
1156 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001158
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001159#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1160 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001161 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001162 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1163 session->master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001164 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001165 iv_copy_len );
1166 }
1167#endif
1168
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001169 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001173 return( ret );
1174 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001175
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001176 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001177 cipher_info ) ) != 0 )
1178 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001180 return( ret );
1181 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001184 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001188 return( ret );
1189 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001192 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001196 return( ret );
1197 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_CIPHER_MODE_CBC)
1200 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1203 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001206 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001207 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1210 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001213 return( ret );
1214 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001218 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001221 // Initialize compression
1222 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001224 {
Paul Bakker16770332013-10-11 09:59:44 +02001225 if( ssl->compress_buf == NULL )
1226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001228 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001229 if( ssl->compress_buf == NULL )
1230 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001232 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001233 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001234 }
1235 }
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001238
Paul Bakker48916f92012-09-16 19:57:18 +00001239 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1240 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001241
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001242 if( deflateInit( &transform->ctx_deflate,
1243 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001244 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1247 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001248 }
1249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 return( 0 );
1255}
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_SSL_PROTO_SSL3)
1258void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001260 mbedtls_md5_context md5;
1261 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 unsigned char pad_1[48];
1263 unsigned char pad_2[48];
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001267 mbedtls_md5_init( &md5 );
1268 mbedtls_sha1_init( &sha1 );
1269
1270 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1271 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Paul Bakker380da532012-04-18 16:10:25 +00001273 memset( pad_1, 0x36, 48 );
1274 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001276 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1277 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1278 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001280 mbedtls_md5_starts_ret( &md5 );
1281 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1282 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1283 mbedtls_md5_update_ret( &md5, hash, 16 );
1284 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001286 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1287 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1288 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001289
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001290 mbedtls_sha1_starts_ret( &sha1 );
1291 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1292 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1293 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1294 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001298
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001299 mbedtls_md5_free( &md5 );
1300 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001301
Paul Bakker380da532012-04-18 16:10:25 +00001302 return;
1303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1307void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001308{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001309 mbedtls_md5_context md5;
1310 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001313
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001314 mbedtls_md5_init( &md5 );
1315 mbedtls_sha1_init( &sha1 );
1316
1317 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1318 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001319
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001320 mbedtls_md5_finish_ret( &md5, hash );
1321 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001325
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001326 mbedtls_md5_free( &md5 );
1327 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001328
Paul Bakker380da532012-04-18 16:10:25 +00001329 return;
1330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1334#if defined(MBEDTLS_SHA256_C)
1335void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001336{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001337 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001338
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001339 mbedtls_sha256_init( &sha256 );
1340
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001342
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001343 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001344 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001348
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001349 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001350
Paul Bakker380da532012-04-18 16:10:25 +00001351 return;
1352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_SHA512_C)
1356void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001357{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001358 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001359
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001360 mbedtls_sha512_init( &sha512 );
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001363
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001364 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001365 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001370 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001371
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 return;
1373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_SHA512_C */
1375#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1378int 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 +02001379{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001380 unsigned char *p = ssl->handshake->premaster;
1381 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001382 const unsigned char *psk = ssl->conf->psk;
1383 size_t psk_len = ssl->conf->psk_len;
1384
1385 /* If the psk callback was called, use its result */
1386 if( ssl->handshake->psk != NULL )
1387 {
1388 psk = ssl->handshake->psk;
1389 psk_len = ssl->handshake->psk_len;
1390 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001391
1392 /*
1393 * PMS = struct {
1394 * opaque other_secret<0..2^16-1>;
1395 * opaque psk<0..2^16-1>;
1396 * };
1397 * with "other_secret" depending on the particular key exchange
1398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1400 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001401 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001402 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001404
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001405 *(p++) = (unsigned char)( psk_len >> 8 );
1406 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001407
1408 if( end < p || (size_t)( end - p ) < psk_len )
1409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1410
1411 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001412 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001413 }
1414 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1416#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1417 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001418 {
1419 /*
1420 * other_secret already set by the ClientKeyExchange message,
1421 * and is 48 bytes long
1422 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001423 if( end - p < 2 )
1424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1425
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001426 *p++ = 0;
1427 *p++ = 48;
1428 p += 48;
1429 }
1430 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1432#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1433 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001434 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001435 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001436 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001437
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001438 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001440 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001441 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001444 return( ret );
1445 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001446 *(p++) = (unsigned char)( len >> 8 );
1447 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001448 p += len;
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001451 }
1452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1454#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1455 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001456 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001457 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001458 size_t zlen;
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001461 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001462 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001465 return( ret );
1466 }
1467
1468 *(p++) = (unsigned char)( zlen >> 8 );
1469 *(p++) = (unsigned char)( zlen );
1470 p += zlen;
1471
Janos Follath3fbdada2018-08-15 10:26:53 +01001472 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1473 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001474 }
1475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001480 }
1481
1482 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001483 if( end - p < 2 )
1484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001485
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001486 *(p++) = (unsigned char)( psk_len >> 8 );
1487 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001488
1489 if( end < p || (size_t)( end - p ) < psk_len )
1490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1491
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001492 memcpy( p, psk, psk_len );
1493 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001494
1495 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1496
1497 return( 0 );
1498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001502/*
1503 * SSLv3.0 MAC functions
1504 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001505#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001506static void ssl_mac( mbedtls_md_context_t *md_ctx,
1507 const unsigned char *secret,
1508 const unsigned char *buf, size_t len,
1509 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001510 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001511{
1512 unsigned char header[11];
1513 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001514 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1516 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001517
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001518 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001520 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001521 else
Paul Bakker68884e32013-01-07 18:20:04 +01001522 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
1524 memcpy( header, ctr, 8 );
1525 header[ 8] = (unsigned char) type;
1526 header[ 9] = (unsigned char)( len >> 8 );
1527 header[10] = (unsigned char)( len );
1528
Paul Bakker68884e32013-01-07 18:20:04 +01001529 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mbedtls_md_starts( md_ctx );
1531 mbedtls_md_update( md_ctx, secret, md_size );
1532 mbedtls_md_update( md_ctx, padding, padlen );
1533 mbedtls_md_update( md_ctx, header, 11 );
1534 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001535 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Paul Bakker68884e32013-01-07 18:20:04 +01001537 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_md_starts( md_ctx );
1539 mbedtls_md_update( md_ctx, secret, md_size );
1540 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001541 mbedtls_md_update( md_ctx, out, md_size );
1542 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001545
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001546/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001547 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001548#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001549 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1550 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1551 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1552/* This function makes sure every byte in the memory region is accessed
1553 * (in ascending addresses order) */
1554static void ssl_read_memory( unsigned char *p, size_t len )
1555{
1556 unsigned char acc = 0;
1557 volatile unsigned char force;
1558
1559 for( ; len != 0; p++, len-- )
1560 acc ^= *p;
1561
1562 force = acc;
1563 (void) force;
1564}
1565#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1566
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001567/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001569 */
Hanno Becker3307b532017-12-27 21:37:21 +00001570
Hanno Beckera5a2b082019-05-15 14:03:01 +01001571#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001572/* This functions transforms a DTLS plaintext fragment and a record content
1573 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001574 *
1575 * struct {
1576 * opaque content[DTLSPlaintext.length];
1577 * ContentType real_type;
1578 * uint8 zeros[length_of_padding];
1579 * } DTLSInnerPlaintext;
1580 *
1581 * Input:
1582 * - `content`: The beginning of the buffer holding the
1583 * plaintext to be wrapped.
1584 * - `*content_size`: The length of the plaintext in Bytes.
1585 * - `max_len`: The number of Bytes available starting from
1586 * `content`. This must be `>= *content_size`.
1587 * - `rec_type`: The desired record content type.
1588 *
1589 * Output:
1590 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1591 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1592 *
1593 * Returns:
1594 * - `0` on success.
1595 * - A negative error code if `max_len` didn't offer enough space
1596 * for the expansion.
1597 */
1598static int ssl_cid_build_inner_plaintext( unsigned char *content,
1599 size_t *content_size,
1600 size_t remaining,
1601 uint8_t rec_type )
1602{
1603 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001604 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1605 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1606 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001607
1608 /* Write real content type */
1609 if( remaining == 0 )
1610 return( -1 );
1611 content[ len ] = rec_type;
1612 len++;
1613 remaining--;
1614
1615 if( remaining < pad )
1616 return( -1 );
1617 memset( content + len, 0, pad );
1618 len += pad;
1619 remaining -= pad;
1620
1621 *content_size = len;
1622 return( 0 );
1623}
1624
Hanno Becker7dc25772019-05-20 15:08:01 +01001625/* This function parses a DTLSInnerPlaintext structure.
1626 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001627static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1628 size_t *content_size,
1629 uint8_t *rec_type )
1630{
1631 size_t remaining = *content_size;
1632
1633 /* Determine length of padding by skipping zeroes from the back. */
1634 do
1635 {
1636 if( remaining == 0 )
1637 return( -1 );
1638 remaining--;
1639 } while( content[ remaining ] == 0 );
1640
1641 *content_size = remaining;
1642 *rec_type = content[ remaining ];
1643
1644 return( 0 );
1645}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001646#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001647
Hanno Becker99abf512019-05-20 14:50:53 +01001648/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001649 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001650static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001651 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001652 mbedtls_record *rec )
1653{
Hanno Becker99abf512019-05-20 14:50:53 +01001654 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001655 *
1656 * additional_data = seq_num + TLSCompressed.type +
1657 * TLSCompressed.version + TLSCompressed.length;
1658 *
Hanno Becker99abf512019-05-20 14:50:53 +01001659 * For the CID extension, this is extended as follows
1660 * (quoting draft-ietf-tls-dtls-connection-id-05,
1661 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001662 *
1663 * additional_data = seq_num + DTLSPlaintext.type +
1664 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001665 * cid +
1666 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001667 * length_of_DTLSInnerPlaintext;
1668 */
1669
Hanno Becker3307b532017-12-27 21:37:21 +00001670 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1671 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001672 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001673
Hanno Beckera5a2b082019-05-15 14:03:01 +01001674#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001675 if( rec->cid_len != 0 )
1676 {
1677 memcpy( add_data + 11, rec->cid, rec->cid_len );
1678 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1679 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1680 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1681 *add_data_len = 13 + 1 + rec->cid_len;
1682 }
1683 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001684#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001685 {
1686 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1687 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1688 *add_data_len = 13;
1689 }
Hanno Becker3307b532017-12-27 21:37:21 +00001690}
1691
Hanno Becker611a83b2018-01-03 14:27:32 +00001692int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1693 mbedtls_ssl_transform *transform,
1694 mbedtls_record *rec,
1695 int (*f_rng)(void *, unsigned char *, size_t),
1696 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001699 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001700 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001701 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001702 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001703 size_t post_avail;
1704
1705 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001706#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker3307b532017-12-27 21:37:21 +00001707 ((void) ssl);
1708#endif
1709
1710 /* The PRNG is used for dynamic IV generation that's used
1711 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1712#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1713 ( defined(MBEDTLS_AES_C) || \
1714 defined(MBEDTLS_ARIA_C) || \
1715 defined(MBEDTLS_CAMELLIA_C) ) && \
1716 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1717 ((void) f_rng);
1718 ((void) p_rng);
1719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Hanno Becker3307b532017-12-27 21:37:21 +00001723 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001724 {
Hanno Becker3307b532017-12-27 21:37:21 +00001725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1727 }
Hanno Becker505089d2019-05-01 09:45:57 +01001728 if( rec == NULL
1729 || rec->buf == NULL
1730 || rec->buf_len < rec->data_offset
1731 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001732#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001733 || rec->cid_len != 0
1734#endif
1735 )
Hanno Becker3307b532017-12-27 21:37:21 +00001736 {
1737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001739 }
1740
Hanno Becker3307b532017-12-27 21:37:21 +00001741 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001742 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001744 data, rec->data_len );
1745
1746 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1747
1748 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1749 {
1750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1751 (unsigned) rec->data_len,
1752 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1754 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001755
Hanno Beckera5a2b082019-05-15 14:03:01 +01001756#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001757 /*
1758 * Add CID information
1759 */
1760 rec->cid_len = transform->out_cid_len;
1761 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1762 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001763
1764 if( rec->cid_len != 0 )
1765 {
1766 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001767 * Wrap plaintext into DTLSInnerPlaintext structure.
1768 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001769 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001770 * Note that this changes `rec->data_len`, and hence
1771 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001772 */
1773 if( ssl_cid_build_inner_plaintext( data,
1774 &rec->data_len,
1775 post_avail,
1776 rec->type ) != 0 )
1777 {
1778 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1779 }
1780
1781 rec->type = MBEDTLS_SSL_MSG_CID;
1782 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001783#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001784
Hanno Becker92c930f2019-04-29 17:31:37 +01001785 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1786
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001788 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001790#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 if( mode == MBEDTLS_MODE_STREAM ||
1792 ( mode == MBEDTLS_MODE_CBC
1793#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001794 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001795#endif
1796 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 {
Hanno Becker3307b532017-12-27 21:37:21 +00001798 if( post_avail < transform->maclen )
1799 {
1800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1801 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1802 }
1803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001805 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001807 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001808 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1809 data, rec->data_len, rec->ctr, rec->type, mac );
1810 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001811 }
1812 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1815 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001816 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001817 {
Hanno Becker992b6872017-11-09 18:57:39 +00001818 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1819
Hanno Beckere83efe62019-04-29 13:52:53 +01001820 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001821
Hanno Becker3307b532017-12-27 21:37:21 +00001822 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001823 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001824 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1825 data, rec->data_len );
1826 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1827 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1828
1829 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 }
1831 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001836 }
1837
Hanno Becker3307b532017-12-27 21:37:21 +00001838 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1839 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840
Hanno Becker3307b532017-12-27 21:37:21 +00001841 rec->data_len += transform->maclen;
1842 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001843 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001844 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001845#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 /*
1848 * Encrypt
1849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1851 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001853 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001854 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001856 "including %d bytes of padding",
1857 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Hanno Becker3307b532017-12-27 21:37:21 +00001859 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1860 transform->iv_enc, transform->ivlen,
1861 data, rec->data_len,
1862 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001865 return( ret );
1866 }
1867
Hanno Becker3307b532017-12-27 21:37:21 +00001868 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 }
Paul Bakker68884e32013-01-07 18:20:04 +01001874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00001876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001877#if defined(MBEDTLS_GCM_C) || \
1878 defined(MBEDTLS_CCM_C) || \
1879 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001881 mode == MBEDTLS_MODE_CCM ||
1882 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001883 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001884 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001885 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00001886 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001887
Hanno Becker3307b532017-12-27 21:37:21 +00001888 /* Check that there's space for both the authentication tag
1889 * and the explicit IV before and after the record content. */
1890 if( post_avail < transform->taglen ||
1891 rec->data_offset < explicit_iv_len )
1892 {
1893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1894 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1895 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001896
Paul Bakker68884e32013-01-07 18:20:04 +01001897 /*
1898 * Generate IV
1899 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001900 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1901 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001902 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001903 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00001904 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1905 explicit_iv_len );
1906 /* Prefix record content with explicit IV. */
1907 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 }
1909 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1910 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001911 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001912 unsigned char i;
1913
1914 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1915
1916 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001917 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 }
1919 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001920 {
1921 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1923 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001924 }
1925
Hanno Beckere83efe62019-04-29 13:52:53 +01001926 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01001927
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001928 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1929 iv, transform->ivlen );
1930 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00001931 data - explicit_iv_len, explicit_iv_len );
1932 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01001933 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001935 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00001936 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001937
Paul Bakker68884e32013-01-07 18:20:04 +01001938 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001939 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001940 */
Hanno Becker3307b532017-12-27 21:37:21 +00001941
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001942 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00001943 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01001944 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00001945 data, rec->data_len, /* source */
1946 data, &rec->data_len, /* destination */
1947 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001950 return( ret );
1951 }
1952
Hanno Becker3307b532017-12-27 21:37:21 +00001953 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1954 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001955
Hanno Becker3307b532017-12-27 21:37:21 +00001956 rec->data_len += transform->taglen + explicit_iv_len;
1957 rec->data_offset -= explicit_iv_len;
1958 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001959 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1963#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001964 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001967 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001968 size_t padlen, i;
1969 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001970
Hanno Becker3307b532017-12-27 21:37:21 +00001971 /* Currently we're always using minimal padding
1972 * (up to 255 bytes would be allowed). */
1973 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1974 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 padlen = 0;
1976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 /* Check there's enough space in the buffer for the padding. */
1978 if( post_avail < padlen + 1 )
1979 {
1980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1981 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1982 }
1983
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001985 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Hanno Becker3307b532017-12-27 21:37:21 +00001987 rec->data_len += padlen + 1;
1988 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001991 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001992 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1993 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 */
Hanno Becker3307b532017-12-27 21:37:21 +00001995 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 {
Hanno Becker3307b532017-12-27 21:37:21 +00001997 if( f_rng == NULL )
1998 {
1999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2000 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2001 }
2002
2003 if( rec->data_offset < transform->ivlen )
2004 {
2005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2006 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2007 }
2008
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009 /*
2010 * Generate IV
2011 */
Hanno Becker3307b532017-12-27 21:37:21 +00002012 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002013 if( ret != 0 )
2014 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002015
Hanno Becker3307b532017-12-27 21:37:21 +00002016 memcpy( data - transform->ivlen, transform->iv_enc,
2017 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002018
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002023 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002024 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002025 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
Hanno Becker3307b532017-12-27 21:37:21 +00002027 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2028 transform->iv_enc,
2029 transform->ivlen,
2030 data, rec->data_len,
2031 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002034 return( ret );
2035 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002036
Hanno Becker3307b532017-12-27 21:37:21 +00002037 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2040 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002041 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002044 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002045 {
2046 /*
2047 * Save IV in SSL3 and TLS1
2048 */
Hanno Becker3307b532017-12-27 21:37:21 +00002049 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2050 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 }
Hanno Becker3307b532017-12-27 21:37:21 +00002052 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002053#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002054 {
2055 data -= transform->ivlen;
2056 rec->data_offset -= transform->ivlen;
2057 rec->data_len += transform->ivlen;
2058 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002061 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002062 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002063 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2064
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002065 /*
2066 * MAC(MAC_write_key, seq_num +
2067 * TLSCipherText.type +
2068 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002069 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002070 * IV + // except for TLS 1.0
2071 * ENC(content + padding + padding_length));
2072 */
Hanno Becker3307b532017-12-27 21:37:21 +00002073
2074 if( post_avail < transform->maclen)
2075 {
2076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2077 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2078 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002079
Hanno Beckere83efe62019-04-29 13:52:53 +01002080 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002083 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002084 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002085
Hanno Becker3307b532017-12-27 21:37:21 +00002086 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002087 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002088 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2089 data, rec->data_len );
2090 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2091 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002092
Hanno Becker3307b532017-12-27 21:37:21 +00002093 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002094
Hanno Becker3307b532017-12-27 21:37:21 +00002095 rec->data_len += transform->maclen;
2096 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002097 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002098 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002103 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002109 /* Make extra sure authentication was performed, exactly once */
2110 if( auth_done != 1 )
2111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2113 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002114 }
2115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117
2118 return( 0 );
2119}
2120
Hanno Becker611a83b2018-01-03 14:27:32 +00002121int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2122 mbedtls_ssl_transform *transform,
2123 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002124{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002125 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002127 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002128#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002129 size_t padlen = 0, correct = 1;
2130#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002131 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002132 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002133 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002134
Hanno Becker611a83b2018-01-03 14:27:32 +00002135#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002136 ((void) ssl);
2137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002140 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002141 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2144 }
2145 if( rec == NULL ||
2146 rec->buf == NULL ||
2147 rec->buf_len < rec->data_offset ||
2148 rec->buf_len - rec->data_offset < rec->data_len )
2149 {
2150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002152 }
2153
Hanno Becker4c6876b2017-12-27 21:28:58 +00002154 data = rec->buf + rec->data_offset;
2155 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Hanno Beckera5a2b082019-05-15 14:03:01 +01002157#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002158 /*
2159 * Match record's CID with incoming CID.
2160 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002161 if( rec->cid_len != transform->in_cid_len ||
2162 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2163 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002164 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002165 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2169 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002170 {
2171 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002172 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2173 transform->iv_dec,
2174 transform->ivlen,
2175 data, rec->data_len,
2176 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002179 return( ret );
2180 }
2181
Hanno Becker4c6876b2017-12-27 21:28:58 +00002182 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002186 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
Paul Bakker68884e32013-01-07 18:20:04 +01002188 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002190#if defined(MBEDTLS_GCM_C) || \
2191 defined(MBEDTLS_CCM_C) || \
2192 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002194 mode == MBEDTLS_MODE_CCM ||
2195 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002196 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002197 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002198 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002199
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002200 /*
2201 * Compute and update sizes
2202 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002203 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002206 "+ taglen (%d)", rec->data_len,
2207 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002209 }
Paul Bakker68884e32013-01-07 18:20:04 +01002210
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002211 /*
2212 * Prepare IV
2213 */
2214 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2215 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002216 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002217 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002218 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002219
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002220 }
2221 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2222 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002223 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002224 unsigned char i;
2225
2226 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2227
2228 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002229 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002230 }
2231 else
2232 {
2233 /* Reminder if we ever add an AEAD mode with a different size */
2234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2236 }
2237
Hanno Becker4c6876b2017-12-27 21:28:58 +00002238 data += explicit_iv_len;
2239 rec->data_offset += explicit_iv_len;
2240 rec->data_len -= explicit_iv_len + transform->taglen;
2241
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002243 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002244 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002245
2246 memcpy( transform->iv_dec + transform->fixed_ivlen,
2247 data - explicit_iv_len, explicit_iv_len );
2248
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002249 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002250 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002251 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002252
Paul Bakker68884e32013-01-07 18:20:04 +01002253
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002254 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002255 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002256 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002257 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2258 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002259 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002260 data, rec->data_len,
2261 data, &olen,
2262 data + rec->data_len,
2263 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2268 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002269
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002270 return( ret );
2271 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002272 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002273
Hanno Becker4c6876b2017-12-27 21:28:58 +00002274 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002278 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2282#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002283 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002286 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002287
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 /*
Paul Bakker45829992013-01-03 14:52:21 +01002289 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2293 {
2294 /* The ciphertext is prefixed with the CBC IV. */
2295 minlen += transform->ivlen;
2296 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002297#endif
Paul Bakker45829992013-01-03 14:52:21 +01002298
Hanno Becker4c6876b2017-12-27 21:28:58 +00002299 /* Size considerations:
2300 *
2301 * - The CBC cipher text must not be empty and hence
2302 * at least of size transform->ivlen.
2303 *
2304 * Together with the potential IV-prefix, this explains
2305 * the first of the two checks below.
2306 *
2307 * - The record must contain a MAC, either in plain or
2308 * encrypted, depending on whether Encrypt-then-MAC
2309 * is used or not.
2310 * - If it is, the message contains the IV-prefix,
2311 * the CBC ciphertext, and the MAC.
2312 * - If it is not, the padded plaintext, and hence
2313 * the CBC ciphertext, has at least length maclen + 1
2314 * because there is at least the padding length byte.
2315 *
2316 * As the CBC ciphertext is not empty, both cases give the
2317 * lower bound minlen + maclen + 1 on the record size, which
2318 * we test for in the second check below.
2319 */
2320 if( rec->data_len < minlen + transform->ivlen ||
2321 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002324 "+ 1 ) ( + expl IV )", rec->data_len,
2325 transform->ivlen,
2326 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002328 }
2329
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002330 /*
2331 * Authenticate before decrypt if enabled
2332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002334 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002335 {
Hanno Becker992b6872017-11-09 18:57:39 +00002336 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002339
Hanno Becker4c6876b2017-12-27 21:28:58 +00002340 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2341 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002342
Hanno Beckere83efe62019-04-29 13:52:53 +01002343 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002344
Hanno Beckere83efe62019-04-29 13:52:53 +01002345 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2346 add_data_len );
2347 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2348 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002349 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2350 data, rec->data_len );
2351 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2352 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002353
Hanno Becker4c6876b2017-12-27 21:28:58 +00002354 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2355 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002356 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002357 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002358
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2360 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002364 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002365 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002368
2369 /*
2370 * Check length sanity
2371 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002372 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002375 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002377 }
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002380 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002381 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002382 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002383 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002384 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385 /* This is safe because data_len >= minlen + maclen + 1 initially,
2386 * and at this point we have at most subtracted maclen (note that
2387 * minlen == transform->ivlen here). */
2388 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002389
Hanno Becker4c6876b2017-12-27 21:28:58 +00002390 data += transform->ivlen;
2391 rec->data_offset += transform->ivlen;
2392 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002393 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002395
Hanno Becker4c6876b2017-12-27 21:28:58 +00002396 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2397 transform->iv_dec, transform->ivlen,
2398 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002401 return( ret );
2402 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002403
Hanno Becker4c6876b2017-12-27 21:28:58 +00002404 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002408 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002411 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002412 {
2413 /*
2414 * Save IV in SSL3 and TLS1
2415 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002416 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2417 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002419#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Hanno Becker4c6876b2017-12-27 21:28:58 +00002421 /* Safe since data_len >= minlen + maclen + 1, so after having
2422 * subtracted at most minlen and maclen up to this point,
2423 * data_len > 0. */
2424 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002425
Hanno Becker4c6876b2017-12-27 21:28:58 +00002426 if( auth_done == 1 )
2427 {
2428 correct *= ( rec->data_len >= padlen + 1 );
2429 padlen *= ( rec->data_len >= padlen + 1 );
2430 }
2431 else
Paul Bakker45829992013-01-03 14:52:21 +01002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002434 if( rec->data_len < transform->maclen + padlen + 1 )
2435 {
2436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2437 rec->data_len,
2438 transform->maclen,
2439 padlen + 1 ) );
2440 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002441#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002442
2443 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2444 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Hanno Becker4c6876b2017-12-27 21:28:58 +00002447 padlen++;
2448
2449 /* Regardless of the validity of the padding,
2450 * we have data_len >= padlen here. */
2451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002453 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#if defined(MBEDTLS_SSL_DEBUG_ALL)
2458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002459 "should be no more than %d",
2460 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002461#endif
Paul Bakker45829992013-01-03 14:52:21 +01002462 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 }
2464 }
2465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2467#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2468 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002469 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002471 /* The padding check involves a series of up to 256
2472 * consecutive memory reads at the end of the record
2473 * plaintext buffer. In order to hide the length and
2474 * validity of the padding, always perform exactly
2475 * `min(256,plaintext_len)` reads (but take into account
2476 * only the last `padlen` bytes for the padding check). */
2477 size_t pad_count = 0;
2478 size_t real_count = 0;
2479 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002480
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 /* Index of first padding byte; it has been ensured above
2482 * that the subtraction is safe. */
2483 size_t const padding_idx = rec->data_len - padlen;
2484 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2485 size_t const start_idx = rec->data_len - num_checks;
2486 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002487
Hanno Becker4c6876b2017-12-27 21:28:58 +00002488 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002489 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 real_count |= ( idx >= padding_idx );
2491 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002492 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002493 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002496 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002498#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002499 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002501 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2503 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002507 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002508
Hanno Becker4c6876b2017-12-27 21:28:58 +00002509 /* If the padding was found to be invalid, padlen == 0
2510 * and the subtraction is safe. If the padding was found valid,
2511 * padlen hasn't been changed and the previous assertion
2512 * data_len >= padlen still holds. */
2513 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002517 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002523#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002525 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 * Authenticate if not done yet.
2530 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002532#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002533 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002534 {
Hanno Becker992b6872017-11-09 18:57:39 +00002535 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002536
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 /* If the initial value of padlen was such that
2538 * data_len < maclen + padlen + 1, then padlen
2539 * got reset to 1, and the initial check
2540 * data_len >= minlen + maclen + 1
2541 * guarantees that at this point we still
2542 * have at least data_len >= maclen.
2543 *
2544 * If the initial value of padlen was such that
2545 * data_len >= maclen + padlen + 1, then we have
2546 * subtracted either padlen + 1 (if the padding was correct)
2547 * or 0 (if the padding was incorrect) since then,
2548 * hence data_len >= maclen in any case.
2549 */
2550 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Hanno Beckere83efe62019-04-29 13:52:53 +01002552 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002555 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002556 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 ssl_mac( &transform->md_ctx_dec,
2558 transform->mac_dec,
2559 data, rec->data_len,
2560 rec->ctr, rec->type,
2561 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002562 }
2563 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2565#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2566 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002568 {
2569 /*
2570 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002571 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002572 *
2573 * Known timing attacks:
2574 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2575 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002576 * To compensate for different timings for the MAC calculation
2577 * depending on how much padding was removed (which is determined
2578 * by padlen), process extra_run more blocks through the hash
2579 * function.
2580 *
2581 * The formula in the paper is
2582 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2583 * where L1 is the size of the header plus the decrypted message
2584 * plus CBC padding and L2 is the size of the header plus the
2585 * decrypted message. This is for an underlying hash function
2586 * with 64-byte blocks.
2587 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2588 * correctly. We round down instead of up, so -56 is the correct
2589 * value for our calculations instead of -55.
2590 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002591 * Repeat the formula rather than defining a block_size variable.
2592 * This avoids requiring division by a variable at runtime
2593 * (which would be marginally less efficient and would require
2594 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002595 */
2596 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002598
2599 /*
2600 * The next two sizes are the minimum and maximum values of
2601 * in_msglen over all padlen values.
2602 *
2603 * They're independent of padlen, since we previously did
2604 * in_msglen -= padlen.
2605 *
2606 * Note that max_len + maclen is never more than the buffer
2607 * length, as we previously did in_msglen -= maclen too.
2608 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002610 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2611
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 memset( tmp, 0, sizeof( tmp ) );
2613
2614 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002615 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002616#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2617 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002618 case MBEDTLS_MD_MD5:
2619 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002620 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002621 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002622 extra_run =
2623 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2624 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002625 break;
2626#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002627#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002628 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002629 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002630 extra_run =
2631 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2632 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002633 break;
2634#endif
2635 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2638 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002639
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002640 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002641
Hanno Beckere83efe62019-04-29 13:52:53 +01002642 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2643 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2645 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002646 /* Make sure we access everything even when padlen > 0. This
2647 * makes the synchronisation requirements for just-in-time
2648 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 ssl_read_memory( data + rec->data_len, padlen );
2650 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002651
2652 /* Call mbedtls_md_process at least once due to cache attacks
2653 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002654 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002655 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002656
Hanno Becker4c6876b2017-12-27 21:28:58 +00002657 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002658
2659 /* Make sure we access all the memory that could contain the MAC,
2660 * before we check it in the next code block. This makes the
2661 * synchronisation requirements for just-in-time Prime+Probe
2662 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002663 ssl_read_memory( data + min_len,
2664 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002665 }
2666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2668 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002672 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002674#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002675 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2676 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002677#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Hanno Becker4c6876b2017-12-27 21:28:58 +00002679 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2680 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682#if defined(MBEDTLS_SSL_DEBUG_ALL)
2683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002684#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002685 correct = 0;
2686 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002687 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002688 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002689
2690 /*
2691 * Finally check the correct flag
2692 */
2693 if( correct == 0 )
2694 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002695#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002696
2697 /* Make extra sure authentication was performed, exactly once */
2698 if( auth_done != 1 )
2699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002702 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Hanno Beckera5a2b082019-05-15 14:03:01 +01002704#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002705 if( rec->cid_len != 0 )
2706 {
2707 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2708 &rec->type );
2709 if( ret != 0 )
2710 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2711 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002712#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716 return( 0 );
2717}
2718
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002719#undef MAC_NONE
2720#undef MAC_PLAINTEXT
2721#undef MAC_CIPHERTEXT
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724/*
2725 * Compression/decompression functions
2726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728{
2729 int ret;
2730 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002731 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002733 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002736
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002737 if( len_pre == 0 )
2738 return( 0 );
2739
Paul Bakker2770fbd2012-07-03 13:30:23 +00002740 memcpy( msg_pre, ssl->out_msg, len_pre );
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002743 ssl->out_msglen ) );
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002746 ssl->out_msg, ssl->out_msglen );
2747
Paul Bakker48916f92012-09-16 19:57:18 +00002748 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2749 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2750 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002751 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002752
Paul Bakker48916f92012-09-16 19:57:18 +00002753 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002754 if( ret != Z_OK )
2755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2757 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002758 }
2759
Angus Grattond8213d02016-05-25 20:56:48 +10002760 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002761 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002764 ssl->out_msglen ) );
2765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002767 ssl->out_msg, ssl->out_msglen );
2768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002770
2771 return( 0 );
2772}
2773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002775{
2776 int ret;
2777 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002778 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002779 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002780 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002783
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002784 if( len_pre == 0 )
2785 return( 0 );
2786
Paul Bakker2770fbd2012-07-03 13:30:23 +00002787 memcpy( msg_pre, ssl->in_msg, len_pre );
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002790 ssl->in_msglen ) );
2791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002793 ssl->in_msg, ssl->in_msglen );
2794
Paul Bakker48916f92012-09-16 19:57:18 +00002795 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2796 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2797 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002798 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002799 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002800
Paul Bakker48916f92012-09-16 19:57:18 +00002801 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802 if( ret != Z_OK )
2803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2805 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806 }
2807
Angus Grattond8213d02016-05-25 20:56:48 +10002808 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002809 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002812 ssl->in_msglen ) );
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002815 ssl->in_msg, ssl->in_msglen );
2816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002818
2819 return( 0 );
2820}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2824static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#if defined(MBEDTLS_SSL_PROTO_DTLS)
2827static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002828{
2829 /* If renegotiation is not enforced, retransmit until we would reach max
2830 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002831 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002832 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002833 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002834 unsigned char doublings = 1;
2835
2836 while( ratio != 0 )
2837 {
2838 ++doublings;
2839 ratio >>= 1;
2840 }
2841
2842 if( ++ssl->renego_records_seen > doublings )
2843 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002845 return( 0 );
2846 }
2847 }
2848
2849 return( ssl_write_hello_request( ssl ) );
2850}
2851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002853
Paul Bakker5121ce52009-01-03 21:22:43 +00002854/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002855 * Fill the input message buffer by appending data to it.
2856 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002857 *
2858 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2859 * available (from this read and/or a previous one). Otherwise, an error code
2860 * is returned (possibly EOF or WANT_READ).
2861 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002862 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2863 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2864 * since we always read a whole datagram at once.
2865 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002866 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002867 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002870{
Paul Bakker23986e52011-04-24 08:57:21 +00002871 int ret;
2872 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002876 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002879 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002881 }
2882
Angus Grattond8213d02016-05-25 20:56:48 +10002883 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002887 }
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002890 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002892 uint32_t timeout;
2893
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002894 /* Just to be sure */
2895 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2896 {
2897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2898 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2900 }
2901
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002902 /*
2903 * The point is, we need to always read a full datagram at once, so we
2904 * sometimes read more then requested, and handle the additional data.
2905 * It could be the rest of the current record (while fetching the
2906 * header) and/or some other records in the same datagram.
2907 */
2908
2909 /*
2910 * Move to the next record in the already read datagram if applicable
2911 */
2912 if( ssl->next_record_offset != 0 )
2913 {
2914 if( ssl->in_left < ssl->next_record_offset )
2915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002918 }
2919
2920 ssl->in_left -= ssl->next_record_offset;
2921
2922 if( ssl->in_left != 0 )
2923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002925 ssl->next_record_offset ) );
2926 memmove( ssl->in_hdr,
2927 ssl->in_hdr + ssl->next_record_offset,
2928 ssl->in_left );
2929 }
2930
2931 ssl->next_record_offset = 0;
2932 }
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002936
2937 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002938 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002939 */
2940 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002943 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002944 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002945
2946 /*
2947 * A record can't be split accross datagrams. If we need to read but
2948 * are not at the beginning of a new record, the caller did something
2949 * wrong.
2950 */
2951 if( ssl->in_left != 0 )
2952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002955 }
2956
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002957 /*
2958 * Don't even try to read if time's out already.
2959 * This avoids by-passing the timer when repeatedly receiving messages
2960 * that will end up being dropped.
2961 */
2962 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002963 {
2964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002965 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002966 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002967 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002968 {
Angus Grattond8213d02016-05-25 20:56:48 +10002969 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002972 timeout = ssl->handshake->retransmit_timeout;
2973 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002974 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002977
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002978 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002979 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2980 timeout );
2981 else
2982 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002985
2986 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002988 }
2989
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002990 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002993 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002996 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002997 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003000 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003001 }
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003006 return( ret );
3007 }
3008
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003009 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003010 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003012 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003014 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003015 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003018 return( ret );
3019 }
3020
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003021 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003024 }
3025
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 if( ret < 0 )
3027 return( ret );
3028
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003029 ssl->in_left = ret;
3030 }
3031 else
3032#endif
3033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003035 ssl->in_left, nb_want ) );
3036
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003037 while( ssl->in_left < nb_want )
3038 {
3039 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003040
3041 if( ssl_check_timer( ssl ) != 0 )
3042 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3043 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003044 {
3045 if( ssl->f_recv_timeout != NULL )
3046 {
3047 ret = ssl->f_recv_timeout( ssl->p_bio,
3048 ssl->in_hdr + ssl->in_left, len,
3049 ssl->conf->read_timeout );
3050 }
3051 else
3052 {
3053 ret = ssl->f_recv( ssl->p_bio,
3054 ssl->in_hdr + ssl->in_left, len );
3055 }
3056 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003059 ssl->in_left, nb_want ) );
3060 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003061
3062 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003064
3065 if( ret < 0 )
3066 return( ret );
3067
mohammad160352aecb92018-03-28 23:41:40 -07003068 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003069 {
Darryl Green11999bb2018-03-13 15:22:58 +00003070 MBEDTLS_SSL_DEBUG_MSG( 1,
3071 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003072 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3074 }
3075
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003076 ssl->in_left += ret;
3077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 }
3079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
3082 return( 0 );
3083}
3084
3085/*
3086 * Flush any data not yet written
3087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003090 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003091 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003095 if( ssl->f_send == NULL )
3096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003098 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003100 }
3101
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003102 /* Avoid incrementing counter if data is flushed */
3103 if( ssl->out_left == 0 )
3104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003106 return( 0 );
3107 }
3108
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 while( ssl->out_left > 0 )
3110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003112 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Hanno Becker2b1e3542018-08-06 11:19:13 +01003114 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003115 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
3119 if( ret <= 0 )
3120 return( ret );
3121
mohammad160352aecb92018-03-28 23:41:40 -07003122 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003123 {
Darryl Green11999bb2018-03-13 15:22:58 +00003124 MBEDTLS_SSL_DEBUG_MSG( 1,
3125 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003126 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3128 }
3129
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 ssl->out_left -= ret;
3131 }
3132
Hanno Becker2b1e3542018-08-06 11:19:13 +01003133#if defined(MBEDTLS_SSL_PROTO_DTLS)
3134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003135 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003136 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003137 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003138 else
3139#endif
3140 {
3141 ssl->out_hdr = ssl->out_buf + 8;
3142 }
3143 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
3147 return( 0 );
3148}
3149
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003150/*
3151 * Functions to handle the DTLS retransmission state machine
3152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154/*
3155 * Append current handshake message to current outgoing flight
3156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3161 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3162 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163
3164 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003165 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003166 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003169 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170 }
3171
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003172 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003176 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003177 }
3178
3179 /* Copy current handshake message with headers */
3180 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3181 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003182 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003183 msg->next = NULL;
3184
3185 /* Append to the current flight */
3186 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003187 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003188 else
3189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191 while( cur->next != NULL )
3192 cur = cur->next;
3193 cur->next = msg;
3194 }
3195
Hanno Becker3b235902018-08-06 09:54:53 +01003196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003197 return( 0 );
3198}
3199
3200/*
3201 * Free the current flight of handshake messages
3202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 mbedtls_ssl_flight_item *cur = flight;
3206 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003207
3208 while( cur != NULL )
3209 {
3210 next = cur->next;
3211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212 mbedtls_free( cur->p );
3213 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003214
3215 cur = next;
3216 }
3217}
3218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3220static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003221#endif
3222
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003223/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003224 * Swap transform_out and out_ctr with the alternative ones
3225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003229 unsigned char tmp_out_ctr[8];
3230
3231 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003234 return;
3235 }
3236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003238
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003239 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003240 tmp_transform = ssl->transform_out;
3241 ssl->transform_out = ssl->handshake->alt_transform_out;
3242 ssl->handshake->alt_transform_out = tmp_transform;
3243
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003244 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003245 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3246 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003247 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003248
3249 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003250 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3253 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3258 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003259 }
3260 }
3261#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003262}
3263
3264/*
3265 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003266 */
3267int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3268{
3269 int ret = 0;
3270
3271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3272
3273 ret = mbedtls_ssl_flight_transmit( ssl );
3274
3275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3276
3277 return( ret );
3278}
3279
3280/*
3281 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003282 *
3283 * Need to remember the current message in case flush_output returns
3284 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003285 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003286 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003287int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003288{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003289 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003293 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003295
3296 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003297 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003298 ssl_swap_epochs( ssl );
3299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003301 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003302
3303 while( ssl->handshake->cur_msg != NULL )
3304 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003305 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003306 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003307
Hanno Beckere1dcb032018-08-17 16:47:58 +01003308 int const is_finished =
3309 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3310 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3311
Hanno Becker04da1892018-08-14 13:22:10 +01003312 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3313 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3314
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003315 /* Swap epochs before sending Finished: we can't do it after
3316 * sending ChangeCipherSpec, in case write returns WANT_READ.
3317 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003318 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003319 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003321 ssl_swap_epochs( ssl );
3322 }
3323
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 ret = ssl_get_remaining_payload_in_datagram( ssl );
3325 if( ret < 0 )
3326 return( ret );
3327 max_frag_len = (size_t) ret;
3328
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003329 /* CCS is copied as is, while HS messages may need fragmentation */
3330 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3331 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003332 if( max_frag_len == 0 )
3333 {
3334 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3335 return( ret );
3336
3337 continue;
3338 }
3339
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003340 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003341 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003342 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003344 /* Update position inside current message */
3345 ssl->handshake->cur_msg_p += cur->len;
3346 }
3347 else
3348 {
3349 const unsigned char * const p = ssl->handshake->cur_msg_p;
3350 const size_t hs_len = cur->len - 12;
3351 const size_t frag_off = p - ( cur->p + 12 );
3352 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003353 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003354
Hanno Beckere1dcb032018-08-17 16:47:58 +01003355 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003356 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003357 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003358 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359
Hanno Becker67bc7c32018-08-06 11:33:50 +01003360 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3361 return( ret );
3362
3363 continue;
3364 }
3365 max_hs_frag_len = max_frag_len - 12;
3366
3367 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3368 max_hs_frag_len : rem_len;
3369
3370 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003371 {
3372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003373 (unsigned) cur_hs_frag_len,
3374 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003375 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003376
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003377 /* Messages are stored with handshake headers as if not fragmented,
3378 * copy beginning of headers then fill fragmentation fields.
3379 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3380 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003381
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003382 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3383 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3384 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3385
Hanno Becker67bc7c32018-08-06 11:33:50 +01003386 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3387 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3388 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003389
3390 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3391
Hanno Becker3f7b9732018-08-28 09:53:25 +01003392 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003393 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3394 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003395 ssl->out_msgtype = cur->type;
3396
3397 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003398 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003399 }
3400
3401 /* If done with the current message move to the next one if any */
3402 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3403 {
3404 if( cur->next != NULL )
3405 {
3406 ssl->handshake->cur_msg = cur->next;
3407 ssl->handshake->cur_msg_p = cur->next->p + 12;
3408 }
3409 else
3410 {
3411 ssl->handshake->cur_msg = NULL;
3412 ssl->handshake->cur_msg_p = NULL;
3413 }
3414 }
3415
3416 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003417 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003420 return( ret );
3421 }
3422 }
3423
Hanno Becker67bc7c32018-08-06 11:33:50 +01003424 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3425 return( ret );
3426
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003427 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3429 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003430 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003433 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3434 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003435
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003437
3438 return( 0 );
3439}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003440
3441/*
3442 * To be called when the last message of an incoming flight is received.
3443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445{
3446 /* We won't need to resend that one any more */
3447 ssl_flight_free( ssl->handshake->flight );
3448 ssl->handshake->flight = NULL;
3449 ssl->handshake->cur_msg = NULL;
3450
3451 /* The next incoming flight will start with this msg_seq */
3452 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3453
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003454 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003455 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003456
Hanno Becker0271f962018-08-16 13:23:47 +01003457 /* Clear future message buffering structure. */
3458 ssl_buffering_free( ssl );
3459
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003460 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003461 ssl_set_timer( ssl, 0 );
3462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3464 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003467 }
3468 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003470}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003471
3472/*
3473 * To be called when the last message of an outgoing flight is send.
3474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003476{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003477 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003478 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3481 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003484 }
3485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003489
Paul Bakker5121ce52009-01-03 21:22:43 +00003490/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003491 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003492 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003493
3494/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003495 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003496 *
3497 * - fill in handshake headers
3498 * - update handshake checksum
3499 * - DTLS: save message for resending
3500 * - then pass to the record layer
3501 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003502 * DTLS: except for HelloRequest, messages are only queued, and will only be
3503 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003504 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003505 * Inputs:
3506 * - ssl->out_msglen: 4 + actual handshake message len
3507 * (4 is the size of handshake headers for TLS)
3508 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3509 * - ssl->out_msg + 4: the handshake message body
3510 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003511 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003512 * - ssl->out_msglen: the length of the record contents
3513 * (including handshake headers but excluding record headers)
3514 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003515 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003516int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003517{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003518 int ret;
3519 const size_t hs_len = ssl->out_msglen - 4;
3520 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3523
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003524 /*
3525 * Sanity checks
3526 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003527 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003528 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3529 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003530 /* In SSLv3, the client might send a NoCertificate alert. */
3531#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3532 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3533 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3534 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3535#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3536 {
3537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3539 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003542 /* Whenever we send anything different from a
3543 * HelloRequest we should be in a handshake - double check. */
3544 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3545 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003546 ssl->handshake == NULL )
3547 {
3548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003553 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003554 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003556 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003559 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003560#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003561
Hanno Beckerb50a2532018-08-06 11:52:54 +01003562 /* Double-check that we did not exceed the bounds
3563 * of the outgoing record buffer.
3564 * This should never fail as the various message
3565 * writing functions must obey the bounds of the
3566 * outgoing record buffer, but better be safe.
3567 *
3568 * Note: We deliberately do not check for the MTU or MFL here.
3569 */
3570 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3571 {
3572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3573 "size %u, maximum %u",
3574 (unsigned) ssl->out_msglen,
3575 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3577 }
3578
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003579 /*
3580 * Fill handshake headers
3581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003584 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3585 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3586 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003588 /*
3589 * DTLS has additional fields in the Handshake layer,
3590 * between the length field and the actual payload:
3591 * uint16 message_seq;
3592 * uint24 fragment_offset;
3593 * uint24 fragment_length;
3594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003597 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003598 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003599 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003600 {
3601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3602 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003603 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003604 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3606 }
3607
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003608 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003609 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003610
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003611 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003612 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003613 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003614 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3615 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3616 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003617 }
3618 else
3619 {
3620 ssl->out_msg[4] = 0;
3621 ssl->out_msg[5] = 0;
3622 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003623
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003624 /* Handshake hashes are computed without fragmentation,
3625 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003626 memset( ssl->out_msg + 6, 0x00, 3 );
3627 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003630
Hanno Becker0207e532018-08-28 10:28:28 +01003631 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003632 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3633 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 }
3635
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003636 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003638 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003639 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3640 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641 {
3642 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003645 return( ret );
3646 }
3647 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003648 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003650 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003651 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003652 {
3653 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3654 return( ret );
3655 }
3656 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657
3658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3659
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003660 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003661}
3662
3663/*
3664 * Record layer functions
3665 */
3666
3667/*
3668 * Write current record.
3669 *
3670 * Uses:
3671 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3672 * - ssl->out_msglen: length of the record content (excl headers)
3673 * - ssl->out_msg: record content
3674 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003675int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676{
3677 int ret, done = 0;
3678 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003679 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003680
3681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003684 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686 {
3687 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003690 return( ret );
3691 }
3692
3693 len = ssl->out_msglen;
3694 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3698 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 ret = mbedtls_ssl_hw_record_write( ssl );
3703 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3706 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003707 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003708
3709 if( ret == 0 )
3710 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003713 if( !done )
3714 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003715 unsigned i;
3716 size_t protected_record_size;
3717
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003718 /* Skip writing the record content type to after the encryption,
3719 * as it may change when using the CID extension. */
3720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003722 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003723
Hanno Becker19859472018-08-06 09:40:20 +01003724 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003725 ssl->out_len[0] = (unsigned char)( len >> 8 );
3726 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003727
Paul Bakker48916f92012-09-16 19:57:18 +00003728 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003729 {
Hanno Becker3307b532017-12-27 21:37:21 +00003730 mbedtls_record rec;
3731
3732 rec.buf = ssl->out_iv;
3733 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3734 ( ssl->out_iv - ssl->out_buf );
3735 rec.data_len = ssl->out_msglen;
3736 rec.data_offset = ssl->out_msg - rec.buf;
3737
3738 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3739 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3740 ssl->conf->transport, rec.ver );
3741 rec.type = ssl->out_msgtype;
3742
Hanno Beckera5a2b082019-05-15 14:03:01 +01003743#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003744 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003745 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003746#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003747
Hanno Becker611a83b2018-01-03 14:27:32 +00003748 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003749 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003752 return( ret );
3753 }
3754
Hanno Becker3307b532017-12-27 21:37:21 +00003755 if( rec.data_offset != 0 )
3756 {
3757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3759 }
3760
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003761 /* Update the record content type and CID. */
3762 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003763#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003764 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003765#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003766 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003767 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3768 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003769 }
3770
Hanno Becker43395762019-05-03 14:46:38 +01003771 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003772
3773#if defined(MBEDTLS_SSL_PROTO_DTLS)
3774 /* In case of DTLS, double-check that we don't exceed
3775 * the remaining space in the datagram. */
3776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3777 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003778 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003779 if( ret < 0 )
3780 return( ret );
3781
3782 if( protected_record_size > (size_t) ret )
3783 {
3784 /* Should never happen */
3785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3786 }
3787 }
3788#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003789
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003790 /* Now write the potentially updated record content type. */
3791 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003794 "version = [%d:%d], msglen = %d",
3795 ssl->out_hdr[0], ssl->out_hdr[1],
3796 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003799 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003800
3801 ssl->out_left += protected_record_size;
3802 ssl->out_hdr += protected_record_size;
3803 ssl_update_out_pointers( ssl, ssl->transform_out );
3804
Hanno Becker04484622018-08-06 09:49:38 +01003805 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3806 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3807 break;
3808
3809 /* The loop goes to its end iff the counter is wrapping */
3810 if( i == ssl_ep_len( ssl ) )
3811 {
3812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3813 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003815 }
3816
Hanno Becker67bc7c32018-08-06 11:33:50 +01003817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003818 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3819 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003821 size_t remaining;
3822 ret = ssl_get_remaining_payload_in_datagram( ssl );
3823 if( ret < 0 )
3824 {
3825 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3826 ret );
3827 return( ret );
3828 }
3829
3830 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003831 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003832 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003833 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003834 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835 else
3836 {
Hanno Becker513815a2018-08-20 11:56:09 +01003837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003838 }
3839 }
3840#endif /* MBEDTLS_SSL_PROTO_DTLS */
3841
3842 if( ( flush == SSL_FORCE_FLUSH ) &&
3843 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 return( ret );
3847 }
3848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850
3851 return( 0 );
3852}
3853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003855
3856static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3857{
3858 if( ssl->in_msglen < ssl->in_hslen ||
3859 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3860 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3861 {
3862 return( 1 );
3863 }
3864 return( 0 );
3865}
Hanno Becker44650b72018-08-16 12:51:11 +01003866
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003867static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003868{
3869 return( ( ssl->in_msg[9] << 16 ) |
3870 ( ssl->in_msg[10] << 8 ) |
3871 ssl->in_msg[11] );
3872}
3873
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003874static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003875{
3876 return( ( ssl->in_msg[6] << 16 ) |
3877 ( ssl->in_msg[7] << 8 ) |
3878 ssl->in_msg[8] );
3879}
3880
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003881static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003882{
3883 uint32_t msg_len, frag_off, frag_len;
3884
3885 msg_len = ssl_get_hs_total_len( ssl );
3886 frag_off = ssl_get_hs_frag_off( ssl );
3887 frag_len = ssl_get_hs_frag_len( ssl );
3888
3889 if( frag_off > msg_len )
3890 return( -1 );
3891
3892 if( frag_len > msg_len - frag_off )
3893 return( -1 );
3894
3895 if( frag_len + 12 > ssl->in_msglen )
3896 return( -1 );
3897
3898 return( 0 );
3899}
3900
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003901/*
3902 * Mark bits in bitmask (used for DTLS HS reassembly)
3903 */
3904static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3905{
3906 unsigned int start_bits, end_bits;
3907
3908 start_bits = 8 - ( offset % 8 );
3909 if( start_bits != 8 )
3910 {
3911 size_t first_byte_idx = offset / 8;
3912
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003913 /* Special case */
3914 if( len <= start_bits )
3915 {
3916 for( ; len != 0; len-- )
3917 mask[first_byte_idx] |= 1 << ( start_bits - len );
3918
3919 /* Avoid potential issues with offset or len becoming invalid */
3920 return;
3921 }
3922
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003923 offset += start_bits; /* Now offset % 8 == 0 */
3924 len -= start_bits;
3925
3926 for( ; start_bits != 0; start_bits-- )
3927 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3928 }
3929
3930 end_bits = len % 8;
3931 if( end_bits != 0 )
3932 {
3933 size_t last_byte_idx = ( offset + len ) / 8;
3934
3935 len -= end_bits; /* Now len % 8 == 0 */
3936
3937 for( ; end_bits != 0; end_bits-- )
3938 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3939 }
3940
3941 memset( mask + offset / 8, 0xFF, len / 8 );
3942}
3943
3944/*
3945 * Check that bitmask is full
3946 */
3947static int ssl_bitmask_check( unsigned char *mask, size_t len )
3948{
3949 size_t i;
3950
3951 for( i = 0; i < len / 8; i++ )
3952 if( mask[i] != 0xFF )
3953 return( -1 );
3954
3955 for( i = 0; i < len % 8; i++ )
3956 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3957 return( -1 );
3958
3959 return( 0 );
3960}
3961
Hanno Becker56e205e2018-08-16 09:06:12 +01003962/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003963static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003964 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003965{
Hanno Becker56e205e2018-08-16 09:06:12 +01003966 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003967
Hanno Becker56e205e2018-08-16 09:06:12 +01003968 alloc_len = 12; /* Handshake header */
3969 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003970
Hanno Beckerd07df862018-08-16 09:14:58 +01003971 if( add_bitmap )
3972 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003973
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003974 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003975}
Hanno Becker56e205e2018-08-16 09:06:12 +01003976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003978
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003979static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003980{
3981 return( ( ssl->in_msg[1] << 16 ) |
3982 ( ssl->in_msg[2] << 8 ) |
3983 ssl->in_msg[3] );
3984}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003985
Simon Butcher99000142016-10-13 17:21:01 +01003986int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003991 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003993 }
3994
Hanno Becker12555c62018-08-16 12:47:53 +01003995 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003998 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003999 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004002 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004003 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004004 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004005 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004006
Hanno Becker44650b72018-08-16 12:51:11 +01004007 if( ssl_check_hs_header( ssl ) != 0 )
4008 {
4009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4010 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4011 }
4012
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004013 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004014 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4015 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4016 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4017 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004018 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004019 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4020 {
4021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4022 recv_msg_seq,
4023 ssl->handshake->in_msg_seq ) );
4024 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4025 }
4026
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004027 /* Retransmit only on last message from previous flight, to avoid
4028 * too many retransmissions.
4029 * Besides, No sane server ever retransmits HelloVerifyRequest */
4030 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004034 "message_seq = %d, start_of_flight = %d",
4035 recv_msg_seq,
4036 ssl->handshake->in_flight_start_seq ) );
4037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004041 return( ret );
4042 }
4043 }
4044 else
4045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004047 "message_seq = %d, expected = %d",
4048 recv_msg_seq,
4049 ssl->handshake->in_msg_seq ) );
4050 }
4051
Hanno Becker90333da2017-10-10 11:27:13 +01004052 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004053 }
4054 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004055
Hanno Becker6d97ef52018-08-16 13:09:04 +01004056 /* Message reassembly is handled alongside buffering of future
4057 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004058 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004059 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004060 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004063 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004064 }
4065 }
4066 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004068 /* With TLS we don't handle fragmentation (for now) */
4069 if( ssl->in_msglen < ssl->in_hslen )
4070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4072 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004073 }
4074
Simon Butcher99000142016-10-13 17:21:01 +01004075 return( 0 );
4076}
4077
4078void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4079{
Hanno Becker0271f962018-08-16 13:23:47 +01004080 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004081
Hanno Becker0271f962018-08-16 13:23:47 +01004082 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004083 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004084 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004085 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004086
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004087 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004090 ssl->handshake != NULL )
4091 {
Hanno Becker0271f962018-08-16 13:23:47 +01004092 unsigned offset;
4093 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004094
Hanno Becker0271f962018-08-16 13:23:47 +01004095 /* Increment handshake sequence number */
4096 hs->in_msg_seq++;
4097
4098 /*
4099 * Clear up handshake buffering and reassembly structure.
4100 */
4101
4102 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004103 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004104
4105 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004106 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4107 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004108 offset++, hs_buf++ )
4109 {
4110 *hs_buf = *(hs_buf + 1);
4111 }
4112
4113 /* Create a fresh last entry */
4114 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004115 }
4116#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004117}
4118
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004119/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004120 * DTLS anti-replay: RFC 6347 4.1.2.6
4121 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004122 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4123 * Bit n is set iff record number in_window_top - n has been seen.
4124 *
4125 * Usually, in_window_top is the last record number seen and the lsb of
4126 * in_window is set. The only exception is the initial state (record number 0
4127 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4130static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004131{
4132 ssl->in_window_top = 0;
4133 ssl->in_window = 0;
4134}
4135
4136static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4137{
4138 return( ( (uint64_t) buf[0] << 40 ) |
4139 ( (uint64_t) buf[1] << 32 ) |
4140 ( (uint64_t) buf[2] << 24 ) |
4141 ( (uint64_t) buf[3] << 16 ) |
4142 ( (uint64_t) buf[4] << 8 ) |
4143 ( (uint64_t) buf[5] ) );
4144}
4145
4146/*
4147 * Return 0 if sequence number is acceptable, -1 otherwise
4148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004150{
4151 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4152 uint64_t bit;
4153
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004154 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004155 return( 0 );
4156
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004157 if( rec_seqnum > ssl->in_window_top )
4158 return( 0 );
4159
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004160 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004161
4162 if( bit >= 64 )
4163 return( -1 );
4164
4165 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4166 return( -1 );
4167
4168 return( 0 );
4169}
4170
4171/*
4172 * Update replay window on new validated record
4173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004175{
4176 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4177
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004178 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004179 return;
4180
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004181 if( rec_seqnum > ssl->in_window_top )
4182 {
4183 /* Update window_top and the contents of the window */
4184 uint64_t shift = rec_seqnum - ssl->in_window_top;
4185
4186 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004187 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004188 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004189 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004190 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004191 ssl->in_window |= 1;
4192 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004193
4194 ssl->in_window_top = rec_seqnum;
4195 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004196 else
4197 {
4198 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004199 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004200
4201 if( bit < 64 ) /* Always true, but be extra sure */
4202 ssl->in_window |= (uint64_t) 1 << bit;
4203 }
4204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004206
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004207#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004208/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004209static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4210
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004211/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004212 * Without any SSL context, check if a datagram looks like a ClientHello with
4213 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004214 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004215 *
4216 * - if cookie is valid, return 0
4217 * - if ClientHello looks superficially valid but cookie is not,
4218 * fill obuf and set olen, then
4219 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4220 * - otherwise return a specific error code
4221 */
4222static int ssl_check_dtls_clihlo_cookie(
4223 mbedtls_ssl_cookie_write_t *f_cookie_write,
4224 mbedtls_ssl_cookie_check_t *f_cookie_check,
4225 void *p_cookie,
4226 const unsigned char *cli_id, size_t cli_id_len,
4227 const unsigned char *in, size_t in_len,
4228 unsigned char *obuf, size_t buf_len, size_t *olen )
4229{
4230 size_t sid_len, cookie_len;
4231 unsigned char *p;
4232
4233 if( f_cookie_write == NULL || f_cookie_check == NULL )
4234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4235
4236 /*
4237 * Structure of ClientHello with record and handshake headers,
4238 * and expected values. We don't need to check a lot, more checks will be
4239 * done when actually parsing the ClientHello - skipping those checks
4240 * avoids code duplication and does not make cookie forging any easier.
4241 *
4242 * 0-0 ContentType type; copied, must be handshake
4243 * 1-2 ProtocolVersion version; copied
4244 * 3-4 uint16 epoch; copied, must be 0
4245 * 5-10 uint48 sequence_number; copied
4246 * 11-12 uint16 length; (ignored)
4247 *
4248 * 13-13 HandshakeType msg_type; (ignored)
4249 * 14-16 uint24 length; (ignored)
4250 * 17-18 uint16 message_seq; copied
4251 * 19-21 uint24 fragment_offset; copied, must be 0
4252 * 22-24 uint24 fragment_length; (ignored)
4253 *
4254 * 25-26 ProtocolVersion client_version; (ignored)
4255 * 27-58 Random random; (ignored)
4256 * 59-xx SessionID session_id; 1 byte len + sid_len content
4257 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4258 * ...
4259 *
4260 * Minimum length is 61 bytes.
4261 */
4262 if( in_len < 61 ||
4263 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4264 in[3] != 0 || in[4] != 0 ||
4265 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4266 {
4267 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4268 }
4269
4270 sid_len = in[59];
4271 if( sid_len > in_len - 61 )
4272 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4273
4274 cookie_len = in[60 + sid_len];
4275 if( cookie_len > in_len - 60 )
4276 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4277
4278 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4279 cli_id, cli_id_len ) == 0 )
4280 {
4281 /* Valid cookie */
4282 return( 0 );
4283 }
4284
4285 /*
4286 * If we get here, we've got an invalid cookie, let's prepare HVR.
4287 *
4288 * 0-0 ContentType type; copied
4289 * 1-2 ProtocolVersion version; copied
4290 * 3-4 uint16 epoch; copied
4291 * 5-10 uint48 sequence_number; copied
4292 * 11-12 uint16 length; olen - 13
4293 *
4294 * 13-13 HandshakeType msg_type; hello_verify_request
4295 * 14-16 uint24 length; olen - 25
4296 * 17-18 uint16 message_seq; copied
4297 * 19-21 uint24 fragment_offset; copied
4298 * 22-24 uint24 fragment_length; olen - 25
4299 *
4300 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4301 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4302 *
4303 * Minimum length is 28.
4304 */
4305 if( buf_len < 28 )
4306 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4307
4308 /* Copy most fields and adapt others */
4309 memcpy( obuf, in, 25 );
4310 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4311 obuf[25] = 0xfe;
4312 obuf[26] = 0xff;
4313
4314 /* Generate and write actual cookie */
4315 p = obuf + 28;
4316 if( f_cookie_write( p_cookie,
4317 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4318 {
4319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4320 }
4321
4322 *olen = p - obuf;
4323
4324 /* Go back and fill length fields */
4325 obuf[27] = (unsigned char)( *olen - 28 );
4326
4327 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4328 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4329 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4330
4331 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4332 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4333
4334 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4335}
4336
4337/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004338 * Handle possible client reconnect with the same UDP quadruplet
4339 * (RFC 6347 Section 4.2.8).
4340 *
4341 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4342 * that looks like a ClientHello.
4343 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004344 * - if the input looks like a ClientHello without cookies,
4345 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004346 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004347 * - if the input looks like a ClientHello with a valid cookie,
4348 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004349 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004350 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004351 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004352 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004353 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4354 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004355 */
4356static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4357{
4358 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004359 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004360
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004361 ret = ssl_check_dtls_clihlo_cookie(
4362 ssl->conf->f_cookie_write,
4363 ssl->conf->f_cookie_check,
4364 ssl->conf->p_cookie,
4365 ssl->cli_id, ssl->cli_id_len,
4366 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004367 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004368
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004369 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4370
4371 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004372 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004373 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004374 * If the error is permanent we'll catch it later,
4375 * if it's not, then hopefully it'll work next time. */
4376 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4377
4378 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004379 }
4380
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004381 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004382 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004383 /* Got a valid cookie, partially reset context */
4384 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4385 {
4386 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4387 return( ret );
4388 }
4389
4390 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004391 }
4392
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004393 return( ret );
4394}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004395#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004396
Hanno Becker46483f12019-05-03 13:25:54 +01004397static int ssl_check_record_type( uint8_t record_type )
4398{
4399 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4400 record_type != MBEDTLS_SSL_MSG_ALERT &&
4401 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4402 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4403 {
4404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4405 }
4406
4407 return( 0 );
4408}
4409
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004410/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004411 * ContentType type;
4412 * ProtocolVersion version;
4413 * uint16 epoch; // DTLS only
4414 * uint48 sequence_number; // DTLS only
4415 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004416 *
4417 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004418 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004419 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4420 *
4421 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004422 * 1. proceed with the record if this function returns 0
4423 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4424 * 3. return CLIENT_RECONNECT if this function return that value
4425 * 4. drop the whole datagram if this function returns anything else.
4426 * Point 2 is needed when the peer is resending, and we have already received
4427 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004430{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004431 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004432 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004433
Hanno Becker8b09b732019-05-08 12:03:28 +01004434 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004435
Paul Bakker5121ce52009-01-03 21:22:43 +00004436 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004437 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004438
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004439 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004440#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004441 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4442 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4443 ssl->conf->cid_len != 0 )
4444 {
4445 /* Shift pointers to account for record header including CID
4446 * struct {
4447 * ContentType special_type = tls12_cid;
4448 * ProtocolVersion version;
4449 * uint16 epoch;
4450 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004451 * opaque cid[cid_length]; // Additional field compared to
4452 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004453 * uint16 length;
4454 * opaque enc_content[DTLSCiphertext.length];
4455 * } DTLSCiphertext;
4456 */
4457
4458 /* So far, we only support static CID lengths
4459 * fixed in the configuration. */
4460 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4461 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4462 }
4463 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004464#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004465 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004468
4469#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004470 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4471 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004472 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4473#endif /* MBEDTLS_SSL_PROTO_DTLS */
4474 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4475 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004478 }
4479
4480 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004481 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4484 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004485 }
4486
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004487 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4490 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 }
4492
Hanno Becker8b09b732019-05-08 12:03:28 +01004493 /* Now that the total length of the record header is known, ensure
4494 * that the current datagram is large enough to hold it.
4495 * This would fail, for example, if we received a datagram of
4496 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4497 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4498 if( ret != 0 )
4499 {
4500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4501 return( ret );
4502 }
4503 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4504
4505 /* Parse and validate record length
4506 * This must happen after the CID parsing because
4507 * its position in the record header depends on
4508 * the presence of a CID. */
4509
4510 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004511 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004512 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4515 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004516 }
4517
Hanno Becker8b09b732019-05-08 12:03:28 +01004518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004519 "version = [%d:%d], msglen = %d",
4520 ssl->in_msgtype,
4521 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004522
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004523 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004524 * DTLS-related tests.
4525 * Check epoch before checking length constraint because
4526 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4527 * message gets duplicated before the corresponding Finished message,
4528 * the second ChangeCipherSpec should be discarded because it belongs
4529 * to an old epoch, but not because its length is shorter than
4530 * the minimum record length for packets using the new record transform.
4531 * Note that these two kinds of failures are handled differently,
4532 * as an unexpected record is silently skipped but an invalid
4533 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004534 */
4535#if defined(MBEDTLS_SSL_PROTO_DTLS)
4536 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4537 {
4538 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4539
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004540 /* Check epoch (and sequence number) with DTLS */
4541 if( rec_epoch != ssl->in_epoch )
4542 {
4543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4544 "expected %d, received %d",
4545 ssl->in_epoch, rec_epoch ) );
4546
4547#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4548 /*
4549 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4550 * access the first byte of record content (handshake type), as we
4551 * have an active transform (possibly iv_len != 0), so use the
4552 * fact that the record header len is 13 instead.
4553 */
4554 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4555 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4556 rec_epoch == 0 &&
4557 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4558 ssl->in_left > 13 &&
4559 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4560 {
4561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4562 "from the same port" ) );
4563 return( ssl_handle_possible_reconnect( ssl ) );
4564 }
4565 else
4566#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004567 {
4568 /* Consider buffering the record. */
4569 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4570 {
4571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4572 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4573 }
4574
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004575 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004576 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004577 }
4578
4579#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4580 /* Replay detection only works for the current epoch */
4581 if( rec_epoch == ssl->in_epoch &&
4582 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4583 {
4584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4585 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4586 }
4587#endif
4588 }
4589#endif /* MBEDTLS_SSL_PROTO_DTLS */
4590
Hanno Becker52c6dc62017-05-26 16:07:36 +01004591
4592 /* Check length against bounds of the current transform and version */
4593 if( ssl->transform_in == NULL )
4594 {
4595 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004596 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004597 {
4598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4599 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4600 }
4601 }
4602 else
4603 {
4604 if( ssl->in_msglen < ssl->transform_in->minlen )
4605 {
4606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4607 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4608 }
4609
4610#if defined(MBEDTLS_SSL_PROTO_SSL3)
4611 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004612 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004613 {
4614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4615 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4616 }
4617#endif
4618#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4619 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4620 /*
4621 * TLS encrypted messages can have up to 256 bytes of padding
4622 */
4623 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4624 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004625 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004626 {
4627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4628 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4629 }
4630#endif
4631 }
4632
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004633 return( 0 );
4634}
Paul Bakker5121ce52009-01-03 21:22:43 +00004635
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004636/*
4637 * If applicable, decrypt (and decompress) record content
4638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004640{
4641 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004644 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4647 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 ret = mbedtls_ssl_hw_record_read( ssl );
4652 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004656 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004657
4658 if( ret == 0 )
4659 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004662 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004664 mbedtls_record rec;
4665
4666 rec.buf = ssl->in_iv;
4667 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4668 - ( ssl->in_iv - ssl->in_buf );
4669 rec.data_len = ssl->in_msglen;
4670 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004671#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004672 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004673 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004674#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004675
4676 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4677 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4678 ssl->conf->transport, rec.ver );
4679 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004680 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4681 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004684
Hanno Beckera5a2b082019-05-15 14:03:01 +01004685#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004686 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4687 ssl->conf->ignore_unexpected_cid
4688 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4689 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004691 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004692 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004693#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004694
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 return( ret );
4696 }
4697
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004698 if( ssl->in_msgtype != rec.type )
4699 {
4700 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4701 ssl->in_msgtype, rec.type ) );
4702 }
4703
4704 /* The record content type may change during decryption,
4705 * so re-read it. */
4706 ssl->in_msgtype = rec.type;
4707 /* Also update the input buffer, because unfortunately
4708 * the server-side ssl_parse_client_hello() reparses the
4709 * record header when receiving a ClientHello initiating
4710 * a renegotiation. */
4711 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004712 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004713 ssl->in_msglen = rec.data_len;
4714 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4715 ssl->in_len[1] = (unsigned char)( rec.data_len );
4716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 ssl->in_msg, ssl->in_msglen );
4719
Hanno Beckera5a2b082019-05-15 14:03:01 +01004720#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004721 /* We have already checked the record content type
4722 * in ssl_parse_record_header(), failing or silently
4723 * dropping the record in the case of an unknown type.
4724 *
4725 * Since with the use of CIDs, the record content type
4726 * might change during decryption, re-check the record
4727 * content type, but treat a failure as fatal this time. */
4728 if( ssl_check_record_type( ssl->in_msgtype ) )
4729 {
4730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4731 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4732 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004733#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004734
Angus Grattond8213d02016-05-25 20:56:48 +10004735 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4738 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004740 else if( ssl->in_msglen == 0 )
4741 {
4742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4743 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4744 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4745 {
4746 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4748 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4749 }
4750#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4751
4752 ssl->nb_zero++;
4753
4754 /*
4755 * Three or more empty messages may be a DoS attack
4756 * (excessive CPU consumption).
4757 */
4758 if( ssl->nb_zero > 3 )
4759 {
4760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004761 "messages, possible DoS attack" ) );
4762 /* Treat the records as if they were not properly authenticated,
4763 * thereby failing the connection if we see more than allowed
4764 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004765 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4766 }
4767 }
4768 else
4769 ssl->nb_zero = 0;
4770
4771#if defined(MBEDTLS_SSL_PROTO_DTLS)
4772 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4773 {
4774 ; /* in_ctr read from peer, not maintained internally */
4775 }
4776 else
4777#endif
4778 {
4779 unsigned i;
4780 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4781 if( ++ssl->in_ctr[i - 1] != 0 )
4782 break;
4783
4784 /* The loop goes to its end iff the counter is wrapping */
4785 if( i == ssl_ep_len( ssl ) )
4786 {
4787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4788 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4789 }
4790 }
4791
Paul Bakker5121ce52009-01-03 21:22:43 +00004792 }
4793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004795 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004797 {
4798 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004801 return( ret );
4802 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004807 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004810 }
4811#endif
4812
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004813 return( 0 );
4814}
4815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004817
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004818/*
4819 * Read a record.
4820 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004821 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4822 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4823 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004824 */
Hanno Becker1097b342018-08-15 14:09:41 +01004825
4826/* Helper functions for mbedtls_ssl_read_record(). */
4827static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004828static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4829static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004830
Hanno Becker327c93b2018-08-15 13:56:18 +01004831int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004832 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004833{
4834 int ret;
4835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004837
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004838 if( ssl->keep_current_message == 0 )
4839 {
4840 do {
Simon Butcher99000142016-10-13 17:21:01 +01004841
Hanno Becker26994592018-08-15 14:14:59 +01004842 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004843 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004844 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004845
Hanno Beckere74d5562018-08-15 14:26:08 +01004846 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004847 {
Hanno Becker40f50842018-08-15 14:48:01 +01004848#if defined(MBEDTLS_SSL_PROTO_DTLS)
4849 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004850
Hanno Becker40f50842018-08-15 14:48:01 +01004851 /* We only check for buffered messages if the
4852 * current datagram is fully consumed. */
4853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004854 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004855 {
Hanno Becker40f50842018-08-15 14:48:01 +01004856 if( ssl_load_buffered_message( ssl ) == 0 )
4857 have_buffered = 1;
4858 }
4859
4860 if( have_buffered == 0 )
4861#endif /* MBEDTLS_SSL_PROTO_DTLS */
4862 {
4863 ret = ssl_get_next_record( ssl );
4864 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4865 continue;
4866
4867 if( ret != 0 )
4868 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004869 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004870 return( ret );
4871 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004872 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004873 }
4874
4875 ret = mbedtls_ssl_handle_message_type( ssl );
4876
Hanno Becker40f50842018-08-15 14:48:01 +01004877#if defined(MBEDTLS_SSL_PROTO_DTLS)
4878 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4879 {
4880 /* Buffer future message */
4881 ret = ssl_buffer_message( ssl );
4882 if( ret != 0 )
4883 return( ret );
4884
4885 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4886 }
4887#endif /* MBEDTLS_SSL_PROTO_DTLS */
4888
Hanno Becker90333da2017-10-10 11:27:13 +01004889 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4890 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004891
4892 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004893 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004894 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004895 return( ret );
4896 }
4897
Hanno Becker327c93b2018-08-15 13:56:18 +01004898 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004899 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004900 {
4901 mbedtls_ssl_update_handshake_status( ssl );
4902 }
Simon Butcher99000142016-10-13 17:21:01 +01004903 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004904 else
Simon Butcher99000142016-10-13 17:21:01 +01004905 {
Hanno Becker02f59072018-08-15 14:00:24 +01004906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004907 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004908 }
4909
4910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4911
4912 return( 0 );
4913}
4914
Hanno Becker40f50842018-08-15 14:48:01 +01004915#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004916static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004917{
Hanno Becker40f50842018-08-15 14:48:01 +01004918 if( ssl->in_left > ssl->next_record_offset )
4919 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004920
Hanno Becker40f50842018-08-15 14:48:01 +01004921 return( 0 );
4922}
4923
4924static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4925{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004926 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004927 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004928 int ret = 0;
4929
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004930 if( hs == NULL )
4931 return( -1 );
4932
Hanno Beckere00ae372018-08-20 09:39:42 +01004933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4934
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004935 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4936 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4937 {
4938 /* Check if we have seen a ChangeCipherSpec before.
4939 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004940 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004941 {
4942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4943 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004944 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004945 }
4946
Hanno Becker39b8bc92018-08-28 17:17:13 +01004947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004948 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4949 ssl->in_msglen = 1;
4950 ssl->in_msg[0] = 1;
4951
4952 /* As long as they are equal, the exact value doesn't matter. */
4953 ssl->in_left = 0;
4954 ssl->next_record_offset = 0;
4955
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004956 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004957 goto exit;
4958 }
Hanno Becker37f95322018-08-16 13:55:32 +01004959
Hanno Beckerb8f50142018-08-28 10:01:34 +01004960#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004961 /* Debug only */
4962 {
4963 unsigned offset;
4964 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4965 {
4966 hs_buf = &hs->buffering.hs[offset];
4967 if( hs_buf->is_valid == 1 )
4968 {
4969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4970 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004971 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004972 }
4973 }
4974 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004975#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004976
4977 /* Check if we have buffered and/or fully reassembled the
4978 * next handshake message. */
4979 hs_buf = &hs->buffering.hs[0];
4980 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4981 {
4982 /* Synthesize a record containing the buffered HS message. */
4983 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4984 ( hs_buf->data[2] << 8 ) |
4985 hs_buf->data[3];
4986
4987 /* Double-check that we haven't accidentally buffered
4988 * a message that doesn't fit into the input buffer. */
4989 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4990 {
4991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4992 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4993 }
4994
4995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4996 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4997 hs_buf->data, msg_len + 12 );
4998
4999 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5000 ssl->in_hslen = msg_len + 12;
5001 ssl->in_msglen = msg_len + 12;
5002 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5003
5004 ret = 0;
5005 goto exit;
5006 }
5007 else
5008 {
5009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5010 hs->in_msg_seq ) );
5011 }
5012
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005013 ret = -1;
5014
5015exit:
5016
5017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5018 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005019}
5020
Hanno Beckera02b0b42018-08-21 17:20:27 +01005021static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5022 size_t desired )
5023{
5024 int offset;
5025 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5027 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005028
Hanno Becker01315ea2018-08-21 17:22:17 +01005029 /* Get rid of future records epoch first, if such exist. */
5030 ssl_free_buffered_record( ssl );
5031
5032 /* Check if we have enough space available now. */
5033 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5034 hs->buffering.total_bytes_buffered ) )
5035 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005037 return( 0 );
5038 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005039
Hanno Becker4f432ad2018-08-28 10:02:32 +01005040 /* We don't have enough space to buffer the next expected handshake
5041 * message. Remove buffers used for future messages to gain space,
5042 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005043 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5044 offset >= 0; offset-- )
5045 {
5046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5047 offset ) );
5048
Hanno Beckerb309b922018-08-23 13:18:05 +01005049 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005050
5051 /* Check if we have enough space available now. */
5052 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5053 hs->buffering.total_bytes_buffered ) )
5054 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005056 return( 0 );
5057 }
5058 }
5059
5060 return( -1 );
5061}
5062
Hanno Becker40f50842018-08-15 14:48:01 +01005063static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5064{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005065 int ret = 0;
5066 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5067
5068 if( hs == NULL )
5069 return( 0 );
5070
5071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5072
5073 switch( ssl->in_msgtype )
5074 {
5075 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005077
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005078 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005079 break;
5080
5081 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005082 {
5083 unsigned recv_msg_seq_offset;
5084 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5085 mbedtls_ssl_hs_buffer *hs_buf;
5086 size_t msg_len = ssl->in_hslen - 12;
5087
5088 /* We should never receive an old handshake
5089 * message - double-check nonetheless. */
5090 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5091 {
5092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5094 }
5095
5096 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5097 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5098 {
5099 /* Silently ignore -- message too far in the future */
5100 MBEDTLS_SSL_DEBUG_MSG( 2,
5101 ( "Ignore future HS message with sequence number %u, "
5102 "buffering window %u - %u",
5103 recv_msg_seq, ssl->handshake->in_msg_seq,
5104 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5105
5106 goto exit;
5107 }
5108
5109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5110 recv_msg_seq, recv_msg_seq_offset ) );
5111
5112 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5113
5114 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005115 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005116 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005117 size_t reassembly_buf_sz;
5118
Hanno Becker37f95322018-08-16 13:55:32 +01005119 hs_buf->is_fragmented =
5120 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5121
5122 /* We copy the message back into the input buffer
5123 * after reassembly, so check that it's not too large.
5124 * This is an implementation-specific limitation
5125 * and not one from the standard, hence it is not
5126 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005127 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005128 {
5129 /* Ignore message */
5130 goto exit;
5131 }
5132
Hanno Beckere0b150f2018-08-21 15:51:03 +01005133 /* Check if we have enough space to buffer the message. */
5134 if( hs->buffering.total_bytes_buffered >
5135 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5136 {
5137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5139 }
5140
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005141 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5142 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005143
5144 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5145 hs->buffering.total_bytes_buffered ) )
5146 {
5147 if( recv_msg_seq_offset > 0 )
5148 {
5149 /* If we can't buffer a future message because
5150 * of space limitations -- ignore. */
5151 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",
5152 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5153 (unsigned) hs->buffering.total_bytes_buffered ) );
5154 goto exit;
5155 }
Hanno Beckere1801392018-08-21 16:51:05 +01005156 else
5157 {
5158 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",
5159 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5160 (unsigned) hs->buffering.total_bytes_buffered ) );
5161 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005162
Hanno Beckera02b0b42018-08-21 17:20:27 +01005163 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005164 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005165 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",
5166 (unsigned) msg_len,
5167 (unsigned) reassembly_buf_sz,
5168 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005169 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005170 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5171 goto exit;
5172 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005173 }
5174
5175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5176 msg_len ) );
5177
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005178 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5179 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005180 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005181 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005182 goto exit;
5183 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005184 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005185
5186 /* Prepare final header: copy msg_type, length and message_seq,
5187 * then add standardised fragment_offset and fragment_length */
5188 memcpy( hs_buf->data, ssl->in_msg, 6 );
5189 memset( hs_buf->data + 6, 0, 3 );
5190 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5191
5192 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005193
5194 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005195 }
5196 else
5197 {
5198 /* Make sure msg_type and length are consistent */
5199 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5200 {
5201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5202 /* Ignore */
5203 goto exit;
5204 }
5205 }
5206
Hanno Becker4422bbb2018-08-20 09:40:19 +01005207 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005208 {
5209 size_t frag_len, frag_off;
5210 unsigned char * const msg = hs_buf->data + 12;
5211
5212 /*
5213 * Check and copy current fragment
5214 */
5215
5216 /* Validation of header fields already done in
5217 * mbedtls_ssl_prepare_handshake_record(). */
5218 frag_off = ssl_get_hs_frag_off( ssl );
5219 frag_len = ssl_get_hs_frag_len( ssl );
5220
5221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5222 frag_off, frag_len ) );
5223 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5224
5225 if( hs_buf->is_fragmented )
5226 {
5227 unsigned char * const bitmask = msg + msg_len;
5228 ssl_bitmask_set( bitmask, frag_off, frag_len );
5229 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5230 msg_len ) == 0 );
5231 }
5232 else
5233 {
5234 hs_buf->is_complete = 1;
5235 }
5236
5237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5238 hs_buf->is_complete ? "" : "not yet " ) );
5239 }
5240
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005241 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005242 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005243
5244 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005245 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005246 break;
5247 }
5248
5249exit:
5250
5251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5252 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005253}
5254#endif /* MBEDTLS_SSL_PROTO_DTLS */
5255
Hanno Becker1097b342018-08-15 14:09:41 +01005256static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005257{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005258 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005259 * Consume last content-layer message and potentially
5260 * update in_msglen which keeps track of the contents'
5261 * consumption state.
5262 *
5263 * (1) Handshake messages:
5264 * Remove last handshake message, move content
5265 * and adapt in_msglen.
5266 *
5267 * (2) Alert messages:
5268 * Consume whole record content, in_msglen = 0.
5269 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005270 * (3) Change cipher spec:
5271 * Consume whole record content, in_msglen = 0.
5272 *
5273 * (4) Application data:
5274 * Don't do anything - the record layer provides
5275 * the application data as a stream transport
5276 * and consumes through mbedtls_ssl_read only.
5277 *
5278 */
5279
5280 /* Case (1): Handshake messages */
5281 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005282 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005283 /* Hard assertion to be sure that no application data
5284 * is in flight, as corrupting ssl->in_msglen during
5285 * ssl->in_offt != NULL is fatal. */
5286 if( ssl->in_offt != NULL )
5287 {
5288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5290 }
5291
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005292 /*
5293 * Get next Handshake message in the current record
5294 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005295
Hanno Becker4a810fb2017-05-24 16:27:30 +01005296 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005297 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005298 * current handshake content: If DTLS handshake
5299 * fragmentation is used, that's the fragment
5300 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005301 * size here is faulty and should be changed at
5302 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005303 * (2) While it doesn't seem to cause problems, one
5304 * has to be very careful not to assume that in_hslen
5305 * is always <= in_msglen in a sensible communication.
5306 * Again, it's wrong for DTLS handshake fragmentation.
5307 * The following check is therefore mandatory, and
5308 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005309 * Additionally, ssl->in_hslen might be arbitrarily out of
5310 * bounds after handling a DTLS message with an unexpected
5311 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005312 */
5313 if( ssl->in_hslen < ssl->in_msglen )
5314 {
5315 ssl->in_msglen -= ssl->in_hslen;
5316 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5317 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005318
Hanno Becker4a810fb2017-05-24 16:27:30 +01005319 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5320 ssl->in_msg, ssl->in_msglen );
5321 }
5322 else
5323 {
5324 ssl->in_msglen = 0;
5325 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005326
Hanno Becker4a810fb2017-05-24 16:27:30 +01005327 ssl->in_hslen = 0;
5328 }
5329 /* Case (4): Application data */
5330 else if( ssl->in_offt != NULL )
5331 {
5332 return( 0 );
5333 }
5334 /* Everything else (CCS & Alerts) */
5335 else
5336 {
5337 ssl->in_msglen = 0;
5338 }
5339
Hanno Becker1097b342018-08-15 14:09:41 +01005340 return( 0 );
5341}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005342
Hanno Beckere74d5562018-08-15 14:26:08 +01005343static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5344{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005345 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005346 return( 1 );
5347
5348 return( 0 );
5349}
5350
Hanno Becker5f066e72018-08-16 14:56:31 +01005351#if defined(MBEDTLS_SSL_PROTO_DTLS)
5352
5353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5354{
5355 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5356 if( hs == NULL )
5357 return;
5358
Hanno Becker01315ea2018-08-21 17:22:17 +01005359 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005360 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005361 hs->buffering.total_bytes_buffered -=
5362 hs->buffering.future_record.len;
5363
5364 mbedtls_free( hs->buffering.future_record.data );
5365 hs->buffering.future_record.data = NULL;
5366 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005367}
5368
5369static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5370{
5371 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5372 unsigned char * rec;
5373 size_t rec_len;
5374 unsigned rec_epoch;
5375
5376 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5377 return( 0 );
5378
5379 if( hs == NULL )
5380 return( 0 );
5381
Hanno Becker5f066e72018-08-16 14:56:31 +01005382 rec = hs->buffering.future_record.data;
5383 rec_len = hs->buffering.future_record.len;
5384 rec_epoch = hs->buffering.future_record.epoch;
5385
5386 if( rec == NULL )
5387 return( 0 );
5388
Hanno Becker4cb782d2018-08-20 11:19:05 +01005389 /* Only consider loading future records if the
5390 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005391 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005392 return( 0 );
5393
Hanno Becker5f066e72018-08-16 14:56:31 +01005394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5395
5396 if( rec_epoch != ssl->in_epoch )
5397 {
5398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5399 goto exit;
5400 }
5401
5402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5403
5404 /* Double-check that the record is not too large */
5405 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5406 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5407 {
5408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5410 }
5411
5412 memcpy( ssl->in_hdr, rec, rec_len );
5413 ssl->in_left = rec_len;
5414 ssl->next_record_offset = 0;
5415
5416 ssl_free_buffered_record( ssl );
5417
5418exit:
5419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5420 return( 0 );
5421}
5422
5423static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5424{
5425 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5426 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005427 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005428
5429 /* Don't buffer future records outside handshakes. */
5430 if( hs == NULL )
5431 return( 0 );
5432
5433 /* Only buffer handshake records (we are only interested
5434 * in Finished messages). */
5435 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5436 return( 0 );
5437
5438 /* Don't buffer more than one future epoch record. */
5439 if( hs->buffering.future_record.data != NULL )
5440 return( 0 );
5441
Hanno Becker01315ea2018-08-21 17:22:17 +01005442 /* Don't buffer record if there's not enough buffering space remaining. */
5443 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5444 hs->buffering.total_bytes_buffered ) )
5445 {
5446 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",
5447 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5448 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005449 return( 0 );
5450 }
5451
Hanno Becker5f066e72018-08-16 14:56:31 +01005452 /* Buffer record */
5453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5454 ssl->in_epoch + 1 ) );
5455 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5456 rec_hdr_len + ssl->in_msglen );
5457
5458 /* ssl_parse_record_header() only considers records
5459 * of the next epoch as candidates for buffering. */
5460 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005461 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005462
5463 hs->buffering.future_record.data =
5464 mbedtls_calloc( 1, hs->buffering.future_record.len );
5465 if( hs->buffering.future_record.data == NULL )
5466 {
5467 /* If we run out of RAM trying to buffer a
5468 * record from the next epoch, just ignore. */
5469 return( 0 );
5470 }
5471
Hanno Becker01315ea2018-08-21 17:22:17 +01005472 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005473
Hanno Becker01315ea2018-08-21 17:22:17 +01005474 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005475 return( 0 );
5476}
5477
5478#endif /* MBEDTLS_SSL_PROTO_DTLS */
5479
Hanno Beckere74d5562018-08-15 14:26:08 +01005480static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005481{
5482 int ret;
5483
Hanno Becker5f066e72018-08-16 14:56:31 +01005484#if defined(MBEDTLS_SSL_PROTO_DTLS)
5485 /* We might have buffered a future record; if so,
5486 * and if the epoch matches now, load it.
5487 * On success, this call will set ssl->in_left to
5488 * the length of the buffered record, so that
5489 * the calls to ssl_fetch_input() below will
5490 * essentially be no-ops. */
5491 ret = ssl_load_buffered_record( ssl );
5492 if( ret != 0 )
5493 return( ret );
5494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005495
Hanno Becker8b09b732019-05-08 12:03:28 +01005496 /* Reset in pointers to default state for TLS/DTLS records,
5497 * assuming no CID and no offset between record content and
5498 * record plaintext. */
5499 ssl_update_in_pointers( ssl );
5500
5501 /* Ensure that we have enough space available for the default form
5502 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5503 * with no space for CIDs counted in). */
5504 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5505 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005508 return( ret );
5509 }
5510
5511 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5515 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005516 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005517 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5518 {
5519 ret = ssl_buffer_future_record( ssl );
5520 if( ret != 0 )
5521 return( ret );
5522
5523 /* Fall through to handling of unexpected records */
5524 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5525 }
5526
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005527 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5528 {
5529 /* Skip unexpected record (but not whole datagram) */
5530 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005531 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005532
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5534 "(header)" ) );
5535 }
5536 else
5537 {
5538 /* Skip invalid record and the rest of the datagram */
5539 ssl->next_record_offset = 0;
5540 ssl->in_left = 0;
5541
5542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5543 "(header)" ) );
5544 }
5545
5546 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005547 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005548 }
5549#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005550 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005551 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005552
5553 /*
5554 * Read and optionally decrypt the message contents
5555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005557 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005560 return( ret );
5561 }
5562
5563 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005566 {
Hanno Becker43395762019-05-03 14:46:38 +01005567 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005568 if( ssl->next_record_offset < ssl->in_left )
5569 {
5570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5571 }
5572 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005573 else
5574#endif
5575 ssl->in_left = 0;
5576
5577 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005580 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005581 {
5582 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005583 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005584 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005585 /* Except when waiting for Finished as a bad mac here
5586 * probably means something went wrong in the handshake
5587 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5588 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5589 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5590 {
5591#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5592 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5593 {
5594 mbedtls_ssl_send_alert_message( ssl,
5595 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5596 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5597 }
5598#endif
5599 return( ret );
5600 }
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005603 if( ssl->conf->badmac_limit != 0 &&
5604 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5607 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005608 }
5609#endif
5610
Hanno Becker4a810fb2017-05-24 16:27:30 +01005611 /* As above, invalid records cause
5612 * dismissal of the whole datagram. */
5613
5614 ssl->next_record_offset = 0;
5615 ssl->in_left = 0;
5616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005618 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005619 }
5620
5621 return( ret );
5622 }
5623 else
5624#endif
5625 {
5626 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5628 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 mbedtls_ssl_send_alert_message( ssl,
5631 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5632 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005633 }
5634#endif
5635 return( ret );
5636 }
5637 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005638
Simon Butcher99000142016-10-13 17:21:01 +01005639 return( 0 );
5640}
5641
5642int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5643{
5644 int ret;
5645
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005646 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005647 * Handle particular types of records
5648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 {
Simon Butcher99000142016-10-13 17:21:01 +01005651 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5652 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005653 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005655 }
5656
Hanno Beckere678eaa2018-08-21 14:57:46 +01005657 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005658 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005659 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005660 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5662 ssl->in_msglen ) );
5663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005664 }
5665
Hanno Beckere678eaa2018-08-21 14:57:46 +01005666 if( ssl->in_msg[0] != 1 )
5667 {
5668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5669 ssl->in_msg[0] ) );
5670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5671 }
5672
5673#if defined(MBEDTLS_SSL_PROTO_DTLS)
5674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5675 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5676 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5677 {
5678 if( ssl->handshake == NULL )
5679 {
5680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5681 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5682 }
5683
5684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5685 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5686 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005687#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005688 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005691 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005692 if( ssl->in_msglen != 2 )
5693 {
5694 /* Note: Standard allows for more than one 2 byte alert
5695 to be packed in a single message, but Mbed TLS doesn't
5696 currently support this. */
5697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5698 ssl->in_msglen ) );
5699 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5700 }
5701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 ssl->in_msg[0], ssl->in_msg[1] ) );
5704
5705 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005706 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005711 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 }
5714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5716 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5719 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005721
5722#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5723 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5724 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5725 {
Hanno Becker90333da2017-10-10 11:27:13 +01005726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005727 /* Will be handled when trying to parse ServerHello */
5728 return( 0 );
5729 }
5730#endif
5731
5732#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5733 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5734 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5735 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5736 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5737 {
5738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5739 /* Will be handled in mbedtls_ssl_parse_certificate() */
5740 return( 0 );
5741 }
5742#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5743
5744 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005745 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005746 }
5747
Hanno Beckerc76c6192017-06-06 10:03:17 +01005748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005750 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005751 /* Drop unexpected ApplicationData records,
5752 * except at the beginning of renegotiations */
5753 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5754 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5755#if defined(MBEDTLS_SSL_RENEGOTIATION)
5756 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5757 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005758#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005759 )
5760 {
5761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5762 return( MBEDTLS_ERR_SSL_NON_FATAL );
5763 }
5764
5765 if( ssl->handshake != NULL &&
5766 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5767 {
5768 ssl_handshake_wrapup_free_hs_transform( ssl );
5769 }
5770 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005771#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005772
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 return( 0 );
5774}
5775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005777{
5778 int ret;
5779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5781 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5782 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005783 {
5784 return( ret );
5785 }
5786
5787 return( 0 );
5788}
5789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005791 unsigned char level,
5792 unsigned char message )
5793{
5794 int ret;
5795
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005796 if( ssl == NULL || ssl->conf == NULL )
5797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005803 ssl->out_msglen = 2;
5804 ssl->out_msg[0] = level;
5805 ssl->out_msg[1] = message;
5806
Hanno Becker67bc7c32018-08-06 11:33:50 +01005807 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005810 return( ret );
5811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005813
5814 return( 0 );
5815}
5816
Paul Bakker5121ce52009-01-03 21:22:43 +00005817/*
5818 * Handshake functions
5819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5821 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5822 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5823 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5824 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5825 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5826 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005827/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005829{
Hanno Becker8759e162017-12-27 21:34:08 +00005830 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5835 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005836 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5837 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005840 ssl->state++;
5841 return( 0 );
5842 }
5843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005846}
5847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005849{
Hanno Becker8759e162017-12-27 21:34:08 +00005850 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5855 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005856 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5857 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005860 ssl->state++;
5861 return( 0 );
5862 }
5863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005866}
Gilles Peskinef9828522017-05-03 12:28:43 +02005867
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005868#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005869/* Some certificate support -> implement write and parse */
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005874 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005876 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5881 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005882 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5883 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005886 ssl->state++;
5887 return( 0 );
5888 }
5889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005891 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005892 {
5893 if( ssl->client_auth == 0 )
5894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 ssl->state++;
5897 return( 0 );
5898 }
5899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005901 /*
5902 * If using SSLv3 and got no cert, send an Alert message
5903 * (otherwise an empty Certificate message will be sent).
5904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5906 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 {
5908 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5910 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5911 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005914 goto write_msg;
5915 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005917 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918#endif /* MBEDTLS_SSL_CLI_C */
5919#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005920 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5925 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 }
5927 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005931
5932 /*
5933 * 0 . 0 handshake type
5934 * 1 . 3 handshake length
5935 * 4 . 6 length of all certs
5936 * 7 . 9 length of cert. 1
5937 * 10 . n-1 peer certificate
5938 * n . n+2 length of cert. 2
5939 * n+3 . ... upper level cert, etc.
5940 */
5941 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005943
Paul Bakker29087132010-03-21 21:03:34 +00005944 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005945 {
5946 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005947 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005950 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005952 }
5953
5954 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5955 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5956 ssl->out_msg[i + 2] = (unsigned char)( n );
5957
5958 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5959 i += n; crt = crt->next;
5960 }
5961
5962 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5963 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5964 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5965
5966 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5968 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005970#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005971write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005972#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005973
5974 ssl->state++;
5975
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005976 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005977 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005979 return( ret );
5980 }
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005983
Paul Bakkered27a042013-04-18 22:46:23 +02005984 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005985}
5986
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005987/*
5988 * Once the certificate message is read, parse it into a cert chain and
5989 * perform basic checks, but leave actual verification to the caller
5990 */
5991static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005993 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005994 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005995 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#if defined(MBEDTLS_SSL_SRV_C)
5998#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005999 /*
6000 * Check if the client sent an empty certificate
6001 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006004 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006005 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6007 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6008 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006011
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006012 /* The client was asked for a certificate but didn't send
6013 one. The client should know what's going on, so we
6014 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006015 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006016 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 }
6018 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6022 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006023 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6027 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6028 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6029 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006033 /* The client was asked for a certificate but didn't send
6034 one. The client should know what's going on, so we
6035 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006036 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006037 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006038 }
6039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6041 MBEDTLS_SSL_PROTO_TLS1_2 */
6042#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006047 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6048 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 }
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6053 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006056 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6057 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 }
6060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006062
Paul Bakker5121ce52009-01-03 21:22:43 +00006063 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006065 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006066 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006068 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006072 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6073 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 }
6076
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006077 /* In case we tried to reuse a session but it failed */
6078 if( ssl->session_negotiate->peer_cert != NULL )
6079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6081 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006082 }
6083
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006084 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006089 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6090 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006091 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 }
6093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006095
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006096 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
6098 while( i < ssl->in_hslen )
6099 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006100 if ( i + 3 > ssl->in_hslen ) {
6101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6103 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6104 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6105 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 if( ssl->in_msg[i] != 0 )
6107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6110 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 }
6113
6114 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6115 | (unsigned int) ssl->in_msg[i + 2];
6116 i += 3;
6117
6118 if( n < 128 || i + n > ssl->in_hslen )
6119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006121 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6122 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 }
6125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006127 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006128 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006130 case 0: /*ok*/
6131 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6132 /* Ignore certificate with an unknown algorithm: maybe a
6133 prior certificate was already trusted. */
6134 break;
6135
6136 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6137 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6138 goto crt_parse_der_failed;
6139
6140 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6141 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6142 goto crt_parse_der_failed;
6143
6144 default:
6145 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6146 crt_parse_der_failed:
6147 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006149 return( ret );
6150 }
6151
6152 i += n;
6153 }
6154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006156
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006157 /*
6158 * On client, make sure the server cert doesn't change during renego to
6159 * avoid "triple handshake" attack: https://secure-resumption.com/
6160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006162 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006164 {
6165 if( ssl->session->peer_cert == NULL )
6166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006168 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6169 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006171 }
6172
6173 if( ssl->session->peer_cert->raw.len !=
6174 ssl->session_negotiate->peer_cert->raw.len ||
6175 memcmp( ssl->session->peer_cert->raw.p,
6176 ssl->session_negotiate->peer_cert->raw.p,
6177 ssl->session->peer_cert->raw.len ) != 0 )
6178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6181 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006183 }
6184 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006186
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006187 return( 0 );
6188}
6189
6190int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6191{
6192 int ret;
6193 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006194 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006195#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6196 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6197 ? ssl->handshake->sni_authmode
6198 : ssl->conf->authmode;
6199#else
6200 const int authmode = ssl->conf->authmode;
6201#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006202 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006203
6204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6205
6206 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6207 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6208 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6209 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6210 {
6211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6212 ssl->state++;
6213 return( 0 );
6214 }
6215
6216#if defined(MBEDTLS_SSL_SRV_C)
6217 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6218 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6219 {
6220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6221 ssl->state++;
6222 return( 0 );
6223 }
6224
6225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6226 authmode == MBEDTLS_SSL_VERIFY_NONE )
6227 {
6228 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006230
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006231 ssl->state++;
6232 return( 0 );
6233 }
6234#endif
6235
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006236#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6237 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006238 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006239 {
6240 goto crt_verify;
6241 }
6242#endif
6243
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006244 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006245 {
6246 /* mbedtls_ssl_read_record may have sent an alert already. We
6247 let it decide whether to alert. */
6248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6249 return( ret );
6250 }
6251
6252 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6253 {
6254#if defined(MBEDTLS_SSL_SRV_C)
6255 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6256 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6257 {
6258 ret = 0;
6259 }
6260#endif
6261
6262 ssl->state++;
6263 return( ret );
6264 }
6265
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006266#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6267 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006268 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006269
6270crt_verify:
6271 if( ssl->handshake->ecrs_enabled)
6272 rs_ctx = &ssl->handshake->ecrs_ctx;
6273#endif
6274
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006275 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006276 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006277 mbedtls_x509_crt *ca_chain;
6278 mbedtls_x509_crl *ca_crl;
6279
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006280#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006281 if( ssl->handshake->sni_ca_chain != NULL )
6282 {
6283 ca_chain = ssl->handshake->sni_ca_chain;
6284 ca_crl = ssl->handshake->sni_ca_crl;
6285 }
6286 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006287#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006288 {
6289 ca_chain = ssl->conf->ca_chain;
6290 ca_crl = ssl->conf->ca_crl;
6291 }
6292
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006293 /*
6294 * Main check: verify certificate
6295 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006296 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006297 ssl->session_negotiate->peer_cert,
6298 ca_chain, ca_crl,
6299 ssl->conf->cert_profile,
6300 ssl->hostname,
6301 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006302 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006303
6304 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006307 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006308
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006309#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6310 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006311 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006312#endif
6313
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006314 /*
6315 * Secondary checks: always done, but change 'ret' only if it was 0
6316 */
6317
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006318#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006321
6322 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006324 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006325 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006326 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006329 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006331 }
6332 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006333#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006336 ciphersuite_info,
6337 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006338 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006341 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006343 }
6344
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006345 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6346 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6347 * with details encoded in the verification flags. All other kinds
6348 * of error codes, including those from the user provided f_vrfy
6349 * functions, are treated as fatal and lead to a failure of
6350 * ssl_parse_certificate even if verification was optional. */
6351 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6352 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6353 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6354 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006356 }
6357
6358 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6359 {
6360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6361 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6362 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006363
6364 if( ret != 0 )
6365 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006366 uint8_t alert;
6367
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006368 /* The certificate may have been rejected for several reasons.
6369 Pick one and send the corresponding alert. Which alert to send
6370 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006371 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6372 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6373 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6374 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6375 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6376 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6377 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6378 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6379 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6380 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6381 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6382 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6383 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6384 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6385 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6386 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6387 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6388 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6389 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6390 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006391 else
6392 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006393 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6394 alert );
6395 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006396
Hanno Beckere6706e62017-05-15 16:05:15 +01006397#if defined(MBEDTLS_DEBUG_C)
6398 if( ssl->session_negotiate->verify_result != 0 )
6399 {
6400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6401 ssl->session_negotiate->verify_result ) );
6402 }
6403 else
6404 {
6405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6406 }
6407#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006408 }
6409
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006410 ssl->state++;
6411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006413
6414 return( ret );
6415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6417 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6418 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6419 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6420 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6421 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6422 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006425{
6426 int ret;
6427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006431 ssl->out_msglen = 1;
6432 ssl->out_msg[0] = 1;
6433
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 ssl->state++;
6435
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006436 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439 return( ret );
6440 }
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006443
6444 return( 0 );
6445}
6446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006448{
6449 int ret;
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006452
Hanno Becker327c93b2018-08-15 13:56:18 +01006453 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006456 return( ret );
6457 }
6458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6463 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006465 }
6466
Hanno Beckere678eaa2018-08-21 14:57:46 +01006467 /* CCS records are only accepted if they have length 1 and content '1',
6468 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006470 /*
6471 * Switch to our negotiated transform and session parameters for inbound
6472 * data.
6473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006475 ssl->transform_in = ssl->transform_negotiate;
6476 ssl->session_in = ssl->session_negotiate;
6477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006479 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006482 ssl_dtls_replay_reset( ssl );
6483#endif
6484
6485 /* Increment epoch */
6486 if( ++ssl->in_epoch == 0 )
6487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006489 /* This is highly unlikely to happen for legitimate reasons, so
6490 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006492 }
6493 }
6494 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006496 memset( ssl->in_ctr, 0, 8 );
6497
Hanno Beckerf5970a02019-05-08 09:38:41 +01006498 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6501 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006506 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6507 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006509 }
6510 }
6511#endif
6512
Paul Bakker5121ce52009-01-03 21:22:43 +00006513 ssl->state++;
6514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006516
6517 return( 0 );
6518}
6519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6521 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006522{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006523 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6526 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6527 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006528 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006529 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6532#if defined(MBEDTLS_SHA512_C)
6533 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006534 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6535 else
6536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if defined(MBEDTLS_SHA256_C)
6538 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006539 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006540 else
6541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006545 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006546 }
Paul Bakker380da532012-04-18 16:10:25 +00006547}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6552 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006553 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6554 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6557#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006558 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006561 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006564}
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006567 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6570 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006571 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6572 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006573#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6575#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006576 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006579 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006582}
6583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6585 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6586static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006587 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006588{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006589 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6590 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006591}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006592#endif
Paul Bakker380da532012-04-18 16:10:25 +00006593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6595#if defined(MBEDTLS_SHA256_C)
6596static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006597 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006598{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006599 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006600}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006601#endif
Paul Bakker380da532012-04-18 16:10:25 +00006602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603#if defined(MBEDTLS_SHA512_C)
6604static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006605 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006606{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006607 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006608}
Paul Bakker769075d2012-11-24 11:26:46 +01006609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006613static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006615{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006616 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006617 mbedtls_md5_context md5;
6618 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006619
Paul Bakker5121ce52009-01-03 21:22:43 +00006620 unsigned char padbuf[48];
6621 unsigned char md5sum[16];
6622 unsigned char sha1sum[20];
6623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006625 if( !session )
6626 session = ssl->session;
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006629
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006630 mbedtls_md5_init( &md5 );
6631 mbedtls_sha1_init( &sha1 );
6632
6633 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6634 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635
6636 /*
6637 * SSLv3:
6638 * hash =
6639 * MD5( master + pad2 +
6640 * MD5( handshake + sender + master + pad1 ) )
6641 * + SHA1( master + pad2 +
6642 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006643 */
6644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006646 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6647 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006648#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006651 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6652 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006656 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006657
Paul Bakker1ef83d62012-04-11 12:09:53 +00006658 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006660 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6661 mbedtls_md5_update_ret( &md5, session->master, 48 );
6662 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6663 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006664
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006665 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6666 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6667 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6668 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
Paul Bakker1ef83d62012-04-11 12:09:53 +00006670 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006672 mbedtls_md5_starts_ret( &md5 );
6673 mbedtls_md5_update_ret( &md5, session->master, 48 );
6674 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6675 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6676 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006678 mbedtls_sha1_starts_ret( &sha1 );
6679 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6680 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6681 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6682 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006685
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006686 mbedtls_md5_free( &md5 );
6687 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006688
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006689 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6690 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6691 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006698static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006700{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006701 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006702 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006703 mbedtls_md5_context md5;
6704 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006705 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006708 if( !session )
6709 session = ssl->session;
6710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006712
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006713 mbedtls_md5_init( &md5 );
6714 mbedtls_sha1_init( &sha1 );
6715
6716 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6717 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006718
Paul Bakker1ef83d62012-04-11 12:09:53 +00006719 /*
6720 * TLSv1:
6721 * hash = PRF( master, finished_label,
6722 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6723 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006726 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6727 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006728#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006731 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6732 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006733#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006736 ? "client finished"
6737 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006738
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006739 mbedtls_md5_finish_ret( &md5, padbuf );
6740 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006741
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006742 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006743 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006746
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006747 mbedtls_md5_free( &md5 );
6748 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006749
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006750 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6757#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006758static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006760{
6761 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006762 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006763 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006764 unsigned char padbuf[32];
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006767 if( !session )
6768 session = ssl->session;
6769
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006770 mbedtls_sha256_init( &sha256 );
6771
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006774 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006775
6776 /*
6777 * TLSv1.2:
6778 * hash = PRF( master, finished_label,
6779 * Hash( handshake ) )[0.11]
6780 */
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782#if !defined(MBEDTLS_SHA256_ALT)
6783 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006784 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006785#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006788 ? "client finished"
6789 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006790
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006791 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006792
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006793 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006794 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006797
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006798 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006799
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006800 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006803}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006807static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006809{
6810 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006811 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006812 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006813 unsigned char padbuf[48];
6814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006816 if( !session )
6817 session = ssl->session;
6818
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006819 mbedtls_sha512_init( &sha512 );
6820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006822
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006823 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006824
6825 /*
6826 * TLSv1.2:
6827 * hash = PRF( master, finished_label,
6828 * Hash( handshake ) )[0.11]
6829 */
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006832 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6833 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006834#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006837 ? "client finished"
6838 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006839
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006840 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006841
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006842 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006843 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006846
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006847 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006848
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006849 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853#endif /* MBEDTLS_SHA512_C */
6854#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006857{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006859
6860 /*
6861 * Free our handshake params
6862 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006863 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006865 ssl->handshake = NULL;
6866
6867 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006868 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006869 */
6870 if( ssl->transform )
6871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 mbedtls_ssl_transform_free( ssl->transform );
6873 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006874 }
6875 ssl->transform = ssl->transform_negotiate;
6876 ssl->transform_negotiate = NULL;
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006879}
6880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006882{
6883 int resume = ssl->handshake->resume;
6884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SSL_RENEGOTIATION)
6888 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006891 ssl->renego_records_seen = 0;
6892 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006893#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006894
6895 /*
6896 * Free the previous session and switch in the current one
6897 */
Paul Bakker0a597072012-09-25 21:55:46 +00006898 if( ssl->session )
6899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006901 /* RFC 7366 3.1: keep the EtM state */
6902 ssl->session_negotiate->encrypt_then_mac =
6903 ssl->session->encrypt_then_mac;
6904#endif
6905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 mbedtls_ssl_session_free( ssl->session );
6907 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006908 }
6909 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006910 ssl->session_negotiate = NULL;
6911
Paul Bakker0a597072012-09-25 21:55:46 +00006912 /*
6913 * Add cache entry
6914 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006915 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006916 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006917 resume == 0 )
6918 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006919 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006921 }
Paul Bakker0a597072012-09-25 21:55:46 +00006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006924 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006925 ssl->handshake->flight != NULL )
6926 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006927 /* Cancel handshake timer */
6928 ssl_set_timer( ssl, 0 );
6929
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006930 /* Keep last flight around in case we need to resend it:
6931 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006933 }
6934 else
6935#endif
6936 ssl_handshake_wrapup_free_hs_transform( ssl );
6937
Paul Bakker48916f92012-09-16 19:57:18 +00006938 ssl->state++;
6939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006941}
6942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006945 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006948
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006949 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006950
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006951 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006952
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006953 /*
6954 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6955 * may define some other value. Currently (early 2016), no defined
6956 * ciphersuite does this (and this is unlikely to change as activity has
6957 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006962 ssl->verify_data_len = hash_len;
6963 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006964#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006965
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6968 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006969
6970 /*
6971 * In case of session resuming, invert the client and server
6972 * ChangeCipherSpec messages order.
6973 */
Paul Bakker0a597072012-09-25 21:55:46 +00006974 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006977 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006981 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006983#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 }
6985 else
6986 ssl->state++;
6987
Paul Bakker48916f92012-09-16 19:57:18 +00006988 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006989 * Switch to our negotiated transform and session parameters for outbound
6990 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006996 {
6997 unsigned char i;
6998
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006999 /* Remember current epoch settings for resending */
7000 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007001 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007002
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007003 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007004 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007005
7006 /* Increment epoch */
7007 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007008 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007009 break;
7010
7011 /* The loop goes to its end iff the counter is wrapping */
7012 if( i == 0 )
7013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7015 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007016 }
7017 }
7018 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007020 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007022 ssl->transform_out = ssl->transform_negotiate;
7023 ssl->session_out = ssl->session_negotiate;
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7026 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7031 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007032 }
7033 }
7034#endif
7035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007039#endif
7040
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007041 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044 return( ret );
7045 }
7046
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007047#if defined(MBEDTLS_SSL_PROTO_DTLS)
7048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7049 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7050 {
7051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7052 return( ret );
7053 }
7054#endif
7055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
7058 return( 0 );
7059}
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007062#define SSL_MAX_HASH_LEN 36
7063#else
7064#define SSL_MAX_HASH_LEN 12
7065#endif
7066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007068{
Paul Bakker23986e52011-04-24 08:57:21 +00007069 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007070 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007071 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007074
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007075 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076
Hanno Becker327c93b2018-08-15 13:56:18 +01007077 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 return( ret );
7081 }
7082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007086 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7087 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089 }
7090
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007091 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#if defined(MBEDTLS_SSL_PROTO_SSL3)
7093 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007094 hash_len = 36;
7095 else
7096#endif
7097 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7100 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106 }
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007109 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007115 }
7116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007118 ssl->verify_data_len = hash_len;
7119 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007120#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007121
Paul Bakker0a597072012-09-25 21:55:46 +00007122 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007125 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007129 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007132 }
7133 else
7134 ssl->state++;
7135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007137 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007139#endif
7140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007142
7143 return( 0 );
7144}
7145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7151 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7152 mbedtls_md5_init( &handshake->fin_md5 );
7153 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007154 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7155 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7158#if defined(MBEDTLS_SHA256_C)
7159 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007160 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if defined(MBEDTLS_SHA512_C)
7163 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007164 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007167
7168 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007169
7170#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7171 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7172 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7173#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175#if defined(MBEDTLS_DHM_C)
7176 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#if defined(MBEDTLS_ECDH_C)
7179 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007180#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007181#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007182 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007183#if defined(MBEDTLS_SSL_CLI_C)
7184 handshake->ecjpake_cache = NULL;
7185 handshake->ecjpake_cache_len = 0;
7186#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007187#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007188
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007189#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007190 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007191#endif
7192
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007193#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7194 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7195#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007196}
7197
Hanno Becker611a83b2018-01-03 14:27:32 +00007198void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7203 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007204
Hanno Becker92231322018-01-03 15:32:51 +00007205#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 mbedtls_md_init( &transform->md_ctx_enc );
7207 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007208#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007209}
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007214}
7215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007217{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007218 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007219 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007221 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007223 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007224 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007225
7226 /*
7227 * Either the pointers are now NULL or cleared properly and can be freed.
7228 * Now allocate missing structures.
7229 */
7230 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007231 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007232 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007233 }
Paul Bakker48916f92012-09-16 19:57:18 +00007234
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007235 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007236 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007237 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007238 }
Paul Bakker48916f92012-09-16 19:57:18 +00007239
Paul Bakker82788fb2014-10-20 13:59:19 +02007240 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007241 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007242 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007243 }
Paul Bakker48916f92012-09-16 19:57:18 +00007244
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007245 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007246 if( ssl->handshake == NULL ||
7247 ssl->transform_negotiate == NULL ||
7248 ssl->session_negotiate == NULL )
7249 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 mbedtls_free( ssl->handshake );
7253 mbedtls_free( ssl->transform_negotiate );
7254 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007255
7256 ssl->handshake = NULL;
7257 ssl->transform_negotiate = NULL;
7258 ssl->session_negotiate = NULL;
7259
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007261 }
7262
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007263 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007265 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007266 ssl_handshake_params_init( ssl->handshake );
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7270 {
7271 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007272
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007273 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7274 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7275 else
7276 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007277
7278 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007279 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007280#endif
7281
Paul Bakker48916f92012-09-16 19:57:18 +00007282 return( 0 );
7283}
7284
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007285#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007286/* Dummy cookie callbacks for defaults */
7287static int ssl_cookie_write_dummy( void *ctx,
7288 unsigned char **p, unsigned char *end,
7289 const unsigned char *cli_id, size_t cli_id_len )
7290{
7291 ((void) ctx);
7292 ((void) p);
7293 ((void) end);
7294 ((void) cli_id);
7295 ((void) cli_id_len);
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007298}
7299
7300static int ssl_cookie_check_dummy( void *ctx,
7301 const unsigned char *cookie, size_t cookie_len,
7302 const unsigned char *cli_id, size_t cli_id_len )
7303{
7304 ((void) ctx);
7305 ((void) cookie);
7306 ((void) cookie_len);
7307 ((void) cli_id);
7308 ((void) cli_id_len);
7309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007311}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007312#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007313
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007314/* Once ssl->out_hdr as the address of the beginning of the
7315 * next outgoing record is set, deduce the other pointers.
7316 *
7317 * Note: For TLS, we save the implicit record sequence number
7318 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7319 * and the caller has to make sure there's space for this.
7320 */
7321
7322static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7323 mbedtls_ssl_transform *transform )
7324{
7325#if defined(MBEDTLS_SSL_PROTO_DTLS)
7326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7327 {
7328 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007330 ssl->out_cid = ssl->out_ctr + 8;
7331 ssl->out_len = ssl->out_cid;
7332 if( transform != NULL )
7333 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007334#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007335 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007337 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007338 }
7339 else
7340#endif
7341 {
7342 ssl->out_ctr = ssl->out_hdr - 8;
7343 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007344#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007345 ssl->out_cid = ssl->out_len;
7346#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007347 ssl->out_iv = ssl->out_hdr + 5;
7348 }
7349
7350 /* Adjust out_msg to make space for explicit IV, if used. */
7351 if( transform != NULL &&
7352 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7353 {
7354 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7355 }
7356 else
7357 ssl->out_msg = ssl->out_iv;
7358}
7359
7360/* Once ssl->in_hdr as the address of the beginning of the
7361 * next incoming record is set, deduce the other pointers.
7362 *
7363 * Note: For TLS, we save the implicit record sequence number
7364 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7365 * and the caller has to make sure there's space for this.
7366 */
7367
Hanno Beckerf5970a02019-05-08 09:38:41 +01007368static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007369{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007370 /* This function sets the pointers to match the case
7371 * of unprotected TLS/DTLS records, with both ssl->in_iv
7372 * and ssl->in_msg pointing to the beginning of the record
7373 * content.
7374 *
7375 * When decrypting a protected record, ssl->in_msg
7376 * will be shifted to point to the beginning of the
7377 * record plaintext.
7378 */
7379
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007380#if defined(MBEDTLS_SSL_PROTO_DTLS)
7381 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7382 {
Hanno Becker70e79282019-05-03 14:34:53 +01007383 /* This sets the header pointers to match records
7384 * without CID. When we receive a record containing
7385 * a CID, the fields are shifted accordingly in
7386 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007387 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007388#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007389 ssl->in_cid = ssl->in_ctr + 8;
7390 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007391#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007392 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007393#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007394 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007395 }
7396 else
7397#endif
7398 {
7399 ssl->in_ctr = ssl->in_hdr - 8;
7400 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007401#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007402 ssl->in_cid = ssl->in_len;
7403#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007404 ssl->in_iv = ssl->in_hdr + 5;
7405 }
7406
Hanno Beckerf5970a02019-05-08 09:38:41 +01007407 /* This will be adjusted at record decryption time. */
7408 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007409}
7410
Paul Bakker5121ce52009-01-03 21:22:43 +00007411/*
7412 * Initialize an SSL context
7413 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007414void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7415{
7416 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7417}
7418
7419/*
7420 * Setup an SSL context
7421 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007422
7423static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7424{
7425 /* Set the incoming and outgoing record pointers. */
7426#if defined(MBEDTLS_SSL_PROTO_DTLS)
7427 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7428 {
7429 ssl->out_hdr = ssl->out_buf;
7430 ssl->in_hdr = ssl->in_buf;
7431 }
7432 else
7433#endif /* MBEDTLS_SSL_PROTO_DTLS */
7434 {
7435 ssl->out_hdr = ssl->out_buf + 8;
7436 ssl->in_hdr = ssl->in_buf + 8;
7437 }
7438
7439 /* Derive other internal pointers. */
7440 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007441 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007442}
7443
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007444int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007445 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007446{
Paul Bakker48916f92012-09-16 19:57:18 +00007447 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007449 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007450
7451 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007452 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007453 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007454
7455 /* Set to NULL in case of an error condition */
7456 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007457
Angus Grattond8213d02016-05-25 20:56:48 +10007458 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7459 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007460 {
Angus Grattond8213d02016-05-25 20:56:48 +10007461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007462 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007463 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007464 }
7465
7466 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7467 if( ssl->out_buf == NULL )
7468 {
7469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007470 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007471 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007472 }
7473
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007474 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007475
Paul Bakker48916f92012-09-16 19:57:18 +00007476 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007477 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007478
7479 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007480
7481error:
7482 mbedtls_free( ssl->in_buf );
7483 mbedtls_free( ssl->out_buf );
7484
7485 ssl->conf = NULL;
7486
7487 ssl->in_buf = NULL;
7488 ssl->out_buf = NULL;
7489
7490 ssl->in_hdr = NULL;
7491 ssl->in_ctr = NULL;
7492 ssl->in_len = NULL;
7493 ssl->in_iv = NULL;
7494 ssl->in_msg = NULL;
7495
7496 ssl->out_hdr = NULL;
7497 ssl->out_ctr = NULL;
7498 ssl->out_len = NULL;
7499 ssl->out_iv = NULL;
7500 ssl->out_msg = NULL;
7501
k-stachowiak9f7798e2018-07-31 16:52:32 +02007502 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007503}
7504
7505/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007506 * Reset an initialized and used SSL context for re-use while retaining
7507 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007508 *
7509 * If partial is non-zero, keep data in the input buffer and client ID.
7510 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007511 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007512static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007513{
Paul Bakker48916f92012-09-16 19:57:18 +00007514 int ret;
7515
Hanno Becker7e772132018-08-10 12:38:21 +01007516#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7517 !defined(MBEDTLS_SSL_SRV_C)
7518 ((void) partial);
7519#endif
7520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007522
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007523 /* Cancel any possibly running timer */
7524 ssl_set_timer( ssl, 0 );
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SSL_RENEGOTIATION)
7527 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007528 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007529
7530 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7532 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007535
Paul Bakker7eb013f2011-10-06 12:37:39 +00007536 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007537 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007538
7539 ssl->in_msgtype = 0;
7540 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007542 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007543 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007546 ssl_dtls_replay_reset( ssl );
7547#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007548
7549 ssl->in_hslen = 0;
7550 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007551
7552 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007553
7554 ssl->out_msgtype = 0;
7555 ssl->out_msglen = 0;
7556 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7558 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007559 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007560#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007561
Hanno Becker19859472018-08-06 09:40:20 +01007562 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7563
Paul Bakker48916f92012-09-16 19:57:18 +00007564 ssl->transform_in = NULL;
7565 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007566
Hanno Becker78640902018-08-13 16:35:15 +01007567 ssl->session_in = NULL;
7568 ssl->session_out = NULL;
7569
Angus Grattond8213d02016-05-25 20:56:48 +10007570 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007571
7572#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007573 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007574#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7575 {
7576 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007577 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007578 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7581 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7584 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7587 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007588 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007589 }
7590#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007591
Paul Bakker48916f92012-09-16 19:57:18 +00007592 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 mbedtls_ssl_transform_free( ssl->transform );
7595 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007596 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007597 }
Paul Bakker48916f92012-09-16 19:57:18 +00007598
Paul Bakkerc0463502013-02-14 11:19:38 +01007599 if( ssl->session )
7600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 mbedtls_ssl_session_free( ssl->session );
7602 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007603 ssl->session = NULL;
7604 }
7605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007607 ssl->alpn_chosen = NULL;
7608#endif
7609
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007610#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007611#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007612 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007613#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007614 {
7615 mbedtls_free( ssl->cli_id );
7616 ssl->cli_id = NULL;
7617 ssl->cli_id_len = 0;
7618 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007619#endif
7620
Paul Bakker48916f92012-09-16 19:57:18 +00007621 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7622 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007623
7624 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007625}
7626
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007627/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007628 * Reset an initialized and used SSL context for re-use while retaining
7629 * all application-set variables, function pointers and data.
7630 */
7631int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7632{
7633 return( ssl_session_reset_int( ssl, 0 ) );
7634}
7635
7636/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007637 * SSL set accessors
7638 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007639void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007641 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007642}
7643
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007644void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007645{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007646 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007647}
7648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007650void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007651{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007652 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007653}
7654#endif
7655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007657void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007659 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007660}
7661#endif
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007664
Hanno Becker1841b0a2018-08-24 11:13:57 +01007665void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7666 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007667{
7668 ssl->disable_datagram_packing = !allow_packing;
7669}
7670
7671void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7672 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007674 conf->hs_timeout_min = min;
7675 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007676}
7677#endif
7678
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007679void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007681 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007682}
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007685void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007686 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007687 void *p_vrfy )
7688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007689 conf->f_vrfy = f_vrfy;
7690 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007693
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007694void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007695 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007696 void *p_rng )
7697{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007698 conf->f_rng = f_rng;
7699 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007700}
7701
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007702void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007703 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007704 void *p_dbg )
7705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007706 conf->f_dbg = f_dbg;
7707 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007708}
7709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007711 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007712 mbedtls_ssl_send_t *f_send,
7713 mbedtls_ssl_recv_t *f_recv,
7714 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007715{
7716 ssl->p_bio = p_bio;
7717 ssl->f_send = f_send;
7718 ssl->f_recv = f_recv;
7719 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007720}
7721
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007722#if defined(MBEDTLS_SSL_PROTO_DTLS)
7723void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7724{
7725 ssl->mtu = mtu;
7726}
7727#endif
7728
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007729void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007730{
7731 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007732}
7733
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007734void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7735 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007736 mbedtls_ssl_set_timer_t *f_set_timer,
7737 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007738{
7739 ssl->p_timer = p_timer;
7740 ssl->f_set_timer = f_set_timer;
7741 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007742
7743 /* Make sure we start with no timer running */
7744 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007745}
7746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007748void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007749 void *p_cache,
7750 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7751 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007752{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007753 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007754 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007755 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#if defined(MBEDTLS_SSL_CLI_C)
7760int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007761{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007762 int ret;
7763
7764 if( ssl == NULL ||
7765 session == NULL ||
7766 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007767 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007770 }
7771
7772 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7773 return( ret );
7774
Paul Bakker0a597072012-09-25 21:55:46 +00007775 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007776
7777 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007780
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007781void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007782 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007783{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007784 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7785 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7786 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7787 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007788}
7789
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007790void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007791 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007792 int major, int minor )
7793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007795 return;
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007798 return;
7799
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007800 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007801}
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007804void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007805 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007806{
7807 conf->cert_profile = profile;
7808}
7809
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007810/* Append a new keycert entry to a (possibly empty) list */
7811static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7812 mbedtls_x509_crt *cert,
7813 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007814{
niisato8ee24222018-06-25 19:05:48 +09007815 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007816
niisato8ee24222018-06-25 19:05:48 +09007817 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7818 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007820
niisato8ee24222018-06-25 19:05:48 +09007821 new_cert->cert = cert;
7822 new_cert->key = key;
7823 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007824
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007825 /* Update head is the list was null, else add to the end */
7826 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007827 {
niisato8ee24222018-06-25 19:05:48 +09007828 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007829 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007830 else
7831 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007832 mbedtls_ssl_key_cert *cur = *head;
7833 while( cur->next != NULL )
7834 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007835 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007836 }
7837
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007838 return( 0 );
7839}
7840
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007841int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007842 mbedtls_x509_crt *own_cert,
7843 mbedtls_pk_context *pk_key )
7844{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007845 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007846}
7847
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007848void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007849 mbedtls_x509_crt *ca_chain,
7850 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007851{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007852 conf->ca_chain = ca_chain;
7853 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007856
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007857#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7858int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7859 mbedtls_x509_crt *own_cert,
7860 mbedtls_pk_context *pk_key )
7861{
7862 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7863 own_cert, pk_key ) );
7864}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007865
7866void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7867 mbedtls_x509_crt *ca_chain,
7868 mbedtls_x509_crl *ca_crl )
7869{
7870 ssl->handshake->sni_ca_chain = ca_chain;
7871 ssl->handshake->sni_ca_crl = ca_crl;
7872}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007873
7874void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7875 int authmode )
7876{
7877 ssl->handshake->sni_authmode = authmode;
7878}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007879#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7880
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007881#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007882/*
7883 * Set EC J-PAKE password for current handshake
7884 */
7885int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7886 const unsigned char *pw,
7887 size_t pw_len )
7888{
7889 mbedtls_ecjpake_role role;
7890
Janos Follath8eb64132016-06-03 15:40:57 +01007891 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7893
7894 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7895 role = MBEDTLS_ECJPAKE_SERVER;
7896 else
7897 role = MBEDTLS_ECJPAKE_CLIENT;
7898
7899 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7900 role,
7901 MBEDTLS_MD_SHA256,
7902 MBEDTLS_ECP_DP_SECP256R1,
7903 pw, pw_len ) );
7904}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007905#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007908int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007909 const unsigned char *psk, size_t psk_len,
7910 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007911{
Paul Bakker6db455e2013-09-18 17:29:31 +02007912 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007917
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007918 /* Identity len will be encoded on two bytes */
7919 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007920 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007921 {
7922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7923 }
7924
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007925 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007926 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007927 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007928
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007929 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007930 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007931 conf->psk_len = 0;
7932 }
7933 if( conf->psk_identity != NULL )
7934 {
7935 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007936 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007937 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007938 }
7939
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007940 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7941 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007942 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007943 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007944 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007945 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007946 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007948 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007949
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007950 conf->psk_len = psk_len;
7951 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007952
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007953 memcpy( conf->psk, psk, conf->psk_len );
7954 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007955
7956 return( 0 );
7957}
7958
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007959int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7960 const unsigned char *psk, size_t psk_len )
7961{
7962 if( psk == NULL || ssl->handshake == NULL )
7963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7964
7965 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7967
7968 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007969 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007970 mbedtls_platform_zeroize( ssl->handshake->psk,
7971 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007972 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007973 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007974 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007975
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007976 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007977 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007978
7979 ssl->handshake->psk_len = psk_len;
7980 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7981
7982 return( 0 );
7983}
7984
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007985void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007987 size_t),
7988 void *p_psk )
7989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007990 conf->f_psk = f_psk;
7991 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007994
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007995#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007996
7997#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007998int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007999{
8000 int ret;
8001
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008002 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8003 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8004 {
8005 mbedtls_mpi_free( &conf->dhm_P );
8006 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008007 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008008 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008009
8010 return( 0 );
8011}
Hanno Becker470a8c42017-10-04 15:28:46 +01008012#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008013
Hanno Beckera90658f2017-10-04 15:29:08 +01008014int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8015 const unsigned char *dhm_P, size_t P_len,
8016 const unsigned char *dhm_G, size_t G_len )
8017{
8018 int ret;
8019
8020 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8021 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8022 {
8023 mbedtls_mpi_free( &conf->dhm_P );
8024 mbedtls_mpi_free( &conf->dhm_G );
8025 return( ret );
8026 }
8027
8028 return( 0 );
8029}
Paul Bakker5121ce52009-01-03 21:22:43 +00008030
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008031int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008032{
8033 int ret;
8034
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008035 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8036 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8037 {
8038 mbedtls_mpi_free( &conf->dhm_P );
8039 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008040 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008041 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008042
8043 return( 0 );
8044}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008045#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008046
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008047#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8048/*
8049 * Set the minimum length for Diffie-Hellman parameters
8050 */
8051void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8052 unsigned int bitlen )
8053{
8054 conf->dhm_min_bitlen = bitlen;
8055}
8056#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8057
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008058#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008059/*
8060 * Set allowed/preferred hashes for handshake signatures
8061 */
8062void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8063 const int *hashes )
8064{
8065 conf->sig_hashes = hashes;
8066}
Hanno Becker947194e2017-04-07 13:25:49 +01008067#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008068
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008069#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008070/*
8071 * Set the allowed elliptic curves
8072 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008073void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008074 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008076 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008077}
Hanno Becker947194e2017-04-07 13:25:49 +01008078#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008079
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008080#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008082{
Hanno Becker947194e2017-04-07 13:25:49 +01008083 /* Initialize to suppress unnecessary compiler warning */
8084 size_t hostname_len = 0;
8085
8086 /* Check if new hostname is valid before
8087 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008088 if( hostname != NULL )
8089 {
8090 hostname_len = strlen( hostname );
8091
8092 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8094 }
8095
8096 /* Now it's clear that we will overwrite the old hostname,
8097 * so we can free it safely */
8098
8099 if( ssl->hostname != NULL )
8100 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008101 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008102 mbedtls_free( ssl->hostname );
8103 }
8104
8105 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008106
Paul Bakker5121ce52009-01-03 21:22:43 +00008107 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008108 {
8109 ssl->hostname = NULL;
8110 }
8111 else
8112 {
8113 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008114 if( ssl->hostname == NULL )
8115 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008116
Hanno Becker947194e2017-04-07 13:25:49 +01008117 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008118
Hanno Becker947194e2017-04-07 13:25:49 +01008119 ssl->hostname[hostname_len] = '\0';
8120 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008121
8122 return( 0 );
8123}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008124#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008125
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008126#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008127void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008129 const unsigned char *, size_t),
8130 void *p_sni )
8131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008132 conf->f_sni = f_sni;
8133 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008138int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008139{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008140 size_t cur_len, tot_len;
8141 const char **p;
8142
8143 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008144 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8145 * MUST NOT be truncated."
8146 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008147 */
8148 tot_len = 0;
8149 for( p = protos; *p != NULL; p++ )
8150 {
8151 cur_len = strlen( *p );
8152 tot_len += cur_len;
8153
8154 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008156 }
8157
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008158 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008159
8160 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008161}
8162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008164{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008165 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008168
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008169void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008171 conf->max_major_ver = major;
8172 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008173}
8174
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008175void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008177 conf->min_major_ver = major;
8178 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008179}
8180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008182void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008183{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008184 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008185}
8186#endif
8187
Janos Follath088ce432017-04-10 12:42:31 +01008188#if defined(MBEDTLS_SSL_SRV_C)
8189void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8190 char cert_req_ca_list )
8191{
8192 conf->cert_req_ca_list = cert_req_ca_list;
8193}
8194#endif
8195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008197void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008198{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008199 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008200}
8201#endif
8202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008204void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008205{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008206 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008207}
8208#endif
8209
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008210#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008211void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008213 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008214}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008215#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008218int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008221 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008224 }
8225
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008226 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008227
8228 return( 0 );
8229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008233void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008234{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008235 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008240void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008241{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008242 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008243}
8244#endif
8245
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008246void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008248 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008249}
8250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008252void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008254 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008255}
8256
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008257void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008259 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008260}
8261
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008262void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008263 const unsigned char period[8] )
8264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008265 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008270#if defined(MBEDTLS_SSL_CLI_C)
8271void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008272{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008273 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008274}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008275#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008276
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008277#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008278void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8279 mbedtls_ssl_ticket_write_t *f_ticket_write,
8280 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8281 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008282{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008283 conf->f_ticket_write = f_ticket_write;
8284 conf->f_ticket_parse = f_ticket_parse;
8285 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008286}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008287#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008289
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008290#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8291void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8292 mbedtls_ssl_export_keys_t *f_export_keys,
8293 void *p_export_keys )
8294{
8295 conf->f_export_keys = f_export_keys;
8296 conf->p_export_keys = p_export_keys;
8297}
8298#endif
8299
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008300#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008301void mbedtls_ssl_conf_async_private_cb(
8302 mbedtls_ssl_config *conf,
8303 mbedtls_ssl_async_sign_t *f_async_sign,
8304 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8305 mbedtls_ssl_async_resume_t *f_async_resume,
8306 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008307 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008308{
8309 conf->f_async_sign_start = f_async_sign;
8310 conf->f_async_decrypt_start = f_async_decrypt;
8311 conf->f_async_resume = f_async_resume;
8312 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008313 conf->p_async_config_data = async_config_data;
8314}
8315
Gilles Peskine8f97af72018-04-26 11:46:10 +02008316void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8317{
8318 return( conf->p_async_config_data );
8319}
8320
Gilles Peskine1febfef2018-04-30 11:54:39 +02008321void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008322{
8323 if( ssl->handshake == NULL )
8324 return( NULL );
8325 else
8326 return( ssl->handshake->user_async_ctx );
8327}
8328
Gilles Peskine1febfef2018-04-30 11:54:39 +02008329void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008330 void *ctx )
8331{
8332 if( ssl->handshake != NULL )
8333 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008334}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008335#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008336
Paul Bakker5121ce52009-01-03 21:22:43 +00008337/*
8338 * SSL get accessors
8339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008341{
8342 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8343}
8344
Hanno Becker8b170a02017-10-10 11:51:19 +01008345int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8346{
8347 /*
8348 * Case A: We're currently holding back
8349 * a message for further processing.
8350 */
8351
8352 if( ssl->keep_current_message == 1 )
8353 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008355 return( 1 );
8356 }
8357
8358 /*
8359 * Case B: Further records are pending in the current datagram.
8360 */
8361
8362#if defined(MBEDTLS_SSL_PROTO_DTLS)
8363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8364 ssl->in_left > ssl->next_record_offset )
8365 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008367 return( 1 );
8368 }
8369#endif /* MBEDTLS_SSL_PROTO_DTLS */
8370
8371 /*
8372 * Case C: A handshake message is being processed.
8373 */
8374
Hanno Becker8b170a02017-10-10 11:51:19 +01008375 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8376 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008378 return( 1 );
8379 }
8380
8381 /*
8382 * Case D: An application data message is being processed
8383 */
8384 if( ssl->in_offt != NULL )
8385 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008387 return( 1 );
8388 }
8389
8390 /*
8391 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008392 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008393 * we implement support for multiple alerts in single records.
8394 */
8395
8396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8397 return( 0 );
8398}
8399
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008400uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008401{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008402 if( ssl->session != NULL )
8403 return( ssl->session->verify_result );
8404
8405 if( ssl->session_negotiate != NULL )
8406 return( ssl->session_negotiate->verify_result );
8407
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008408 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008409}
8410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008412{
Paul Bakker926c8e42013-03-06 10:23:34 +01008413 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008414 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008417}
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008423 {
8424 switch( ssl->minor_ver )
8425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008427 return( "DTLSv1.0" );
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008430 return( "DTLSv1.2" );
8431
8432 default:
8433 return( "unknown (DTLS)" );
8434 }
8435 }
8436#endif
8437
Paul Bakker43ca69c2011-01-15 17:35:19 +00008438 switch( ssl->minor_ver )
8439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008441 return( "SSLv3.0" );
8442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008444 return( "TLSv1.0" );
8445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008447 return( "TLSv1.1" );
8448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008450 return( "TLSv1.2" );
8451
Paul Bakker43ca69c2011-01-15 17:35:19 +00008452 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008453 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008454 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008455}
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008458{
Hanno Becker3136ede2018-08-17 15:28:19 +01008459 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008461 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008462
Hanno Becker43395762019-05-03 14:46:38 +01008463 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8464
Hanno Becker78640902018-08-13 16:35:15 +01008465 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008466 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#if defined(MBEDTLS_ZLIB_SUPPORT)
8469 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8470 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008471#endif
8472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 case MBEDTLS_MODE_GCM:
8476 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008477 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008479 transform_expansion = transform->minlen;
8480 break;
8481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008483
8484 block_size = mbedtls_cipher_get_block_size(
8485 &transform->cipher_ctx_enc );
8486
Hanno Becker3136ede2018-08-17 15:28:19 +01008487 /* Expansion due to the addition of the MAC. */
8488 transform_expansion += transform->maclen;
8489
8490 /* Expansion due to the addition of CBC padding;
8491 * Theoretically up to 256 bytes, but we never use
8492 * more than the block size of the underlying cipher. */
8493 transform_expansion += block_size;
8494
8495 /* For TLS 1.1 or higher, an explicit IV is added
8496 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008497#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8498 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008499 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008500#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008501
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008502 break;
8503
8504 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008507 }
8508
Hanno Beckera5a2b082019-05-15 14:03:01 +01008509#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008510 if( transform->out_cid_len != 0 )
8511 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008512#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008513
Hanno Becker43395762019-05-03 14:46:38 +01008514 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008515}
8516
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008517#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8518size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8519{
8520 size_t max_len;
8521
8522 /*
8523 * Assume mfl_code is correct since it was checked when set
8524 */
Angus Grattond8213d02016-05-25 20:56:48 +10008525 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008526
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008527 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008528 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008529 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008530 {
Angus Grattond8213d02016-05-25 20:56:48 +10008531 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008532 }
8533
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008534 /* During a handshake, use the value being negotiated */
8535 if( ssl->session_negotiate != NULL &&
8536 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8537 {
8538 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8539 }
8540
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008541 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008542}
8543#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8544
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008545#if defined(MBEDTLS_SSL_PROTO_DTLS)
8546static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8547{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008548 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8550 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8551 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8552 return ( 0 );
8553
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008554 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8555 return( ssl->mtu );
8556
8557 if( ssl->mtu == 0 )
8558 return( ssl->handshake->mtu );
8559
8560 return( ssl->mtu < ssl->handshake->mtu ?
8561 ssl->mtu : ssl->handshake->mtu );
8562}
8563#endif /* MBEDTLS_SSL_PROTO_DTLS */
8564
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008565int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8566{
8567 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8568
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008569#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8570 !defined(MBEDTLS_SSL_PROTO_DTLS)
8571 (void) ssl;
8572#endif
8573
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008574#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8575 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8576
8577 if( max_len > mfl )
8578 max_len = mfl;
8579#endif
8580
8581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008582 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008583 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008584 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008585 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8586 const size_t overhead = (size_t) ret;
8587
8588 if( ret < 0 )
8589 return( ret );
8590
8591 if( mtu <= overhead )
8592 {
8593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8594 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8595 }
8596
8597 if( max_len > mtu - overhead )
8598 max_len = mtu - overhead;
8599 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008600#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008601
Hanno Becker0defedb2018-08-10 12:35:02 +01008602#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8603 !defined(MBEDTLS_SSL_PROTO_DTLS)
8604 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008605#endif
8606
8607 return( (int) max_len );
8608}
8609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#if defined(MBEDTLS_X509_CRT_PARSE_C)
8611const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008612{
8613 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008614 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008615
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008616 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SSL_CLI_C)
8621int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008622{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008623 if( ssl == NULL ||
8624 dst == NULL ||
8625 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008626 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008629 }
8630
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008631 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008634
Paul Bakker5121ce52009-01-03 21:22:43 +00008635/*
Paul Bakker1961b702013-01-25 14:49:24 +01008636 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008641
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008642 if( ssl == NULL || ssl->conf == NULL )
8643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008646 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008650 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008652#endif
8653
Paul Bakker1961b702013-01-25 14:49:24 +01008654 return( ret );
8655}
8656
8657/*
8658 * Perform the SSL handshake
8659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008661{
8662 int ret = 0;
8663
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008664 if( ssl == NULL || ssl->conf == NULL )
8665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008672
8673 if( ret != 0 )
8674 break;
8675 }
8676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008678
8679 return( ret );
8680}
8681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#if defined(MBEDTLS_SSL_RENEGOTIATION)
8683#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008684/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008685 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008688{
8689 int ret;
8690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008692
8693 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8695 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008696
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008697 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008698 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008700 return( ret );
8701 }
8702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008704
8705 return( 0 );
8706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008708
8709/*
8710 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711 * - any side: calling mbedtls_ssl_renegotiate(),
8712 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8713 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008714 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008715 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008719{
8720 int ret;
8721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008723
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008724 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8725 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008726
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008727 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8728 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008732 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008733 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008734 ssl->handshake->out_msg_seq = 1;
8735 else
8736 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008737 }
8738#endif
8739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8741 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008746 return( ret );
8747 }
8748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008750
8751 return( 0 );
8752}
8753
8754/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008755 * Renegotiate current connection on client,
8756 * or request renegotiation on server
8757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008761
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008762 if( ssl == NULL || ssl->conf == NULL )
8763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008766 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008767 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008773
8774 /* Did we already try/start sending HelloRequest? */
8775 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008777
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008778 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008779 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008783 /*
8784 * On client, either start the renegotiation process or,
8785 * if already in progress, continue the handshake
8786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008791
8792 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008795 return( ret );
8796 }
8797 }
8798 else
8799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008803 return( ret );
8804 }
8805 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008807
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008808 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008809}
8810
8811/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008812 * Check record counters and renegotiate if they're above the limit.
8813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008815{
Andres AG2196c7f2016-12-15 17:01:16 +00008816 size_t ep_len = ssl_ep_len( ssl );
8817 int in_ctr_cmp;
8818 int out_ctr_cmp;
8819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8821 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008822 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008823 {
8824 return( 0 );
8825 }
8826
Andres AG2196c7f2016-12-15 17:01:16 +00008827 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8828 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008829 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008830 ssl->conf->renego_period + ep_len, 8 - ep_len );
8831
8832 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008833 {
8834 return( 0 );
8835 }
8836
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008841
8842/*
8843 * Receive application data decrypted from the SSL layer
8844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008846{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008847 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008848 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008849
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008850 if( ssl == NULL || ssl->conf == NULL )
8851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008856 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008859 return( ret );
8860
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008861 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008863 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008864 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008865 return( ret );
8866 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008867 }
8868#endif
8869
Hanno Becker4a810fb2017-05-24 16:27:30 +01008870 /*
8871 * Check if renegotiation is necessary and/or handshake is
8872 * in process. If yes, perform/continue, and fall through
8873 * if an unexpected packet is received while the client
8874 * is waiting for the ServerHello.
8875 *
8876 * (There is no equivalent to the last condition on
8877 * the server-side as it is not treated as within
8878 * a handshake while waiting for the ClientHello
8879 * after a renegotiation request.)
8880 */
8881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008883 ret = ssl_check_ctr_renegotiate( ssl );
8884 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8885 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008888 return( ret );
8889 }
8890#endif
8891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008895 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8896 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008899 return( ret );
8900 }
8901 }
8902
Hanno Beckere41158b2017-10-23 13:30:32 +01008903 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008904 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008905 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008906 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008907 if( ssl->f_get_timer != NULL &&
8908 ssl->f_get_timer( ssl->p_timer ) == -1 )
8909 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008910 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008911 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008912
Hanno Becker327c93b2018-08-15 13:56:18 +01008913 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008914 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008915 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8916 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008917
Hanno Becker4a810fb2017-05-24 16:27:30 +01008918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8919 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008920 }
8921
8922 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008924 {
8925 /*
8926 * OpenSSL sends empty messages to randomize the IV
8927 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008928 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008931 return( 0 );
8932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008934 return( ret );
8935 }
8936 }
8937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008941
Hanno Becker4a810fb2017-05-24 16:27:30 +01008942 /*
8943 * - For client-side, expect SERVER_HELLO_REQUEST.
8944 * - For server-side, expect CLIENT_HELLO.
8945 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8946 */
8947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008951 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008954
8955 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008958 {
8959 continue;
8960 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008963 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008964#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008965
Hanno Becker4a810fb2017-05-24 16:27:30 +01008966#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008967 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008971
8972 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008975 {
8976 continue;
8977 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008979 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008980 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008981#endif /* MBEDTLS_SSL_SRV_C */
8982
Hanno Becker21df7f92017-10-17 11:03:26 +01008983#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008984 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008985 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8986 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8987 ssl->conf->allow_legacy_renegotiation ==
8988 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8989 {
8990 /*
8991 * Accept renegotiation request
8992 */
Paul Bakker48916f92012-09-16 19:57:18 +00008993
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008994 /* DTLS clients need to know renego is server-initiated */
8995#if defined(MBEDTLS_SSL_PROTO_DTLS)
8996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8997 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8998 {
8999 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9000 }
9001#endif
9002 ret = ssl_start_renegotiation( ssl );
9003 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9004 ret != 0 )
9005 {
9006 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9007 return( ret );
9008 }
9009 }
9010 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009011#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009012 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009013 /*
9014 * Refuse renegotiation
9015 */
9016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#if defined(MBEDTLS_SSL_PROTO_SSL3)
9020 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009021 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009022 /* SSLv3 does not have a "no_renegotiation" warning, so
9023 we send a fatal alert and abort the connection. */
9024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9025 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9026 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009027 }
9028 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9030#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9031 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9032 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9035 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9036 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009037 {
9038 return( ret );
9039 }
Paul Bakker48916f92012-09-16 19:57:18 +00009040 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009041 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9043 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009047 }
Paul Bakker48916f92012-09-16 19:57:18 +00009048 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009049
Hanno Becker90333da2017-10-10 11:27:13 +01009050 /* At this point, we don't know whether the renegotiation has been
9051 * completed or not. The cases to consider are the following:
9052 * 1) The renegotiation is complete. In this case, no new record
9053 * has been read yet.
9054 * 2) The renegotiation is incomplete because the client received
9055 * an application data record while awaiting the ServerHello.
9056 * 3) The renegotiation is incomplete because the client received
9057 * a non-handshake, non-application data message while awaiting
9058 * the ServerHello.
9059 * In each of these case, looping will be the proper action:
9060 * - For 1), the next iteration will read a new record and check
9061 * if it's application data.
9062 * - For 2), the loop condition isn't satisfied as application data
9063 * is present, hence continue is the same as break
9064 * - For 3), the loop condition is satisfied and read_record
9065 * will re-deliver the message that was held back by the client
9066 * when expecting the ServerHello.
9067 */
9068 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009069 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009070#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009072 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009073 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009074 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009075 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009078 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009080 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009081 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009082 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9086 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009089 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009090 }
9091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9095 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009096 }
9097
9098 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009099
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009100 /* We're going to return something now, cancel timer,
9101 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009103 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009104
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009105#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009106 /* If we requested renego but received AppData, resend HelloRequest.
9107 * Do it now, after setting in_offt, to avoid taking this branch
9108 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009110 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009112 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009113 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009116 return( ret );
9117 }
9118 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009120#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009121 }
9122
9123 n = ( len < ssl->in_msglen )
9124 ? len : ssl->in_msglen;
9125
9126 memcpy( buf, ssl->in_offt, n );
9127 ssl->in_msglen -= n;
9128
9129 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009130 {
9131 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009132 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009133 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009135 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009136 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009137 /* more data available */
9138 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009142
Paul Bakker23986e52011-04-24 08:57:21 +00009143 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009144}
9145
9146/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009147 * Send application data to be encrypted by the SSL layer, taking care of max
9148 * fragment length and buffer size.
9149 *
9150 * According to RFC 5246 Section 6.2.1:
9151 *
9152 * Zero-length fragments of Application data MAY be sent as they are
9153 * potentially useful as a traffic analysis countermeasure.
9154 *
9155 * Therefore, it is possible that the input message length is 0 and the
9156 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009157 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009158static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009159 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009160{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009161 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9162 const size_t max_len = (size_t) ret;
9163
9164 if( ret < 0 )
9165 {
9166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9167 return( ret );
9168 }
9169
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009170 if( len > max_len )
9171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009176 "maximum fragment length: %d > %d",
9177 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009179 }
9180 else
9181#endif
9182 len = max_len;
9183 }
Paul Bakker887bd502011-06-08 13:10:54 +00009184
Paul Bakker5121ce52009-01-03 21:22:43 +00009185 if( ssl->out_left != 0 )
9186 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009187 /*
9188 * The user has previously tried to send the data and
9189 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9190 * written. In this case, we expect the high-level write function
9191 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009196 return( ret );
9197 }
9198 }
Paul Bakker887bd502011-06-08 13:10:54 +00009199 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009200 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009201 /*
9202 * The user is trying to send a message the first time, so we need to
9203 * copy the data into the internal buffers and setup the data structure
9204 * to keep track of partial writes
9205 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009206 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009208 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009209
Hanno Becker67bc7c32018-08-06 11:33:50 +01009210 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009213 return( ret );
9214 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009215 }
9216
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009217 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009218}
9219
9220/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009221 * Write application data, doing 1/n-1 splitting if necessary.
9222 *
9223 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009224 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009225 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009228static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009229 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009230{
9231 int ret;
9232
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009233 if( ssl->conf->cbc_record_splitting ==
9234 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009235 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009236 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9237 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9238 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009239 {
9240 return( ssl_write_real( ssl, buf, len ) );
9241 }
9242
9243 if( ssl->split_done == 0 )
9244 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009245 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009246 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009247 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009248 }
9249
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009250 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9251 return( ret );
9252 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009253
9254 return( ret + 1 );
9255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009257
9258/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009259 * Write application data (public-facing wrapper)
9260 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009261int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009262{
9263 int ret;
9264
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009266
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é-Gonnard144bc222015-04-17 20:39:07 +02009270#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009271 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9272 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009273 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009274 return( ret );
9275 }
9276#endif
9277
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009278 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009279 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009280 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009281 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009283 return( ret );
9284 }
9285 }
9286
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009287#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009288 ret = ssl_write_split( ssl, buf, len );
9289#else
9290 ret = ssl_write_real( ssl, buf, len );
9291#endif
9292
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009294
9295 return( ret );
9296}
9297
9298/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009299 * Notify the peer that the connection is being closed
9300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009302{
9303 int ret;
9304
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009305 if( ssl == NULL || ssl->conf == NULL )
9306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009309
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009310 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9316 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9317 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009320 return( ret );
9321 }
9322 }
9323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009325
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009326 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009327}
9328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009330{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009331 if( transform == NULL )
9332 return;
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009335 deflateEnd( &transform->ctx_deflate );
9336 inflateEnd( &transform->ctx_inflate );
9337#endif
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9340 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009341
Hanno Becker92231322018-01-03 15:32:51 +00009342#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 mbedtls_md_free( &transform->md_ctx_enc );
9344 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009345#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009346
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009347 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009348}
9349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_X509_CRT_PARSE_C)
9351static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009354
9355 while( cur != NULL )
9356 {
9357 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009359 cur = next;
9360 }
9361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009363
Hanno Becker0271f962018-08-16 13:23:47 +01009364#if defined(MBEDTLS_SSL_PROTO_DTLS)
9365
9366static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9367{
9368 unsigned offset;
9369 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9370
9371 if( hs == NULL )
9372 return;
9373
Hanno Becker283f5ef2018-08-24 09:34:47 +01009374 ssl_free_buffered_record( ssl );
9375
Hanno Becker0271f962018-08-16 13:23:47 +01009376 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009377 ssl_buffering_free_slot( ssl, offset );
9378}
9379
9380static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9381 uint8_t slot )
9382{
9383 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9384 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009385
9386 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9387 return;
9388
Hanno Beckere605b192018-08-21 15:59:07 +01009389 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009390 {
Hanno Beckere605b192018-08-21 15:59:07 +01009391 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009392 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009393 mbedtls_free( hs_buf->data );
9394 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009395 }
9396}
9397
9398#endif /* MBEDTLS_SSL_PROTO_DTLS */
9399
Gilles Peskine9b562d52018-04-25 20:32:43 +02009400void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009401{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009402 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9403
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009404 if( handshake == NULL )
9405 return;
9406
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009407#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9408 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9409 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009410 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009411 handshake->async_in_progress = 0;
9412 }
9413#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9414
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009415#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9416 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9417 mbedtls_md5_free( &handshake->fin_md5 );
9418 mbedtls_sha1_free( &handshake->fin_sha1 );
9419#endif
9420#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9421#if defined(MBEDTLS_SHA256_C)
9422 mbedtls_sha256_free( &handshake->fin_sha256 );
9423#endif
9424#if defined(MBEDTLS_SHA512_C)
9425 mbedtls_sha512_free( &handshake->fin_sha512 );
9426#endif
9427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429#if defined(MBEDTLS_DHM_C)
9430 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#if defined(MBEDTLS_ECDH_C)
9433 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009434#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009435#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009436 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009437#if defined(MBEDTLS_SSL_CLI_C)
9438 mbedtls_free( handshake->ecjpake_cache );
9439 handshake->ecjpake_cache = NULL;
9440 handshake->ecjpake_cache_len = 0;
9441#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009442#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009443
Janos Follath4ae5c292016-02-10 11:27:43 +00009444#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9445 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009446 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009448#endif
9449
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009450#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9451 if( handshake->psk != NULL )
9452 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009453 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009454 mbedtls_free( handshake->psk );
9455 }
9456#endif
9457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9459 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009460 /*
9461 * Free only the linked list wrapper, not the keys themselves
9462 * since the belong to the SNI callback
9463 */
9464 if( handshake->sni_key_cert != NULL )
9465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009467
9468 while( cur != NULL )
9469 {
9470 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009472 cur = next;
9473 }
9474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009476
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009477#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009478 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009479#endif
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481#if defined(MBEDTLS_SSL_PROTO_DTLS)
9482 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009483 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009484 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009485#endif
9486
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009487 mbedtls_platform_zeroize( handshake,
9488 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009489}
9490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009492{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009493 if( session == NULL )
9494 return;
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009497 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 mbedtls_x509_crt_free( session->peer_cert );
9500 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009501 }
Paul Bakkered27a042013-04-18 22:46:23 +02009502#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009503
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009504#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009506#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009507
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009508 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009509}
9510
Paul Bakker5121ce52009-01-03 21:22:43 +00009511/*
9512 * Free an SSL context
9513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009515{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009516 if( ssl == NULL )
9517 return;
9518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009520
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009521 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009522 {
Angus Grattond8213d02016-05-25 20:56:48 +10009523 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009525 }
9526
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009527 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009528 {
Angus Grattond8213d02016-05-25 20:56:48 +10009529 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009531 }
9532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009534 if( ssl->compress_buf != NULL )
9535 {
Angus Grattond8213d02016-05-25 20:56:48 +10009536 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009538 }
9539#endif
9540
Paul Bakker48916f92012-09-16 19:57:18 +00009541 if( ssl->transform )
9542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 mbedtls_ssl_transform_free( ssl->transform );
9544 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009545 }
9546
9547 if( ssl->handshake )
9548 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009549 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9551 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 mbedtls_free( ssl->handshake );
9554 mbedtls_free( ssl->transform_negotiate );
9555 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009556 }
9557
Paul Bakkerc0463502013-02-14 11:19:38 +01009558 if( ssl->session )
9559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 mbedtls_ssl_session_free( ssl->session );
9561 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009562 }
9563
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009564#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009565 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009566 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009567 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009569 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9573 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9576 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009577 }
9578#endif
9579
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009580#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009582#endif
9583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009585
Paul Bakker86f04f42013-02-14 11:20:09 +01009586 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009587 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009588}
9589
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009590/*
9591 * Initialze mbedtls_ssl_config
9592 */
9593void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9594{
9595 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9596}
9597
Simon Butcherc97b6972015-12-27 23:48:17 +00009598#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009599static int ssl_preset_default_hashes[] = {
9600#if defined(MBEDTLS_SHA512_C)
9601 MBEDTLS_MD_SHA512,
9602 MBEDTLS_MD_SHA384,
9603#endif
9604#if defined(MBEDTLS_SHA256_C)
9605 MBEDTLS_MD_SHA256,
9606 MBEDTLS_MD_SHA224,
9607#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009608#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009609 MBEDTLS_MD_SHA1,
9610#endif
9611 MBEDTLS_MD_NONE
9612};
Simon Butcherc97b6972015-12-27 23:48:17 +00009613#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009614
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009615static int ssl_preset_suiteb_ciphersuites[] = {
9616 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9617 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9618 0
9619};
9620
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009621#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009622static int ssl_preset_suiteb_hashes[] = {
9623 MBEDTLS_MD_SHA256,
9624 MBEDTLS_MD_SHA384,
9625 MBEDTLS_MD_NONE
9626};
9627#endif
9628
9629#if defined(MBEDTLS_ECP_C)
9630static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9631 MBEDTLS_ECP_DP_SECP256R1,
9632 MBEDTLS_ECP_DP_SECP384R1,
9633 MBEDTLS_ECP_DP_NONE
9634};
9635#endif
9636
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009637/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009638 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009639 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009640int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009641 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009642{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009643#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009644 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009645#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009646
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009647 /* Use the functions here so that they are covered in tests,
9648 * but otherwise access member directly for efficiency */
9649 mbedtls_ssl_conf_endpoint( conf, endpoint );
9650 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009651
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009652 /*
9653 * Things that are common to all presets
9654 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009655#if defined(MBEDTLS_SSL_CLI_C)
9656 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9657 {
9658 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9659#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9660 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9661#endif
9662 }
9663#endif
9664
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009665#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009666 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009667#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009668
9669#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9670 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9671#endif
9672
9673#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9674 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9675#endif
9676
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009677#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9678 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9679#endif
9680
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009681#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009682 conf->f_cookie_write = ssl_cookie_write_dummy;
9683 conf->f_cookie_check = ssl_cookie_check_dummy;
9684#endif
9685
9686#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9687 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9688#endif
9689
Janos Follath088ce432017-04-10 12:42:31 +01009690#if defined(MBEDTLS_SSL_SRV_C)
9691 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9692#endif
9693
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009694#if defined(MBEDTLS_SSL_PROTO_DTLS)
9695 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9696 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9697#endif
9698
9699#if defined(MBEDTLS_SSL_RENEGOTIATION)
9700 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009701 memset( conf->renego_period, 0x00, 2 );
9702 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009703#endif
9704
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009705#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9706 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9707 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009708 const unsigned char dhm_p[] =
9709 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9710 const unsigned char dhm_g[] =
9711 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9712
Hanno Beckera90658f2017-10-04 15:29:08 +01009713 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9714 dhm_p, sizeof( dhm_p ),
9715 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009716 {
9717 return( ret );
9718 }
9719 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009720#endif
9721
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009722 /*
9723 * Preset-specific defaults
9724 */
9725 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009726 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009727 /*
9728 * NSA Suite B
9729 */
9730 case MBEDTLS_SSL_PRESET_SUITEB:
9731 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9732 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9733 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9734 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9735
9736 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9737 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9738 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9739 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9740 ssl_preset_suiteb_ciphersuites;
9741
9742#if defined(MBEDTLS_X509_CRT_PARSE_C)
9743 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009744#endif
9745
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009746#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009747 conf->sig_hashes = ssl_preset_suiteb_hashes;
9748#endif
9749
9750#if defined(MBEDTLS_ECP_C)
9751 conf->curve_list = ssl_preset_suiteb_curves;
9752#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009753 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009754
9755 /*
9756 * Default
9757 */
9758 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009759 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9760 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9761 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9762 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9763 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9764 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9765 MBEDTLS_SSL_MIN_MINOR_VERSION :
9766 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009767 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9768 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9769
9770#if defined(MBEDTLS_SSL_PROTO_DTLS)
9771 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9772 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9773#endif
9774
9775 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9776 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9777 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9778 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9779 mbedtls_ssl_list_ciphersuites();
9780
9781#if defined(MBEDTLS_X509_CRT_PARSE_C)
9782 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9783#endif
9784
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009785#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009786 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009787#endif
9788
9789#if defined(MBEDTLS_ECP_C)
9790 conf->curve_list = mbedtls_ecp_grp_id_list();
9791#endif
9792
9793#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9794 conf->dhm_min_bitlen = 1024;
9795#endif
9796 }
9797
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009798 return( 0 );
9799}
9800
9801/*
9802 * Free mbedtls_ssl_config
9803 */
9804void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9805{
9806#if defined(MBEDTLS_DHM_C)
9807 mbedtls_mpi_free( &conf->dhm_P );
9808 mbedtls_mpi_free( &conf->dhm_G );
9809#endif
9810
9811#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9812 if( conf->psk != NULL )
9813 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009814 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009815 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009816 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009817 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009818 }
9819
9820 if( conf->psk_identity != NULL )
9821 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009822 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009823 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009824 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009825 conf->psk_identity_len = 0;
9826 }
9827#endif
9828
9829#if defined(MBEDTLS_X509_CRT_PARSE_C)
9830 ssl_key_cert_free( conf->key_cert );
9831#endif
9832
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009833 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009834}
9835
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009836#if defined(MBEDTLS_PK_C) && \
9837 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009838/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009842{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#if defined(MBEDTLS_RSA_C)
9844 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9845 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847#if defined(MBEDTLS_ECDSA_C)
9848 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9849 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009852}
9853
Hanno Becker7e5437a2017-04-28 17:15:26 +01009854unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9855{
9856 switch( type ) {
9857 case MBEDTLS_PK_RSA:
9858 return( MBEDTLS_SSL_SIG_RSA );
9859 case MBEDTLS_PK_ECDSA:
9860 case MBEDTLS_PK_ECKEY:
9861 return( MBEDTLS_SSL_SIG_ECDSA );
9862 default:
9863 return( MBEDTLS_SSL_SIG_ANON );
9864 }
9865}
9866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009868{
9869 switch( sig )
9870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871#if defined(MBEDTLS_RSA_C)
9872 case MBEDTLS_SSL_SIG_RSA:
9873 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875#if defined(MBEDTLS_ECDSA_C)
9876 case MBEDTLS_SSL_SIG_ECDSA:
9877 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009878#endif
9879 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009881 }
9882}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009883#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009884
Hanno Becker7e5437a2017-04-28 17:15:26 +01009885#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9886 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9887
9888/* Find an entry in a signature-hash set matching a given hash algorithm. */
9889mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9890 mbedtls_pk_type_t sig_alg )
9891{
9892 switch( sig_alg )
9893 {
9894 case MBEDTLS_PK_RSA:
9895 return( set->rsa );
9896 case MBEDTLS_PK_ECDSA:
9897 return( set->ecdsa );
9898 default:
9899 return( MBEDTLS_MD_NONE );
9900 }
9901}
9902
9903/* Add a signature-hash-pair to a signature-hash set */
9904void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9905 mbedtls_pk_type_t sig_alg,
9906 mbedtls_md_type_t md_alg )
9907{
9908 switch( sig_alg )
9909 {
9910 case MBEDTLS_PK_RSA:
9911 if( set->rsa == MBEDTLS_MD_NONE )
9912 set->rsa = md_alg;
9913 break;
9914
9915 case MBEDTLS_PK_ECDSA:
9916 if( set->ecdsa == MBEDTLS_MD_NONE )
9917 set->ecdsa = md_alg;
9918 break;
9919
9920 default:
9921 break;
9922 }
9923}
9924
9925/* Allow exactly one hash algorithm for each signature. */
9926void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9927 mbedtls_md_type_t md_alg )
9928{
9929 set->rsa = md_alg;
9930 set->ecdsa = md_alg;
9931}
9932
9933#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9934 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9935
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009936/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009937 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009940{
9941 switch( hash )
9942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_MD5_C)
9944 case MBEDTLS_SSL_HASH_MD5:
9945 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_SHA1_C)
9948 case MBEDTLS_SSL_HASH_SHA1:
9949 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951#if defined(MBEDTLS_SHA256_C)
9952 case MBEDTLS_SSL_HASH_SHA224:
9953 return( MBEDTLS_MD_SHA224 );
9954 case MBEDTLS_SSL_HASH_SHA256:
9955 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957#if defined(MBEDTLS_SHA512_C)
9958 case MBEDTLS_SSL_HASH_SHA384:
9959 return( MBEDTLS_MD_SHA384 );
9960 case MBEDTLS_SSL_HASH_SHA512:
9961 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009962#endif
9963 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009965 }
9966}
9967
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009968/*
9969 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9970 */
9971unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9972{
9973 switch( md )
9974 {
9975#if defined(MBEDTLS_MD5_C)
9976 case MBEDTLS_MD_MD5:
9977 return( MBEDTLS_SSL_HASH_MD5 );
9978#endif
9979#if defined(MBEDTLS_SHA1_C)
9980 case MBEDTLS_MD_SHA1:
9981 return( MBEDTLS_SSL_HASH_SHA1 );
9982#endif
9983#if defined(MBEDTLS_SHA256_C)
9984 case MBEDTLS_MD_SHA224:
9985 return( MBEDTLS_SSL_HASH_SHA224 );
9986 case MBEDTLS_MD_SHA256:
9987 return( MBEDTLS_SSL_HASH_SHA256 );
9988#endif
9989#if defined(MBEDTLS_SHA512_C)
9990 case MBEDTLS_MD_SHA384:
9991 return( MBEDTLS_SSL_HASH_SHA384 );
9992 case MBEDTLS_MD_SHA512:
9993 return( MBEDTLS_SSL_HASH_SHA512 );
9994#endif
9995 default:
9996 return( MBEDTLS_SSL_HASH_NONE );
9997 }
9998}
9999
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010000#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010001/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010002 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010003 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010004 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010005int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010008
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010009 if( ssl->conf->curve_list == NULL )
10010 return( -1 );
10011
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010012 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010013 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010014 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010015
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010016 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010017}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010018#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010019
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010020#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010021/*
10022 * Check if a hash proposed by the peer is in our list.
10023 * Return 0 if we're willing to use it, -1 otherwise.
10024 */
10025int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10026 mbedtls_md_type_t md )
10027{
10028 const int *cur;
10029
10030 if( ssl->conf->sig_hashes == NULL )
10031 return( -1 );
10032
10033 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10034 if( *cur == (int) md )
10035 return( 0 );
10036
10037 return( -1 );
10038}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010039#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041#if defined(MBEDTLS_X509_CRT_PARSE_C)
10042int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10043 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010044 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010045 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010046{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010047 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010049 int usage = 0;
10050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010052 const char *ext_oid;
10053 size_t ext_len;
10054#endif
10055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10057 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010058 ((void) cert);
10059 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010060 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010061#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10064 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010065 {
10066 /* Server part of the key exchange */
10067 switch( ciphersuite->key_exchange )
10068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 case MBEDTLS_KEY_EXCHANGE_RSA:
10070 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010071 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010072 break;
10073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10075 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10076 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10077 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010078 break;
10079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10081 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010082 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010083 break;
10084
10085 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 case MBEDTLS_KEY_EXCHANGE_NONE:
10087 case MBEDTLS_KEY_EXCHANGE_PSK:
10088 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10089 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010090 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010091 usage = 0;
10092 }
10093 }
10094 else
10095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10097 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010098 }
10099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010101 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010102 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010103 ret = -1;
10104 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010105#else
10106 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10110 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10113 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010114 }
10115 else
10116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10118 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010119 }
10120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010122 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010123 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010124 ret = -1;
10125 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010127
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010128 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010131
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010132/*
10133 * Convert version numbers to/from wire format
10134 * and, for DTLS, to/from TLS equivalent.
10135 *
10136 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010137 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010138 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10139 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010142 unsigned char ver[2] )
10143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144#if defined(MBEDTLS_SSL_PROTO_DTLS)
10145 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010148 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10149
10150 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10151 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10152 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010153 else
10154#else
10155 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010156#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010157 {
10158 ver[0] = (unsigned char) major;
10159 ver[1] = (unsigned char) minor;
10160 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010161}
10162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010163void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010164 const unsigned char ver[2] )
10165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166#if defined(MBEDTLS_SSL_PROTO_DTLS)
10167 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010168 {
10169 *major = 255 - ver[0] + 2;
10170 *minor = 255 - ver[1] + 1;
10171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010173 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10174 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010175 else
10176#else
10177 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010178#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010179 {
10180 *major = ver[0];
10181 *minor = ver[1];
10182 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010183}
10184
Simon Butcher99000142016-10-13 17:21:01 +010010185int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10186{
10187#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10188 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10189 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10190
10191 switch( md )
10192 {
10193#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10194#if defined(MBEDTLS_MD5_C)
10195 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010196 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010197#endif
10198#if defined(MBEDTLS_SHA1_C)
10199 case MBEDTLS_SSL_HASH_SHA1:
10200 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10201 break;
10202#endif
10203#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10204#if defined(MBEDTLS_SHA512_C)
10205 case MBEDTLS_SSL_HASH_SHA384:
10206 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10207 break;
10208#endif
10209#if defined(MBEDTLS_SHA256_C)
10210 case MBEDTLS_SSL_HASH_SHA256:
10211 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10212 break;
10213#endif
10214 default:
10215 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10216 }
10217
10218 return 0;
10219#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10220 (void) ssl;
10221 (void) md;
10222
10223 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10224#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10225}
10226
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010227#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10228 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10229int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10230 unsigned char *output,
10231 unsigned char *data, size_t data_len )
10232{
10233 int ret = 0;
10234 mbedtls_md5_context mbedtls_md5;
10235 mbedtls_sha1_context mbedtls_sha1;
10236
10237 mbedtls_md5_init( &mbedtls_md5 );
10238 mbedtls_sha1_init( &mbedtls_sha1 );
10239
10240 /*
10241 * digitally-signed struct {
10242 * opaque md5_hash[16];
10243 * opaque sha_hash[20];
10244 * };
10245 *
10246 * md5_hash
10247 * MD5(ClientHello.random + ServerHello.random
10248 * + ServerParams);
10249 * sha_hash
10250 * SHA(ClientHello.random + ServerHello.random
10251 * + ServerParams);
10252 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010253 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010254 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010255 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010256 goto exit;
10257 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010258 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010259 ssl->handshake->randbytes, 64 ) ) != 0 )
10260 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010262 goto exit;
10263 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010264 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010265 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010267 goto exit;
10268 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010269 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010270 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010272 goto exit;
10273 }
10274
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010275 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010276 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010278 goto exit;
10279 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010280 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010281 ssl->handshake->randbytes, 64 ) ) != 0 )
10282 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010284 goto exit;
10285 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010286 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010287 data_len ) ) != 0 )
10288 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010290 goto exit;
10291 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010292 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010293 output + 16 ) ) != 0 )
10294 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010296 goto exit;
10297 }
10298
10299exit:
10300 mbedtls_md5_free( &mbedtls_md5 );
10301 mbedtls_sha1_free( &mbedtls_sha1 );
10302
10303 if( ret != 0 )
10304 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10305 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10306
10307 return( ret );
10308
10309}
10310#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10311 MBEDTLS_SSL_PROTO_TLS1_1 */
10312
10313#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10314 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10315int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010316 unsigned char *hash, size_t *hashlen,
10317 unsigned char *data, size_t data_len,
10318 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010319{
10320 int ret = 0;
10321 mbedtls_md_context_t ctx;
10322 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010323 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010324
10325 mbedtls_md_init( &ctx );
10326
10327 /*
10328 * digitally-signed struct {
10329 * opaque client_random[32];
10330 * opaque server_random[32];
10331 * ServerDHParams params;
10332 * };
10333 */
10334 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10335 {
10336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10337 goto exit;
10338 }
10339 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10340 {
10341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10342 goto exit;
10343 }
10344 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10345 {
10346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10347 goto exit;
10348 }
10349 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10350 {
10351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10352 goto exit;
10353 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010354 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010355 {
10356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10357 goto exit;
10358 }
10359
10360exit:
10361 mbedtls_md_free( &ctx );
10362
10363 if( ret != 0 )
10364 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10365 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10366
10367 return( ret );
10368}
10369#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10370 MBEDTLS_SSL_PROTO_TLS1_2 */
10371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372#endif /* MBEDTLS_SSL_TLS_C */