blob: 22891fc6a0a8c34449ef5876aa2173a6f43b7146 [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;
4451 * opaque cid[cid_length]; // New field
4452 * uint16 length;
4453 * opaque enc_content[DTLSCiphertext.length];
4454 * } DTLSCiphertext;
4455 */
4456
4457 /* So far, we only support static CID lengths
4458 * fixed in the configuration. */
4459 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4460 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4461 }
4462 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004463#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004464 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004467
4468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004469 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4470 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004471 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4472#endif /* MBEDTLS_SSL_PROTO_DTLS */
4473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4474 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004477 }
4478
4479 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004480 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4483 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 }
4485
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004486 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4489 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 }
4491
Hanno Becker8b09b732019-05-08 12:03:28 +01004492 /* Now that the total length of the record header is known, ensure
4493 * that the current datagram is large enough to hold it.
4494 * This would fail, for example, if we received a datagram of
4495 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4496 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4497 if( ret != 0 )
4498 {
4499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4500 return( ret );
4501 }
4502 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4503
4504 /* Parse and validate record length
4505 * This must happen after the CID parsing because
4506 * its position in the record header depends on
4507 * the presence of a CID. */
4508
4509 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004510 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004511 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004515 }
4516
Hanno Becker8b09b732019-05-08 12:03:28 +01004517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4518 "version = [%d:%d], msglen = %d",
4519 ssl->in_msgtype,
4520 major_ver, minor_ver, ssl->in_msglen ) );
4521
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004522 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004523 * DTLS-related tests.
4524 * Check epoch before checking length constraint because
4525 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4526 * message gets duplicated before the corresponding Finished message,
4527 * the second ChangeCipherSpec should be discarded because it belongs
4528 * to an old epoch, but not because its length is shorter than
4529 * the minimum record length for packets using the new record transform.
4530 * Note that these two kinds of failures are handled differently,
4531 * as an unexpected record is silently skipped but an invalid
4532 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004533 */
4534#if defined(MBEDTLS_SSL_PROTO_DTLS)
4535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4536 {
4537 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4538
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004539 /* Check epoch (and sequence number) with DTLS */
4540 if( rec_epoch != ssl->in_epoch )
4541 {
4542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4543 "expected %d, received %d",
4544 ssl->in_epoch, rec_epoch ) );
4545
4546#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4547 /*
4548 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4549 * access the first byte of record content (handshake type), as we
4550 * have an active transform (possibly iv_len != 0), so use the
4551 * fact that the record header len is 13 instead.
4552 */
4553 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4554 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4555 rec_epoch == 0 &&
4556 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4557 ssl->in_left > 13 &&
4558 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4559 {
4560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4561 "from the same port" ) );
4562 return( ssl_handle_possible_reconnect( ssl ) );
4563 }
4564 else
4565#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004566 {
4567 /* Consider buffering the record. */
4568 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4569 {
4570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4571 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4572 }
4573
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004574 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004575 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004576 }
4577
4578#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4579 /* Replay detection only works for the current epoch */
4580 if( rec_epoch == ssl->in_epoch &&
4581 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4582 {
4583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4584 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4585 }
4586#endif
4587 }
4588#endif /* MBEDTLS_SSL_PROTO_DTLS */
4589
Hanno Becker52c6dc62017-05-26 16:07:36 +01004590
4591 /* Check length against bounds of the current transform and version */
4592 if( ssl->transform_in == NULL )
4593 {
4594 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004595 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004596 {
4597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4598 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4599 }
4600 }
4601 else
4602 {
4603 if( ssl->in_msglen < ssl->transform_in->minlen )
4604 {
4605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4606 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4607 }
4608
4609#if defined(MBEDTLS_SSL_PROTO_SSL3)
4610 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004611 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004612 {
4613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4614 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4615 }
4616#endif
4617#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4618 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4619 /*
4620 * TLS encrypted messages can have up to 256 bytes of padding
4621 */
4622 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4623 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004624 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004625 {
4626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4628 }
4629#endif
4630 }
4631
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004632 return( 0 );
4633}
Paul Bakker5121ce52009-01-03 21:22:43 +00004634
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004635/*
4636 * If applicable, decrypt (and decompress) record content
4637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004639{
4640 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004643 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4646 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 ret = mbedtls_ssl_hw_record_read( ssl );
4651 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004655 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004656
4657 if( ret == 0 )
4658 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004661 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004662 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004663 mbedtls_record rec;
4664
4665 rec.buf = ssl->in_iv;
4666 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4667 - ( ssl->in_iv - ssl->in_buf );
4668 rec.data_len = ssl->in_msglen;
4669 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004670#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004671 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004672 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004673#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004674
4675 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4676 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4677 ssl->conf->transport, rec.ver );
4678 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004679 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4680 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004683
Hanno Beckera5a2b082019-05-15 14:03:01 +01004684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004685 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4686 ssl->conf->ignore_unexpected_cid
4687 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4688 {
Hanno Becker687e0fb2019-05-08 13:02:55 +01004689 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004690 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004691#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004692
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 return( ret );
4694 }
4695
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004696 if( ssl->in_msgtype != rec.type )
4697 {
4698 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4699 ssl->in_msgtype, rec.type ) );
4700 }
4701
4702 /* The record content type may change during decryption,
4703 * so re-read it. */
4704 ssl->in_msgtype = rec.type;
4705 /* Also update the input buffer, because unfortunately
4706 * the server-side ssl_parse_client_hello() reparses the
4707 * record header when receiving a ClientHello initiating
4708 * a renegotiation. */
4709 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004710 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004711 ssl->in_msglen = rec.data_len;
4712 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4713 ssl->in_len[1] = (unsigned char)( rec.data_len );
4714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 ssl->in_msg, ssl->in_msglen );
4717
Hanno Beckera5a2b082019-05-15 14:03:01 +01004718#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004719 /* We have already checked the record content type
4720 * in ssl_parse_record_header(), failing or silently
4721 * dropping the record in the case of an unknown type.
4722 *
4723 * Since with the use of CIDs, the record content type
4724 * might change during decryption, re-check the record
4725 * content type, but treat a failure as fatal this time. */
4726 if( ssl_check_record_type( ssl->in_msgtype ) )
4727 {
4728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4729 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4730 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004731#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004732
Angus Grattond8213d02016-05-25 20:56:48 +10004733 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4736 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004738 else if( ssl->in_msglen == 0 )
4739 {
4740#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4741 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4742 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4743 {
4744 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4746 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4747 }
4748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4749
4750 ssl->nb_zero++;
4751
4752 /*
4753 * Three or more empty messages may be a DoS attack
4754 * (excessive CPU consumption).
4755 */
4756 if( ssl->nb_zero > 3 )
4757 {
4758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004759 "messages, possible DoS attack" ) );
4760 /* Treat the records as if they were not properly authenticated,
4761 * thereby failing the connection if we see more than allowed
4762 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004763 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4764 }
4765 }
4766 else
4767 ssl->nb_zero = 0;
4768
4769#if defined(MBEDTLS_SSL_PROTO_DTLS)
4770 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4771 {
4772 ; /* in_ctr read from peer, not maintained internally */
4773 }
4774 else
4775#endif
4776 {
4777 unsigned i;
4778 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4779 if( ++ssl->in_ctr[i - 1] != 0 )
4780 break;
4781
4782 /* The loop goes to its end iff the counter is wrapping */
4783 if( i == ssl_ep_len( ssl ) )
4784 {
4785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4786 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4787 }
4788 }
4789
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 }
4791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004793 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004795 {
4796 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004799 return( ret );
4800 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004805 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004808 }
4809#endif
4810
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004811 return( 0 );
4812}
4813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004815
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004816/*
4817 * Read a record.
4818 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004819 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4820 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4821 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004822 */
Hanno Becker1097b342018-08-15 14:09:41 +01004823
4824/* Helper functions for mbedtls_ssl_read_record(). */
4825static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004826static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4827static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004828
Hanno Becker327c93b2018-08-15 13:56:18 +01004829int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004830 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004831{
4832 int ret;
4833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004835
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004836 if( ssl->keep_current_message == 0 )
4837 {
4838 do {
Simon Butcher99000142016-10-13 17:21:01 +01004839
Hanno Becker26994592018-08-15 14:14:59 +01004840 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004841 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004842 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004843
Hanno Beckere74d5562018-08-15 14:26:08 +01004844 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004845 {
Hanno Becker40f50842018-08-15 14:48:01 +01004846#if defined(MBEDTLS_SSL_PROTO_DTLS)
4847 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004848
Hanno Becker40f50842018-08-15 14:48:01 +01004849 /* We only check for buffered messages if the
4850 * current datagram is fully consumed. */
4851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004852 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004853 {
Hanno Becker40f50842018-08-15 14:48:01 +01004854 if( ssl_load_buffered_message( ssl ) == 0 )
4855 have_buffered = 1;
4856 }
4857
4858 if( have_buffered == 0 )
4859#endif /* MBEDTLS_SSL_PROTO_DTLS */
4860 {
4861 ret = ssl_get_next_record( ssl );
4862 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4863 continue;
4864
4865 if( ret != 0 )
4866 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004867 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004868 return( ret );
4869 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004870 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004871 }
4872
4873 ret = mbedtls_ssl_handle_message_type( ssl );
4874
Hanno Becker40f50842018-08-15 14:48:01 +01004875#if defined(MBEDTLS_SSL_PROTO_DTLS)
4876 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4877 {
4878 /* Buffer future message */
4879 ret = ssl_buffer_message( ssl );
4880 if( ret != 0 )
4881 return( ret );
4882
4883 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4884 }
4885#endif /* MBEDTLS_SSL_PROTO_DTLS */
4886
Hanno Becker90333da2017-10-10 11:27:13 +01004887 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4888 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004889
4890 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004891 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004892 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004893 return( ret );
4894 }
4895
Hanno Becker327c93b2018-08-15 13:56:18 +01004896 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004897 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004898 {
4899 mbedtls_ssl_update_handshake_status( ssl );
4900 }
Simon Butcher99000142016-10-13 17:21:01 +01004901 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004902 else
Simon Butcher99000142016-10-13 17:21:01 +01004903 {
Hanno Becker02f59072018-08-15 14:00:24 +01004904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004905 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004906 }
4907
4908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4909
4910 return( 0 );
4911}
4912
Hanno Becker40f50842018-08-15 14:48:01 +01004913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004914static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004915{
Hanno Becker40f50842018-08-15 14:48:01 +01004916 if( ssl->in_left > ssl->next_record_offset )
4917 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004918
Hanno Becker40f50842018-08-15 14:48:01 +01004919 return( 0 );
4920}
4921
4922static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4923{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004924 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004925 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004926 int ret = 0;
4927
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004928 if( hs == NULL )
4929 return( -1 );
4930
Hanno Beckere00ae372018-08-20 09:39:42 +01004931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4932
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004933 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4934 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4935 {
4936 /* Check if we have seen a ChangeCipherSpec before.
4937 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004938 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004939 {
4940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4941 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004942 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004943 }
4944
Hanno Becker39b8bc92018-08-28 17:17:13 +01004945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004946 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4947 ssl->in_msglen = 1;
4948 ssl->in_msg[0] = 1;
4949
4950 /* As long as they are equal, the exact value doesn't matter. */
4951 ssl->in_left = 0;
4952 ssl->next_record_offset = 0;
4953
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004954 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004955 goto exit;
4956 }
Hanno Becker37f95322018-08-16 13:55:32 +01004957
Hanno Beckerb8f50142018-08-28 10:01:34 +01004958#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004959 /* Debug only */
4960 {
4961 unsigned offset;
4962 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4963 {
4964 hs_buf = &hs->buffering.hs[offset];
4965 if( hs_buf->is_valid == 1 )
4966 {
4967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4968 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004969 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004970 }
4971 }
4972 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004973#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004974
4975 /* Check if we have buffered and/or fully reassembled the
4976 * next handshake message. */
4977 hs_buf = &hs->buffering.hs[0];
4978 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4979 {
4980 /* Synthesize a record containing the buffered HS message. */
4981 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4982 ( hs_buf->data[2] << 8 ) |
4983 hs_buf->data[3];
4984
4985 /* Double-check that we haven't accidentally buffered
4986 * a message that doesn't fit into the input buffer. */
4987 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4988 {
4989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4991 }
4992
4993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4994 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4995 hs_buf->data, msg_len + 12 );
4996
4997 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4998 ssl->in_hslen = msg_len + 12;
4999 ssl->in_msglen = msg_len + 12;
5000 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5001
5002 ret = 0;
5003 goto exit;
5004 }
5005 else
5006 {
5007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5008 hs->in_msg_seq ) );
5009 }
5010
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005011 ret = -1;
5012
5013exit:
5014
5015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5016 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005017}
5018
Hanno Beckera02b0b42018-08-21 17:20:27 +01005019static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5020 size_t desired )
5021{
5022 int offset;
5023 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5025 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005026
Hanno Becker01315ea2018-08-21 17:22:17 +01005027 /* Get rid of future records epoch first, if such exist. */
5028 ssl_free_buffered_record( ssl );
5029
5030 /* Check if we have enough space available now. */
5031 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5032 hs->buffering.total_bytes_buffered ) )
5033 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005035 return( 0 );
5036 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005037
Hanno Becker4f432ad2018-08-28 10:02:32 +01005038 /* We don't have enough space to buffer the next expected handshake
5039 * message. Remove buffers used for future messages to gain space,
5040 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005041 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5042 offset >= 0; offset-- )
5043 {
5044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5045 offset ) );
5046
Hanno Beckerb309b922018-08-23 13:18:05 +01005047 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005048
5049 /* Check if we have enough space available now. */
5050 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5051 hs->buffering.total_bytes_buffered ) )
5052 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005054 return( 0 );
5055 }
5056 }
5057
5058 return( -1 );
5059}
5060
Hanno Becker40f50842018-08-15 14:48:01 +01005061static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5062{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005063 int ret = 0;
5064 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5065
5066 if( hs == NULL )
5067 return( 0 );
5068
5069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5070
5071 switch( ssl->in_msgtype )
5072 {
5073 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005075
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005076 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005077 break;
5078
5079 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005080 {
5081 unsigned recv_msg_seq_offset;
5082 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5083 mbedtls_ssl_hs_buffer *hs_buf;
5084 size_t msg_len = ssl->in_hslen - 12;
5085
5086 /* We should never receive an old handshake
5087 * message - double-check nonetheless. */
5088 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5089 {
5090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5092 }
5093
5094 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5095 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5096 {
5097 /* Silently ignore -- message too far in the future */
5098 MBEDTLS_SSL_DEBUG_MSG( 2,
5099 ( "Ignore future HS message with sequence number %u, "
5100 "buffering window %u - %u",
5101 recv_msg_seq, ssl->handshake->in_msg_seq,
5102 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5103
5104 goto exit;
5105 }
5106
5107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5108 recv_msg_seq, recv_msg_seq_offset ) );
5109
5110 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5111
5112 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005113 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005114 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005115 size_t reassembly_buf_sz;
5116
Hanno Becker37f95322018-08-16 13:55:32 +01005117 hs_buf->is_fragmented =
5118 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5119
5120 /* We copy the message back into the input buffer
5121 * after reassembly, so check that it's not too large.
5122 * This is an implementation-specific limitation
5123 * and not one from the standard, hence it is not
5124 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005125 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005126 {
5127 /* Ignore message */
5128 goto exit;
5129 }
5130
Hanno Beckere0b150f2018-08-21 15:51:03 +01005131 /* Check if we have enough space to buffer the message. */
5132 if( hs->buffering.total_bytes_buffered >
5133 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5134 {
5135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5137 }
5138
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005139 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5140 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005141
5142 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5143 hs->buffering.total_bytes_buffered ) )
5144 {
5145 if( recv_msg_seq_offset > 0 )
5146 {
5147 /* If we can't buffer a future message because
5148 * of space limitations -- ignore. */
5149 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",
5150 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5151 (unsigned) hs->buffering.total_bytes_buffered ) );
5152 goto exit;
5153 }
Hanno Beckere1801392018-08-21 16:51:05 +01005154 else
5155 {
5156 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",
5157 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5158 (unsigned) hs->buffering.total_bytes_buffered ) );
5159 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005160
Hanno Beckera02b0b42018-08-21 17:20:27 +01005161 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005162 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005163 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",
5164 (unsigned) msg_len,
5165 (unsigned) reassembly_buf_sz,
5166 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005167 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005168 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5169 goto exit;
5170 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005171 }
5172
5173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5174 msg_len ) );
5175
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005176 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5177 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005178 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005179 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005180 goto exit;
5181 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005182 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005183
5184 /* Prepare final header: copy msg_type, length and message_seq,
5185 * then add standardised fragment_offset and fragment_length */
5186 memcpy( hs_buf->data, ssl->in_msg, 6 );
5187 memset( hs_buf->data + 6, 0, 3 );
5188 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5189
5190 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005191
5192 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005193 }
5194 else
5195 {
5196 /* Make sure msg_type and length are consistent */
5197 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5198 {
5199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5200 /* Ignore */
5201 goto exit;
5202 }
5203 }
5204
Hanno Becker4422bbb2018-08-20 09:40:19 +01005205 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005206 {
5207 size_t frag_len, frag_off;
5208 unsigned char * const msg = hs_buf->data + 12;
5209
5210 /*
5211 * Check and copy current fragment
5212 */
5213
5214 /* Validation of header fields already done in
5215 * mbedtls_ssl_prepare_handshake_record(). */
5216 frag_off = ssl_get_hs_frag_off( ssl );
5217 frag_len = ssl_get_hs_frag_len( ssl );
5218
5219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5220 frag_off, frag_len ) );
5221 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5222
5223 if( hs_buf->is_fragmented )
5224 {
5225 unsigned char * const bitmask = msg + msg_len;
5226 ssl_bitmask_set( bitmask, frag_off, frag_len );
5227 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5228 msg_len ) == 0 );
5229 }
5230 else
5231 {
5232 hs_buf->is_complete = 1;
5233 }
5234
5235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5236 hs_buf->is_complete ? "" : "not yet " ) );
5237 }
5238
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005239 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005240 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005241
5242 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005243 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005244 break;
5245 }
5246
5247exit:
5248
5249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5250 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005251}
5252#endif /* MBEDTLS_SSL_PROTO_DTLS */
5253
Hanno Becker1097b342018-08-15 14:09:41 +01005254static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005255{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005256 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005257 * Consume last content-layer message and potentially
5258 * update in_msglen which keeps track of the contents'
5259 * consumption state.
5260 *
5261 * (1) Handshake messages:
5262 * Remove last handshake message, move content
5263 * and adapt in_msglen.
5264 *
5265 * (2) Alert messages:
5266 * Consume whole record content, in_msglen = 0.
5267 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005268 * (3) Change cipher spec:
5269 * Consume whole record content, in_msglen = 0.
5270 *
5271 * (4) Application data:
5272 * Don't do anything - the record layer provides
5273 * the application data as a stream transport
5274 * and consumes through mbedtls_ssl_read only.
5275 *
5276 */
5277
5278 /* Case (1): Handshake messages */
5279 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005280 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005281 /* Hard assertion to be sure that no application data
5282 * is in flight, as corrupting ssl->in_msglen during
5283 * ssl->in_offt != NULL is fatal. */
5284 if( ssl->in_offt != NULL )
5285 {
5286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5288 }
5289
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005290 /*
5291 * Get next Handshake message in the current record
5292 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005293
Hanno Becker4a810fb2017-05-24 16:27:30 +01005294 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005295 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005296 * current handshake content: If DTLS handshake
5297 * fragmentation is used, that's the fragment
5298 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005299 * size here is faulty and should be changed at
5300 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005301 * (2) While it doesn't seem to cause problems, one
5302 * has to be very careful not to assume that in_hslen
5303 * is always <= in_msglen in a sensible communication.
5304 * Again, it's wrong for DTLS handshake fragmentation.
5305 * The following check is therefore mandatory, and
5306 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005307 * Additionally, ssl->in_hslen might be arbitrarily out of
5308 * bounds after handling a DTLS message with an unexpected
5309 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005310 */
5311 if( ssl->in_hslen < ssl->in_msglen )
5312 {
5313 ssl->in_msglen -= ssl->in_hslen;
5314 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5315 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005316
Hanno Becker4a810fb2017-05-24 16:27:30 +01005317 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5318 ssl->in_msg, ssl->in_msglen );
5319 }
5320 else
5321 {
5322 ssl->in_msglen = 0;
5323 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005324
Hanno Becker4a810fb2017-05-24 16:27:30 +01005325 ssl->in_hslen = 0;
5326 }
5327 /* Case (4): Application data */
5328 else if( ssl->in_offt != NULL )
5329 {
5330 return( 0 );
5331 }
5332 /* Everything else (CCS & Alerts) */
5333 else
5334 {
5335 ssl->in_msglen = 0;
5336 }
5337
Hanno Becker1097b342018-08-15 14:09:41 +01005338 return( 0 );
5339}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005340
Hanno Beckere74d5562018-08-15 14:26:08 +01005341static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5342{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005343 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005344 return( 1 );
5345
5346 return( 0 );
5347}
5348
Hanno Becker5f066e72018-08-16 14:56:31 +01005349#if defined(MBEDTLS_SSL_PROTO_DTLS)
5350
5351static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5352{
5353 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5354 if( hs == NULL )
5355 return;
5356
Hanno Becker01315ea2018-08-21 17:22:17 +01005357 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005358 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005359 hs->buffering.total_bytes_buffered -=
5360 hs->buffering.future_record.len;
5361
5362 mbedtls_free( hs->buffering.future_record.data );
5363 hs->buffering.future_record.data = NULL;
5364 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005365}
5366
5367static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5368{
5369 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5370 unsigned char * rec;
5371 size_t rec_len;
5372 unsigned rec_epoch;
5373
5374 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5375 return( 0 );
5376
5377 if( hs == NULL )
5378 return( 0 );
5379
Hanno Becker5f066e72018-08-16 14:56:31 +01005380 rec = hs->buffering.future_record.data;
5381 rec_len = hs->buffering.future_record.len;
5382 rec_epoch = hs->buffering.future_record.epoch;
5383
5384 if( rec == NULL )
5385 return( 0 );
5386
Hanno Becker4cb782d2018-08-20 11:19:05 +01005387 /* Only consider loading future records if the
5388 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005389 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005390 return( 0 );
5391
Hanno Becker5f066e72018-08-16 14:56:31 +01005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5393
5394 if( rec_epoch != ssl->in_epoch )
5395 {
5396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5397 goto exit;
5398 }
5399
5400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5401
5402 /* Double-check that the record is not too large */
5403 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5404 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5405 {
5406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5408 }
5409
5410 memcpy( ssl->in_hdr, rec, rec_len );
5411 ssl->in_left = rec_len;
5412 ssl->next_record_offset = 0;
5413
5414 ssl_free_buffered_record( ssl );
5415
5416exit:
5417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5418 return( 0 );
5419}
5420
5421static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5422{
5423 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5424 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005425 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005426
5427 /* Don't buffer future records outside handshakes. */
5428 if( hs == NULL )
5429 return( 0 );
5430
5431 /* Only buffer handshake records (we are only interested
5432 * in Finished messages). */
5433 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5434 return( 0 );
5435
5436 /* Don't buffer more than one future epoch record. */
5437 if( hs->buffering.future_record.data != NULL )
5438 return( 0 );
5439
Hanno Becker01315ea2018-08-21 17:22:17 +01005440 /* Don't buffer record if there's not enough buffering space remaining. */
5441 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5442 hs->buffering.total_bytes_buffered ) )
5443 {
5444 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",
5445 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5446 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005447 return( 0 );
5448 }
5449
Hanno Becker5f066e72018-08-16 14:56:31 +01005450 /* Buffer record */
5451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5452 ssl->in_epoch + 1 ) );
5453 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5454 rec_hdr_len + ssl->in_msglen );
5455
5456 /* ssl_parse_record_header() only considers records
5457 * of the next epoch as candidates for buffering. */
5458 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005459 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005460
5461 hs->buffering.future_record.data =
5462 mbedtls_calloc( 1, hs->buffering.future_record.len );
5463 if( hs->buffering.future_record.data == NULL )
5464 {
5465 /* If we run out of RAM trying to buffer a
5466 * record from the next epoch, just ignore. */
5467 return( 0 );
5468 }
5469
Hanno Becker01315ea2018-08-21 17:22:17 +01005470 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005471
Hanno Becker01315ea2018-08-21 17:22:17 +01005472 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005473 return( 0 );
5474}
5475
5476#endif /* MBEDTLS_SSL_PROTO_DTLS */
5477
Hanno Beckere74d5562018-08-15 14:26:08 +01005478static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005479{
5480 int ret;
5481
Hanno Becker5f066e72018-08-16 14:56:31 +01005482#if defined(MBEDTLS_SSL_PROTO_DTLS)
5483 /* We might have buffered a future record; if so,
5484 * and if the epoch matches now, load it.
5485 * On success, this call will set ssl->in_left to
5486 * the length of the buffered record, so that
5487 * the calls to ssl_fetch_input() below will
5488 * essentially be no-ops. */
5489 ret = ssl_load_buffered_record( ssl );
5490 if( ret != 0 )
5491 return( ret );
5492#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005493
Hanno Becker8b09b732019-05-08 12:03:28 +01005494 /* Reset in pointers to default state for TLS/DTLS records,
5495 * assuming no CID and no offset between record content and
5496 * record plaintext. */
5497 ssl_update_in_pointers( ssl );
5498
5499 /* Ensure that we have enough space available for the default form
5500 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5501 * with no space for CIDs counted in). */
5502 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5503 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005506 return( ret );
5507 }
5508
5509 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5513 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005514 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005515 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5516 {
5517 ret = ssl_buffer_future_record( ssl );
5518 if( ret != 0 )
5519 return( ret );
5520
5521 /* Fall through to handling of unexpected records */
5522 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5523 }
5524
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005525 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5526 {
5527 /* Skip unexpected record (but not whole datagram) */
5528 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005529 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005530
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5532 "(header)" ) );
5533 }
5534 else
5535 {
5536 /* Skip invalid record and the rest of the datagram */
5537 ssl->next_record_offset = 0;
5538 ssl->in_left = 0;
5539
5540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5541 "(header)" ) );
5542 }
5543
5544 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005545 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005546 }
5547#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005548 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005549 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005550
5551 /*
5552 * Read and optionally decrypt the message contents
5553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005555 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005558 return( ret );
5559 }
5560
5561 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005564 {
Hanno Becker43395762019-05-03 14:46:38 +01005565 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005566 if( ssl->next_record_offset < ssl->in_left )
5567 {
5568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5569 }
5570 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005571 else
5572#endif
5573 ssl->in_left = 0;
5574
5575 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005579 {
5580 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005581 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005582 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005583 /* Except when waiting for Finished as a bad mac here
5584 * probably means something went wrong in the handshake
5585 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5586 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5587 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5588 {
5589#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5590 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5591 {
5592 mbedtls_ssl_send_alert_message( ssl,
5593 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5594 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5595 }
5596#endif
5597 return( ret );
5598 }
5599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005601 if( ssl->conf->badmac_limit != 0 &&
5602 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5605 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005606 }
5607#endif
5608
Hanno Becker4a810fb2017-05-24 16:27:30 +01005609 /* As above, invalid records cause
5610 * dismissal of the whole datagram. */
5611
5612 ssl->next_record_offset = 0;
5613 ssl->in_left = 0;
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005616 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005617 }
5618
5619 return( ret );
5620 }
5621 else
5622#endif
5623 {
5624 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5626 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 mbedtls_ssl_send_alert_message( ssl,
5629 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5630 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005631 }
5632#endif
5633 return( ret );
5634 }
5635 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005636
Simon Butcher99000142016-10-13 17:21:01 +01005637 return( 0 );
5638}
5639
5640int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5641{
5642 int ret;
5643
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005644 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005645 * Handle particular types of records
5646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005648 {
Simon Butcher99000142016-10-13 17:21:01 +01005649 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5650 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005651 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005652 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005653 }
5654
Hanno Beckere678eaa2018-08-21 14:57:46 +01005655 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005656 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005657 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005658 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5660 ssl->in_msglen ) );
5661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005662 }
5663
Hanno Beckere678eaa2018-08-21 14:57:46 +01005664 if( ssl->in_msg[0] != 1 )
5665 {
5666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5667 ssl->in_msg[0] ) );
5668 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5669 }
5670
5671#if defined(MBEDTLS_SSL_PROTO_DTLS)
5672 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5673 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5674 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5675 {
5676 if( ssl->handshake == NULL )
5677 {
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5679 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5680 }
5681
5682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5683 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5684 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005685#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005686 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005690 if( ssl->in_msglen != 2 )
5691 {
5692 /* Note: Standard allows for more than one 2 byte alert
5693 to be packed in a single message, but Mbed TLS doesn't
5694 currently support this. */
5695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5696 ssl->in_msglen ) );
5697 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5698 }
5699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 ssl->in_msg[0], ssl->in_msg[1] ) );
5702
5703 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005704 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005709 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 }
5712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5714 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5717 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005718 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005719
5720#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5721 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5722 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5723 {
Hanno Becker90333da2017-10-10 11:27:13 +01005724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005725 /* Will be handled when trying to parse ServerHello */
5726 return( 0 );
5727 }
5728#endif
5729
5730#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5731 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5732 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5733 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5734 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5735 {
5736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5737 /* Will be handled in mbedtls_ssl_parse_certificate() */
5738 return( 0 );
5739 }
5740#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5741
5742 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005743 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 }
5745
Hanno Beckerc76c6192017-06-06 10:03:17 +01005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005747 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005748 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005749 /* Drop unexpected ApplicationData records,
5750 * except at the beginning of renegotiations */
5751 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5752 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5753#if defined(MBEDTLS_SSL_RENEGOTIATION)
5754 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5755 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005756#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005757 )
5758 {
5759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5760 return( MBEDTLS_ERR_SSL_NON_FATAL );
5761 }
5762
5763 if( ssl->handshake != NULL &&
5764 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5765 {
5766 ssl_handshake_wrapup_free_hs_transform( ssl );
5767 }
5768 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005769#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005770
Paul Bakker5121ce52009-01-03 21:22:43 +00005771 return( 0 );
5772}
5773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005775{
5776 int ret;
5777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5779 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5780 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005781 {
5782 return( ret );
5783 }
5784
5785 return( 0 );
5786}
5787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005789 unsigned char level,
5790 unsigned char message )
5791{
5792 int ret;
5793
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005794 if( ssl == NULL || ssl->conf == NULL )
5795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005801 ssl->out_msglen = 2;
5802 ssl->out_msg[0] = level;
5803 ssl->out_msg[1] = message;
5804
Hanno Becker67bc7c32018-08-06 11:33:50 +01005805 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005808 return( ret );
5809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005811
5812 return( 0 );
5813}
5814
Paul Bakker5121ce52009-01-03 21:22:43 +00005815/*
5816 * Handshake functions
5817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5819 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5820 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5821 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5822 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5823 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5824 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005825/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005827{
Hanno Becker8759e162017-12-27 21:34:08 +00005828 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5833 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005834 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5835 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005838 ssl->state++;
5839 return( 0 );
5840 }
5841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005844}
5845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005847{
Hanno Becker8759e162017-12-27 21:34:08 +00005848 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5853 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005854 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5855 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005858 ssl->state++;
5859 return( 0 );
5860 }
5861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005864}
Gilles Peskinef9828522017-05-03 12:28:43 +02005865
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005866#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005867/* Some certificate support -> implement write and parse */
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005872 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005874 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5879 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005880 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5881 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005884 ssl->state++;
5885 return( 0 );
5886 }
5887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005889 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 {
5891 if( ssl->client_auth == 0 )
5892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005894 ssl->state++;
5895 return( 0 );
5896 }
5897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 /*
5900 * If using SSLv3 and got no cert, send an Alert message
5901 * (otherwise an empty Certificate message will be sent).
5902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5904 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005905 {
5906 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5908 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5909 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 goto write_msg;
5913 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#endif /* MBEDTLS_SSL_CLI_C */
5917#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5923 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005924 }
5925 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005926#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005929
5930 /*
5931 * 0 . 0 handshake type
5932 * 1 . 3 handshake length
5933 * 4 . 6 length of all certs
5934 * 7 . 9 length of cert. 1
5935 * 10 . n-1 peer certificate
5936 * n . n+2 length of cert. 2
5937 * n+3 . ... upper level cert, etc.
5938 */
5939 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005941
Paul Bakker29087132010-03-21 21:03:34 +00005942 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 {
5944 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005945 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005948 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 }
5951
5952 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5953 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5954 ssl->out_msg[i + 2] = (unsigned char)( n );
5955
5956 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5957 i += n; crt = crt->next;
5958 }
5959
5960 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5961 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5962 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5963
5964 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5966 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005967
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005968#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005969write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005970#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005971
5972 ssl->state++;
5973
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005974 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005975 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005977 return( ret );
5978 }
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005981
Paul Bakkered27a042013-04-18 22:46:23 +02005982 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005983}
5984
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005985/*
5986 * Once the certificate message is read, parse it into a cert chain and
5987 * perform basic checks, but leave actual verification to the caller
5988 */
5989static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005990{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005991 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005992 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005993 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#if defined(MBEDTLS_SSL_SRV_C)
5996#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005997 /*
5998 * Check if the client sent an empty certificate
5999 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006000 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006002 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006003 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6005 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6006 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006009
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006010 /* The client was asked for a certificate but didn't send
6011 one. The client should know what's going on, so we
6012 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006013 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006014 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 }
6016 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6020 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006021 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6025 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6026 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6027 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006031 /* The client was asked for a certificate but didn't send
6032 one. The client should know what's going on, so we
6033 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006034 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006035 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 }
6037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6039 MBEDTLS_SSL_PROTO_TLS1_2 */
6040#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006045 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6046 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006048 }
6049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6051 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6055 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 }
6058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006060
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006063 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006064 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006066 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006070 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6071 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 }
6074
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006075 /* In case we tried to reuse a session but it failed */
6076 if( ssl->session_negotiate->peer_cert != NULL )
6077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6079 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006080 }
6081
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006082 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6088 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006089 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 }
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006093
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006094 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006095
6096 while( i < ssl->in_hslen )
6097 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006098 if ( i + 3 > ssl->in_hslen ) {
6099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6102 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006104 if( ssl->in_msg[i] != 0 )
6105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006107 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6108 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006110 }
6111
6112 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6113 | (unsigned int) ssl->in_msg[i + 2];
6114 i += 3;
6115
6116 if( n < 128 || i + n > ssl->in_hslen )
6117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6120 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 }
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006125 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006126 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006128 case 0: /*ok*/
6129 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6130 /* Ignore certificate with an unknown algorithm: maybe a
6131 prior certificate was already trusted. */
6132 break;
6133
6134 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6135 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6136 goto crt_parse_der_failed;
6137
6138 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6139 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6140 goto crt_parse_der_failed;
6141
6142 default:
6143 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6144 crt_parse_der_failed:
6145 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006147 return( ret );
6148 }
6149
6150 i += n;
6151 }
6152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006155 /*
6156 * On client, make sure the server cert doesn't change during renego to
6157 * avoid "triple handshake" attack: https://secure-resumption.com/
6158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006160 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006162 {
6163 if( ssl->session->peer_cert == NULL )
6164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6167 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006169 }
6170
6171 if( ssl->session->peer_cert->raw.len !=
6172 ssl->session_negotiate->peer_cert->raw.len ||
6173 memcmp( ssl->session->peer_cert->raw.p,
6174 ssl->session_negotiate->peer_cert->raw.p,
6175 ssl->session->peer_cert->raw.len ) != 0 )
6176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6179 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006181 }
6182 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006184
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006185 return( 0 );
6186}
6187
6188int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6189{
6190 int ret;
6191 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006192 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006193#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6194 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6195 ? ssl->handshake->sni_authmode
6196 : ssl->conf->authmode;
6197#else
6198 const int authmode = ssl->conf->authmode;
6199#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006200 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006201
6202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6203
6204 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6205 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6206 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6207 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6208 {
6209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6210 ssl->state++;
6211 return( 0 );
6212 }
6213
6214#if defined(MBEDTLS_SSL_SRV_C)
6215 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6216 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6217 {
6218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6219 ssl->state++;
6220 return( 0 );
6221 }
6222
6223 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6224 authmode == MBEDTLS_SSL_VERIFY_NONE )
6225 {
6226 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006228
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006229 ssl->state++;
6230 return( 0 );
6231 }
6232#endif
6233
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006234#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6235 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006236 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006237 {
6238 goto crt_verify;
6239 }
6240#endif
6241
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006242 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006243 {
6244 /* mbedtls_ssl_read_record may have sent an alert already. We
6245 let it decide whether to alert. */
6246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6247 return( ret );
6248 }
6249
6250 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6251 {
6252#if defined(MBEDTLS_SSL_SRV_C)
6253 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6254 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6255 {
6256 ret = 0;
6257 }
6258#endif
6259
6260 ssl->state++;
6261 return( ret );
6262 }
6263
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006264#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6265 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006266 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006267
6268crt_verify:
6269 if( ssl->handshake->ecrs_enabled)
6270 rs_ctx = &ssl->handshake->ecrs_ctx;
6271#endif
6272
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006273 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006274 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006275 mbedtls_x509_crt *ca_chain;
6276 mbedtls_x509_crl *ca_crl;
6277
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006278#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006279 if( ssl->handshake->sni_ca_chain != NULL )
6280 {
6281 ca_chain = ssl->handshake->sni_ca_chain;
6282 ca_crl = ssl->handshake->sni_ca_crl;
6283 }
6284 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006285#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006286 {
6287 ca_chain = ssl->conf->ca_chain;
6288 ca_crl = ssl->conf->ca_crl;
6289 }
6290
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006291 /*
6292 * Main check: verify certificate
6293 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006294 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006295 ssl->session_negotiate->peer_cert,
6296 ca_chain, ca_crl,
6297 ssl->conf->cert_profile,
6298 ssl->hostname,
6299 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006300 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006301
6302 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006305 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006306
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006307#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6308 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006309 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006310#endif
6311
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006312 /*
6313 * Secondary checks: always done, but change 'ret' only if it was 0
6314 */
6315
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006316#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006319
6320 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006322 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006323 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006324 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006327 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006329 }
6330 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006331#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006334 ciphersuite_info,
6335 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006336 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006339 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006341 }
6342
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006343 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6344 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6345 * with details encoded in the verification flags. All other kinds
6346 * of error codes, including those from the user provided f_vrfy
6347 * functions, are treated as fatal and lead to a failure of
6348 * ssl_parse_certificate even if verification was optional. */
6349 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6350 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6351 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6352 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006354 }
6355
6356 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6357 {
6358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6359 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6360 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006361
6362 if( ret != 0 )
6363 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006364 uint8_t alert;
6365
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006366 /* The certificate may have been rejected for several reasons.
6367 Pick one and send the corresponding alert. Which alert to send
6368 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006369 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6370 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6371 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6372 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6373 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6374 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6375 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6376 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6377 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6378 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6379 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6380 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6381 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6382 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6383 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6384 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6385 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6386 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6387 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6388 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006389 else
6390 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6392 alert );
6393 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006394
Hanno Beckere6706e62017-05-15 16:05:15 +01006395#if defined(MBEDTLS_DEBUG_C)
6396 if( ssl->session_negotiate->verify_result != 0 )
6397 {
6398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6399 ssl->session_negotiate->verify_result ) );
6400 }
6401 else
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6404 }
6405#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006406 }
6407
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006408 ssl->state++;
6409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006411
6412 return( ret );
6413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6415 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6416 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6417 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6418 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6419 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6420 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006423{
6424 int ret;
6425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 ssl->out_msglen = 1;
6430 ssl->out_msg[0] = 1;
6431
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 ssl->state++;
6433
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006434 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 return( ret );
6438 }
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006441
6442 return( 0 );
6443}
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006446{
6447 int ret;
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006450
Hanno Becker327c93b2018-08-15 13:56:18 +01006451 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006454 return( ret );
6455 }
6456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6461 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006463 }
6464
Hanno Beckere678eaa2018-08-21 14:57:46 +01006465 /* CCS records are only accepted if they have length 1 and content '1',
6466 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006467
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006468 /*
6469 * Switch to our negotiated transform and session parameters for inbound
6470 * data.
6471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006473 ssl->transform_in = ssl->transform_negotiate;
6474 ssl->session_in = ssl->session_negotiate;
6475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006477 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006480 ssl_dtls_replay_reset( ssl );
6481#endif
6482
6483 /* Increment epoch */
6484 if( ++ssl->in_epoch == 0 )
6485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006487 /* This is highly unlikely to happen for legitimate reasons, so
6488 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006490 }
6491 }
6492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006494 memset( ssl->in_ctr, 0, 8 );
6495
Hanno Beckerf5970a02019-05-08 09:38:41 +01006496 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6499 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006504 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6505 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006507 }
6508 }
6509#endif
6510
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 ssl->state++;
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006514
6515 return( 0 );
6516}
6517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6519 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006520{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006521 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6524 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6525 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006526 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006527 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6530#if defined(MBEDTLS_SHA512_C)
6531 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006532 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6533 else
6534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535#if defined(MBEDTLS_SHA256_C)
6536 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006537 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006538 else
6539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006543 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006544 }
Paul Bakker380da532012-04-18 16:10:25 +00006545}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6550 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006551 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6552 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6555#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006556 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006557#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006559 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006560#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006562}
6563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006565 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6568 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006569 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6570 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6573#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006574 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006575#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006577 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006580}
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6583 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6584static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006585 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006586{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006587 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6588 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006589}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006590#endif
Paul Bakker380da532012-04-18 16:10:25 +00006591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6593#if defined(MBEDTLS_SHA256_C)
6594static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006595 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006596{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006597 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006598}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006599#endif
Paul Bakker380da532012-04-18 16:10:25 +00006600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601#if defined(MBEDTLS_SHA512_C)
6602static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006603 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006604{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006605 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006606}
Paul Bakker769075d2012-11-24 11:26:46 +01006607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006611static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006613{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006614 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006615 mbedtls_md5_context md5;
6616 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006617
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 unsigned char padbuf[48];
6619 unsigned char md5sum[16];
6620 unsigned char sha1sum[20];
6621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006623 if( !session )
6624 session = ssl->session;
6625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006627
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006628 mbedtls_md5_init( &md5 );
6629 mbedtls_sha1_init( &sha1 );
6630
6631 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6632 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006633
6634 /*
6635 * SSLv3:
6636 * hash =
6637 * MD5( master + pad2 +
6638 * MD5( handshake + sender + master + pad1 ) )
6639 * + SHA1( master + pad2 +
6640 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006641 */
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006644 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6645 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006649 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6650 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006651#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006654 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006655
Paul Bakker1ef83d62012-04-11 12:09:53 +00006656 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006658 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6659 mbedtls_md5_update_ret( &md5, session->master, 48 );
6660 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6661 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006662
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006663 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6664 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6665 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6666 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006667
Paul Bakker1ef83d62012-04-11 12:09:53 +00006668 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006670 mbedtls_md5_starts_ret( &md5 );
6671 mbedtls_md5_update_ret( &md5, session->master, 48 );
6672 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6673 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6674 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006676 mbedtls_sha1_starts_ret( &sha1 );
6677 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6678 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6679 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6680 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006683
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006684 mbedtls_md5_free( &md5 );
6685 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006686
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006687 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6688 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6689 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006696static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006698{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006699 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006700 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006701 mbedtls_md5_context md5;
6702 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006703 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006706 if( !session )
6707 session = ssl->session;
6708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006710
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006711 mbedtls_md5_init( &md5 );
6712 mbedtls_sha1_init( &sha1 );
6713
6714 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6715 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Paul Bakker1ef83d62012-04-11 12:09:53 +00006717 /*
6718 * TLSv1:
6719 * hash = PRF( master, finished_label,
6720 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6721 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006724 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6725 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006726#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006729 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6730 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006731#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006734 ? "client finished"
6735 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006736
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006737 mbedtls_md5_finish_ret( &md5, padbuf );
6738 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006739
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006740 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006741 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006744
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006745 mbedtls_md5_free( &md5 );
6746 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006747
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006748 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6755#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006756static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006758{
6759 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006760 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006761 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006762 unsigned char padbuf[32];
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006765 if( !session )
6766 session = ssl->session;
6767
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006768 mbedtls_sha256_init( &sha256 );
6769
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006771
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006772 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
6774 /*
6775 * TLSv1.2:
6776 * hash = PRF( master, finished_label,
6777 * Hash( handshake ) )[0.11]
6778 */
6779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#if !defined(MBEDTLS_SHA256_ALT)
6781 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006782 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006783#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006786 ? "client finished"
6787 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006788
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006789 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006790
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006791 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006792 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006795
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006796 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006797
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006798 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006805static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006807{
6808 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006809 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006810 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006811 unsigned char padbuf[48];
6812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006814 if( !session )
6815 session = ssl->session;
6816
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006817 mbedtls_sha512_init( &sha512 );
6818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006820
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006821 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006822
6823 /*
6824 * TLSv1.2:
6825 * hash = PRF( master, finished_label,
6826 * Hash( handshake ) )[0.11]
6827 */
6828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006830 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6831 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006832#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006835 ? "client finished"
6836 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006837
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006838 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006839
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006840 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006841 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006844
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006845 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006846
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006847 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#endif /* MBEDTLS_SHA512_C */
6852#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006857
6858 /*
6859 * Free our handshake params
6860 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006861 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006863 ssl->handshake = NULL;
6864
6865 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006866 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006867 */
6868 if( ssl->transform )
6869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 mbedtls_ssl_transform_free( ssl->transform );
6871 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006872 }
6873 ssl->transform = ssl->transform_negotiate;
6874 ssl->transform_negotiate = NULL;
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006877}
6878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006880{
6881 int resume = ssl->handshake->resume;
6882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SSL_RENEGOTIATION)
6886 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006889 ssl->renego_records_seen = 0;
6890 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006891#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006892
6893 /*
6894 * Free the previous session and switch in the current one
6895 */
Paul Bakker0a597072012-09-25 21:55:46 +00006896 if( ssl->session )
6897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006899 /* RFC 7366 3.1: keep the EtM state */
6900 ssl->session_negotiate->encrypt_then_mac =
6901 ssl->session->encrypt_then_mac;
6902#endif
6903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 mbedtls_ssl_session_free( ssl->session );
6905 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006906 }
6907 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006908 ssl->session_negotiate = NULL;
6909
Paul Bakker0a597072012-09-25 21:55:46 +00006910 /*
6911 * Add cache entry
6912 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006913 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006914 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006915 resume == 0 )
6916 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006917 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006919 }
Paul Bakker0a597072012-09-25 21:55:46 +00006920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006923 ssl->handshake->flight != NULL )
6924 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006925 /* Cancel handshake timer */
6926 ssl_set_timer( ssl, 0 );
6927
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006928 /* Keep last flight around in case we need to resend it:
6929 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006931 }
6932 else
6933#endif
6934 ssl_handshake_wrapup_free_hs_transform( ssl );
6935
Paul Bakker48916f92012-09-16 19:57:18 +00006936 ssl->state++;
6937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006939}
6940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006942{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006943 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006947 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006948
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006949 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006950
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006951 /*
6952 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6953 * may define some other value. Currently (early 2016), no defined
6954 * ciphersuite does this (and this is unlikely to change as activity has
6955 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006960 ssl->verify_data_len = hash_len;
6961 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006962#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006963
Paul Bakker5121ce52009-01-03 21:22:43 +00006964 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6966 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006967
6968 /*
6969 * In case of session resuming, invert the client and server
6970 * ChangeCipherSpec messages order.
6971 */
Paul Bakker0a597072012-09-25 21:55:46 +00006972 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006981#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 }
6983 else
6984 ssl->state++;
6985
Paul Bakker48916f92012-09-16 19:57:18 +00006986 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006987 * Switch to our negotiated transform and session parameters for outbound
6988 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006994 {
6995 unsigned char i;
6996
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006997 /* Remember current epoch settings for resending */
6998 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006999 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007000
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007001 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007002 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007003
7004 /* Increment epoch */
7005 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007006 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007007 break;
7008
7009 /* The loop goes to its end iff the counter is wrapping */
7010 if( i == 0 )
7011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7013 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007014 }
7015 }
7016 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007018 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007020 ssl->transform_out = ssl->transform_negotiate;
7021 ssl->session_out = ssl->session_negotiate;
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7024 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7029 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007030 }
7031 }
7032#endif
7033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007037#endif
7038
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007039 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007040 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 return( ret );
7043 }
7044
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007045#if defined(MBEDTLS_SSL_PROTO_DTLS)
7046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7047 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7048 {
7049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7050 return( ret );
7051 }
7052#endif
7053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007055
7056 return( 0 );
7057}
7058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007060#define SSL_MAX_HASH_LEN 36
7061#else
7062#define SSL_MAX_HASH_LEN 12
7063#endif
7064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066{
Paul Bakker23986e52011-04-24 08:57:21 +00007067 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007068 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007069 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007073 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007074
Hanno Becker327c93b2018-08-15 13:56:18 +01007075 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078 return( ret );
7079 }
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007084 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7085 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087 }
7088
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007089 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#if defined(MBEDTLS_SSL_PROTO_SSL3)
7091 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007092 hash_len = 36;
7093 else
7094#endif
7095 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7098 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007101 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7102 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 }
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007107 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007110 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7111 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 }
7114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007116 ssl->verify_data_len = hash_len;
7117 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007118#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007119
Paul Bakker0a597072012-09-25 21:55:46 +00007120 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007123 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 }
7131 else
7132 ssl->state++;
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007137#endif
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007140
7141 return( 0 );
7142}
7143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7149 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7150 mbedtls_md5_init( &handshake->fin_md5 );
7151 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007152 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7153 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7156#if defined(MBEDTLS_SHA256_C)
7157 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007158 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SHA512_C)
7161 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007162 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007165
7166 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007167
7168#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7169 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7170 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7171#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_DHM_C)
7174 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_ECDH_C)
7177 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007178#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007179#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007180 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007181#if defined(MBEDTLS_SSL_CLI_C)
7182 handshake->ecjpake_cache = NULL;
7183 handshake->ecjpake_cache_len = 0;
7184#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007185#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007186
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007187#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007188 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007189#endif
7190
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007191#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7192 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7193#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007194}
7195
Hanno Becker611a83b2018-01-03 14:27:32 +00007196void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7201 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007202
Hanno Becker92231322018-01-03 15:32:51 +00007203#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 mbedtls_md_init( &transform->md_ctx_enc );
7205 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007206#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007207}
7208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007212}
7213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007215{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007216 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007217 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007219 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007221 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007222 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007223
7224 /*
7225 * Either the pointers are now NULL or cleared properly and can be freed.
7226 * Now allocate missing structures.
7227 */
7228 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007229 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007230 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007231 }
Paul Bakker48916f92012-09-16 19:57:18 +00007232
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007233 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007234 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007235 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007236 }
Paul Bakker48916f92012-09-16 19:57:18 +00007237
Paul Bakker82788fb2014-10-20 13:59:19 +02007238 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007239 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007240 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007241 }
Paul Bakker48916f92012-09-16 19:57:18 +00007242
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007243 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007244 if( ssl->handshake == NULL ||
7245 ssl->transform_negotiate == NULL ||
7246 ssl->session_negotiate == NULL )
7247 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 mbedtls_free( ssl->handshake );
7251 mbedtls_free( ssl->transform_negotiate );
7252 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007253
7254 ssl->handshake = NULL;
7255 ssl->transform_negotiate = NULL;
7256 ssl->session_negotiate = NULL;
7257
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007258 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007259 }
7260
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007261 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007263 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007264 ssl_handshake_params_init( ssl->handshake );
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7268 {
7269 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007270
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7272 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7273 else
7274 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007275
7276 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007277 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007278#endif
7279
Paul Bakker48916f92012-09-16 19:57:18 +00007280 return( 0 );
7281}
7282
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007283#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007284/* Dummy cookie callbacks for defaults */
7285static int ssl_cookie_write_dummy( void *ctx,
7286 unsigned char **p, unsigned char *end,
7287 const unsigned char *cli_id, size_t cli_id_len )
7288{
7289 ((void) ctx);
7290 ((void) p);
7291 ((void) end);
7292 ((void) cli_id);
7293 ((void) cli_id_len);
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007296}
7297
7298static int ssl_cookie_check_dummy( void *ctx,
7299 const unsigned char *cookie, size_t cookie_len,
7300 const unsigned char *cli_id, size_t cli_id_len )
7301{
7302 ((void) ctx);
7303 ((void) cookie);
7304 ((void) cookie_len);
7305 ((void) cli_id);
7306 ((void) cli_id_len);
7307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007309}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007310#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007311
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007312/* Once ssl->out_hdr as the address of the beginning of the
7313 * next outgoing record is set, deduce the other pointers.
7314 *
7315 * Note: For TLS, we save the implicit record sequence number
7316 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7317 * and the caller has to make sure there's space for this.
7318 */
7319
7320static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7321 mbedtls_ssl_transform *transform )
7322{
7323#if defined(MBEDTLS_SSL_PROTO_DTLS)
7324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7325 {
7326 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007327#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007328 ssl->out_cid = ssl->out_ctr + 8;
7329 ssl->out_len = ssl->out_cid;
7330 if( transform != NULL )
7331 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007332#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007333 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007334#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007335 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007336 }
7337 else
7338#endif
7339 {
7340 ssl->out_ctr = ssl->out_hdr - 8;
7341 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007342#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007343 ssl->out_cid = ssl->out_len;
7344#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007345 ssl->out_iv = ssl->out_hdr + 5;
7346 }
7347
7348 /* Adjust out_msg to make space for explicit IV, if used. */
7349 if( transform != NULL &&
7350 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7351 {
7352 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7353 }
7354 else
7355 ssl->out_msg = ssl->out_iv;
7356}
7357
7358/* Once ssl->in_hdr as the address of the beginning of the
7359 * next incoming record is set, deduce the other pointers.
7360 *
7361 * Note: For TLS, we save the implicit record sequence number
7362 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7363 * and the caller has to make sure there's space for this.
7364 */
7365
Hanno Beckerf5970a02019-05-08 09:38:41 +01007366static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007367{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007368 /* This function sets the pointers to match the case
7369 * of unprotected TLS/DTLS records, with both ssl->in_iv
7370 * and ssl->in_msg pointing to the beginning of the record
7371 * content.
7372 *
7373 * When decrypting a protected record, ssl->in_msg
7374 * will be shifted to point to the beginning of the
7375 * record plaintext.
7376 */
7377
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007378#if defined(MBEDTLS_SSL_PROTO_DTLS)
7379 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7380 {
Hanno Becker70e79282019-05-03 14:34:53 +01007381 /* This sets the header pointers to match records
7382 * without CID. When we receive a record containing
7383 * a CID, the fields are shifted accordingly in
7384 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007385 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007386#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007387 ssl->in_cid = ssl->in_ctr + 8;
7388 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007389#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007390 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007391#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007392 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007393 }
7394 else
7395#endif
7396 {
7397 ssl->in_ctr = ssl->in_hdr - 8;
7398 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007399#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007400 ssl->in_cid = ssl->in_len;
7401#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007402 ssl->in_iv = ssl->in_hdr + 5;
7403 }
7404
Hanno Beckerf5970a02019-05-08 09:38:41 +01007405 /* This will be adjusted at record decryption time. */
7406 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007407}
7408
Paul Bakker5121ce52009-01-03 21:22:43 +00007409/*
7410 * Initialize an SSL context
7411 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007412void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7413{
7414 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7415}
7416
7417/*
7418 * Setup an SSL context
7419 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007420
7421static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7422{
7423 /* Set the incoming and outgoing record pointers. */
7424#if defined(MBEDTLS_SSL_PROTO_DTLS)
7425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7426 {
7427 ssl->out_hdr = ssl->out_buf;
7428 ssl->in_hdr = ssl->in_buf;
7429 }
7430 else
7431#endif /* MBEDTLS_SSL_PROTO_DTLS */
7432 {
7433 ssl->out_hdr = ssl->out_buf + 8;
7434 ssl->in_hdr = ssl->in_buf + 8;
7435 }
7436
7437 /* Derive other internal pointers. */
7438 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007439 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007440}
7441
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007442int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007443 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007444{
Paul Bakker48916f92012-09-16 19:57:18 +00007445 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007446
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007447 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007448
7449 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007450 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007451 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007452
7453 /* Set to NULL in case of an error condition */
7454 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007455
Angus Grattond8213d02016-05-25 20:56:48 +10007456 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7457 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 {
Angus Grattond8213d02016-05-25 20:56:48 +10007459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007460 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007461 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007462 }
7463
7464 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7465 if( ssl->out_buf == NULL )
7466 {
7467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007468 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007469 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007470 }
7471
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007472 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007473
Paul Bakker48916f92012-09-16 19:57:18 +00007474 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007475 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007476
7477 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007478
7479error:
7480 mbedtls_free( ssl->in_buf );
7481 mbedtls_free( ssl->out_buf );
7482
7483 ssl->conf = NULL;
7484
7485 ssl->in_buf = NULL;
7486 ssl->out_buf = NULL;
7487
7488 ssl->in_hdr = NULL;
7489 ssl->in_ctr = NULL;
7490 ssl->in_len = NULL;
7491 ssl->in_iv = NULL;
7492 ssl->in_msg = NULL;
7493
7494 ssl->out_hdr = NULL;
7495 ssl->out_ctr = NULL;
7496 ssl->out_len = NULL;
7497 ssl->out_iv = NULL;
7498 ssl->out_msg = NULL;
7499
k-stachowiak9f7798e2018-07-31 16:52:32 +02007500 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007501}
7502
7503/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007504 * Reset an initialized and used SSL context for re-use while retaining
7505 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007506 *
7507 * If partial is non-zero, keep data in the input buffer and client ID.
7508 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007509 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007510static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007511{
Paul Bakker48916f92012-09-16 19:57:18 +00007512 int ret;
7513
Hanno Becker7e772132018-08-10 12:38:21 +01007514#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7515 !defined(MBEDTLS_SSL_SRV_C)
7516 ((void) partial);
7517#endif
7518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007520
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007521 /* Cancel any possibly running timer */
7522 ssl_set_timer( ssl, 0 );
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SSL_RENEGOTIATION)
7525 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007526 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007527
7528 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7530 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007533
Paul Bakker7eb013f2011-10-06 12:37:39 +00007534 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007535 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007536
7537 ssl->in_msgtype = 0;
7538 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007540 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007541 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007544 ssl_dtls_replay_reset( ssl );
7545#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007546
7547 ssl->in_hslen = 0;
7548 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007549
7550 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007551
7552 ssl->out_msgtype = 0;
7553 ssl->out_msglen = 0;
7554 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7556 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007557 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007558#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007559
Hanno Becker19859472018-08-06 09:40:20 +01007560 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7561
Paul Bakker48916f92012-09-16 19:57:18 +00007562 ssl->transform_in = NULL;
7563 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007564
Hanno Becker78640902018-08-13 16:35:15 +01007565 ssl->session_in = NULL;
7566 ssl->session_out = NULL;
7567
Angus Grattond8213d02016-05-25 20:56:48 +10007568 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007569
7570#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007571 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007572#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7573 {
7574 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007575 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007576 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7579 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7582 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7585 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007586 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007587 }
7588#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007589
Paul Bakker48916f92012-09-16 19:57:18 +00007590 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 mbedtls_ssl_transform_free( ssl->transform );
7593 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007594 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007595 }
Paul Bakker48916f92012-09-16 19:57:18 +00007596
Paul Bakkerc0463502013-02-14 11:19:38 +01007597 if( ssl->session )
7598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 mbedtls_ssl_session_free( ssl->session );
7600 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007601 ssl->session = NULL;
7602 }
7603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007605 ssl->alpn_chosen = NULL;
7606#endif
7607
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007608#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007609#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007610 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007611#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007612 {
7613 mbedtls_free( ssl->cli_id );
7614 ssl->cli_id = NULL;
7615 ssl->cli_id_len = 0;
7616 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007617#endif
7618
Paul Bakker48916f92012-09-16 19:57:18 +00007619 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7620 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007621
7622 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007623}
7624
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007625/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007626 * Reset an initialized and used SSL context for re-use while retaining
7627 * all application-set variables, function pointers and data.
7628 */
7629int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7630{
7631 return( ssl_session_reset_int( ssl, 0 ) );
7632}
7633
7634/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007635 * SSL set accessors
7636 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007637void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007638{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007639 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007640}
7641
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007642void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007644 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007645}
7646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007648void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007650 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007651}
7652#endif
7653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007655void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007657 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007658}
7659#endif
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007662
Hanno Becker1841b0a2018-08-24 11:13:57 +01007663void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7664 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007665{
7666 ssl->disable_datagram_packing = !allow_packing;
7667}
7668
7669void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7670 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007672 conf->hs_timeout_min = min;
7673 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007674}
7675#endif
7676
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007677void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007679 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007680}
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007683void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007684 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007685 void *p_vrfy )
7686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007687 conf->f_vrfy = f_vrfy;
7688 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007691
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007692void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007693 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007694 void *p_rng )
7695{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007696 conf->f_rng = f_rng;
7697 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007698}
7699
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007700void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007701 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007702 void *p_dbg )
7703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007704 conf->f_dbg = f_dbg;
7705 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007706}
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007709 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007710 mbedtls_ssl_send_t *f_send,
7711 mbedtls_ssl_recv_t *f_recv,
7712 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007713{
7714 ssl->p_bio = p_bio;
7715 ssl->f_send = f_send;
7716 ssl->f_recv = f_recv;
7717 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007718}
7719
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007720#if defined(MBEDTLS_SSL_PROTO_DTLS)
7721void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7722{
7723 ssl->mtu = mtu;
7724}
7725#endif
7726
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007727void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007728{
7729 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007730}
7731
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007732void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7733 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007734 mbedtls_ssl_set_timer_t *f_set_timer,
7735 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007736{
7737 ssl->p_timer = p_timer;
7738 ssl->f_set_timer = f_set_timer;
7739 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007740
7741 /* Make sure we start with no timer running */
7742 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007743}
7744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007746void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007747 void *p_cache,
7748 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7749 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007750{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007751 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007752 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007753 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_SSL_CLI_C)
7758int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007759{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007760 int ret;
7761
7762 if( ssl == NULL ||
7763 session == NULL ||
7764 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007765 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007768 }
7769
7770 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7771 return( ret );
7772
Paul Bakker0a597072012-09-25 21:55:46 +00007773 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007774
7775 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007778
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007779void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007780 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007781{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007782 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7783 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7784 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7785 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007786}
7787
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007788void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007789 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007790 int major, int minor )
7791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007793 return;
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007796 return;
7797
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007798 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007799}
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007802void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007803 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007804{
7805 conf->cert_profile = profile;
7806}
7807
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007808/* Append a new keycert entry to a (possibly empty) list */
7809static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7810 mbedtls_x509_crt *cert,
7811 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007812{
niisato8ee24222018-06-25 19:05:48 +09007813 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007814
niisato8ee24222018-06-25 19:05:48 +09007815 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7816 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007818
niisato8ee24222018-06-25 19:05:48 +09007819 new_cert->cert = cert;
7820 new_cert->key = key;
7821 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007822
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007823 /* Update head is the list was null, else add to the end */
7824 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007825 {
niisato8ee24222018-06-25 19:05:48 +09007826 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007827 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007828 else
7829 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007830 mbedtls_ssl_key_cert *cur = *head;
7831 while( cur->next != NULL )
7832 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007833 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007834 }
7835
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007836 return( 0 );
7837}
7838
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007839int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007840 mbedtls_x509_crt *own_cert,
7841 mbedtls_pk_context *pk_key )
7842{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007843 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007844}
7845
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007846void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007847 mbedtls_x509_crt *ca_chain,
7848 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007850 conf->ca_chain = ca_chain;
7851 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007854
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007855#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7856int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7857 mbedtls_x509_crt *own_cert,
7858 mbedtls_pk_context *pk_key )
7859{
7860 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7861 own_cert, pk_key ) );
7862}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007863
7864void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7865 mbedtls_x509_crt *ca_chain,
7866 mbedtls_x509_crl *ca_crl )
7867{
7868 ssl->handshake->sni_ca_chain = ca_chain;
7869 ssl->handshake->sni_ca_crl = ca_crl;
7870}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007871
7872void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7873 int authmode )
7874{
7875 ssl->handshake->sni_authmode = authmode;
7876}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007877#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7878
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007879#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007880/*
7881 * Set EC J-PAKE password for current handshake
7882 */
7883int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7884 const unsigned char *pw,
7885 size_t pw_len )
7886{
7887 mbedtls_ecjpake_role role;
7888
Janos Follath8eb64132016-06-03 15:40:57 +01007889 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7891
7892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7893 role = MBEDTLS_ECJPAKE_SERVER;
7894 else
7895 role = MBEDTLS_ECJPAKE_CLIENT;
7896
7897 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7898 role,
7899 MBEDTLS_MD_SHA256,
7900 MBEDTLS_ECP_DP_SECP256R1,
7901 pw, pw_len ) );
7902}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007903#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007906int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007907 const unsigned char *psk, size_t psk_len,
7908 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007909{
Paul Bakker6db455e2013-09-18 17:29:31 +02007910 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007915
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007916 /* Identity len will be encoded on two bytes */
7917 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007918 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007919 {
7920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7921 }
7922
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007923 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007924 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007925 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007926
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007927 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007928 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007929 conf->psk_len = 0;
7930 }
7931 if( conf->psk_identity != NULL )
7932 {
7933 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007934 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007935 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007936 }
7937
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007938 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7939 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007940 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007941 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007942 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007943 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007944 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007945 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007946 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007947
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007948 conf->psk_len = psk_len;
7949 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007950
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007951 memcpy( conf->psk, psk, conf->psk_len );
7952 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007953
7954 return( 0 );
7955}
7956
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007957int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7958 const unsigned char *psk, size_t psk_len )
7959{
7960 if( psk == NULL || ssl->handshake == NULL )
7961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7962
7963 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7965
7966 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007967 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007968 mbedtls_platform_zeroize( ssl->handshake->psk,
7969 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007970 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007971 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007972 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007973
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007974 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007975 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007976
7977 ssl->handshake->psk_len = psk_len;
7978 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7979
7980 return( 0 );
7981}
7982
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007983void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007985 size_t),
7986 void *p_psk )
7987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007988 conf->f_psk = f_psk;
7989 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007992
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007993#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007994
7995#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007996int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007997{
7998 int ret;
7999
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008000 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8001 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8002 {
8003 mbedtls_mpi_free( &conf->dhm_P );
8004 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008005 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008007
8008 return( 0 );
8009}
Hanno Becker470a8c42017-10-04 15:28:46 +01008010#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008011
Hanno Beckera90658f2017-10-04 15:29:08 +01008012int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8013 const unsigned char *dhm_P, size_t P_len,
8014 const unsigned char *dhm_G, size_t G_len )
8015{
8016 int ret;
8017
8018 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8019 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8020 {
8021 mbedtls_mpi_free( &conf->dhm_P );
8022 mbedtls_mpi_free( &conf->dhm_G );
8023 return( ret );
8024 }
8025
8026 return( 0 );
8027}
Paul Bakker5121ce52009-01-03 21:22:43 +00008028
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008029int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008030{
8031 int ret;
8032
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008033 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8034 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8035 {
8036 mbedtls_mpi_free( &conf->dhm_P );
8037 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008038 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008039 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008040
8041 return( 0 );
8042}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008043#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008044
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008045#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8046/*
8047 * Set the minimum length for Diffie-Hellman parameters
8048 */
8049void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8050 unsigned int bitlen )
8051{
8052 conf->dhm_min_bitlen = bitlen;
8053}
8054#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8055
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008056#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008057/*
8058 * Set allowed/preferred hashes for handshake signatures
8059 */
8060void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8061 const int *hashes )
8062{
8063 conf->sig_hashes = hashes;
8064}
Hanno Becker947194e2017-04-07 13:25:49 +01008065#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008066
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008067#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008068/*
8069 * Set the allowed elliptic curves
8070 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008071void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008072 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008073{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008074 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008075}
Hanno Becker947194e2017-04-07 13:25:49 +01008076#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008077
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008078#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080{
Hanno Becker947194e2017-04-07 13:25:49 +01008081 /* Initialize to suppress unnecessary compiler warning */
8082 size_t hostname_len = 0;
8083
8084 /* Check if new hostname is valid before
8085 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008086 if( hostname != NULL )
8087 {
8088 hostname_len = strlen( hostname );
8089
8090 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8092 }
8093
8094 /* Now it's clear that we will overwrite the old hostname,
8095 * so we can free it safely */
8096
8097 if( ssl->hostname != NULL )
8098 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008099 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008100 mbedtls_free( ssl->hostname );
8101 }
8102
8103 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008104
Paul Bakker5121ce52009-01-03 21:22:43 +00008105 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008106 {
8107 ssl->hostname = NULL;
8108 }
8109 else
8110 {
8111 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008112 if( ssl->hostname == NULL )
8113 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008114
Hanno Becker947194e2017-04-07 13:25:49 +01008115 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008116
Hanno Becker947194e2017-04-07 13:25:49 +01008117 ssl->hostname[hostname_len] = '\0';
8118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008119
8120 return( 0 );
8121}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008122#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008123
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008124#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008125void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008127 const unsigned char *, size_t),
8128 void *p_sni )
8129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008130 conf->f_sni = f_sni;
8131 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008136int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008137{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008138 size_t cur_len, tot_len;
8139 const char **p;
8140
8141 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008142 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8143 * MUST NOT be truncated."
8144 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008145 */
8146 tot_len = 0;
8147 for( p = protos; *p != NULL; p++ )
8148 {
8149 cur_len = strlen( *p );
8150 tot_len += cur_len;
8151
8152 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008154 }
8155
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008156 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008157
8158 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008159}
8160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008162{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008163 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008166
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008167void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008169 conf->max_major_ver = major;
8170 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008171}
8172
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008173void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008174{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008175 conf->min_major_ver = major;
8176 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008177}
8178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008180void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008181{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008182 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008183}
8184#endif
8185
Janos Follath088ce432017-04-10 12:42:31 +01008186#if defined(MBEDTLS_SSL_SRV_C)
8187void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8188 char cert_req_ca_list )
8189{
8190 conf->cert_req_ca_list = cert_req_ca_list;
8191}
8192#endif
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008195void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008196{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008197 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008198}
8199#endif
8200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008202void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008203{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008204 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008205}
8206#endif
8207
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008208#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008209void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008210{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008211 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008212}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008213#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008216int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008219 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008222 }
8223
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008224 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008225
8226 return( 0 );
8227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008231void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008233 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008238void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008239{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008240 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008241}
8242#endif
8243
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008244void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008246 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008247}
8248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008250void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008251{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008252 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008253}
8254
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008255void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008257 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008258}
8259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008260void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008261 const unsigned char period[8] )
8262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008263 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008268#if defined(MBEDTLS_SSL_CLI_C)
8269void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008270{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008271 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008272}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008273#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008274
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008275#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008276void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8277 mbedtls_ssl_ticket_write_t *f_ticket_write,
8278 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8279 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008280{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008281 conf->f_ticket_write = f_ticket_write;
8282 conf->f_ticket_parse = f_ticket_parse;
8283 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008284}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008287
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008288#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8289void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8290 mbedtls_ssl_export_keys_t *f_export_keys,
8291 void *p_export_keys )
8292{
8293 conf->f_export_keys = f_export_keys;
8294 conf->p_export_keys = p_export_keys;
8295}
8296#endif
8297
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008298#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008299void mbedtls_ssl_conf_async_private_cb(
8300 mbedtls_ssl_config *conf,
8301 mbedtls_ssl_async_sign_t *f_async_sign,
8302 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8303 mbedtls_ssl_async_resume_t *f_async_resume,
8304 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008305 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008306{
8307 conf->f_async_sign_start = f_async_sign;
8308 conf->f_async_decrypt_start = f_async_decrypt;
8309 conf->f_async_resume = f_async_resume;
8310 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008311 conf->p_async_config_data = async_config_data;
8312}
8313
Gilles Peskine8f97af72018-04-26 11:46:10 +02008314void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8315{
8316 return( conf->p_async_config_data );
8317}
8318
Gilles Peskine1febfef2018-04-30 11:54:39 +02008319void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008320{
8321 if( ssl->handshake == NULL )
8322 return( NULL );
8323 else
8324 return( ssl->handshake->user_async_ctx );
8325}
8326
Gilles Peskine1febfef2018-04-30 11:54:39 +02008327void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008328 void *ctx )
8329{
8330 if( ssl->handshake != NULL )
8331 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008332}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008333#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008334
Paul Bakker5121ce52009-01-03 21:22:43 +00008335/*
8336 * SSL get accessors
8337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008339{
8340 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8341}
8342
Hanno Becker8b170a02017-10-10 11:51:19 +01008343int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8344{
8345 /*
8346 * Case A: We're currently holding back
8347 * a message for further processing.
8348 */
8349
8350 if( ssl->keep_current_message == 1 )
8351 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008353 return( 1 );
8354 }
8355
8356 /*
8357 * Case B: Further records are pending in the current datagram.
8358 */
8359
8360#if defined(MBEDTLS_SSL_PROTO_DTLS)
8361 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8362 ssl->in_left > ssl->next_record_offset )
8363 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008365 return( 1 );
8366 }
8367#endif /* MBEDTLS_SSL_PROTO_DTLS */
8368
8369 /*
8370 * Case C: A handshake message is being processed.
8371 */
8372
Hanno Becker8b170a02017-10-10 11:51:19 +01008373 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8374 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008376 return( 1 );
8377 }
8378
8379 /*
8380 * Case D: An application data message is being processed
8381 */
8382 if( ssl->in_offt != NULL )
8383 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008385 return( 1 );
8386 }
8387
8388 /*
8389 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008390 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008391 * we implement support for multiple alerts in single records.
8392 */
8393
8394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8395 return( 0 );
8396}
8397
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008398uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008399{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008400 if( ssl->session != NULL )
8401 return( ssl->session->verify_result );
8402
8403 if( ssl->session_negotiate != NULL )
8404 return( ssl->session_negotiate->verify_result );
8405
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008406 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008407}
8408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008410{
Paul Bakker926c8e42013-03-06 10:23:34 +01008411 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008412 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008415}
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008421 {
8422 switch( ssl->minor_ver )
8423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008425 return( "DTLSv1.0" );
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008428 return( "DTLSv1.2" );
8429
8430 default:
8431 return( "unknown (DTLS)" );
8432 }
8433 }
8434#endif
8435
Paul Bakker43ca69c2011-01-15 17:35:19 +00008436 switch( ssl->minor_ver )
8437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008439 return( "SSLv3.0" );
8440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008442 return( "TLSv1.0" );
8443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008445 return( "TLSv1.1" );
8446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008448 return( "TLSv1.2" );
8449
Paul Bakker43ca69c2011-01-15 17:35:19 +00008450 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008451 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008452 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008453}
8454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008456{
Hanno Becker3136ede2018-08-17 15:28:19 +01008457 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008459 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008460
Hanno Becker43395762019-05-03 14:46:38 +01008461 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8462
Hanno Becker78640902018-08-13 16:35:15 +01008463 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008464 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466#if defined(MBEDTLS_ZLIB_SUPPORT)
8467 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8468 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008469#endif
8470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473 case MBEDTLS_MODE_GCM:
8474 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008475 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008477 transform_expansion = transform->minlen;
8478 break;
8479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008481
8482 block_size = mbedtls_cipher_get_block_size(
8483 &transform->cipher_ctx_enc );
8484
Hanno Becker3136ede2018-08-17 15:28:19 +01008485 /* Expansion due to the addition of the MAC. */
8486 transform_expansion += transform->maclen;
8487
8488 /* Expansion due to the addition of CBC padding;
8489 * Theoretically up to 256 bytes, but we never use
8490 * more than the block size of the underlying cipher. */
8491 transform_expansion += block_size;
8492
8493 /* For TLS 1.1 or higher, an explicit IV is added
8494 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008495#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8496 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008497 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008498#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008499
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008500 break;
8501
8502 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008505 }
8506
Hanno Beckera5a2b082019-05-15 14:03:01 +01008507#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008508 if( transform->out_cid_len != 0 )
8509 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008510#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008511
Hanno Becker43395762019-05-03 14:46:38 +01008512 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008513}
8514
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008515#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8516size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8517{
8518 size_t max_len;
8519
8520 /*
8521 * Assume mfl_code is correct since it was checked when set
8522 */
Angus Grattond8213d02016-05-25 20:56:48 +10008523 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008524
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008525 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008526 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008527 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008528 {
Angus Grattond8213d02016-05-25 20:56:48 +10008529 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008530 }
8531
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008532 /* During a handshake, use the value being negotiated */
8533 if( ssl->session_negotiate != NULL &&
8534 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8535 {
8536 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8537 }
8538
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008539 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008540}
8541#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8542
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008543#if defined(MBEDTLS_SSL_PROTO_DTLS)
8544static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8545{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008546 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8547 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8548 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8549 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8550 return ( 0 );
8551
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008552 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8553 return( ssl->mtu );
8554
8555 if( ssl->mtu == 0 )
8556 return( ssl->handshake->mtu );
8557
8558 return( ssl->mtu < ssl->handshake->mtu ?
8559 ssl->mtu : ssl->handshake->mtu );
8560}
8561#endif /* MBEDTLS_SSL_PROTO_DTLS */
8562
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008563int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8564{
8565 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8566
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008567#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8568 !defined(MBEDTLS_SSL_PROTO_DTLS)
8569 (void) ssl;
8570#endif
8571
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008572#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8573 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8574
8575 if( max_len > mfl )
8576 max_len = mfl;
8577#endif
8578
8579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008580 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008581 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008582 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008583 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8584 const size_t overhead = (size_t) ret;
8585
8586 if( ret < 0 )
8587 return( ret );
8588
8589 if( mtu <= overhead )
8590 {
8591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8592 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8593 }
8594
8595 if( max_len > mtu - overhead )
8596 max_len = mtu - overhead;
8597 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008598#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008599
Hanno Becker0defedb2018-08-10 12:35:02 +01008600#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8601 !defined(MBEDTLS_SSL_PROTO_DTLS)
8602 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008603#endif
8604
8605 return( (int) max_len );
8606}
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608#if defined(MBEDTLS_X509_CRT_PARSE_C)
8609const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008610{
8611 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008612 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008613
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008614 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_CLI_C)
8619int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008620{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008621 if( ssl == NULL ||
8622 dst == NULL ||
8623 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008624 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008627 }
8628
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008629 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008632
Paul Bakker5121ce52009-01-03 21:22:43 +00008633/*
Paul Bakker1961b702013-01-25 14:49:24 +01008634 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008639
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008640 if( ssl == NULL || ssl->conf == NULL )
8641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008644 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008646#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008648 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008650#endif
8651
Paul Bakker1961b702013-01-25 14:49:24 +01008652 return( ret );
8653}
8654
8655/*
8656 * Perform the SSL handshake
8657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008659{
8660 int ret = 0;
8661
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008662 if( ssl == NULL || ssl->conf == NULL )
8663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008670
8671 if( ret != 0 )
8672 break;
8673 }
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008676
8677 return( ret );
8678}
8679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680#if defined(MBEDTLS_SSL_RENEGOTIATION)
8681#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008682/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008683 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008686{
8687 int ret;
8688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008690
8691 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8693 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008694
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008695 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008696 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008698 return( ret );
8699 }
8700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008702
8703 return( 0 );
8704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008706
8707/*
8708 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 * - any side: calling mbedtls_ssl_renegotiate(),
8710 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8711 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008712 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008713 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008717{
8718 int ret;
8719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008721
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008722 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8723 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008724
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008725 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8726 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008728 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008730 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008731 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008732 ssl->handshake->out_msg_seq = 1;
8733 else
8734 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008735 }
8736#endif
8737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8739 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008744 return( ret );
8745 }
8746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008748
8749 return( 0 );
8750}
8751
8752/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008753 * Renegotiate current connection on client,
8754 * or request renegotiation on server
8755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008759
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008760 if( ssl == NULL || ssl->conf == NULL )
8761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008764 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008765 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008771
8772 /* Did we already try/start sending HelloRequest? */
8773 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008775
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008776 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008781 /*
8782 * On client, either start the renegotiation process or,
8783 * if already in progress, continue the handshake
8784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008789
8790 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008793 return( ret );
8794 }
8795 }
8796 else
8797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008801 return( ret );
8802 }
8803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008805
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008806 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008807}
8808
8809/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008810 * Check record counters and renegotiate if they're above the limit.
8811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008813{
Andres AG2196c7f2016-12-15 17:01:16 +00008814 size_t ep_len = ssl_ep_len( ssl );
8815 int in_ctr_cmp;
8816 int out_ctr_cmp;
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8819 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008820 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008821 {
8822 return( 0 );
8823 }
8824
Andres AG2196c7f2016-12-15 17:01:16 +00008825 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8826 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008827 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008828 ssl->conf->renego_period + ep_len, 8 - ep_len );
8829
8830 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008831 {
8832 return( 0 );
8833 }
8834
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008839
8840/*
8841 * Receive application data decrypted from the SSL layer
8842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008845 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008846 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008847
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008848 if( ssl == NULL || ssl->conf == NULL )
8849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008854 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008857 return( ret );
8858
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008859 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008861 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008862 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008863 return( ret );
8864 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008865 }
8866#endif
8867
Hanno Becker4a810fb2017-05-24 16:27:30 +01008868 /*
8869 * Check if renegotiation is necessary and/or handshake is
8870 * in process. If yes, perform/continue, and fall through
8871 * if an unexpected packet is received while the client
8872 * is waiting for the ServerHello.
8873 *
8874 * (There is no equivalent to the last condition on
8875 * the server-side as it is not treated as within
8876 * a handshake while waiting for the ClientHello
8877 * after a renegotiation request.)
8878 */
8879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008881 ret = ssl_check_ctr_renegotiate( ssl );
8882 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8883 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008886 return( ret );
8887 }
8888#endif
8889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008893 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8894 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008897 return( ret );
8898 }
8899 }
8900
Hanno Beckere41158b2017-10-23 13:30:32 +01008901 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008902 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008903 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008904 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008905 if( ssl->f_get_timer != NULL &&
8906 ssl->f_get_timer( ssl->p_timer ) == -1 )
8907 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008908 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008909 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008910
Hanno Becker327c93b2018-08-15 13:56:18 +01008911 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008912 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008913 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8914 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008915
Hanno Becker4a810fb2017-05-24 16:27:30 +01008916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8917 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008918 }
8919
8920 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008922 {
8923 /*
8924 * OpenSSL sends empty messages to randomize the IV
8925 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008926 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008929 return( 0 );
8930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008932 return( ret );
8933 }
8934 }
8935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008939
Hanno Becker4a810fb2017-05-24 16:27:30 +01008940 /*
8941 * - For client-side, expect SERVER_HELLO_REQUEST.
8942 * - For server-side, expect CLIENT_HELLO.
8943 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8944 */
8945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008949 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008952
8953 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008956 {
8957 continue;
8958 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008961 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008962#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008963
Hanno Becker4a810fb2017-05-24 16:27:30 +01008964#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008969
8970 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008973 {
8974 continue;
8975 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008978 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008979#endif /* MBEDTLS_SSL_SRV_C */
8980
Hanno Becker21df7f92017-10-17 11:03:26 +01008981#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008982 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008983 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8984 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8985 ssl->conf->allow_legacy_renegotiation ==
8986 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8987 {
8988 /*
8989 * Accept renegotiation request
8990 */
Paul Bakker48916f92012-09-16 19:57:18 +00008991
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008992 /* DTLS clients need to know renego is server-initiated */
8993#if defined(MBEDTLS_SSL_PROTO_DTLS)
8994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8995 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8996 {
8997 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8998 }
8999#endif
9000 ret = ssl_start_renegotiation( ssl );
9001 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9002 ret != 0 )
9003 {
9004 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9005 return( ret );
9006 }
9007 }
9008 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009009#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009010 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009011 /*
9012 * Refuse renegotiation
9013 */
9014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017#if defined(MBEDTLS_SSL_PROTO_SSL3)
9018 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009019 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009020 /* SSLv3 does not have a "no_renegotiation" warning, so
9021 we send a fatal alert and abort the connection. */
9022 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9023 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9024 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009025 }
9026 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9028#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9029 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9030 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9033 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9034 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009035 {
9036 return( ret );
9037 }
Paul Bakker48916f92012-09-16 19:57:18 +00009038 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009039 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9041 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009045 }
Paul Bakker48916f92012-09-16 19:57:18 +00009046 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009047
Hanno Becker90333da2017-10-10 11:27:13 +01009048 /* At this point, we don't know whether the renegotiation has been
9049 * completed or not. The cases to consider are the following:
9050 * 1) The renegotiation is complete. In this case, no new record
9051 * has been read yet.
9052 * 2) The renegotiation is incomplete because the client received
9053 * an application data record while awaiting the ServerHello.
9054 * 3) The renegotiation is incomplete because the client received
9055 * a non-handshake, non-application data message while awaiting
9056 * the ServerHello.
9057 * In each of these case, looping will be the proper action:
9058 * - For 1), the next iteration will read a new record and check
9059 * if it's application data.
9060 * - For 2), the loop condition isn't satisfied as application data
9061 * is present, hence continue is the same as break
9062 * - For 3), the loop condition is satisfied and read_record
9063 * will re-deliver the message that was held back by the client
9064 * when expecting the ServerHello.
9065 */
9066 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009067 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009068#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009070 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009071 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009072 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009073 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009076 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009078 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009079 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9084 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009087 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009088 }
9089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9093 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009094 }
9095
9096 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009097
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009098 /* We're going to return something now, cancel timer,
9099 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009101 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009102
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009103#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009104 /* If we requested renego but received AppData, resend HelloRequest.
9105 * Do it now, after setting in_offt, to avoid taking this branch
9106 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009108 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009110 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009111 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009114 return( ret );
9115 }
9116 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009117#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009118#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009119 }
9120
9121 n = ( len < ssl->in_msglen )
9122 ? len : ssl->in_msglen;
9123
9124 memcpy( buf, ssl->in_offt, n );
9125 ssl->in_msglen -= n;
9126
9127 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009128 {
9129 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009130 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009131 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009132 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009133 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009134 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009135 /* more data available */
9136 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009140
Paul Bakker23986e52011-04-24 08:57:21 +00009141 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009142}
9143
9144/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009145 * Send application data to be encrypted by the SSL layer, taking care of max
9146 * fragment length and buffer size.
9147 *
9148 * According to RFC 5246 Section 6.2.1:
9149 *
9150 * Zero-length fragments of Application data MAY be sent as they are
9151 * potentially useful as a traffic analysis countermeasure.
9152 *
9153 * Therefore, it is possible that the input message length is 0 and the
9154 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009155 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009156static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009157 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009158{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009159 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9160 const size_t max_len = (size_t) ret;
9161
9162 if( ret < 0 )
9163 {
9164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9165 return( ret );
9166 }
9167
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009168 if( len > max_len )
9169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009171 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009174 "maximum fragment length: %d > %d",
9175 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009177 }
9178 else
9179#endif
9180 len = max_len;
9181 }
Paul Bakker887bd502011-06-08 13:10:54 +00009182
Paul Bakker5121ce52009-01-03 21:22:43 +00009183 if( ssl->out_left != 0 )
9184 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009185 /*
9186 * The user has previously tried to send the data and
9187 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9188 * written. In this case, we expect the high-level write function
9189 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009194 return( ret );
9195 }
9196 }
Paul Bakker887bd502011-06-08 13:10:54 +00009197 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009198 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009199 /*
9200 * The user is trying to send a message the first time, so we need to
9201 * copy the data into the internal buffers and setup the data structure
9202 * to keep track of partial writes
9203 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009204 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009206 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009207
Hanno Becker67bc7c32018-08-06 11:33:50 +01009208 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009211 return( ret );
9212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009213 }
9214
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009215 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009216}
9217
9218/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009219 * Write application data, doing 1/n-1 splitting if necessary.
9220 *
9221 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009222 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009223 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009226static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009227 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009228{
9229 int ret;
9230
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009231 if( ssl->conf->cbc_record_splitting ==
9232 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009233 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9235 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9236 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009237 {
9238 return( ssl_write_real( ssl, buf, len ) );
9239 }
9240
9241 if( ssl->split_done == 0 )
9242 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009243 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009244 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009245 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009246 }
9247
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009248 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9249 return( ret );
9250 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009251
9252 return( ret + 1 );
9253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009255
9256/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009257 * Write application data (public-facing wrapper)
9258 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009259int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009260{
9261 int ret;
9262
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009264
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009265 if( ssl == NULL || ssl->conf == NULL )
9266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9267
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009268#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009269 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9270 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009271 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009272 return( ret );
9273 }
9274#endif
9275
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009276 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009277 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009278 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009279 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009281 return( ret );
9282 }
9283 }
9284
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009285#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009286 ret = ssl_write_split( ssl, buf, len );
9287#else
9288 ret = ssl_write_real( ssl, buf, len );
9289#endif
9290
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009292
9293 return( ret );
9294}
9295
9296/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009297 * Notify the peer that the connection is being closed
9298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009300{
9301 int ret;
9302
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009303 if( ssl == NULL || ssl->conf == NULL )
9304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009307
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009308 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9314 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9315 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009318 return( ret );
9319 }
9320 }
9321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009323
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009324 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009325}
9326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009328{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009329 if( transform == NULL )
9330 return;
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009333 deflateEnd( &transform->ctx_deflate );
9334 inflateEnd( &transform->ctx_inflate );
9335#endif
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9338 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009339
Hanno Becker92231322018-01-03 15:32:51 +00009340#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 mbedtls_md_free( &transform->md_ctx_enc );
9342 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009343#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009344
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009345 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009346}
9347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_X509_CRT_PARSE_C)
9349static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009352
9353 while( cur != NULL )
9354 {
9355 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009357 cur = next;
9358 }
9359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009361
Hanno Becker0271f962018-08-16 13:23:47 +01009362#if defined(MBEDTLS_SSL_PROTO_DTLS)
9363
9364static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9365{
9366 unsigned offset;
9367 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9368
9369 if( hs == NULL )
9370 return;
9371
Hanno Becker283f5ef2018-08-24 09:34:47 +01009372 ssl_free_buffered_record( ssl );
9373
Hanno Becker0271f962018-08-16 13:23:47 +01009374 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009375 ssl_buffering_free_slot( ssl, offset );
9376}
9377
9378static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9379 uint8_t slot )
9380{
9381 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9382 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009383
9384 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9385 return;
9386
Hanno Beckere605b192018-08-21 15:59:07 +01009387 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009388 {
Hanno Beckere605b192018-08-21 15:59:07 +01009389 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009390 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009391 mbedtls_free( hs_buf->data );
9392 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009393 }
9394}
9395
9396#endif /* MBEDTLS_SSL_PROTO_DTLS */
9397
Gilles Peskine9b562d52018-04-25 20:32:43 +02009398void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009399{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009400 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9401
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009402 if( handshake == NULL )
9403 return;
9404
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009405#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9406 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9407 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009408 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009409 handshake->async_in_progress = 0;
9410 }
9411#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9412
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009413#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9414 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9415 mbedtls_md5_free( &handshake->fin_md5 );
9416 mbedtls_sha1_free( &handshake->fin_sha1 );
9417#endif
9418#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9419#if defined(MBEDTLS_SHA256_C)
9420 mbedtls_sha256_free( &handshake->fin_sha256 );
9421#endif
9422#if defined(MBEDTLS_SHA512_C)
9423 mbedtls_sha512_free( &handshake->fin_sha512 );
9424#endif
9425#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#if defined(MBEDTLS_DHM_C)
9428 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430#if defined(MBEDTLS_ECDH_C)
9431 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009432#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009433#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009434 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009435#if defined(MBEDTLS_SSL_CLI_C)
9436 mbedtls_free( handshake->ecjpake_cache );
9437 handshake->ecjpake_cache = NULL;
9438 handshake->ecjpake_cache_len = 0;
9439#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009440#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009441
Janos Follath4ae5c292016-02-10 11:27:43 +00009442#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9443 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009444 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009446#endif
9447
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009448#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9449 if( handshake->psk != NULL )
9450 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009451 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009452 mbedtls_free( handshake->psk );
9453 }
9454#endif
9455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9457 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009458 /*
9459 * Free only the linked list wrapper, not the keys themselves
9460 * since the belong to the SNI callback
9461 */
9462 if( handshake->sni_key_cert != NULL )
9463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009465
9466 while( cur != NULL )
9467 {
9468 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009470 cur = next;
9471 }
9472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009474
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009475#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009476 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009477#endif
9478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479#if defined(MBEDTLS_SSL_PROTO_DTLS)
9480 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009481 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009482 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009483#endif
9484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009485 mbedtls_platform_zeroize( handshake,
9486 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009487}
9488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009490{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009491 if( session == NULL )
9492 return;
9493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009495 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 mbedtls_x509_crt_free( session->peer_cert );
9498 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009499 }
Paul Bakkered27a042013-04-18 22:46:23 +02009500#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009501
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009502#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009504#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009505
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009506 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009507}
9508
Paul Bakker5121ce52009-01-03 21:22:43 +00009509/*
9510 * Free an SSL context
9511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009513{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009514 if( ssl == NULL )
9515 return;
9516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009518
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009519 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009520 {
Angus Grattond8213d02016-05-25 20:56:48 +10009521 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009523 }
9524
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009525 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009526 {
Angus Grattond8213d02016-05-25 20:56:48 +10009527 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009529 }
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009532 if( ssl->compress_buf != NULL )
9533 {
Angus Grattond8213d02016-05-25 20:56:48 +10009534 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009536 }
9537#endif
9538
Paul Bakker48916f92012-09-16 19:57:18 +00009539 if( ssl->transform )
9540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 mbedtls_ssl_transform_free( ssl->transform );
9542 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009543 }
9544
9545 if( ssl->handshake )
9546 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009547 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9549 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 mbedtls_free( ssl->handshake );
9552 mbedtls_free( ssl->transform_negotiate );
9553 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009554 }
9555
Paul Bakkerc0463502013-02-14 11:19:38 +01009556 if( ssl->session )
9557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 mbedtls_ssl_session_free( ssl->session );
9559 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009560 }
9561
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009562#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009563 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009564 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009565 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009567 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009568#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9571 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9574 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009575 }
9576#endif
9577
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009578#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009580#endif
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009583
Paul Bakker86f04f42013-02-14 11:20:09 +01009584 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009585 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009586}
9587
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009588/*
9589 * Initialze mbedtls_ssl_config
9590 */
9591void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9592{
9593 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9594}
9595
Simon Butcherc97b6972015-12-27 23:48:17 +00009596#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009597static int ssl_preset_default_hashes[] = {
9598#if defined(MBEDTLS_SHA512_C)
9599 MBEDTLS_MD_SHA512,
9600 MBEDTLS_MD_SHA384,
9601#endif
9602#if defined(MBEDTLS_SHA256_C)
9603 MBEDTLS_MD_SHA256,
9604 MBEDTLS_MD_SHA224,
9605#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009606#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009607 MBEDTLS_MD_SHA1,
9608#endif
9609 MBEDTLS_MD_NONE
9610};
Simon Butcherc97b6972015-12-27 23:48:17 +00009611#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009612
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009613static int ssl_preset_suiteb_ciphersuites[] = {
9614 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9615 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9616 0
9617};
9618
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009619#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009620static int ssl_preset_suiteb_hashes[] = {
9621 MBEDTLS_MD_SHA256,
9622 MBEDTLS_MD_SHA384,
9623 MBEDTLS_MD_NONE
9624};
9625#endif
9626
9627#if defined(MBEDTLS_ECP_C)
9628static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9629 MBEDTLS_ECP_DP_SECP256R1,
9630 MBEDTLS_ECP_DP_SECP384R1,
9631 MBEDTLS_ECP_DP_NONE
9632};
9633#endif
9634
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009635/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009636 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009637 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009638int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009639 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009640{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009641#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009642 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009643#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009644
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009645 /* Use the functions here so that they are covered in tests,
9646 * but otherwise access member directly for efficiency */
9647 mbedtls_ssl_conf_endpoint( conf, endpoint );
9648 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009649
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009650 /*
9651 * Things that are common to all presets
9652 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009653#if defined(MBEDTLS_SSL_CLI_C)
9654 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9655 {
9656 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9657#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9658 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9659#endif
9660 }
9661#endif
9662
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009663#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009664 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009665#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009666
9667#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9668 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9669#endif
9670
9671#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9672 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9673#endif
9674
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009675#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9676 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9677#endif
9678
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009679#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009680 conf->f_cookie_write = ssl_cookie_write_dummy;
9681 conf->f_cookie_check = ssl_cookie_check_dummy;
9682#endif
9683
9684#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9685 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9686#endif
9687
Janos Follath088ce432017-04-10 12:42:31 +01009688#if defined(MBEDTLS_SSL_SRV_C)
9689 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9690#endif
9691
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009692#if defined(MBEDTLS_SSL_PROTO_DTLS)
9693 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9694 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9695#endif
9696
9697#if defined(MBEDTLS_SSL_RENEGOTIATION)
9698 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009699 memset( conf->renego_period, 0x00, 2 );
9700 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009701#endif
9702
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009703#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9704 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9705 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009706 const unsigned char dhm_p[] =
9707 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9708 const unsigned char dhm_g[] =
9709 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9710
Hanno Beckera90658f2017-10-04 15:29:08 +01009711 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9712 dhm_p, sizeof( dhm_p ),
9713 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009714 {
9715 return( ret );
9716 }
9717 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009718#endif
9719
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009720 /*
9721 * Preset-specific defaults
9722 */
9723 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009724 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009725 /*
9726 * NSA Suite B
9727 */
9728 case MBEDTLS_SSL_PRESET_SUITEB:
9729 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9730 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9731 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9732 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9733
9734 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9735 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9736 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9737 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9738 ssl_preset_suiteb_ciphersuites;
9739
9740#if defined(MBEDTLS_X509_CRT_PARSE_C)
9741 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009742#endif
9743
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009744#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009745 conf->sig_hashes = ssl_preset_suiteb_hashes;
9746#endif
9747
9748#if defined(MBEDTLS_ECP_C)
9749 conf->curve_list = ssl_preset_suiteb_curves;
9750#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009751 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009752
9753 /*
9754 * Default
9755 */
9756 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009757 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9758 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9759 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9760 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9761 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9762 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9763 MBEDTLS_SSL_MIN_MINOR_VERSION :
9764 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009765 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9766 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9767
9768#if defined(MBEDTLS_SSL_PROTO_DTLS)
9769 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9770 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9771#endif
9772
9773 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9774 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9775 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9776 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9777 mbedtls_ssl_list_ciphersuites();
9778
9779#if defined(MBEDTLS_X509_CRT_PARSE_C)
9780 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9781#endif
9782
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009783#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009784 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009785#endif
9786
9787#if defined(MBEDTLS_ECP_C)
9788 conf->curve_list = mbedtls_ecp_grp_id_list();
9789#endif
9790
9791#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9792 conf->dhm_min_bitlen = 1024;
9793#endif
9794 }
9795
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009796 return( 0 );
9797}
9798
9799/*
9800 * Free mbedtls_ssl_config
9801 */
9802void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9803{
9804#if defined(MBEDTLS_DHM_C)
9805 mbedtls_mpi_free( &conf->dhm_P );
9806 mbedtls_mpi_free( &conf->dhm_G );
9807#endif
9808
9809#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9810 if( conf->psk != NULL )
9811 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009812 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009813 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009814 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009815 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009816 }
9817
9818 if( conf->psk_identity != NULL )
9819 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009820 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009821 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009822 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009823 conf->psk_identity_len = 0;
9824 }
9825#endif
9826
9827#if defined(MBEDTLS_X509_CRT_PARSE_C)
9828 ssl_key_cert_free( conf->key_cert );
9829#endif
9830
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009831 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009832}
9833
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009834#if defined(MBEDTLS_PK_C) && \
9835 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009836/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#if defined(MBEDTLS_RSA_C)
9842 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9843 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845#if defined(MBEDTLS_ECDSA_C)
9846 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9847 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009850}
9851
Hanno Becker7e5437a2017-04-28 17:15:26 +01009852unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9853{
9854 switch( type ) {
9855 case MBEDTLS_PK_RSA:
9856 return( MBEDTLS_SSL_SIG_RSA );
9857 case MBEDTLS_PK_ECDSA:
9858 case MBEDTLS_PK_ECKEY:
9859 return( MBEDTLS_SSL_SIG_ECDSA );
9860 default:
9861 return( MBEDTLS_SSL_SIG_ANON );
9862 }
9863}
9864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009866{
9867 switch( sig )
9868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869#if defined(MBEDTLS_RSA_C)
9870 case MBEDTLS_SSL_SIG_RSA:
9871 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873#if defined(MBEDTLS_ECDSA_C)
9874 case MBEDTLS_SSL_SIG_ECDSA:
9875 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009876#endif
9877 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009879 }
9880}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009881#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009882
Hanno Becker7e5437a2017-04-28 17:15:26 +01009883#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9884 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9885
9886/* Find an entry in a signature-hash set matching a given hash algorithm. */
9887mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9888 mbedtls_pk_type_t sig_alg )
9889{
9890 switch( sig_alg )
9891 {
9892 case MBEDTLS_PK_RSA:
9893 return( set->rsa );
9894 case MBEDTLS_PK_ECDSA:
9895 return( set->ecdsa );
9896 default:
9897 return( MBEDTLS_MD_NONE );
9898 }
9899}
9900
9901/* Add a signature-hash-pair to a signature-hash set */
9902void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9903 mbedtls_pk_type_t sig_alg,
9904 mbedtls_md_type_t md_alg )
9905{
9906 switch( sig_alg )
9907 {
9908 case MBEDTLS_PK_RSA:
9909 if( set->rsa == MBEDTLS_MD_NONE )
9910 set->rsa = md_alg;
9911 break;
9912
9913 case MBEDTLS_PK_ECDSA:
9914 if( set->ecdsa == MBEDTLS_MD_NONE )
9915 set->ecdsa = md_alg;
9916 break;
9917
9918 default:
9919 break;
9920 }
9921}
9922
9923/* Allow exactly one hash algorithm for each signature. */
9924void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9925 mbedtls_md_type_t md_alg )
9926{
9927 set->rsa = md_alg;
9928 set->ecdsa = md_alg;
9929}
9930
9931#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9932 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9933
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009934/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009935 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009938{
9939 switch( hash )
9940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941#if defined(MBEDTLS_MD5_C)
9942 case MBEDTLS_SSL_HASH_MD5:
9943 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945#if defined(MBEDTLS_SHA1_C)
9946 case MBEDTLS_SSL_HASH_SHA1:
9947 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949#if defined(MBEDTLS_SHA256_C)
9950 case MBEDTLS_SSL_HASH_SHA224:
9951 return( MBEDTLS_MD_SHA224 );
9952 case MBEDTLS_SSL_HASH_SHA256:
9953 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_SHA512_C)
9956 case MBEDTLS_SSL_HASH_SHA384:
9957 return( MBEDTLS_MD_SHA384 );
9958 case MBEDTLS_SSL_HASH_SHA512:
9959 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009960#endif
9961 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009963 }
9964}
9965
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009966/*
9967 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9968 */
9969unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9970{
9971 switch( md )
9972 {
9973#if defined(MBEDTLS_MD5_C)
9974 case MBEDTLS_MD_MD5:
9975 return( MBEDTLS_SSL_HASH_MD5 );
9976#endif
9977#if defined(MBEDTLS_SHA1_C)
9978 case MBEDTLS_MD_SHA1:
9979 return( MBEDTLS_SSL_HASH_SHA1 );
9980#endif
9981#if defined(MBEDTLS_SHA256_C)
9982 case MBEDTLS_MD_SHA224:
9983 return( MBEDTLS_SSL_HASH_SHA224 );
9984 case MBEDTLS_MD_SHA256:
9985 return( MBEDTLS_SSL_HASH_SHA256 );
9986#endif
9987#if defined(MBEDTLS_SHA512_C)
9988 case MBEDTLS_MD_SHA384:
9989 return( MBEDTLS_SSL_HASH_SHA384 );
9990 case MBEDTLS_MD_SHA512:
9991 return( MBEDTLS_SSL_HASH_SHA512 );
9992#endif
9993 default:
9994 return( MBEDTLS_SSL_HASH_NONE );
9995 }
9996}
9997
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009998#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009999/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010000 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010001 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010002 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010003int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010006
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010007 if( ssl->conf->curve_list == NULL )
10008 return( -1 );
10009
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010010 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010011 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010012 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010013
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010014 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010015}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010016#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010017
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010018#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010019/*
10020 * Check if a hash proposed by the peer is in our list.
10021 * Return 0 if we're willing to use it, -1 otherwise.
10022 */
10023int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10024 mbedtls_md_type_t md )
10025{
10026 const int *cur;
10027
10028 if( ssl->conf->sig_hashes == NULL )
10029 return( -1 );
10030
10031 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10032 if( *cur == (int) md )
10033 return( 0 );
10034
10035 return( -1 );
10036}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010037#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039#if defined(MBEDTLS_X509_CRT_PARSE_C)
10040int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10041 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010042 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010043 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010044{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010045 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010047 int usage = 0;
10048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010050 const char *ext_oid;
10051 size_t ext_len;
10052#endif
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10055 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010056 ((void) cert);
10057 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010058 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010059#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10062 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010063 {
10064 /* Server part of the key exchange */
10065 switch( ciphersuite->key_exchange )
10066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 case MBEDTLS_KEY_EXCHANGE_RSA:
10068 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010069 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010070 break;
10071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10073 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10074 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10075 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010076 break;
10077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10079 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010080 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010081 break;
10082
10083 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 case MBEDTLS_KEY_EXCHANGE_NONE:
10085 case MBEDTLS_KEY_EXCHANGE_PSK:
10086 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10087 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010088 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010089 usage = 0;
10090 }
10091 }
10092 else
10093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10095 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010096 }
10097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010099 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010100 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010101 ret = -1;
10102 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010103#else
10104 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10108 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10111 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010112 }
10113 else
10114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10116 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010117 }
10118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010120 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010121 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010122 ret = -1;
10123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010125
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010126 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010129
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010130/*
10131 * Convert version numbers to/from wire format
10132 * and, for DTLS, to/from TLS equivalent.
10133 *
10134 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010135 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010136 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10137 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010140 unsigned char ver[2] )
10141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142#if defined(MBEDTLS_SSL_PROTO_DTLS)
10143 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010146 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10147
10148 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10149 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10150 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010151 else
10152#else
10153 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010154#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010155 {
10156 ver[0] = (unsigned char) major;
10157 ver[1] = (unsigned char) minor;
10158 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010159}
10160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010162 const unsigned char ver[2] )
10163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164#if defined(MBEDTLS_SSL_PROTO_DTLS)
10165 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010166 {
10167 *major = 255 - ver[0] + 2;
10168 *minor = 255 - ver[1] + 1;
10169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010171 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10172 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010173 else
10174#else
10175 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010176#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010177 {
10178 *major = ver[0];
10179 *minor = ver[1];
10180 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010181}
10182
Simon Butcher99000142016-10-13 17:21:01 +010010183int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10184{
10185#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10186 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10187 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10188
10189 switch( md )
10190 {
10191#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10192#if defined(MBEDTLS_MD5_C)
10193 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010194 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010195#endif
10196#if defined(MBEDTLS_SHA1_C)
10197 case MBEDTLS_SSL_HASH_SHA1:
10198 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10199 break;
10200#endif
10201#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10202#if defined(MBEDTLS_SHA512_C)
10203 case MBEDTLS_SSL_HASH_SHA384:
10204 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10205 break;
10206#endif
10207#if defined(MBEDTLS_SHA256_C)
10208 case MBEDTLS_SSL_HASH_SHA256:
10209 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10210 break;
10211#endif
10212 default:
10213 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10214 }
10215
10216 return 0;
10217#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10218 (void) ssl;
10219 (void) md;
10220
10221 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10223}
10224
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010225#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10226 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10227int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10228 unsigned char *output,
10229 unsigned char *data, size_t data_len )
10230{
10231 int ret = 0;
10232 mbedtls_md5_context mbedtls_md5;
10233 mbedtls_sha1_context mbedtls_sha1;
10234
10235 mbedtls_md5_init( &mbedtls_md5 );
10236 mbedtls_sha1_init( &mbedtls_sha1 );
10237
10238 /*
10239 * digitally-signed struct {
10240 * opaque md5_hash[16];
10241 * opaque sha_hash[20];
10242 * };
10243 *
10244 * md5_hash
10245 * MD5(ClientHello.random + ServerHello.random
10246 * + ServerParams);
10247 * sha_hash
10248 * SHA(ClientHello.random + ServerHello.random
10249 * + ServerParams);
10250 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010251 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010252 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010254 goto exit;
10255 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010256 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010257 ssl->handshake->randbytes, 64 ) ) != 0 )
10258 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010260 goto exit;
10261 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010262 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010263 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010265 goto exit;
10266 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010267 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010268 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010269 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010270 goto exit;
10271 }
10272
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010273 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010274 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010276 goto exit;
10277 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010278 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010279 ssl->handshake->randbytes, 64 ) ) != 0 )
10280 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010282 goto exit;
10283 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010284 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010285 data_len ) ) != 0 )
10286 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010288 goto exit;
10289 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010290 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010291 output + 16 ) ) != 0 )
10292 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010294 goto exit;
10295 }
10296
10297exit:
10298 mbedtls_md5_free( &mbedtls_md5 );
10299 mbedtls_sha1_free( &mbedtls_sha1 );
10300
10301 if( ret != 0 )
10302 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10303 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10304
10305 return( ret );
10306
10307}
10308#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10309 MBEDTLS_SSL_PROTO_TLS1_1 */
10310
10311#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10312 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10313int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010314 unsigned char *hash, size_t *hashlen,
10315 unsigned char *data, size_t data_len,
10316 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010317{
10318 int ret = 0;
10319 mbedtls_md_context_t ctx;
10320 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010321 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010322
10323 mbedtls_md_init( &ctx );
10324
10325 /*
10326 * digitally-signed struct {
10327 * opaque client_random[32];
10328 * opaque server_random[32];
10329 * ServerDHParams params;
10330 * };
10331 */
10332 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10333 {
10334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10335 goto exit;
10336 }
10337 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10338 {
10339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10340 goto exit;
10341 }
10342 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10343 {
10344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10345 goto exit;
10346 }
10347 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10348 {
10349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10350 goto exit;
10351 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010352 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010353 {
10354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10355 goto exit;
10356 }
10357
10358exit:
10359 mbedtls_md_free( &ctx );
10360
10361 if( ret != 0 )
10362 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10363 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10364
10365 return( ret );
10366}
10367#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10368 MBEDTLS_SSL_PROTO_TLS1_2 */
10369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370#endif /* MBEDTLS_SSL_TLS_C */