blob: 5004dc94748248a020cafe56716a4053a45659d9 [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 Becker687e0fb2019-05-08 13:02:55 +01004690 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004691 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004692#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004693
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 return( ret );
4695 }
4696
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004697 if( ssl->in_msgtype != rec.type )
4698 {
4699 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4700 ssl->in_msgtype, rec.type ) );
4701 }
4702
4703 /* The record content type may change during decryption,
4704 * so re-read it. */
4705 ssl->in_msgtype = rec.type;
4706 /* Also update the input buffer, because unfortunately
4707 * the server-side ssl_parse_client_hello() reparses the
4708 * record header when receiving a ClientHello initiating
4709 * a renegotiation. */
4710 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004711 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004712 ssl->in_msglen = rec.data_len;
4713 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4714 ssl->in_len[1] = (unsigned char)( rec.data_len );
4715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004716 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 ssl->in_msg, ssl->in_msglen );
4718
Hanno Beckera5a2b082019-05-15 14:03:01 +01004719#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004720 /* We have already checked the record content type
4721 * in ssl_parse_record_header(), failing or silently
4722 * dropping the record in the case of an unknown type.
4723 *
4724 * Since with the use of CIDs, the record content type
4725 * might change during decryption, re-check the record
4726 * content type, but treat a failure as fatal this time. */
4727 if( ssl_check_record_type( ssl->in_msgtype ) )
4728 {
4729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4730 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4731 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004733
Angus Grattond8213d02016-05-25 20:56:48 +10004734 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4737 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004739 else if( ssl->in_msglen == 0 )
4740 {
4741#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4742 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4743 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4744 {
4745 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4748 }
4749#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4750
4751 ssl->nb_zero++;
4752
4753 /*
4754 * Three or more empty messages may be a DoS attack
4755 * (excessive CPU consumption).
4756 */
4757 if( ssl->nb_zero > 3 )
4758 {
4759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004760 "messages, possible DoS attack" ) );
4761 /* Treat the records as if they were not properly authenticated,
4762 * thereby failing the connection if we see more than allowed
4763 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004764 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4765 }
4766 }
4767 else
4768 ssl->nb_zero = 0;
4769
4770#if defined(MBEDTLS_SSL_PROTO_DTLS)
4771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4772 {
4773 ; /* in_ctr read from peer, not maintained internally */
4774 }
4775 else
4776#endif
4777 {
4778 unsigned i;
4779 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4780 if( ++ssl->in_ctr[i - 1] != 0 )
4781 break;
4782
4783 /* The loop goes to its end iff the counter is wrapping */
4784 if( i == ssl_ep_len( ssl ) )
4785 {
4786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4787 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4788 }
4789 }
4790
Paul Bakker5121ce52009-01-03 21:22:43 +00004791 }
4792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004794 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004796 {
4797 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004800 return( ret );
4801 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004802 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004806 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004809 }
4810#endif
4811
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004812 return( 0 );
4813}
4814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004816
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004817/*
4818 * Read a record.
4819 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004820 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4821 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4822 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004823 */
Hanno Becker1097b342018-08-15 14:09:41 +01004824
4825/* Helper functions for mbedtls_ssl_read_record(). */
4826static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004827static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4828static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004829
Hanno Becker327c93b2018-08-15 13:56:18 +01004830int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004831 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004832{
4833 int ret;
4834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004836
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004837 if( ssl->keep_current_message == 0 )
4838 {
4839 do {
Simon Butcher99000142016-10-13 17:21:01 +01004840
Hanno Becker26994592018-08-15 14:14:59 +01004841 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004842 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004843 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004844
Hanno Beckere74d5562018-08-15 14:26:08 +01004845 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004846 {
Hanno Becker40f50842018-08-15 14:48:01 +01004847#if defined(MBEDTLS_SSL_PROTO_DTLS)
4848 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004849
Hanno Becker40f50842018-08-15 14:48:01 +01004850 /* We only check for buffered messages if the
4851 * current datagram is fully consumed. */
4852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004853 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004854 {
Hanno Becker40f50842018-08-15 14:48:01 +01004855 if( ssl_load_buffered_message( ssl ) == 0 )
4856 have_buffered = 1;
4857 }
4858
4859 if( have_buffered == 0 )
4860#endif /* MBEDTLS_SSL_PROTO_DTLS */
4861 {
4862 ret = ssl_get_next_record( ssl );
4863 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4864 continue;
4865
4866 if( ret != 0 )
4867 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004868 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004869 return( ret );
4870 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004871 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004872 }
4873
4874 ret = mbedtls_ssl_handle_message_type( ssl );
4875
Hanno Becker40f50842018-08-15 14:48:01 +01004876#if defined(MBEDTLS_SSL_PROTO_DTLS)
4877 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4878 {
4879 /* Buffer future message */
4880 ret = ssl_buffer_message( ssl );
4881 if( ret != 0 )
4882 return( ret );
4883
4884 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4885 }
4886#endif /* MBEDTLS_SSL_PROTO_DTLS */
4887
Hanno Becker90333da2017-10-10 11:27:13 +01004888 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4889 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004890
4891 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004892 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004893 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004894 return( ret );
4895 }
4896
Hanno Becker327c93b2018-08-15 13:56:18 +01004897 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004898 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004899 {
4900 mbedtls_ssl_update_handshake_status( ssl );
4901 }
Simon Butcher99000142016-10-13 17:21:01 +01004902 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004903 else
Simon Butcher99000142016-10-13 17:21:01 +01004904 {
Hanno Becker02f59072018-08-15 14:00:24 +01004905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004906 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004907 }
4908
4909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4910
4911 return( 0 );
4912}
4913
Hanno Becker40f50842018-08-15 14:48:01 +01004914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004915static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004916{
Hanno Becker40f50842018-08-15 14:48:01 +01004917 if( ssl->in_left > ssl->next_record_offset )
4918 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004919
Hanno Becker40f50842018-08-15 14:48:01 +01004920 return( 0 );
4921}
4922
4923static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4924{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004925 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004926 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004927 int ret = 0;
4928
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004929 if( hs == NULL )
4930 return( -1 );
4931
Hanno Beckere00ae372018-08-20 09:39:42 +01004932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4933
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004934 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4935 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4936 {
4937 /* Check if we have seen a ChangeCipherSpec before.
4938 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004939 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004940 {
4941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4942 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004943 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004944 }
4945
Hanno Becker39b8bc92018-08-28 17:17:13 +01004946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004947 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4948 ssl->in_msglen = 1;
4949 ssl->in_msg[0] = 1;
4950
4951 /* As long as they are equal, the exact value doesn't matter. */
4952 ssl->in_left = 0;
4953 ssl->next_record_offset = 0;
4954
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004955 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004956 goto exit;
4957 }
Hanno Becker37f95322018-08-16 13:55:32 +01004958
Hanno Beckerb8f50142018-08-28 10:01:34 +01004959#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004960 /* Debug only */
4961 {
4962 unsigned offset;
4963 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4964 {
4965 hs_buf = &hs->buffering.hs[offset];
4966 if( hs_buf->is_valid == 1 )
4967 {
4968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4969 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004970 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004971 }
4972 }
4973 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004974#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004975
4976 /* Check if we have buffered and/or fully reassembled the
4977 * next handshake message. */
4978 hs_buf = &hs->buffering.hs[0];
4979 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4980 {
4981 /* Synthesize a record containing the buffered HS message. */
4982 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4983 ( hs_buf->data[2] << 8 ) |
4984 hs_buf->data[3];
4985
4986 /* Double-check that we haven't accidentally buffered
4987 * a message that doesn't fit into the input buffer. */
4988 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4989 {
4990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4992 }
4993
4994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4995 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4996 hs_buf->data, msg_len + 12 );
4997
4998 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4999 ssl->in_hslen = msg_len + 12;
5000 ssl->in_msglen = msg_len + 12;
5001 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5002
5003 ret = 0;
5004 goto exit;
5005 }
5006 else
5007 {
5008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5009 hs->in_msg_seq ) );
5010 }
5011
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005012 ret = -1;
5013
5014exit:
5015
5016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5017 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005018}
5019
Hanno Beckera02b0b42018-08-21 17:20:27 +01005020static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5021 size_t desired )
5022{
5023 int offset;
5024 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5026 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005027
Hanno Becker01315ea2018-08-21 17:22:17 +01005028 /* Get rid of future records epoch first, if such exist. */
5029 ssl_free_buffered_record( ssl );
5030
5031 /* Check if we have enough space available now. */
5032 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5033 hs->buffering.total_bytes_buffered ) )
5034 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005036 return( 0 );
5037 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005038
Hanno Becker4f432ad2018-08-28 10:02:32 +01005039 /* We don't have enough space to buffer the next expected handshake
5040 * message. Remove buffers used for future messages to gain space,
5041 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005042 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5043 offset >= 0; offset-- )
5044 {
5045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5046 offset ) );
5047
Hanno Beckerb309b922018-08-23 13:18:05 +01005048 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005049
5050 /* Check if we have enough space available now. */
5051 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5052 hs->buffering.total_bytes_buffered ) )
5053 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005055 return( 0 );
5056 }
5057 }
5058
5059 return( -1 );
5060}
5061
Hanno Becker40f50842018-08-15 14:48:01 +01005062static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5063{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005064 int ret = 0;
5065 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5066
5067 if( hs == NULL )
5068 return( 0 );
5069
5070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5071
5072 switch( ssl->in_msgtype )
5073 {
5074 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005076
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005077 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005078 break;
5079
5080 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005081 {
5082 unsigned recv_msg_seq_offset;
5083 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5084 mbedtls_ssl_hs_buffer *hs_buf;
5085 size_t msg_len = ssl->in_hslen - 12;
5086
5087 /* We should never receive an old handshake
5088 * message - double-check nonetheless. */
5089 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5090 {
5091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5093 }
5094
5095 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5096 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5097 {
5098 /* Silently ignore -- message too far in the future */
5099 MBEDTLS_SSL_DEBUG_MSG( 2,
5100 ( "Ignore future HS message with sequence number %u, "
5101 "buffering window %u - %u",
5102 recv_msg_seq, ssl->handshake->in_msg_seq,
5103 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5104
5105 goto exit;
5106 }
5107
5108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5109 recv_msg_seq, recv_msg_seq_offset ) );
5110
5111 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5112
5113 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005114 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005115 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005116 size_t reassembly_buf_sz;
5117
Hanno Becker37f95322018-08-16 13:55:32 +01005118 hs_buf->is_fragmented =
5119 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5120
5121 /* We copy the message back into the input buffer
5122 * after reassembly, so check that it's not too large.
5123 * This is an implementation-specific limitation
5124 * and not one from the standard, hence it is not
5125 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005126 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005127 {
5128 /* Ignore message */
5129 goto exit;
5130 }
5131
Hanno Beckere0b150f2018-08-21 15:51:03 +01005132 /* Check if we have enough space to buffer the message. */
5133 if( hs->buffering.total_bytes_buffered >
5134 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5135 {
5136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5138 }
5139
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005140 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5141 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005142
5143 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5144 hs->buffering.total_bytes_buffered ) )
5145 {
5146 if( recv_msg_seq_offset > 0 )
5147 {
5148 /* If we can't buffer a future message because
5149 * of space limitations -- ignore. */
5150 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",
5151 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5152 (unsigned) hs->buffering.total_bytes_buffered ) );
5153 goto exit;
5154 }
Hanno Beckere1801392018-08-21 16:51:05 +01005155 else
5156 {
5157 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",
5158 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5159 (unsigned) hs->buffering.total_bytes_buffered ) );
5160 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005161
Hanno Beckera02b0b42018-08-21 17:20:27 +01005162 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005163 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005164 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",
5165 (unsigned) msg_len,
5166 (unsigned) reassembly_buf_sz,
5167 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005168 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005169 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5170 goto exit;
5171 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005172 }
5173
5174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5175 msg_len ) );
5176
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005177 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5178 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005179 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005180 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005181 goto exit;
5182 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005183 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005184
5185 /* Prepare final header: copy msg_type, length and message_seq,
5186 * then add standardised fragment_offset and fragment_length */
5187 memcpy( hs_buf->data, ssl->in_msg, 6 );
5188 memset( hs_buf->data + 6, 0, 3 );
5189 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5190
5191 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005192
5193 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005194 }
5195 else
5196 {
5197 /* Make sure msg_type and length are consistent */
5198 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5199 {
5200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5201 /* Ignore */
5202 goto exit;
5203 }
5204 }
5205
Hanno Becker4422bbb2018-08-20 09:40:19 +01005206 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005207 {
5208 size_t frag_len, frag_off;
5209 unsigned char * const msg = hs_buf->data + 12;
5210
5211 /*
5212 * Check and copy current fragment
5213 */
5214
5215 /* Validation of header fields already done in
5216 * mbedtls_ssl_prepare_handshake_record(). */
5217 frag_off = ssl_get_hs_frag_off( ssl );
5218 frag_len = ssl_get_hs_frag_len( ssl );
5219
5220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5221 frag_off, frag_len ) );
5222 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5223
5224 if( hs_buf->is_fragmented )
5225 {
5226 unsigned char * const bitmask = msg + msg_len;
5227 ssl_bitmask_set( bitmask, frag_off, frag_len );
5228 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5229 msg_len ) == 0 );
5230 }
5231 else
5232 {
5233 hs_buf->is_complete = 1;
5234 }
5235
5236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5237 hs_buf->is_complete ? "" : "not yet " ) );
5238 }
5239
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005240 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005241 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005242
5243 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005244 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005245 break;
5246 }
5247
5248exit:
5249
5250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5251 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005252}
5253#endif /* MBEDTLS_SSL_PROTO_DTLS */
5254
Hanno Becker1097b342018-08-15 14:09:41 +01005255static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005256{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005257 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005258 * Consume last content-layer message and potentially
5259 * update in_msglen which keeps track of the contents'
5260 * consumption state.
5261 *
5262 * (1) Handshake messages:
5263 * Remove last handshake message, move content
5264 * and adapt in_msglen.
5265 *
5266 * (2) Alert messages:
5267 * Consume whole record content, in_msglen = 0.
5268 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005269 * (3) Change cipher spec:
5270 * Consume whole record content, in_msglen = 0.
5271 *
5272 * (4) Application data:
5273 * Don't do anything - the record layer provides
5274 * the application data as a stream transport
5275 * and consumes through mbedtls_ssl_read only.
5276 *
5277 */
5278
5279 /* Case (1): Handshake messages */
5280 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005281 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005282 /* Hard assertion to be sure that no application data
5283 * is in flight, as corrupting ssl->in_msglen during
5284 * ssl->in_offt != NULL is fatal. */
5285 if( ssl->in_offt != NULL )
5286 {
5287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5289 }
5290
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005291 /*
5292 * Get next Handshake message in the current record
5293 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294
Hanno Becker4a810fb2017-05-24 16:27:30 +01005295 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005296 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005297 * current handshake content: If DTLS handshake
5298 * fragmentation is used, that's the fragment
5299 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005300 * size here is faulty and should be changed at
5301 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005302 * (2) While it doesn't seem to cause problems, one
5303 * has to be very careful not to assume that in_hslen
5304 * is always <= in_msglen in a sensible communication.
5305 * Again, it's wrong for DTLS handshake fragmentation.
5306 * The following check is therefore mandatory, and
5307 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005308 * Additionally, ssl->in_hslen might be arbitrarily out of
5309 * bounds after handling a DTLS message with an unexpected
5310 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005311 */
5312 if( ssl->in_hslen < ssl->in_msglen )
5313 {
5314 ssl->in_msglen -= ssl->in_hslen;
5315 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5316 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005317
Hanno Becker4a810fb2017-05-24 16:27:30 +01005318 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5319 ssl->in_msg, ssl->in_msglen );
5320 }
5321 else
5322 {
5323 ssl->in_msglen = 0;
5324 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005325
Hanno Becker4a810fb2017-05-24 16:27:30 +01005326 ssl->in_hslen = 0;
5327 }
5328 /* Case (4): Application data */
5329 else if( ssl->in_offt != NULL )
5330 {
5331 return( 0 );
5332 }
5333 /* Everything else (CCS & Alerts) */
5334 else
5335 {
5336 ssl->in_msglen = 0;
5337 }
5338
Hanno Becker1097b342018-08-15 14:09:41 +01005339 return( 0 );
5340}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005341
Hanno Beckere74d5562018-08-15 14:26:08 +01005342static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5343{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005344 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005345 return( 1 );
5346
5347 return( 0 );
5348}
5349
Hanno Becker5f066e72018-08-16 14:56:31 +01005350#if defined(MBEDTLS_SSL_PROTO_DTLS)
5351
5352static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5353{
5354 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5355 if( hs == NULL )
5356 return;
5357
Hanno Becker01315ea2018-08-21 17:22:17 +01005358 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005359 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005360 hs->buffering.total_bytes_buffered -=
5361 hs->buffering.future_record.len;
5362
5363 mbedtls_free( hs->buffering.future_record.data );
5364 hs->buffering.future_record.data = NULL;
5365 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005366}
5367
5368static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5369{
5370 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5371 unsigned char * rec;
5372 size_t rec_len;
5373 unsigned rec_epoch;
5374
5375 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5376 return( 0 );
5377
5378 if( hs == NULL )
5379 return( 0 );
5380
Hanno Becker5f066e72018-08-16 14:56:31 +01005381 rec = hs->buffering.future_record.data;
5382 rec_len = hs->buffering.future_record.len;
5383 rec_epoch = hs->buffering.future_record.epoch;
5384
5385 if( rec == NULL )
5386 return( 0 );
5387
Hanno Becker4cb782d2018-08-20 11:19:05 +01005388 /* Only consider loading future records if the
5389 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005390 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005391 return( 0 );
5392
Hanno Becker5f066e72018-08-16 14:56:31 +01005393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5394
5395 if( rec_epoch != ssl->in_epoch )
5396 {
5397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5398 goto exit;
5399 }
5400
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5402
5403 /* Double-check that the record is not too large */
5404 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5405 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5406 {
5407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5409 }
5410
5411 memcpy( ssl->in_hdr, rec, rec_len );
5412 ssl->in_left = rec_len;
5413 ssl->next_record_offset = 0;
5414
5415 ssl_free_buffered_record( ssl );
5416
5417exit:
5418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5419 return( 0 );
5420}
5421
5422static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5423{
5424 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5425 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005426 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005427
5428 /* Don't buffer future records outside handshakes. */
5429 if( hs == NULL )
5430 return( 0 );
5431
5432 /* Only buffer handshake records (we are only interested
5433 * in Finished messages). */
5434 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5435 return( 0 );
5436
5437 /* Don't buffer more than one future epoch record. */
5438 if( hs->buffering.future_record.data != NULL )
5439 return( 0 );
5440
Hanno Becker01315ea2018-08-21 17:22:17 +01005441 /* Don't buffer record if there's not enough buffering space remaining. */
5442 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5443 hs->buffering.total_bytes_buffered ) )
5444 {
5445 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",
5446 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5447 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005448 return( 0 );
5449 }
5450
Hanno Becker5f066e72018-08-16 14:56:31 +01005451 /* Buffer record */
5452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5453 ssl->in_epoch + 1 ) );
5454 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5455 rec_hdr_len + ssl->in_msglen );
5456
5457 /* ssl_parse_record_header() only considers records
5458 * of the next epoch as candidates for buffering. */
5459 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005460 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005461
5462 hs->buffering.future_record.data =
5463 mbedtls_calloc( 1, hs->buffering.future_record.len );
5464 if( hs->buffering.future_record.data == NULL )
5465 {
5466 /* If we run out of RAM trying to buffer a
5467 * record from the next epoch, just ignore. */
5468 return( 0 );
5469 }
5470
Hanno Becker01315ea2018-08-21 17:22:17 +01005471 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005472
Hanno Becker01315ea2018-08-21 17:22:17 +01005473 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005474 return( 0 );
5475}
5476
5477#endif /* MBEDTLS_SSL_PROTO_DTLS */
5478
Hanno Beckere74d5562018-08-15 14:26:08 +01005479static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005480{
5481 int ret;
5482
Hanno Becker5f066e72018-08-16 14:56:31 +01005483#if defined(MBEDTLS_SSL_PROTO_DTLS)
5484 /* We might have buffered a future record; if so,
5485 * and if the epoch matches now, load it.
5486 * On success, this call will set ssl->in_left to
5487 * the length of the buffered record, so that
5488 * the calls to ssl_fetch_input() below will
5489 * essentially be no-ops. */
5490 ret = ssl_load_buffered_record( ssl );
5491 if( ret != 0 )
5492 return( ret );
5493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005494
Hanno Becker8b09b732019-05-08 12:03:28 +01005495 /* Reset in pointers to default state for TLS/DTLS records,
5496 * assuming no CID and no offset between record content and
5497 * record plaintext. */
5498 ssl_update_in_pointers( ssl );
5499
5500 /* Ensure that we have enough space available for the default form
5501 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5502 * with no space for CIDs counted in). */
5503 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5504 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005507 return( ret );
5508 }
5509
5510 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5514 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005515 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005516 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5517 {
5518 ret = ssl_buffer_future_record( ssl );
5519 if( ret != 0 )
5520 return( ret );
5521
5522 /* Fall through to handling of unexpected records */
5523 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5524 }
5525
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005526 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5527 {
5528 /* Skip unexpected record (but not whole datagram) */
5529 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005530 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005531
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5533 "(header)" ) );
5534 }
5535 else
5536 {
5537 /* Skip invalid record and the rest of the datagram */
5538 ssl->next_record_offset = 0;
5539 ssl->in_left = 0;
5540
5541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5542 "(header)" ) );
5543 }
5544
5545 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005546 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005547 }
5548#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005549 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005550 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005551
5552 /*
5553 * Read and optionally decrypt the message contents
5554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005556 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005559 return( ret );
5560 }
5561
5562 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005564 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005565 {
Hanno Becker43395762019-05-03 14:46:38 +01005566 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005567 if( ssl->next_record_offset < ssl->in_left )
5568 {
5569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5570 }
5571 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005572 else
5573#endif
5574 ssl->in_left = 0;
5575
5576 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005579 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005580 {
5581 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005582 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005583 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005584 /* Except when waiting for Finished as a bad mac here
5585 * probably means something went wrong in the handshake
5586 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5587 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5588 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5589 {
5590#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5591 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5592 {
5593 mbedtls_ssl_send_alert_message( ssl,
5594 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5595 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5596 }
5597#endif
5598 return( ret );
5599 }
5600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005602 if( ssl->conf->badmac_limit != 0 &&
5603 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5606 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005607 }
5608#endif
5609
Hanno Becker4a810fb2017-05-24 16:27:30 +01005610 /* As above, invalid records cause
5611 * dismissal of the whole datagram. */
5612
5613 ssl->next_record_offset = 0;
5614 ssl->in_left = 0;
5615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005617 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005618 }
5619
5620 return( ret );
5621 }
5622 else
5623#endif
5624 {
5625 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5627 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 mbedtls_ssl_send_alert_message( ssl,
5630 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5631 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005632 }
5633#endif
5634 return( ret );
5635 }
5636 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005637
Simon Butcher99000142016-10-13 17:21:01 +01005638 return( 0 );
5639}
5640
5641int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5642{
5643 int ret;
5644
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005645 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005646 * Handle particular types of records
5647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 {
Simon Butcher99000142016-10-13 17:21:01 +01005650 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5651 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005652 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 }
5655
Hanno Beckere678eaa2018-08-21 14:57:46 +01005656 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005657 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005658 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005659 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5661 ssl->in_msglen ) );
5662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005663 }
5664
Hanno Beckere678eaa2018-08-21 14:57:46 +01005665 if( ssl->in_msg[0] != 1 )
5666 {
5667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5668 ssl->in_msg[0] ) );
5669 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5670 }
5671
5672#if defined(MBEDTLS_SSL_PROTO_DTLS)
5673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5674 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5675 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5676 {
5677 if( ssl->handshake == NULL )
5678 {
5679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5680 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5681 }
5682
5683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5684 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5685 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005686#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005687 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005691 if( ssl->in_msglen != 2 )
5692 {
5693 /* Note: Standard allows for more than one 2 byte alert
5694 to be packed in a single message, but Mbed TLS doesn't
5695 currently support this. */
5696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5697 ssl->in_msglen ) );
5698 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5699 }
5700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 ssl->in_msg[0], ssl->in_msg[1] ) );
5703
5704 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005705 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005710 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 }
5713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5715 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5718 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005720
5721#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5722 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5723 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5724 {
Hanno Becker90333da2017-10-10 11:27:13 +01005725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005726 /* Will be handled when trying to parse ServerHello */
5727 return( 0 );
5728 }
5729#endif
5730
5731#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5732 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5733 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5734 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5735 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5736 {
5737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5738 /* Will be handled in mbedtls_ssl_parse_certificate() */
5739 return( 0 );
5740 }
5741#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5742
5743 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005744 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005745 }
5746
Hanno Beckerc76c6192017-06-06 10:03:17 +01005747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005749 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005750 /* Drop unexpected ApplicationData records,
5751 * except at the beginning of renegotiations */
5752 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5753 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5754#if defined(MBEDTLS_SSL_RENEGOTIATION)
5755 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5756 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005757#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005758 )
5759 {
5760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5761 return( MBEDTLS_ERR_SSL_NON_FATAL );
5762 }
5763
5764 if( ssl->handshake != NULL &&
5765 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5766 {
5767 ssl_handshake_wrapup_free_hs_transform( ssl );
5768 }
5769 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005770#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005771
Paul Bakker5121ce52009-01-03 21:22:43 +00005772 return( 0 );
5773}
5774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005776{
5777 int ret;
5778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5780 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5781 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005782 {
5783 return( ret );
5784 }
5785
5786 return( 0 );
5787}
5788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005790 unsigned char level,
5791 unsigned char message )
5792{
5793 int ret;
5794
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005795 if( ssl == NULL || ssl->conf == NULL )
5796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005802 ssl->out_msglen = 2;
5803 ssl->out_msg[0] = level;
5804 ssl->out_msg[1] = message;
5805
Hanno Becker67bc7c32018-08-06 11:33:50 +01005806 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005809 return( ret );
5810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005812
5813 return( 0 );
5814}
5815
Paul Bakker5121ce52009-01-03 21:22:43 +00005816/*
5817 * Handshake functions
5818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5820 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5821 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5822 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5823 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5824 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5825 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005826/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005828{
Hanno Becker8759e162017-12-27 21:34:08 +00005829 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5834 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005835 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5836 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005839 ssl->state++;
5840 return( 0 );
5841 }
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005845}
5846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005848{
Hanno Becker8759e162017-12-27 21:34:08 +00005849 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5854 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005855 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5856 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005859 ssl->state++;
5860 return( 0 );
5861 }
5862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005865}
Gilles Peskinef9828522017-05-03 12:28:43 +02005866
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005867#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005868/* Some certificate support -> implement write and parse */
5869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005873 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005875 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5880 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005881 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5882 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005885 ssl->state++;
5886 return( 0 );
5887 }
5888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005890 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 {
5892 if( ssl->client_auth == 0 )
5893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 ssl->state++;
5896 return( 0 );
5897 }
5898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 /*
5901 * If using SSLv3 and got no cert, send an Alert message
5902 * (otherwise an empty Certificate message will be sent).
5903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5905 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005906 {
5907 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5909 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5910 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 goto write_msg;
5914 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917#endif /* MBEDTLS_SSL_CLI_C */
5918#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005919 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5924 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005925 }
5926 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005927#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
5931 /*
5932 * 0 . 0 handshake type
5933 * 1 . 3 handshake length
5934 * 4 . 6 length of all certs
5935 * 7 . 9 length of cert. 1
5936 * 10 . n-1 peer certificate
5937 * n . n+2 length of cert. 2
5938 * n+3 . ... upper level cert, etc.
5939 */
5940 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942
Paul Bakker29087132010-03-21 21:03:34 +00005943 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005944 {
5945 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005946 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005949 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 }
5952
5953 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5954 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5955 ssl->out_msg[i + 2] = (unsigned char)( n );
5956
5957 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5958 i += n; crt = crt->next;
5959 }
5960
5961 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5962 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5963 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5964
5965 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5967 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005968
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005969#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005970write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005971#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005972
5973 ssl->state++;
5974
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005975 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005976 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005977 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005978 return( ret );
5979 }
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005982
Paul Bakkered27a042013-04-18 22:46:23 +02005983 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005984}
5985
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005986/*
5987 * Once the certificate message is read, parse it into a cert chain and
5988 * perform basic checks, but leave actual verification to the caller
5989 */
5990static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005991{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005992 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005993 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005994 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996#if defined(MBEDTLS_SSL_SRV_C)
5997#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005998 /*
5999 * Check if the client sent an empty certificate
6000 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006001 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006003 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006004 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6006 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6007 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006010
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006011 /* The client was asked for a certificate but didn't send
6012 one. The client should know what's going on, so we
6013 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006014 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006015 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 }
6017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6021 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006022 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6026 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6027 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6028 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006032 /* The client was asked for a certificate but didn't send
6033 one. The client should know what's going on, so we
6034 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006035 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006036 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 }
6038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6040 MBEDTLS_SSL_PROTO_TLS1_2 */
6041#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006046 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6047 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006049 }
6050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6052 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006055 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6056 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 }
6059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006061
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006065 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006067 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6072 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 }
6075
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006076 /* In case we tried to reuse a session but it failed */
6077 if( ssl->session_negotiate->peer_cert != NULL )
6078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6080 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006081 }
6082
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006083 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006088 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6089 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006090 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006091 }
6092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006094
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006095 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006096
6097 while( i < ssl->in_hslen )
6098 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006099 if ( i + 3 > ssl->in_hslen ) {
6100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6101 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6102 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6103 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006105 if( ssl->in_msg[i] != 0 )
6106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6109 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 }
6112
6113 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6114 | (unsigned int) ssl->in_msg[i + 2];
6115 i += 3;
6116
6117 if( n < 128 || i + n > ssl->in_hslen )
6118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6121 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 }
6124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006126 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006127 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006129 case 0: /*ok*/
6130 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6131 /* Ignore certificate with an unknown algorithm: maybe a
6132 prior certificate was already trusted. */
6133 break;
6134
6135 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6136 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6137 goto crt_parse_der_failed;
6138
6139 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6140 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6141 goto crt_parse_der_failed;
6142
6143 default:
6144 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6145 crt_parse_der_failed:
6146 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006148 return( ret );
6149 }
6150
6151 i += n;
6152 }
6153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006155
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006156 /*
6157 * On client, make sure the server cert doesn't change during renego to
6158 * avoid "triple handshake" attack: https://secure-resumption.com/
6159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006161 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006163 {
6164 if( ssl->session->peer_cert == NULL )
6165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006167 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6168 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006170 }
6171
6172 if( ssl->session->peer_cert->raw.len !=
6173 ssl->session_negotiate->peer_cert->raw.len ||
6174 memcmp( ssl->session->peer_cert->raw.p,
6175 ssl->session_negotiate->peer_cert->raw.p,
6176 ssl->session->peer_cert->raw.len ) != 0 )
6177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006179 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6180 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006182 }
6183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006185
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006186 return( 0 );
6187}
6188
6189int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6190{
6191 int ret;
6192 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006193 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006194#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6195 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6196 ? ssl->handshake->sni_authmode
6197 : ssl->conf->authmode;
6198#else
6199 const int authmode = ssl->conf->authmode;
6200#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006201 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006202
6203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6204
6205 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6206 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6207 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6208 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6209 {
6210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6211 ssl->state++;
6212 return( 0 );
6213 }
6214
6215#if defined(MBEDTLS_SSL_SRV_C)
6216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6217 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6218 {
6219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6220 ssl->state++;
6221 return( 0 );
6222 }
6223
6224 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6225 authmode == MBEDTLS_SSL_VERIFY_NONE )
6226 {
6227 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006229
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006230 ssl->state++;
6231 return( 0 );
6232 }
6233#endif
6234
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006235#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6236 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006237 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006238 {
6239 goto crt_verify;
6240 }
6241#endif
6242
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006243 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006244 {
6245 /* mbedtls_ssl_read_record may have sent an alert already. We
6246 let it decide whether to alert. */
6247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6248 return( ret );
6249 }
6250
6251 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6252 {
6253#if defined(MBEDTLS_SSL_SRV_C)
6254 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6255 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6256 {
6257 ret = 0;
6258 }
6259#endif
6260
6261 ssl->state++;
6262 return( ret );
6263 }
6264
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006265#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6266 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006267 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006268
6269crt_verify:
6270 if( ssl->handshake->ecrs_enabled)
6271 rs_ctx = &ssl->handshake->ecrs_ctx;
6272#endif
6273
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006274 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006276 mbedtls_x509_crt *ca_chain;
6277 mbedtls_x509_crl *ca_crl;
6278
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006279#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006280 if( ssl->handshake->sni_ca_chain != NULL )
6281 {
6282 ca_chain = ssl->handshake->sni_ca_chain;
6283 ca_crl = ssl->handshake->sni_ca_crl;
6284 }
6285 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006286#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006287 {
6288 ca_chain = ssl->conf->ca_chain;
6289 ca_crl = ssl->conf->ca_crl;
6290 }
6291
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006292 /*
6293 * Main check: verify certificate
6294 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006295 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006296 ssl->session_negotiate->peer_cert,
6297 ca_chain, ca_crl,
6298 ssl->conf->cert_profile,
6299 ssl->hostname,
6300 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006301 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006302
6303 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006306 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006307
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006308#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6309 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006310 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006311#endif
6312
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006313 /*
6314 * Secondary checks: always done, but change 'ret' only if it was 0
6315 */
6316
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006317#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006320
6321 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006323 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006324 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006325 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006328 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006330 }
6331 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006332#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006335 ciphersuite_info,
6336 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006337 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006340 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006342 }
6343
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006344 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6345 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6346 * with details encoded in the verification flags. All other kinds
6347 * of error codes, including those from the user provided f_vrfy
6348 * functions, are treated as fatal and lead to a failure of
6349 * ssl_parse_certificate even if verification was optional. */
6350 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6351 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6352 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6353 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006355 }
6356
6357 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6358 {
6359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6360 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6361 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006362
6363 if( ret != 0 )
6364 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006365 uint8_t alert;
6366
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006367 /* The certificate may have been rejected for several reasons.
6368 Pick one and send the corresponding alert. Which alert to send
6369 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006370 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6371 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6372 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6373 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6374 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6375 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6376 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6377 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6378 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6379 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6380 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6381 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6382 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6383 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6384 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6385 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6386 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6387 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6388 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6389 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006390 else
6391 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006392 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6393 alert );
6394 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006395
Hanno Beckere6706e62017-05-15 16:05:15 +01006396#if defined(MBEDTLS_DEBUG_C)
6397 if( ssl->session_negotiate->verify_result != 0 )
6398 {
6399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6400 ssl->session_negotiate->verify_result ) );
6401 }
6402 else
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6405 }
6406#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006407 }
6408
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006409 ssl->state++;
6410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
6413 return( ret );
6414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6416 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6417 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6418 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6419 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6420 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6421 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006424{
6425 int ret;
6426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006430 ssl->out_msglen = 1;
6431 ssl->out_msg[0] = 1;
6432
Paul Bakker5121ce52009-01-03 21:22:43 +00006433 ssl->state++;
6434
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006435 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006436 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 return( ret );
6439 }
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006442
6443 return( 0 );
6444}
6445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006447{
6448 int ret;
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006451
Hanno Becker327c93b2018-08-15 13:56:18 +01006452 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006455 return( ret );
6456 }
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006461 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6462 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006464 }
6465
Hanno Beckere678eaa2018-08-21 14:57:46 +01006466 /* CCS records are only accepted if they have length 1 and content '1',
6467 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006468
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006469 /*
6470 * Switch to our negotiated transform and session parameters for inbound
6471 * data.
6472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006474 ssl->transform_in = ssl->transform_negotiate;
6475 ssl->session_in = ssl->session_negotiate;
6476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006481 ssl_dtls_replay_reset( ssl );
6482#endif
6483
6484 /* Increment epoch */
6485 if( ++ssl->in_epoch == 0 )
6486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006488 /* This is highly unlikely to happen for legitimate reasons, so
6489 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006491 }
6492 }
6493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006495 memset( ssl->in_ctr, 0, 8 );
6496
Hanno Beckerf5970a02019-05-08 09:38:41 +01006497 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6500 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006505 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6506 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006508 }
6509 }
6510#endif
6511
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 ssl->state++;
6513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006515
6516 return( 0 );
6517}
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6520 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006521{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006522 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6525 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6526 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006527 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006528 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6531#if defined(MBEDTLS_SHA512_C)
6532 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006533 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6534 else
6535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536#if defined(MBEDTLS_SHA256_C)
6537 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006538 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006539 else
6540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006544 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006545 }
Paul Bakker380da532012-04-18 16:10:25 +00006546}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6551 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006552 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6553 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6556#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006557 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006558#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006560 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006561#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006563}
6564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006566 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6569 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006570 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6571 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6574#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006575 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006578 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006581}
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6584 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6585static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006586 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006587{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006588 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6589 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006590}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006591#endif
Paul Bakker380da532012-04-18 16:10:25 +00006592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6594#if defined(MBEDTLS_SHA256_C)
6595static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006596 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006597{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006598 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006599}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006600#endif
Paul Bakker380da532012-04-18 16:10:25 +00006601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602#if defined(MBEDTLS_SHA512_C)
6603static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006604 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006605{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006606 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006607}
Paul Bakker769075d2012-11-24 11:26:46 +01006608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006612static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006615 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006616 mbedtls_md5_context md5;
6617 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006618
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 unsigned char padbuf[48];
6620 unsigned char md5sum[16];
6621 unsigned char sha1sum[20];
6622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006624 if( !session )
6625 session = ssl->session;
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006628
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006629 mbedtls_md5_init( &md5 );
6630 mbedtls_sha1_init( &sha1 );
6631
6632 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6633 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006634
6635 /*
6636 * SSLv3:
6637 * hash =
6638 * MD5( master + pad2 +
6639 * MD5( handshake + sender + master + pad1 ) )
6640 * + SHA1( master + pad2 +
6641 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 */
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006645 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6646 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006647#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006650 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6651 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006652#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006655 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
Paul Bakker1ef83d62012-04-11 12:09:53 +00006657 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006659 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6660 mbedtls_md5_update_ret( &md5, session->master, 48 );
6661 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6662 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006664 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6665 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6666 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6667 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006668
Paul Bakker1ef83d62012-04-11 12:09:53 +00006669 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006670
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006671 mbedtls_md5_starts_ret( &md5 );
6672 mbedtls_md5_update_ret( &md5, session->master, 48 );
6673 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6674 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6675 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006676
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006677 mbedtls_sha1_starts_ret( &sha1 );
6678 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6679 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6680 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6681 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006685 mbedtls_md5_free( &md5 );
6686 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006687
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006688 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6689 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6690 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006697static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006699{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006700 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006701 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006702 mbedtls_md5_context md5;
6703 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006704 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006707 if( !session )
6708 session = ssl->session;
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006711
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006712 mbedtls_md5_init( &md5 );
6713 mbedtls_sha1_init( &sha1 );
6714
6715 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6716 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006717
Paul Bakker1ef83d62012-04-11 12:09:53 +00006718 /*
6719 * TLSv1:
6720 * hash = PRF( master, finished_label,
6721 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6722 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006725 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6726 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006727#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006730 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6731 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006732#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006735 ? "client finished"
6736 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006737
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006738 mbedtls_md5_finish_ret( &md5, padbuf );
6739 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006740
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006741 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006742 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006745
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006746 mbedtls_md5_free( &md5 );
6747 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006748
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006749 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6756#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006757static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006759{
6760 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006761 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006762 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006763 unsigned char padbuf[32];
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006766 if( !session )
6767 session = ssl->session;
6768
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006769 mbedtls_sha256_init( &sha256 );
6770
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006772
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006773 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006774
6775 /*
6776 * TLSv1.2:
6777 * hash = PRF( master, finished_label,
6778 * Hash( handshake ) )[0.11]
6779 */
6780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#if !defined(MBEDTLS_SHA256_ALT)
6782 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006783 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006784#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006787 ? "client finished"
6788 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006789
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006790 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006791
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006792 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006793 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006796
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006797 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006798
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006799 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006806static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006808{
6809 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006810 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006811 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006812 unsigned char padbuf[48];
6813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006815 if( !session )
6816 session = ssl->session;
6817
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006818 mbedtls_sha512_init( &sha512 );
6819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006821
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006822 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006823
6824 /*
6825 * TLSv1.2:
6826 * hash = PRF( master, finished_label,
6827 * Hash( handshake ) )[0.11]
6828 */
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006831 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6832 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006833#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006836 ? "client finished"
6837 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006838
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006839 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006840
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006841 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006842 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006845
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006846 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006847
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006848 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#endif /* MBEDTLS_SHA512_C */
6853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006856{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006858
6859 /*
6860 * Free our handshake params
6861 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006862 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006864 ssl->handshake = NULL;
6865
6866 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006867 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006868 */
6869 if( ssl->transform )
6870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 mbedtls_ssl_transform_free( ssl->transform );
6872 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006873 }
6874 ssl->transform = ssl->transform_negotiate;
6875 ssl->transform_negotiate = NULL;
6876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006878}
6879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006881{
6882 int resume = ssl->handshake->resume;
6883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886#if defined(MBEDTLS_SSL_RENEGOTIATION)
6887 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006890 ssl->renego_records_seen = 0;
6891 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006892#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006893
6894 /*
6895 * Free the previous session and switch in the current one
6896 */
Paul Bakker0a597072012-09-25 21:55:46 +00006897 if( ssl->session )
6898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006900 /* RFC 7366 3.1: keep the EtM state */
6901 ssl->session_negotiate->encrypt_then_mac =
6902 ssl->session->encrypt_then_mac;
6903#endif
6904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 mbedtls_ssl_session_free( ssl->session );
6906 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006907 }
6908 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006909 ssl->session_negotiate = NULL;
6910
Paul Bakker0a597072012-09-25 21:55:46 +00006911 /*
6912 * Add cache entry
6913 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006914 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006915 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006916 resume == 0 )
6917 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006918 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006920 }
Paul Bakker0a597072012-09-25 21:55:46 +00006921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006923 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006924 ssl->handshake->flight != NULL )
6925 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006926 /* Cancel handshake timer */
6927 ssl_set_timer( ssl, 0 );
6928
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006929 /* Keep last flight around in case we need to resend it:
6930 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006932 }
6933 else
6934#endif
6935 ssl_handshake_wrapup_free_hs_transform( ssl );
6936
Paul Bakker48916f92012-09-16 19:57:18 +00006937 ssl->state++;
6938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006940}
6941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006943{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006944 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006947
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006948 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006949
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006950 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006951
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006952 /*
6953 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6954 * may define some other value. Currently (early 2016), no defined
6955 * ciphersuite does this (and this is unlikely to change as activity has
6956 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006961 ssl->verify_data_len = hash_len;
6962 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006963#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006964
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6967 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006968
6969 /*
6970 * In case of session resuming, invert the client and server
6971 * ChangeCipherSpec messages order.
6972 */
Paul Bakker0a597072012-09-25 21:55:46 +00006973 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006976 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006980 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006982#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 }
6984 else
6985 ssl->state++;
6986
Paul Bakker48916f92012-09-16 19:57:18 +00006987 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006988 * Switch to our negotiated transform and session parameters for outbound
6989 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006995 {
6996 unsigned char i;
6997
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006998 /* Remember current epoch settings for resending */
6999 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007000 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007001
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007002 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007003 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007004
7005 /* Increment epoch */
7006 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007007 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007008 break;
7009
7010 /* The loop goes to its end iff the counter is wrapping */
7011 if( i == 0 )
7012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7014 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007015 }
7016 }
7017 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007019 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007020
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007021 ssl->transform_out = ssl->transform_negotiate;
7022 ssl->session_out = ssl->session_negotiate;
7023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7025 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7030 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007031 }
7032 }
7033#endif
7034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007038#endif
7039
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007040 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007041 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007043 return( ret );
7044 }
7045
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007046#if defined(MBEDTLS_SSL_PROTO_DTLS)
7047 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7048 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7049 {
7050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7051 return( ret );
7052 }
7053#endif
7054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007056
7057 return( 0 );
7058}
7059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007061#define SSL_MAX_HASH_LEN 36
7062#else
7063#define SSL_MAX_HASH_LEN 12
7064#endif
7065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007067{
Paul Bakker23986e52011-04-24 08:57:21 +00007068 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007069 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007070 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007073
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007074 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007075
Hanno Becker327c93b2018-08-15 13:56:18 +01007076 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007079 return( ret );
7080 }
7081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007085 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 }
7089
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007090 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091#if defined(MBEDTLS_SSL_PROTO_SSL3)
7092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007093 hash_len = 36;
7094 else
7095#endif
7096 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7099 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7103 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105 }
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007108 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007111 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007114 }
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007117 ssl->verify_data_len = hash_len;
7118 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007119#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007120
Paul Bakker0a597072012-09-25 21:55:46 +00007121 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007124 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007128 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007131 }
7132 else
7133 ssl->state++;
7134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007138#endif
7139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007141
7142 return( 0 );
7143}
7144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7150 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7151 mbedtls_md5_init( &handshake->fin_md5 );
7152 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007153 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7154 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7157#if defined(MBEDTLS_SHA256_C)
7158 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007159 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161#if defined(MBEDTLS_SHA512_C)
7162 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007163 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007166
7167 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007168
7169#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7170 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7171 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7172#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#if defined(MBEDTLS_DHM_C)
7175 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#if defined(MBEDTLS_ECDH_C)
7178 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007179#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007180#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007181 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007182#if defined(MBEDTLS_SSL_CLI_C)
7183 handshake->ecjpake_cache = NULL;
7184 handshake->ecjpake_cache_len = 0;
7185#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007186#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007187
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007188#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007189 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007190#endif
7191
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007192#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7193 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7194#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007195}
7196
Hanno Becker611a83b2018-01-03 14:27:32 +00007197void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7202 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007203
Hanno Becker92231322018-01-03 15:32:51 +00007204#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 mbedtls_md_init( &transform->md_ctx_enc );
7206 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007207#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007208}
7209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007213}
7214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007216{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007217 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007218 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007220 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007222 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007223 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007224
7225 /*
7226 * Either the pointers are now NULL or cleared properly and can be freed.
7227 * Now allocate missing structures.
7228 */
7229 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007230 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007231 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007232 }
Paul Bakker48916f92012-09-16 19:57:18 +00007233
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007234 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007235 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007236 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007237 }
Paul Bakker48916f92012-09-16 19:57:18 +00007238
Paul Bakker82788fb2014-10-20 13:59:19 +02007239 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007240 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007241 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007242 }
Paul Bakker48916f92012-09-16 19:57:18 +00007243
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007244 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007245 if( ssl->handshake == NULL ||
7246 ssl->transform_negotiate == NULL ||
7247 ssl->session_negotiate == NULL )
7248 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 mbedtls_free( ssl->handshake );
7252 mbedtls_free( ssl->transform_negotiate );
7253 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007254
7255 ssl->handshake = NULL;
7256 ssl->transform_negotiate = NULL;
7257 ssl->session_negotiate = NULL;
7258
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007259 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007260 }
7261
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007262 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007264 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007265 ssl_handshake_params_init( ssl->handshake );
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7269 {
7270 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007271
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007272 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7273 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7274 else
7275 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007276
7277 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007278 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007279#endif
7280
Paul Bakker48916f92012-09-16 19:57:18 +00007281 return( 0 );
7282}
7283
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007284#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007285/* Dummy cookie callbacks for defaults */
7286static int ssl_cookie_write_dummy( void *ctx,
7287 unsigned char **p, unsigned char *end,
7288 const unsigned char *cli_id, size_t cli_id_len )
7289{
7290 ((void) ctx);
7291 ((void) p);
7292 ((void) end);
7293 ((void) cli_id);
7294 ((void) cli_id_len);
7295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007297}
7298
7299static int ssl_cookie_check_dummy( void *ctx,
7300 const unsigned char *cookie, size_t cookie_len,
7301 const unsigned char *cli_id, size_t cli_id_len )
7302{
7303 ((void) ctx);
7304 ((void) cookie);
7305 ((void) cookie_len);
7306 ((void) cli_id);
7307 ((void) cli_id_len);
7308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007310}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007311#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007312
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007313/* Once ssl->out_hdr as the address of the beginning of the
7314 * next outgoing record is set, deduce the other pointers.
7315 *
7316 * Note: For TLS, we save the implicit record sequence number
7317 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7318 * and the caller has to make sure there's space for this.
7319 */
7320
7321static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7322 mbedtls_ssl_transform *transform )
7323{
7324#if defined(MBEDTLS_SSL_PROTO_DTLS)
7325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7326 {
7327 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007329 ssl->out_cid = ssl->out_ctr + 8;
7330 ssl->out_len = ssl->out_cid;
7331 if( transform != NULL )
7332 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007333#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007334 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007335#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007336 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007337 }
7338 else
7339#endif
7340 {
7341 ssl->out_ctr = ssl->out_hdr - 8;
7342 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007343#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007344 ssl->out_cid = ssl->out_len;
7345#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007346 ssl->out_iv = ssl->out_hdr + 5;
7347 }
7348
7349 /* Adjust out_msg to make space for explicit IV, if used. */
7350 if( transform != NULL &&
7351 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7352 {
7353 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7354 }
7355 else
7356 ssl->out_msg = ssl->out_iv;
7357}
7358
7359/* Once ssl->in_hdr as the address of the beginning of the
7360 * next incoming record is set, deduce the other pointers.
7361 *
7362 * Note: For TLS, we save the implicit record sequence number
7363 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7364 * and the caller has to make sure there's space for this.
7365 */
7366
Hanno Beckerf5970a02019-05-08 09:38:41 +01007367static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007368{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007369 /* This function sets the pointers to match the case
7370 * of unprotected TLS/DTLS records, with both ssl->in_iv
7371 * and ssl->in_msg pointing to the beginning of the record
7372 * content.
7373 *
7374 * When decrypting a protected record, ssl->in_msg
7375 * will be shifted to point to the beginning of the
7376 * record plaintext.
7377 */
7378
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007379#if defined(MBEDTLS_SSL_PROTO_DTLS)
7380 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7381 {
Hanno Becker70e79282019-05-03 14:34:53 +01007382 /* This sets the header pointers to match records
7383 * without CID. When we receive a record containing
7384 * a CID, the fields are shifted accordingly in
7385 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007386 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007387#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007388 ssl->in_cid = ssl->in_ctr + 8;
7389 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007390#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007391 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007392#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007393 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007394 }
7395 else
7396#endif
7397 {
7398 ssl->in_ctr = ssl->in_hdr - 8;
7399 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007400#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007401 ssl->in_cid = ssl->in_len;
7402#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007403 ssl->in_iv = ssl->in_hdr + 5;
7404 }
7405
Hanno Beckerf5970a02019-05-08 09:38:41 +01007406 /* This will be adjusted at record decryption time. */
7407 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007408}
7409
Paul Bakker5121ce52009-01-03 21:22:43 +00007410/*
7411 * Initialize an SSL context
7412 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007413void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7414{
7415 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7416}
7417
7418/*
7419 * Setup an SSL context
7420 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007421
7422static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7423{
7424 /* Set the incoming and outgoing record pointers. */
7425#if defined(MBEDTLS_SSL_PROTO_DTLS)
7426 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7427 {
7428 ssl->out_hdr = ssl->out_buf;
7429 ssl->in_hdr = ssl->in_buf;
7430 }
7431 else
7432#endif /* MBEDTLS_SSL_PROTO_DTLS */
7433 {
7434 ssl->out_hdr = ssl->out_buf + 8;
7435 ssl->in_hdr = ssl->in_buf + 8;
7436 }
7437
7438 /* Derive other internal pointers. */
7439 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007440 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007441}
7442
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007443int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007444 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007445{
Paul Bakker48916f92012-09-16 19:57:18 +00007446 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007447
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007448 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007449
7450 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007451 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007452 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007453
7454 /* Set to NULL in case of an error condition */
7455 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007456
Angus Grattond8213d02016-05-25 20:56:48 +10007457 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7458 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007459 {
Angus Grattond8213d02016-05-25 20:56:48 +10007460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007461 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007462 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007463 }
7464
7465 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7466 if( ssl->out_buf == NULL )
7467 {
7468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007469 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007470 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 }
7472
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007473 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007474
Paul Bakker48916f92012-09-16 19:57:18 +00007475 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007476 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007477
7478 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007479
7480error:
7481 mbedtls_free( ssl->in_buf );
7482 mbedtls_free( ssl->out_buf );
7483
7484 ssl->conf = NULL;
7485
7486 ssl->in_buf = NULL;
7487 ssl->out_buf = NULL;
7488
7489 ssl->in_hdr = NULL;
7490 ssl->in_ctr = NULL;
7491 ssl->in_len = NULL;
7492 ssl->in_iv = NULL;
7493 ssl->in_msg = NULL;
7494
7495 ssl->out_hdr = NULL;
7496 ssl->out_ctr = NULL;
7497 ssl->out_len = NULL;
7498 ssl->out_iv = NULL;
7499 ssl->out_msg = NULL;
7500
k-stachowiak9f7798e2018-07-31 16:52:32 +02007501 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502}
7503
7504/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007505 * Reset an initialized and used SSL context for re-use while retaining
7506 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007507 *
7508 * If partial is non-zero, keep data in the input buffer and client ID.
7509 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007510 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007511static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007512{
Paul Bakker48916f92012-09-16 19:57:18 +00007513 int ret;
7514
Hanno Becker7e772132018-08-10 12:38:21 +01007515#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7516 !defined(MBEDTLS_SSL_SRV_C)
7517 ((void) partial);
7518#endif
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007521
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007522 /* Cancel any possibly running timer */
7523 ssl_set_timer( ssl, 0 );
7524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#if defined(MBEDTLS_SSL_RENEGOTIATION)
7526 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007527 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007528
7529 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7531 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007534
Paul Bakker7eb013f2011-10-06 12:37:39 +00007535 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007536 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007537
7538 ssl->in_msgtype = 0;
7539 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007541 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007542 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007545 ssl_dtls_replay_reset( ssl );
7546#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007547
7548 ssl->in_hslen = 0;
7549 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007550
7551 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007552
7553 ssl->out_msgtype = 0;
7554 ssl->out_msglen = 0;
7555 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7557 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007558 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007559#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007560
Hanno Becker19859472018-08-06 09:40:20 +01007561 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7562
Paul Bakker48916f92012-09-16 19:57:18 +00007563 ssl->transform_in = NULL;
7564 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007565
Hanno Becker78640902018-08-13 16:35:15 +01007566 ssl->session_in = NULL;
7567 ssl->session_out = NULL;
7568
Angus Grattond8213d02016-05-25 20:56:48 +10007569 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007570
7571#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007572 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007573#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7574 {
7575 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007576 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007577 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7580 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7583 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7586 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007587 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007588 }
7589#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007590
Paul Bakker48916f92012-09-16 19:57:18 +00007591 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 mbedtls_ssl_transform_free( ssl->transform );
7594 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007595 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007596 }
Paul Bakker48916f92012-09-16 19:57:18 +00007597
Paul Bakkerc0463502013-02-14 11:19:38 +01007598 if( ssl->session )
7599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 mbedtls_ssl_session_free( ssl->session );
7601 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007602 ssl->session = NULL;
7603 }
7604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007606 ssl->alpn_chosen = NULL;
7607#endif
7608
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007609#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007610#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007611 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007612#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007613 {
7614 mbedtls_free( ssl->cli_id );
7615 ssl->cli_id = NULL;
7616 ssl->cli_id_len = 0;
7617 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007618#endif
7619
Paul Bakker48916f92012-09-16 19:57:18 +00007620 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7621 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007622
7623 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007624}
7625
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007626/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007627 * Reset an initialized and used SSL context for re-use while retaining
7628 * all application-set variables, function pointers and data.
7629 */
7630int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7631{
7632 return( ssl_session_reset_int( ssl, 0 ) );
7633}
7634
7635/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007636 * SSL set accessors
7637 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007638void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007640 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007641}
7642
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007643void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007645 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007646}
7647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007649void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007651 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007652}
7653#endif
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007656void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007657{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007658 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007659}
7660#endif
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007663
Hanno Becker1841b0a2018-08-24 11:13:57 +01007664void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7665 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007666{
7667 ssl->disable_datagram_packing = !allow_packing;
7668}
7669
7670void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7671 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007672{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007673 conf->hs_timeout_min = min;
7674 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007675}
7676#endif
7677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007678void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007680 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007681}
7682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007684void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007685 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007686 void *p_vrfy )
7687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007688 conf->f_vrfy = f_vrfy;
7689 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007692
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007693void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007694 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007695 void *p_rng )
7696{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007697 conf->f_rng = f_rng;
7698 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007699}
7700
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007701void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007702 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 void *p_dbg )
7704{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007705 conf->f_dbg = f_dbg;
7706 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007707}
7708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007710 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007711 mbedtls_ssl_send_t *f_send,
7712 mbedtls_ssl_recv_t *f_recv,
7713 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007714{
7715 ssl->p_bio = p_bio;
7716 ssl->f_send = f_send;
7717 ssl->f_recv = f_recv;
7718 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007719}
7720
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007721#if defined(MBEDTLS_SSL_PROTO_DTLS)
7722void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7723{
7724 ssl->mtu = mtu;
7725}
7726#endif
7727
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007728void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007729{
7730 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007731}
7732
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007733void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7734 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007735 mbedtls_ssl_set_timer_t *f_set_timer,
7736 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007737{
7738 ssl->p_timer = p_timer;
7739 ssl->f_set_timer = f_set_timer;
7740 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007741
7742 /* Make sure we start with no timer running */
7743 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007744}
7745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007747void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007748 void *p_cache,
7749 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7750 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007751{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007752 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007753 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007754 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758#if defined(MBEDTLS_SSL_CLI_C)
7759int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007760{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007761 int ret;
7762
7763 if( ssl == NULL ||
7764 session == NULL ||
7765 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007766 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007769 }
7770
7771 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7772 return( ret );
7773
Paul Bakker0a597072012-09-25 21:55:46 +00007774 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007775
7776 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007779
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007780void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007781 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007782{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007783 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7784 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7785 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7786 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007787}
7788
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007789void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007790 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007791 int major, int minor )
7792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007794 return;
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007797 return;
7798
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007799 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007800}
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007803void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007804 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007805{
7806 conf->cert_profile = profile;
7807}
7808
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007809/* Append a new keycert entry to a (possibly empty) list */
7810static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7811 mbedtls_x509_crt *cert,
7812 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007813{
niisato8ee24222018-06-25 19:05:48 +09007814 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007815
niisato8ee24222018-06-25 19:05:48 +09007816 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7817 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007818 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007819
niisato8ee24222018-06-25 19:05:48 +09007820 new_cert->cert = cert;
7821 new_cert->key = key;
7822 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007823
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007824 /* Update head is the list was null, else add to the end */
7825 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007826 {
niisato8ee24222018-06-25 19:05:48 +09007827 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007828 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007829 else
7830 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007831 mbedtls_ssl_key_cert *cur = *head;
7832 while( cur->next != NULL )
7833 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007834 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007835 }
7836
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007837 return( 0 );
7838}
7839
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007840int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007841 mbedtls_x509_crt *own_cert,
7842 mbedtls_pk_context *pk_key )
7843{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007844 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007845}
7846
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007847void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007848 mbedtls_x509_crt *ca_chain,
7849 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007850{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007851 conf->ca_chain = ca_chain;
7852 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007855
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007856#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7857int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7858 mbedtls_x509_crt *own_cert,
7859 mbedtls_pk_context *pk_key )
7860{
7861 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7862 own_cert, pk_key ) );
7863}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007864
7865void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7866 mbedtls_x509_crt *ca_chain,
7867 mbedtls_x509_crl *ca_crl )
7868{
7869 ssl->handshake->sni_ca_chain = ca_chain;
7870 ssl->handshake->sni_ca_crl = ca_crl;
7871}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007872
7873void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7874 int authmode )
7875{
7876 ssl->handshake->sni_authmode = authmode;
7877}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007878#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7879
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007880#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007881/*
7882 * Set EC J-PAKE password for current handshake
7883 */
7884int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7885 const unsigned char *pw,
7886 size_t pw_len )
7887{
7888 mbedtls_ecjpake_role role;
7889
Janos Follath8eb64132016-06-03 15:40:57 +01007890 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7892
7893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7894 role = MBEDTLS_ECJPAKE_SERVER;
7895 else
7896 role = MBEDTLS_ECJPAKE_CLIENT;
7897
7898 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7899 role,
7900 MBEDTLS_MD_SHA256,
7901 MBEDTLS_ECP_DP_SECP256R1,
7902 pw, pw_len ) );
7903}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007904#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007907int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007908 const unsigned char *psk, size_t psk_len,
7909 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007910{
Paul Bakker6db455e2013-09-18 17:29:31 +02007911 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007916
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007917 /* Identity len will be encoded on two bytes */
7918 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007919 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007920 {
7921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7922 }
7923
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007924 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007925 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007926 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007927
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007928 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007929 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007930 conf->psk_len = 0;
7931 }
7932 if( conf->psk_identity != NULL )
7933 {
7934 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007935 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007936 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007937 }
7938
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007939 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7940 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007941 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007942 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007943 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007944 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007945 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007946 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007947 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007948
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007949 conf->psk_len = psk_len;
7950 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007951
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007952 memcpy( conf->psk, psk, conf->psk_len );
7953 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007954
7955 return( 0 );
7956}
7957
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007958int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7959 const unsigned char *psk, size_t psk_len )
7960{
7961 if( psk == NULL || ssl->handshake == NULL )
7962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7963
7964 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7966
7967 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007968 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007969 mbedtls_platform_zeroize( ssl->handshake->psk,
7970 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007971 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007972 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007973 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007974
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007975 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007976 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007977
7978 ssl->handshake->psk_len = psk_len;
7979 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7980
7981 return( 0 );
7982}
7983
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007984void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007986 size_t),
7987 void *p_psk )
7988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007989 conf->f_psk = f_psk;
7990 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007993
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007994#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007995
7996#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007997int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007998{
7999 int ret;
8000
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008001 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8002 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8003 {
8004 mbedtls_mpi_free( &conf->dhm_P );
8005 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008006 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008008
8009 return( 0 );
8010}
Hanno Becker470a8c42017-10-04 15:28:46 +01008011#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008012
Hanno Beckera90658f2017-10-04 15:29:08 +01008013int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8014 const unsigned char *dhm_P, size_t P_len,
8015 const unsigned char *dhm_G, size_t G_len )
8016{
8017 int ret;
8018
8019 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8020 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8021 {
8022 mbedtls_mpi_free( &conf->dhm_P );
8023 mbedtls_mpi_free( &conf->dhm_G );
8024 return( ret );
8025 }
8026
8027 return( 0 );
8028}
Paul Bakker5121ce52009-01-03 21:22:43 +00008029
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008030int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008031{
8032 int ret;
8033
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008034 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8035 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8036 {
8037 mbedtls_mpi_free( &conf->dhm_P );
8038 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008039 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008040 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008041
8042 return( 0 );
8043}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008044#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008045
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008046#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8047/*
8048 * Set the minimum length for Diffie-Hellman parameters
8049 */
8050void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8051 unsigned int bitlen )
8052{
8053 conf->dhm_min_bitlen = bitlen;
8054}
8055#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8056
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008057#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008058/*
8059 * Set allowed/preferred hashes for handshake signatures
8060 */
8061void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8062 const int *hashes )
8063{
8064 conf->sig_hashes = hashes;
8065}
Hanno Becker947194e2017-04-07 13:25:49 +01008066#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008067
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008068#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008069/*
8070 * Set the allowed elliptic curves
8071 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008072void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008073 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008075 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008076}
Hanno Becker947194e2017-04-07 13:25:49 +01008077#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008078
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008079#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008081{
Hanno Becker947194e2017-04-07 13:25:49 +01008082 /* Initialize to suppress unnecessary compiler warning */
8083 size_t hostname_len = 0;
8084
8085 /* Check if new hostname is valid before
8086 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008087 if( hostname != NULL )
8088 {
8089 hostname_len = strlen( hostname );
8090
8091 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8093 }
8094
8095 /* Now it's clear that we will overwrite the old hostname,
8096 * so we can free it safely */
8097
8098 if( ssl->hostname != NULL )
8099 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008100 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008101 mbedtls_free( ssl->hostname );
8102 }
8103
8104 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008105
Paul Bakker5121ce52009-01-03 21:22:43 +00008106 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008107 {
8108 ssl->hostname = NULL;
8109 }
8110 else
8111 {
8112 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008113 if( ssl->hostname == NULL )
8114 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008115
Hanno Becker947194e2017-04-07 13:25:49 +01008116 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008117
Hanno Becker947194e2017-04-07 13:25:49 +01008118 ssl->hostname[hostname_len] = '\0';
8119 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008120
8121 return( 0 );
8122}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008123#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008124
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008125#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008126void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008128 const unsigned char *, size_t),
8129 void *p_sni )
8130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008131 conf->f_sni = f_sni;
8132 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008137int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008138{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008139 size_t cur_len, tot_len;
8140 const char **p;
8141
8142 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008143 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8144 * MUST NOT be truncated."
8145 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008146 */
8147 tot_len = 0;
8148 for( p = protos; *p != NULL; p++ )
8149 {
8150 cur_len = strlen( *p );
8151 tot_len += cur_len;
8152
8153 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008155 }
8156
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008157 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008158
8159 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008160}
8161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008163{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008164 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008167
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008168void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008170 conf->max_major_ver = major;
8171 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008172}
8173
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008174void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008176 conf->min_major_ver = major;
8177 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008178}
8179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008181void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008182{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008183 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008184}
8185#endif
8186
Janos Follath088ce432017-04-10 12:42:31 +01008187#if defined(MBEDTLS_SSL_SRV_C)
8188void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8189 char cert_req_ca_list )
8190{
8191 conf->cert_req_ca_list = cert_req_ca_list;
8192}
8193#endif
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008196void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008197{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008198 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008199}
8200#endif
8201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008203void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008205 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008206}
8207#endif
8208
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008209#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008210void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008212 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008213}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008214#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008217int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008220 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008223 }
8224
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008225 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008226
8227 return( 0 );
8228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008232void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008233{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008234 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008239void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008240{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008241 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008242}
8243#endif
8244
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008245void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008246{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008247 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008248}
8249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008251void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008252{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008253 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008254}
8255
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008256void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008257{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008258 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008259}
8260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008261void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008262 const unsigned char period[8] )
8263{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008264 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008269#if defined(MBEDTLS_SSL_CLI_C)
8270void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008271{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008272 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008273}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008274#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008275
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008276#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008277void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8278 mbedtls_ssl_ticket_write_t *f_ticket_write,
8279 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8280 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008281{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008282 conf->f_ticket_write = f_ticket_write;
8283 conf->f_ticket_parse = f_ticket_parse;
8284 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008285}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008288
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008289#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8290void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8291 mbedtls_ssl_export_keys_t *f_export_keys,
8292 void *p_export_keys )
8293{
8294 conf->f_export_keys = f_export_keys;
8295 conf->p_export_keys = p_export_keys;
8296}
8297#endif
8298
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008299#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008300void mbedtls_ssl_conf_async_private_cb(
8301 mbedtls_ssl_config *conf,
8302 mbedtls_ssl_async_sign_t *f_async_sign,
8303 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8304 mbedtls_ssl_async_resume_t *f_async_resume,
8305 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008306 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008307{
8308 conf->f_async_sign_start = f_async_sign;
8309 conf->f_async_decrypt_start = f_async_decrypt;
8310 conf->f_async_resume = f_async_resume;
8311 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008312 conf->p_async_config_data = async_config_data;
8313}
8314
Gilles Peskine8f97af72018-04-26 11:46:10 +02008315void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8316{
8317 return( conf->p_async_config_data );
8318}
8319
Gilles Peskine1febfef2018-04-30 11:54:39 +02008320void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008321{
8322 if( ssl->handshake == NULL )
8323 return( NULL );
8324 else
8325 return( ssl->handshake->user_async_ctx );
8326}
8327
Gilles Peskine1febfef2018-04-30 11:54:39 +02008328void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008329 void *ctx )
8330{
8331 if( ssl->handshake != NULL )
8332 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008333}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008334#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008335
Paul Bakker5121ce52009-01-03 21:22:43 +00008336/*
8337 * SSL get accessors
8338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008340{
8341 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8342}
8343
Hanno Becker8b170a02017-10-10 11:51:19 +01008344int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8345{
8346 /*
8347 * Case A: We're currently holding back
8348 * a message for further processing.
8349 */
8350
8351 if( ssl->keep_current_message == 1 )
8352 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008354 return( 1 );
8355 }
8356
8357 /*
8358 * Case B: Further records are pending in the current datagram.
8359 */
8360
8361#if defined(MBEDTLS_SSL_PROTO_DTLS)
8362 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8363 ssl->in_left > ssl->next_record_offset )
8364 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008366 return( 1 );
8367 }
8368#endif /* MBEDTLS_SSL_PROTO_DTLS */
8369
8370 /*
8371 * Case C: A handshake message is being processed.
8372 */
8373
Hanno Becker8b170a02017-10-10 11:51:19 +01008374 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8375 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008377 return( 1 );
8378 }
8379
8380 /*
8381 * Case D: An application data message is being processed
8382 */
8383 if( ssl->in_offt != NULL )
8384 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008386 return( 1 );
8387 }
8388
8389 /*
8390 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008391 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008392 * we implement support for multiple alerts in single records.
8393 */
8394
8395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8396 return( 0 );
8397}
8398
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008399uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008400{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008401 if( ssl->session != NULL )
8402 return( ssl->session->verify_result );
8403
8404 if( ssl->session_negotiate != NULL )
8405 return( ssl->session_negotiate->verify_result );
8406
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008407 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008408}
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008411{
Paul Bakker926c8e42013-03-06 10:23:34 +01008412 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008413 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008416}
8417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008419{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008422 {
8423 switch( ssl->minor_ver )
8424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008426 return( "DTLSv1.0" );
8427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008429 return( "DTLSv1.2" );
8430
8431 default:
8432 return( "unknown (DTLS)" );
8433 }
8434 }
8435#endif
8436
Paul Bakker43ca69c2011-01-15 17:35:19 +00008437 switch( ssl->minor_ver )
8438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008440 return( "SSLv3.0" );
8441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008442 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008443 return( "TLSv1.0" );
8444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008446 return( "TLSv1.1" );
8447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008449 return( "TLSv1.2" );
8450
Paul Bakker43ca69c2011-01-15 17:35:19 +00008451 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008452 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008453 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008454}
8455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008457{
Hanno Becker3136ede2018-08-17 15:28:19 +01008458 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008460 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008461
Hanno Becker43395762019-05-03 14:46:38 +01008462 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8463
Hanno Becker78640902018-08-13 16:35:15 +01008464 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008465 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_ZLIB_SUPPORT)
8468 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8469 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008470#endif
8471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474 case MBEDTLS_MODE_GCM:
8475 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008476 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008478 transform_expansion = transform->minlen;
8479 break;
8480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008482
8483 block_size = mbedtls_cipher_get_block_size(
8484 &transform->cipher_ctx_enc );
8485
Hanno Becker3136ede2018-08-17 15:28:19 +01008486 /* Expansion due to the addition of the MAC. */
8487 transform_expansion += transform->maclen;
8488
8489 /* Expansion due to the addition of CBC padding;
8490 * Theoretically up to 256 bytes, but we never use
8491 * more than the block size of the underlying cipher. */
8492 transform_expansion += block_size;
8493
8494 /* For TLS 1.1 or higher, an explicit IV is added
8495 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008496#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8497 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008498 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008499#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008500
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008501 break;
8502
8503 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008506 }
8507
Hanno Beckera5a2b082019-05-15 14:03:01 +01008508#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008509 if( transform->out_cid_len != 0 )
8510 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008511#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008512
Hanno Becker43395762019-05-03 14:46:38 +01008513 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008514}
8515
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008516#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8517size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8518{
8519 size_t max_len;
8520
8521 /*
8522 * Assume mfl_code is correct since it was checked when set
8523 */
Angus Grattond8213d02016-05-25 20:56:48 +10008524 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008525
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008526 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008527 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008528 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008529 {
Angus Grattond8213d02016-05-25 20:56:48 +10008530 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008531 }
8532
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008533 /* During a handshake, use the value being negotiated */
8534 if( ssl->session_negotiate != NULL &&
8535 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8536 {
8537 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8538 }
8539
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008540 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008541}
8542#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8543
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008544#if defined(MBEDTLS_SSL_PROTO_DTLS)
8545static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8546{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008547 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8548 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8549 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8550 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8551 return ( 0 );
8552
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008553 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8554 return( ssl->mtu );
8555
8556 if( ssl->mtu == 0 )
8557 return( ssl->handshake->mtu );
8558
8559 return( ssl->mtu < ssl->handshake->mtu ?
8560 ssl->mtu : ssl->handshake->mtu );
8561}
8562#endif /* MBEDTLS_SSL_PROTO_DTLS */
8563
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008564int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8565{
8566 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8567
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008568#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8569 !defined(MBEDTLS_SSL_PROTO_DTLS)
8570 (void) ssl;
8571#endif
8572
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008573#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8574 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8575
8576 if( max_len > mfl )
8577 max_len = mfl;
8578#endif
8579
8580#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008581 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008582 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008583 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008584 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8585 const size_t overhead = (size_t) ret;
8586
8587 if( ret < 0 )
8588 return( ret );
8589
8590 if( mtu <= overhead )
8591 {
8592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8593 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8594 }
8595
8596 if( max_len > mtu - overhead )
8597 max_len = mtu - overhead;
8598 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008599#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008600
Hanno Becker0defedb2018-08-10 12:35:02 +01008601#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8602 !defined(MBEDTLS_SSL_PROTO_DTLS)
8603 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008604#endif
8605
8606 return( (int) max_len );
8607}
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#if defined(MBEDTLS_X509_CRT_PARSE_C)
8610const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008611{
8612 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008613 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008614
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008615 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_CLI_C)
8620int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008621{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008622 if( ssl == NULL ||
8623 dst == NULL ||
8624 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008625 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008628 }
8629
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008630 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008633
Paul Bakker5121ce52009-01-03 21:22:43 +00008634/*
Paul Bakker1961b702013-01-25 14:49:24 +01008635 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008641 if( ssl == NULL || ssl->conf == NULL )
8642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008647#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008649 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008651#endif
8652
Paul Bakker1961b702013-01-25 14:49:24 +01008653 return( ret );
8654}
8655
8656/*
8657 * Perform the SSL handshake
8658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008660{
8661 int ret = 0;
8662
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008663 if( ssl == NULL || ssl->conf == NULL )
8664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008671
8672 if( ret != 0 )
8673 break;
8674 }
8675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008677
8678 return( ret );
8679}
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_SSL_RENEGOTIATION)
8682#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008683/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008684 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008687{
8688 int ret;
8689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008691
8692 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8694 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008695
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008696 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008697 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008699 return( ret );
8700 }
8701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008703
8704 return( 0 );
8705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008707
8708/*
8709 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710 * - any side: calling mbedtls_ssl_renegotiate(),
8711 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8712 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008713 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008714 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008718{
8719 int ret;
8720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008722
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008723 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8724 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008725
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008726 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8727 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008731 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008733 ssl->handshake->out_msg_seq = 1;
8734 else
8735 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008736 }
8737#endif
8738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8740 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008745 return( ret );
8746 }
8747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008749
8750 return( 0 );
8751}
8752
8753/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008754 * Renegotiate current connection on client,
8755 * or request renegotiation on server
8756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008760
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008761 if( ssl == NULL || ssl->conf == NULL )
8762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008765 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008766 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008772
8773 /* Did we already try/start sending HelloRequest? */
8774 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008776
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008777 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008782 /*
8783 * On client, either start the renegotiation process or,
8784 * if already in progress, continue the handshake
8785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008790
8791 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008794 return( ret );
8795 }
8796 }
8797 else
8798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008802 return( ret );
8803 }
8804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008806
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008807 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008808}
8809
8810/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008811 * Check record counters and renegotiate if they're above the limit.
8812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008814{
Andres AG2196c7f2016-12-15 17:01:16 +00008815 size_t ep_len = ssl_ep_len( ssl );
8816 int in_ctr_cmp;
8817 int out_ctr_cmp;
8818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8820 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008821 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008822 {
8823 return( 0 );
8824 }
8825
Andres AG2196c7f2016-12-15 17:01:16 +00008826 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8827 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008828 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008829 ssl->conf->renego_period + ep_len, 8 - ep_len );
8830
8831 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008832 {
8833 return( 0 );
8834 }
8835
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008840
8841/*
8842 * Receive application data decrypted from the SSL layer
8843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008845{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008846 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008847 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008848
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008849 if( ssl == NULL || ssl->conf == NULL )
8850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008858 return( ret );
8859
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008860 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008862 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008863 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008864 return( ret );
8865 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008866 }
8867#endif
8868
Hanno Becker4a810fb2017-05-24 16:27:30 +01008869 /*
8870 * Check if renegotiation is necessary and/or handshake is
8871 * in process. If yes, perform/continue, and fall through
8872 * if an unexpected packet is received while the client
8873 * is waiting for the ServerHello.
8874 *
8875 * (There is no equivalent to the last condition on
8876 * the server-side as it is not treated as within
8877 * a handshake while waiting for the ClientHello
8878 * after a renegotiation request.)
8879 */
8880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008882 ret = ssl_check_ctr_renegotiate( ssl );
8883 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8884 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008887 return( ret );
8888 }
8889#endif
8890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008894 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8895 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008898 return( ret );
8899 }
8900 }
8901
Hanno Beckere41158b2017-10-23 13:30:32 +01008902 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008903 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008904 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008905 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008906 if( ssl->f_get_timer != NULL &&
8907 ssl->f_get_timer( ssl->p_timer ) == -1 )
8908 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008909 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008910 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008911
Hanno Becker327c93b2018-08-15 13:56:18 +01008912 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008913 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008914 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8915 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008916
Hanno Becker4a810fb2017-05-24 16:27:30 +01008917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008919 }
8920
8921 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008923 {
8924 /*
8925 * OpenSSL sends empty messages to randomize the IV
8926 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008927 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008930 return( 0 );
8931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008933 return( ret );
8934 }
8935 }
8936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008940
Hanno Becker4a810fb2017-05-24 16:27:30 +01008941 /*
8942 * - For client-side, expect SERVER_HELLO_REQUEST.
8943 * - For server-side, expect CLIENT_HELLO.
8944 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8945 */
8946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008948 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008950 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008953
8954 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008957 {
8958 continue;
8959 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008962 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008963#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008964
Hanno Becker4a810fb2017-05-24 16:27:30 +01008965#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008966 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008970
8971 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008973 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008974 {
8975 continue;
8976 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008979 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008980#endif /* MBEDTLS_SSL_SRV_C */
8981
Hanno Becker21df7f92017-10-17 11:03:26 +01008982#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008983 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008984 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8985 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8986 ssl->conf->allow_legacy_renegotiation ==
8987 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8988 {
8989 /*
8990 * Accept renegotiation request
8991 */
Paul Bakker48916f92012-09-16 19:57:18 +00008992
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008993 /* DTLS clients need to know renego is server-initiated */
8994#if defined(MBEDTLS_SSL_PROTO_DTLS)
8995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8996 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8997 {
8998 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8999 }
9000#endif
9001 ret = ssl_start_renegotiation( ssl );
9002 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9003 ret != 0 )
9004 {
9005 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9006 return( ret );
9007 }
9008 }
9009 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009010#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009011 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009012 /*
9013 * Refuse renegotiation
9014 */
9015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018#if defined(MBEDTLS_SSL_PROTO_SSL3)
9019 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009020 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009021 /* SSLv3 does not have a "no_renegotiation" warning, so
9022 we send a fatal alert and abort the connection. */
9023 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9024 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9025 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009026 }
9027 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9029#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9030 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9031 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9034 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9035 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009036 {
9037 return( ret );
9038 }
Paul Bakker48916f92012-09-16 19:57:18 +00009039 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9042 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009046 }
Paul Bakker48916f92012-09-16 19:57:18 +00009047 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009048
Hanno Becker90333da2017-10-10 11:27:13 +01009049 /* At this point, we don't know whether the renegotiation has been
9050 * completed or not. The cases to consider are the following:
9051 * 1) The renegotiation is complete. In this case, no new record
9052 * has been read yet.
9053 * 2) The renegotiation is incomplete because the client received
9054 * an application data record while awaiting the ServerHello.
9055 * 3) The renegotiation is incomplete because the client received
9056 * a non-handshake, non-application data message while awaiting
9057 * the ServerHello.
9058 * In each of these case, looping will be the proper action:
9059 * - For 1), the next iteration will read a new record and check
9060 * if it's application data.
9061 * - For 2), the loop condition isn't satisfied as application data
9062 * is present, hence continue is the same as break
9063 * - For 3), the loop condition is satisfied and read_record
9064 * will re-deliver the message that was held back by the client
9065 * when expecting the ServerHello.
9066 */
9067 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009068 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009069#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009071 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009072 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009073 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009074 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009077 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009079 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009080 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9085 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009088 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009089 }
9090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9094 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009095 }
9096
9097 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009098
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009099 /* We're going to return something now, cancel timer,
9100 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009102 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009103
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009105 /* If we requested renego but received AppData, resend HelloRequest.
9106 * Do it now, after setting in_offt, to avoid taking this branch
9107 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009109 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009111 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009112 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009115 return( ret );
9116 }
9117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009119#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009120 }
9121
9122 n = ( len < ssl->in_msglen )
9123 ? len : ssl->in_msglen;
9124
9125 memcpy( buf, ssl->in_offt, n );
9126 ssl->in_msglen -= n;
9127
9128 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009129 {
9130 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009131 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009132 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009134 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009135 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009136 /* more data available */
9137 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009141
Paul Bakker23986e52011-04-24 08:57:21 +00009142 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009143}
9144
9145/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009146 * Send application data to be encrypted by the SSL layer, taking care of max
9147 * fragment length and buffer size.
9148 *
9149 * According to RFC 5246 Section 6.2.1:
9150 *
9151 * Zero-length fragments of Application data MAY be sent as they are
9152 * potentially useful as a traffic analysis countermeasure.
9153 *
9154 * Therefore, it is possible that the input message length is 0 and the
9155 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009156 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009157static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009158 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009159{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009160 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9161 const size_t max_len = (size_t) ret;
9162
9163 if( ret < 0 )
9164 {
9165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9166 return( ret );
9167 }
9168
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009169 if( len > max_len )
9170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009175 "maximum fragment length: %d > %d",
9176 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009178 }
9179 else
9180#endif
9181 len = max_len;
9182 }
Paul Bakker887bd502011-06-08 13:10:54 +00009183
Paul Bakker5121ce52009-01-03 21:22:43 +00009184 if( ssl->out_left != 0 )
9185 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009186 /*
9187 * The user has previously tried to send the data and
9188 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9189 * written. In this case, we expect the high-level write function
9190 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009195 return( ret );
9196 }
9197 }
Paul Bakker887bd502011-06-08 13:10:54 +00009198 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009199 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009200 /*
9201 * The user is trying to send a message the first time, so we need to
9202 * copy the data into the internal buffers and setup the data structure
9203 * to keep track of partial writes
9204 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009205 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009207 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009208
Hanno Becker67bc7c32018-08-06 11:33:50 +01009209 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009212 return( ret );
9213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009214 }
9215
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009216 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009217}
9218
9219/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009220 * Write application data, doing 1/n-1 splitting if necessary.
9221 *
9222 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009223 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009224 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009226#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009227static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009228 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009229{
9230 int ret;
9231
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009232 if( ssl->conf->cbc_record_splitting ==
9233 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009234 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9236 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9237 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009238 {
9239 return( ssl_write_real( ssl, buf, len ) );
9240 }
9241
9242 if( ssl->split_done == 0 )
9243 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009244 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009245 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009246 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009247 }
9248
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009249 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9250 return( ret );
9251 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009252
9253 return( ret + 1 );
9254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009256
9257/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009258 * Write application data (public-facing wrapper)
9259 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009260int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009261{
9262 int ret;
9263
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009265
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009266 if( ssl == NULL || ssl->conf == NULL )
9267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9268
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009269#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009270 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9271 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009272 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009273 return( ret );
9274 }
9275#endif
9276
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009277 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009278 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009279 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009280 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009282 return( ret );
9283 }
9284 }
9285
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009286#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009287 ret = ssl_write_split( ssl, buf, len );
9288#else
9289 ret = ssl_write_real( ssl, buf, len );
9290#endif
9291
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009293
9294 return( ret );
9295}
9296
9297/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009298 * Notify the peer that the connection is being closed
9299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009301{
9302 int ret;
9303
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009304 if( ssl == NULL || ssl->conf == NULL )
9305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009308
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009309 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9315 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9316 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009319 return( ret );
9320 }
9321 }
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009324
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009325 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009326}
9327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009329{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009330 if( transform == NULL )
9331 return;
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009334 deflateEnd( &transform->ctx_deflate );
9335 inflateEnd( &transform->ctx_inflate );
9336#endif
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9339 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009340
Hanno Becker92231322018-01-03 15:32:51 +00009341#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 mbedtls_md_free( &transform->md_ctx_enc );
9343 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009344#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009346 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009347}
9348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349#if defined(MBEDTLS_X509_CRT_PARSE_C)
9350static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009353
9354 while( cur != NULL )
9355 {
9356 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009358 cur = next;
9359 }
9360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009362
Hanno Becker0271f962018-08-16 13:23:47 +01009363#if defined(MBEDTLS_SSL_PROTO_DTLS)
9364
9365static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9366{
9367 unsigned offset;
9368 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9369
9370 if( hs == NULL )
9371 return;
9372
Hanno Becker283f5ef2018-08-24 09:34:47 +01009373 ssl_free_buffered_record( ssl );
9374
Hanno Becker0271f962018-08-16 13:23:47 +01009375 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009376 ssl_buffering_free_slot( ssl, offset );
9377}
9378
9379static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9380 uint8_t slot )
9381{
9382 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9383 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009384
9385 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9386 return;
9387
Hanno Beckere605b192018-08-21 15:59:07 +01009388 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009389 {
Hanno Beckere605b192018-08-21 15:59:07 +01009390 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009391 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009392 mbedtls_free( hs_buf->data );
9393 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009394 }
9395}
9396
9397#endif /* MBEDTLS_SSL_PROTO_DTLS */
9398
Gilles Peskine9b562d52018-04-25 20:32:43 +02009399void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009400{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009401 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9402
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009403 if( handshake == NULL )
9404 return;
9405
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009406#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9407 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9408 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009409 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009410 handshake->async_in_progress = 0;
9411 }
9412#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9413
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009414#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9415 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9416 mbedtls_md5_free( &handshake->fin_md5 );
9417 mbedtls_sha1_free( &handshake->fin_sha1 );
9418#endif
9419#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9420#if defined(MBEDTLS_SHA256_C)
9421 mbedtls_sha256_free( &handshake->fin_sha256 );
9422#endif
9423#if defined(MBEDTLS_SHA512_C)
9424 mbedtls_sha512_free( &handshake->fin_sha512 );
9425#endif
9426#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#if defined(MBEDTLS_DHM_C)
9429 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431#if defined(MBEDTLS_ECDH_C)
9432 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009433#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009434#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009435 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009436#if defined(MBEDTLS_SSL_CLI_C)
9437 mbedtls_free( handshake->ecjpake_cache );
9438 handshake->ecjpake_cache = NULL;
9439 handshake->ecjpake_cache_len = 0;
9440#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009441#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009442
Janos Follath4ae5c292016-02-10 11:27:43 +00009443#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9444 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009445 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009447#endif
9448
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009449#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9450 if( handshake->psk != NULL )
9451 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009452 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009453 mbedtls_free( handshake->psk );
9454 }
9455#endif
9456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9458 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009459 /*
9460 * Free only the linked list wrapper, not the keys themselves
9461 * since the belong to the SNI callback
9462 */
9463 if( handshake->sni_key_cert != NULL )
9464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009466
9467 while( cur != NULL )
9468 {
9469 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009471 cur = next;
9472 }
9473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009475
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009476#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009477 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009478#endif
9479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480#if defined(MBEDTLS_SSL_PROTO_DTLS)
9481 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009482 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009483 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009484#endif
9485
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009486 mbedtls_platform_zeroize( handshake,
9487 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009488}
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009491{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009492 if( session == NULL )
9493 return;
9494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009496 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 mbedtls_x509_crt_free( session->peer_cert );
9499 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009500 }
Paul Bakkered27a042013-04-18 22:46:23 +02009501#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009502
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009503#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009505#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009506
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009507 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009508}
9509
Paul Bakker5121ce52009-01-03 21:22:43 +00009510/*
9511 * Free an SSL context
9512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009514{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009515 if( ssl == NULL )
9516 return;
9517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009519
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009520 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009521 {
Angus Grattond8213d02016-05-25 20:56:48 +10009522 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009524 }
9525
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009526 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009527 {
Angus Grattond8213d02016-05-25 20:56:48 +10009528 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009530 }
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009533 if( ssl->compress_buf != NULL )
9534 {
Angus Grattond8213d02016-05-25 20:56:48 +10009535 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009537 }
9538#endif
9539
Paul Bakker48916f92012-09-16 19:57:18 +00009540 if( ssl->transform )
9541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 mbedtls_ssl_transform_free( ssl->transform );
9543 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009544 }
9545
9546 if( ssl->handshake )
9547 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009548 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9550 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 mbedtls_free( ssl->handshake );
9553 mbedtls_free( ssl->transform_negotiate );
9554 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009555 }
9556
Paul Bakkerc0463502013-02-14 11:19:38 +01009557 if( ssl->session )
9558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 mbedtls_ssl_session_free( ssl->session );
9560 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009561 }
9562
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009563#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009564 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009565 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009566 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009568 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009569#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9572 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9575 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009576 }
9577#endif
9578
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009579#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009581#endif
9582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009584
Paul Bakker86f04f42013-02-14 11:20:09 +01009585 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009586 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009587}
9588
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009589/*
9590 * Initialze mbedtls_ssl_config
9591 */
9592void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9593{
9594 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9595}
9596
Simon Butcherc97b6972015-12-27 23:48:17 +00009597#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009598static int ssl_preset_default_hashes[] = {
9599#if defined(MBEDTLS_SHA512_C)
9600 MBEDTLS_MD_SHA512,
9601 MBEDTLS_MD_SHA384,
9602#endif
9603#if defined(MBEDTLS_SHA256_C)
9604 MBEDTLS_MD_SHA256,
9605 MBEDTLS_MD_SHA224,
9606#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009607#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009608 MBEDTLS_MD_SHA1,
9609#endif
9610 MBEDTLS_MD_NONE
9611};
Simon Butcherc97b6972015-12-27 23:48:17 +00009612#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009613
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009614static int ssl_preset_suiteb_ciphersuites[] = {
9615 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9616 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9617 0
9618};
9619
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009620#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009621static int ssl_preset_suiteb_hashes[] = {
9622 MBEDTLS_MD_SHA256,
9623 MBEDTLS_MD_SHA384,
9624 MBEDTLS_MD_NONE
9625};
9626#endif
9627
9628#if defined(MBEDTLS_ECP_C)
9629static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9630 MBEDTLS_ECP_DP_SECP256R1,
9631 MBEDTLS_ECP_DP_SECP384R1,
9632 MBEDTLS_ECP_DP_NONE
9633};
9634#endif
9635
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009636/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009637 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009638 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009639int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009640 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009641{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009642#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009643 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009644#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009645
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009646 /* Use the functions here so that they are covered in tests,
9647 * but otherwise access member directly for efficiency */
9648 mbedtls_ssl_conf_endpoint( conf, endpoint );
9649 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009650
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009651 /*
9652 * Things that are common to all presets
9653 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009654#if defined(MBEDTLS_SSL_CLI_C)
9655 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9656 {
9657 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9658#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9659 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9660#endif
9661 }
9662#endif
9663
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009664#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009665 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009666#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009667
9668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9669 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9670#endif
9671
9672#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9673 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9674#endif
9675
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009676#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9677 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9678#endif
9679
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009680#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009681 conf->f_cookie_write = ssl_cookie_write_dummy;
9682 conf->f_cookie_check = ssl_cookie_check_dummy;
9683#endif
9684
9685#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9686 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9687#endif
9688
Janos Follath088ce432017-04-10 12:42:31 +01009689#if defined(MBEDTLS_SSL_SRV_C)
9690 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9691#endif
9692
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009693#if defined(MBEDTLS_SSL_PROTO_DTLS)
9694 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9695 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9696#endif
9697
9698#if defined(MBEDTLS_SSL_RENEGOTIATION)
9699 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009700 memset( conf->renego_period, 0x00, 2 );
9701 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009702#endif
9703
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009704#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9705 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9706 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009707 const unsigned char dhm_p[] =
9708 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9709 const unsigned char dhm_g[] =
9710 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9711
Hanno Beckera90658f2017-10-04 15:29:08 +01009712 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9713 dhm_p, sizeof( dhm_p ),
9714 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009715 {
9716 return( ret );
9717 }
9718 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009719#endif
9720
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009721 /*
9722 * Preset-specific defaults
9723 */
9724 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009725 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009726 /*
9727 * NSA Suite B
9728 */
9729 case MBEDTLS_SSL_PRESET_SUITEB:
9730 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9731 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9732 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9733 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9734
9735 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9736 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9737 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9738 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9739 ssl_preset_suiteb_ciphersuites;
9740
9741#if defined(MBEDTLS_X509_CRT_PARSE_C)
9742 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009743#endif
9744
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009745#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009746 conf->sig_hashes = ssl_preset_suiteb_hashes;
9747#endif
9748
9749#if defined(MBEDTLS_ECP_C)
9750 conf->curve_list = ssl_preset_suiteb_curves;
9751#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009752 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009753
9754 /*
9755 * Default
9756 */
9757 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009758 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9759 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9760 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9761 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9762 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9763 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9764 MBEDTLS_SSL_MIN_MINOR_VERSION :
9765 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009766 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9767 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9768
9769#if defined(MBEDTLS_SSL_PROTO_DTLS)
9770 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9771 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9772#endif
9773
9774 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9775 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9776 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9777 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9778 mbedtls_ssl_list_ciphersuites();
9779
9780#if defined(MBEDTLS_X509_CRT_PARSE_C)
9781 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9782#endif
9783
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009784#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009785 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009786#endif
9787
9788#if defined(MBEDTLS_ECP_C)
9789 conf->curve_list = mbedtls_ecp_grp_id_list();
9790#endif
9791
9792#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9793 conf->dhm_min_bitlen = 1024;
9794#endif
9795 }
9796
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009797 return( 0 );
9798}
9799
9800/*
9801 * Free mbedtls_ssl_config
9802 */
9803void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9804{
9805#if defined(MBEDTLS_DHM_C)
9806 mbedtls_mpi_free( &conf->dhm_P );
9807 mbedtls_mpi_free( &conf->dhm_G );
9808#endif
9809
9810#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9811 if( conf->psk != NULL )
9812 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009813 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009814 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009815 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009816 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009817 }
9818
9819 if( conf->psk_identity != NULL )
9820 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009821 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009822 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009823 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009824 conf->psk_identity_len = 0;
9825 }
9826#endif
9827
9828#if defined(MBEDTLS_X509_CRT_PARSE_C)
9829 ssl_key_cert_free( conf->key_cert );
9830#endif
9831
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009832 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009833}
9834
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009835#if defined(MBEDTLS_PK_C) && \
9836 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009837/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#if defined(MBEDTLS_RSA_C)
9843 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9844 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846#if defined(MBEDTLS_ECDSA_C)
9847 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9848 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009851}
9852
Hanno Becker7e5437a2017-04-28 17:15:26 +01009853unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9854{
9855 switch( type ) {
9856 case MBEDTLS_PK_RSA:
9857 return( MBEDTLS_SSL_SIG_RSA );
9858 case MBEDTLS_PK_ECDSA:
9859 case MBEDTLS_PK_ECKEY:
9860 return( MBEDTLS_SSL_SIG_ECDSA );
9861 default:
9862 return( MBEDTLS_SSL_SIG_ANON );
9863 }
9864}
9865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009867{
9868 switch( sig )
9869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870#if defined(MBEDTLS_RSA_C)
9871 case MBEDTLS_SSL_SIG_RSA:
9872 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874#if defined(MBEDTLS_ECDSA_C)
9875 case MBEDTLS_SSL_SIG_ECDSA:
9876 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009877#endif
9878 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009880 }
9881}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009882#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009883
Hanno Becker7e5437a2017-04-28 17:15:26 +01009884#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9885 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9886
9887/* Find an entry in a signature-hash set matching a given hash algorithm. */
9888mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9889 mbedtls_pk_type_t sig_alg )
9890{
9891 switch( sig_alg )
9892 {
9893 case MBEDTLS_PK_RSA:
9894 return( set->rsa );
9895 case MBEDTLS_PK_ECDSA:
9896 return( set->ecdsa );
9897 default:
9898 return( MBEDTLS_MD_NONE );
9899 }
9900}
9901
9902/* Add a signature-hash-pair to a signature-hash set */
9903void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9904 mbedtls_pk_type_t sig_alg,
9905 mbedtls_md_type_t md_alg )
9906{
9907 switch( sig_alg )
9908 {
9909 case MBEDTLS_PK_RSA:
9910 if( set->rsa == MBEDTLS_MD_NONE )
9911 set->rsa = md_alg;
9912 break;
9913
9914 case MBEDTLS_PK_ECDSA:
9915 if( set->ecdsa == MBEDTLS_MD_NONE )
9916 set->ecdsa = md_alg;
9917 break;
9918
9919 default:
9920 break;
9921 }
9922}
9923
9924/* Allow exactly one hash algorithm for each signature. */
9925void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9926 mbedtls_md_type_t md_alg )
9927{
9928 set->rsa = md_alg;
9929 set->ecdsa = md_alg;
9930}
9931
9932#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9933 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9934
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009935/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009936 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009939{
9940 switch( hash )
9941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942#if defined(MBEDTLS_MD5_C)
9943 case MBEDTLS_SSL_HASH_MD5:
9944 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946#if defined(MBEDTLS_SHA1_C)
9947 case MBEDTLS_SSL_HASH_SHA1:
9948 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950#if defined(MBEDTLS_SHA256_C)
9951 case MBEDTLS_SSL_HASH_SHA224:
9952 return( MBEDTLS_MD_SHA224 );
9953 case MBEDTLS_SSL_HASH_SHA256:
9954 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956#if defined(MBEDTLS_SHA512_C)
9957 case MBEDTLS_SSL_HASH_SHA384:
9958 return( MBEDTLS_MD_SHA384 );
9959 case MBEDTLS_SSL_HASH_SHA512:
9960 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009961#endif
9962 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009964 }
9965}
9966
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009967/*
9968 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9969 */
9970unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9971{
9972 switch( md )
9973 {
9974#if defined(MBEDTLS_MD5_C)
9975 case MBEDTLS_MD_MD5:
9976 return( MBEDTLS_SSL_HASH_MD5 );
9977#endif
9978#if defined(MBEDTLS_SHA1_C)
9979 case MBEDTLS_MD_SHA1:
9980 return( MBEDTLS_SSL_HASH_SHA1 );
9981#endif
9982#if defined(MBEDTLS_SHA256_C)
9983 case MBEDTLS_MD_SHA224:
9984 return( MBEDTLS_SSL_HASH_SHA224 );
9985 case MBEDTLS_MD_SHA256:
9986 return( MBEDTLS_SSL_HASH_SHA256 );
9987#endif
9988#if defined(MBEDTLS_SHA512_C)
9989 case MBEDTLS_MD_SHA384:
9990 return( MBEDTLS_SSL_HASH_SHA384 );
9991 case MBEDTLS_MD_SHA512:
9992 return( MBEDTLS_SSL_HASH_SHA512 );
9993#endif
9994 default:
9995 return( MBEDTLS_SSL_HASH_NONE );
9996 }
9997}
9998
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009999#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010000/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010001 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010002 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010003 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010004int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010007
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010008 if( ssl->conf->curve_list == NULL )
10009 return( -1 );
10010
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010011 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010012 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010013 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010014
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010015 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010016}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010017#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010018
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010019#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010020/*
10021 * Check if a hash proposed by the peer is in our list.
10022 * Return 0 if we're willing to use it, -1 otherwise.
10023 */
10024int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10025 mbedtls_md_type_t md )
10026{
10027 const int *cur;
10028
10029 if( ssl->conf->sig_hashes == NULL )
10030 return( -1 );
10031
10032 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10033 if( *cur == (int) md )
10034 return( 0 );
10035
10036 return( -1 );
10037}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010038#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#if defined(MBEDTLS_X509_CRT_PARSE_C)
10041int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10042 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010043 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010044 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010045{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010046 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010048 int usage = 0;
10049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010051 const char *ext_oid;
10052 size_t ext_len;
10053#endif
10054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10056 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010057 ((void) cert);
10058 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010059 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010060#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10063 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010064 {
10065 /* Server part of the key exchange */
10066 switch( ciphersuite->key_exchange )
10067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 case MBEDTLS_KEY_EXCHANGE_RSA:
10069 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010070 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010071 break;
10072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10074 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10075 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10076 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010077 break;
10078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10080 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010081 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010082 break;
10083
10084 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085 case MBEDTLS_KEY_EXCHANGE_NONE:
10086 case MBEDTLS_KEY_EXCHANGE_PSK:
10087 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10088 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010089 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010090 usage = 0;
10091 }
10092 }
10093 else
10094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10096 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010097 }
10098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010100 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010101 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010102 ret = -1;
10103 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010104#else
10105 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10109 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10112 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010113 }
10114 else
10115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10117 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010118 }
10119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010121 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010122 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010123 ret = -1;
10124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010126
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010127 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010130
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010131/*
10132 * Convert version numbers to/from wire format
10133 * and, for DTLS, to/from TLS equivalent.
10134 *
10135 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010136 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010137 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10138 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010141 unsigned char ver[2] )
10142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143#if defined(MBEDTLS_SSL_PROTO_DTLS)
10144 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010147 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10148
10149 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10150 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10151 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010152 else
10153#else
10154 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010155#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010156 {
10157 ver[0] = (unsigned char) major;
10158 ver[1] = (unsigned char) minor;
10159 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010160}
10161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010163 const unsigned char ver[2] )
10164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165#if defined(MBEDTLS_SSL_PROTO_DTLS)
10166 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010167 {
10168 *major = 255 - ver[0] + 2;
10169 *minor = 255 - ver[1] + 1;
10170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010172 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10173 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010174 else
10175#else
10176 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010177#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010178 {
10179 *major = ver[0];
10180 *minor = ver[1];
10181 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010182}
10183
Simon Butcher99000142016-10-13 17:21:01 +010010184int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10185{
10186#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10187 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10188 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10189
10190 switch( md )
10191 {
10192#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10193#if defined(MBEDTLS_MD5_C)
10194 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010195 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010196#endif
10197#if defined(MBEDTLS_SHA1_C)
10198 case MBEDTLS_SSL_HASH_SHA1:
10199 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10200 break;
10201#endif
10202#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10203#if defined(MBEDTLS_SHA512_C)
10204 case MBEDTLS_SSL_HASH_SHA384:
10205 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10206 break;
10207#endif
10208#if defined(MBEDTLS_SHA256_C)
10209 case MBEDTLS_SSL_HASH_SHA256:
10210 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10211 break;
10212#endif
10213 default:
10214 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10215 }
10216
10217 return 0;
10218#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10219 (void) ssl;
10220 (void) md;
10221
10222 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10223#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10224}
10225
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010226#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10227 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10228int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10229 unsigned char *output,
10230 unsigned char *data, size_t data_len )
10231{
10232 int ret = 0;
10233 mbedtls_md5_context mbedtls_md5;
10234 mbedtls_sha1_context mbedtls_sha1;
10235
10236 mbedtls_md5_init( &mbedtls_md5 );
10237 mbedtls_sha1_init( &mbedtls_sha1 );
10238
10239 /*
10240 * digitally-signed struct {
10241 * opaque md5_hash[16];
10242 * opaque sha_hash[20];
10243 * };
10244 *
10245 * md5_hash
10246 * MD5(ClientHello.random + ServerHello.random
10247 * + ServerParams);
10248 * sha_hash
10249 * SHA(ClientHello.random + ServerHello.random
10250 * + ServerParams);
10251 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010252 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010253 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010255 goto exit;
10256 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010257 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010258 ssl->handshake->randbytes, 64 ) ) != 0 )
10259 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010261 goto exit;
10262 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010263 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010264 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010266 goto exit;
10267 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010268 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010269 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010271 goto exit;
10272 }
10273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010274 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010275 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010277 goto exit;
10278 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010279 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010280 ssl->handshake->randbytes, 64 ) ) != 0 )
10281 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010283 goto exit;
10284 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010285 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010286 data_len ) ) != 0 )
10287 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010289 goto exit;
10290 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010291 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010292 output + 16 ) ) != 0 )
10293 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010295 goto exit;
10296 }
10297
10298exit:
10299 mbedtls_md5_free( &mbedtls_md5 );
10300 mbedtls_sha1_free( &mbedtls_sha1 );
10301
10302 if( ret != 0 )
10303 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10304 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10305
10306 return( ret );
10307
10308}
10309#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10310 MBEDTLS_SSL_PROTO_TLS1_1 */
10311
10312#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10313 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10314int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010315 unsigned char *hash, size_t *hashlen,
10316 unsigned char *data, size_t data_len,
10317 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010318{
10319 int ret = 0;
10320 mbedtls_md_context_t ctx;
10321 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010322 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010323
10324 mbedtls_md_init( &ctx );
10325
10326 /*
10327 * digitally-signed struct {
10328 * opaque client_random[32];
10329 * opaque server_random[32];
10330 * ServerDHParams params;
10331 * };
10332 */
10333 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10334 {
10335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10336 goto exit;
10337 }
10338 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10339 {
10340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10341 goto exit;
10342 }
10343 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10344 {
10345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10346 goto exit;
10347 }
10348 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10349 {
10350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10351 goto exit;
10352 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010353 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010354 {
10355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10356 goto exit;
10357 }
10358
10359exit:
10360 mbedtls_md_free( &ctx );
10361
10362 if( ret != 0 )
10363 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10364 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10365
10366 return( ret );
10367}
10368#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10369 MBEDTLS_SSL_PROTO_TLS1_2 */
10370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371#endif /* MBEDTLS_SSL_TLS_C */