blob: 9c1a39f123f64e333d89061504e064d02c703c2c [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)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200679static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680static 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)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200684static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685static 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 );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200691static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692static 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 );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200697static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static 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é-Gonnard0bcfbc32019-05-06 13:32:17 +0200702/* Type for the TLS PRF */
703typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
704 const unsigned char *, size_t,
705 unsigned char *, size_t);
706
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200707/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200708 * Populate a transform structure with session keys and all the other
709 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200710 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200711 * Parameters:
712 * - [in/out]: transform: structure to populate
713 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200714 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200715 * - [in] ciphersuite
716 * - [in] master
717 * - [in] encrypt_then_mac
718 * - [in] trunc_hmac
719 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200720 * - [in] tls_prf: pointer to PRF to use for key derivation
721 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200722 * - [in] minor_ver: SSL/TLS minor version
723 * - [in] endpoint: client or server
724 * - [in] ssl: optionally used for:
725 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
726 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
727 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200728 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200729static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200730 int ciphersuite,
731 const unsigned char master[48],
732#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
733 int encrypt_then_mac,
734#endif
735#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
736 int trunc_hmac,
737#endif
738#if defined(MBEDTLS_ZLIB_SUPPORT)
739 int compression,
740#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200741 ssl_tls_prf_t tls_prf,
742 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200743 int minor_ver,
744 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200745 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000746{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200747 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 unsigned char keyblk[256];
749 unsigned char *key1;
750 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100751 unsigned char *mac_enc;
752 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000753 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200754 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000755 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000756 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 const mbedtls_cipher_info_t *cipher_info;
758 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100759
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200760#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
761 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
762 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200763 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200764 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000765#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000766
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200767 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200769 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200771 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200773 /*
774 * Get various info structures
775 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200776 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200777 if( ciphersuite_info == NULL )
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200780 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
782 }
783
Hanno Becker8759e162017-12-27 21:34:08 +0000784 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100785 if( cipher_info == NULL )
786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000788 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100790 }
791
Hanno Becker8759e162017-12-27 21:34:08 +0000792 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100793 if( md_info == NULL )
794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000796 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100798 }
799
Hanno Beckera5a2b082019-05-15 14:03:01 +0100800#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100801 /* Copy own and peer's CID if the use of the CID
802 * extension has been negotiated. */
803 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
804 {
805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100806
Hanno Becker4932f9f2019-05-03 15:23:51 +0100807 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100808 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100809 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100810 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100811
812 transform->out_cid_len = ssl->handshake->peer_cid_len;
813 memcpy( transform->out_cid, ssl->handshake->peer_cid,
814 ssl->handshake->peer_cid_len );
815 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
816 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100817 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100818#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100819
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200821 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000822 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200823 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100824 if( ret != 0 )
825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100827 return( ret );
828 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200831 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200832 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200833 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 /*
837 * Determine the appropriate key, IV and MAC length.
838 */
Paul Bakker68884e32013-01-07 18:20:04 +0100839
Hanno Beckere7f2df02017-12-27 08:17:40 +0000840 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200841
Hanno Beckerf1229442018-01-03 15:32:31 +0000842#if defined(MBEDTLS_GCM_C) || \
843 defined(MBEDTLS_CCM_C) || \
844 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200846 cipher_info->mode == MBEDTLS_MODE_CCM ||
847 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 {
Hanno Becker8759e162017-12-27 21:34:08 +0000849 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200850
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200851 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000852 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000853 transform->taglen =
854 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200855
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200856 /* All modes haves 96-bit IVs;
857 * GCM and CCM has 4 implicit and 8 explicit bytes
858 * ChachaPoly has all 12 bytes implicit
859 */
Paul Bakker68884e32013-01-07 18:20:04 +0100860 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200861 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
862 transform->fixed_ivlen = 12;
863 else
864 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200865
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200866 /* Minimum length of encrypted record */
867 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000868 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100869 }
870 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000871#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
872#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
873 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
874 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100875 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
878 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200881 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100882 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200884 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000885 mac_key_len = mbedtls_md_get_size( md_info );
886 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200889 /*
890 * If HMAC is to be truncated, we shall keep the leftmost bytes,
891 * (rfc 6066 page 13 or rfc 2104 section 4),
892 * so we only need to adjust the length here.
893 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200894 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000897
898#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
899 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000900 * HMAC implementation which also truncates the key
901 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000902 mac_key_len = transform->maclen;
903#endif
904 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906
907 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100908 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200910 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200912 transform->minlen = transform->maclen;
913 else
Paul Bakker68884e32013-01-07 18:20:04 +0100914 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200915 /*
916 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100917 * 1. if EtM is in use: one block plus MAC
918 * otherwise: * first multiple of blocklen greater than maclen
919 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200922 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100923 {
924 transform->minlen = transform->maclen
925 + cipher_info->block_size;
926 }
927 else
928#endif
929 {
930 transform->minlen = transform->maclen
931 + cipher_info->block_size
932 - transform->maclen % cipher_info->block_size;
933 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200936 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
937 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200938 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100939 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200942 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
943 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200944 {
945 transform->minlen += transform->ivlen;
946 }
947 else
948#endif
949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
951 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200952 }
Paul Bakker68884e32013-01-07 18:20:04 +0100953 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000955 else
956#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
957 {
958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
960 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Hanno Beckere7f2df02017-12-27 08:17:40 +0000962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
963 (unsigned) keylen,
964 (unsigned) transform->minlen,
965 (unsigned) transform->ivlen,
966 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
968 /*
969 * Finally setup the cipher contexts, IVs and MAC secrets.
970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200972 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000974 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000975 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Paul Bakker68884e32013-01-07 18:20:04 +0100977 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000978 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000980 /*
981 * This is not used in TLS v1.1.
982 */
Paul Bakker48916f92012-09-16 19:57:18 +0000983 iv_copy_len = ( transform->fixed_ivlen ) ?
984 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000985 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
986 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000987 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 }
989 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_SSL_CLI_C */
991#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200992 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000994 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
Hanno Becker81c7b182017-11-09 18:39:33 +0000997 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100998 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001000 /*
1001 * This is not used in TLS v1.1.
1002 */
Paul Bakker48916f92012-09-16 19:57:18 +00001003 iv_copy_len = ( transform->fixed_ivlen ) ?
1004 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001005 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1006 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001007 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Hanno Becker92231322018-01-03 15:32:51 +00001016#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001018 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001019 {
Hanno Becker92231322018-01-03 15:32:51 +00001020 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001024 }
1025
Hanno Becker81c7b182017-11-09 18:39:33 +00001026 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1027 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001028 }
1029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1031#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1032 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001033 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001034 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001035 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1036 For AEAD-based ciphersuites, there is nothing to do here. */
1037 if( mac_key_len != 0 )
1038 {
1039 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1040 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1041 }
Paul Bakker68884e32013-01-07 18:20:04 +01001042 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001043 else
1044#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001048 }
Hanno Becker92231322018-01-03 15:32:51 +00001049#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1052 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001053 {
1054 int ret = 0;
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001057
Hanno Beckere7f2df02017-12-27 08:17:40 +00001058 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001059 transform->iv_enc, transform->iv_dec,
1060 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001061 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001062 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1065 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001066 }
1067 }
Hanno Becker92231322018-01-03 15:32:51 +00001068#else
1069 ((void) mac_dec);
1070 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001072
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001073#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1074 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001075 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001076 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001077 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001078 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001079 iv_copy_len );
1080 }
1081#endif
1082
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001083 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001084 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001087 return( ret );
1088 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001089
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001090 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001091 cipher_info ) ) != 0 )
1092 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001094 return( ret );
1095 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001098 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001102 return( ret );
1103 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001106 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001110 return( ret );
1111 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_CIPHER_MODE_CBC)
1114 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1117 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001120 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001121 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1124 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001132 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001134 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001136 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001139
Paul Bakker48916f92012-09-16 19:57:18 +00001140 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1141 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001142
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001143 if( deflateInit( &transform->ctx_deflate,
1144 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001145 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1148 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001149 }
1150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 return( 0 );
1154}
1155
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001156/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001157 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001158 *
1159 * Inputs:
1160 * - SSL/TLS minor version
1161 * - hash associated with the ciphersuite (only used by TLS 1.2)
1162 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001163 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001164 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1165 */
1166static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1167 int minor_ver,
1168 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001169{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001170#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1171 (void) hash;
1172#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001173
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001174#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001175 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001176 {
1177 handshake->tls_prf = ssl3_prf;
1178 handshake->calc_verify = ssl_calc_verify_ssl;
1179 handshake->calc_finished = ssl_calc_finished_ssl;
1180 }
1181 else
1182#endif
1183#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001184 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001185 {
1186 handshake->tls_prf = tls1_prf;
1187 handshake->calc_verify = ssl_calc_verify_tls;
1188 handshake->calc_finished = ssl_calc_finished_tls;
1189 }
1190 else
1191#endif
1192#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1193#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001194 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1195 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001196 {
1197 handshake->tls_prf = tls_prf_sha384;
1198 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1199 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1200 }
1201 else
1202#endif
1203#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001204 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001205 {
1206 handshake->tls_prf = tls_prf_sha256;
1207 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1208 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1209 }
1210 else
1211#endif
1212#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1213 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1215 }
1216
1217 return( 0 );
1218}
1219
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001220/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001221 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001222 *
1223 * Parameters:
1224 * [in/out] handshake
1225 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1226 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001227 * [out] master
1228 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001229 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001230static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001231 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001232 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001233{
1234 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001235
1236#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001237 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001238 (void) ssl;
1239#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001240
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001241 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001242 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1244 return( 0 );
1245 }
1246
1247 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1248 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001249
1250#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001251 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001252 {
1253 unsigned char session_hash[48];
1254 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001256 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001257
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001258 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1259 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001260
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001262 "extended master secret",
1263 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001264 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265 }
1266 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001267#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001268 {
1269 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1270 "master secret",
1271 handshake->randbytes, 64,
1272 master, 48 );
1273 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001274 if( ret != 0 )
1275 {
1276 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1277 return( ret );
1278 }
1279
1280 mbedtls_platform_zeroize( handshake->premaster,
1281 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001282
1283 return( 0 );
1284}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001285
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001286int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1287{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001288 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001289 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1290 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001291
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1293
1294 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001295 ret = ssl_set_handshake_prfs( ssl->handshake,
1296 ssl->minor_ver,
1297 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001298 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001299 {
1300 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001301 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001302 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001303
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001304 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001305 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001306 ssl->session_negotiate->master,
1307 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001308 if( ret != 0 )
1309 {
1310 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1311 return( ret );
1312 }
1313
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001314 /* Swap the client and server random values:
1315 * - MS derivation wanted client+server (RFC 5246 8.1)
1316 * - key derivation wants server+client (RFC 5246 6.3) */
1317 {
1318 unsigned char tmp[64];
1319 memcpy( tmp, ssl->handshake->randbytes, 64 );
1320 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1321 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1322 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1323 }
1324
1325 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001326 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001327 ssl->session_negotiate->ciphersuite,
1328 ssl->session_negotiate->master,
1329#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1330 ssl->session_negotiate->encrypt_then_mac,
1331#endif
1332#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1333 ssl->session_negotiate->trunc_hmac,
1334#endif
1335#if defined(MBEDTLS_ZLIB_SUPPORT)
1336 ssl->session_negotiate->compression,
1337#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001338 ssl->handshake->tls_prf,
1339 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001340 ssl->minor_ver,
1341 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001342 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001343 if( ret != 0 )
1344 {
1345 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1346 return( ret );
1347 }
1348
1349 /* We no longer need Server/ClientHello.random values */
1350 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1351 sizeof( ssl->handshake->randbytes ) );
1352
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001353 /* Allocate compression buffer */
1354#if defined(MBEDTLS_ZLIB_SUPPORT)
1355 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1356 ssl->compress_buf == NULL )
1357 {
1358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1359 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1360 if( ssl->compress_buf == NULL )
1361 {
1362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001363 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001364 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1365 }
1366 }
1367#endif
1368
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1370
1371 return( 0 );
1372}
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001375void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1376 unsigned char hash[36],
1377 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001379 mbedtls_md5_context md5;
1380 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 unsigned char pad_1[48];
1382 unsigned char pad_2[48];
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001386 mbedtls_md5_init( &md5 );
1387 mbedtls_sha1_init( &sha1 );
1388
1389 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1390 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Paul Bakker380da532012-04-18 16:10:25 +00001392 memset( pad_1, 0x36, 48 );
1393 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001395 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1396 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1397 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001399 mbedtls_md5_starts_ret( &md5 );
1400 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1401 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1402 mbedtls_md5_update_ret( &md5, hash, 16 );
1403 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001405 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1406 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1407 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001408
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001409 mbedtls_sha1_starts_ret( &sha1 );
1410 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1411 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1412 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1413 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001415 *hlen = 36;
1416
1417 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001420 mbedtls_md5_free( &md5 );
1421 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001422
Paul Bakker380da532012-04-18 16:10:25 +00001423 return;
1424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001428void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1429 unsigned char hash[36],
1430 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001431{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001432 mbedtls_md5_context md5;
1433 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001437 mbedtls_md5_init( &md5 );
1438 mbedtls_sha1_init( &sha1 );
1439
1440 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1441 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001443 mbedtls_md5_finish_ret( &md5, hash );
1444 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001445
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001446 *hlen = 36;
1447
1448 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001450
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001451 mbedtls_md5_free( &md5 );
1452 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001453
Paul Bakker380da532012-04-18 16:10:25 +00001454 return;
1455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1459#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001460void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1461 unsigned char hash[32],
1462 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001463{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001464 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001465
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001466 mbedtls_sha256_init( &sha256 );
1467
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001469
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001470 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001471 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001473 *hlen = 32;
1474
1475 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001477
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001478 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001479
Paul Bakker380da532012-04-18 16:10:25 +00001480 return;
1481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001485void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1486 unsigned char hash[48],
1487 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001488{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001489 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001491 mbedtls_sha512_init( &sha512 );
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001496 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001498 *hlen = 48;
1499
1500 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001503 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001504
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 return;
1506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_SHA512_C */
1508#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1511int 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 +02001512{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001513 unsigned char *p = ssl->handshake->premaster;
1514 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001515 const unsigned char *psk = ssl->conf->psk;
1516 size_t psk_len = ssl->conf->psk_len;
1517
1518 /* If the psk callback was called, use its result */
1519 if( ssl->handshake->psk != NULL )
1520 {
1521 psk = ssl->handshake->psk;
1522 psk_len = ssl->handshake->psk_len;
1523 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001524
1525 /*
1526 * PMS = struct {
1527 * opaque other_secret<0..2^16-1>;
1528 * opaque psk<0..2^16-1>;
1529 * };
1530 * with "other_secret" depending on the particular key exchange
1531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1533 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001535 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001537
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001538 *(p++) = (unsigned char)( psk_len >> 8 );
1539 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001540
1541 if( end < p || (size_t)( end - p ) < psk_len )
1542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1543
1544 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001545 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001546 }
1547 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1549#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1550 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001551 {
1552 /*
1553 * other_secret already set by the ClientKeyExchange message,
1554 * and is 48 bytes long
1555 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001556 if( end - p < 2 )
1557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1558
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001559 *p++ = 0;
1560 *p++ = 48;
1561 p += 48;
1562 }
1563 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1565#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1566 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001568 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001569 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001570
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001571 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001573 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001574 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001577 return( ret );
1578 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001579 *(p++) = (unsigned char)( len >> 8 );
1580 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 p += len;
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001584 }
1585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1587#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1588 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001589 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001590 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001591 size_t zlen;
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001594 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001595 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598 return( ret );
1599 }
1600
1601 *(p++) = (unsigned char)( zlen >> 8 );
1602 *(p++) = (unsigned char)( zlen );
1603 p += zlen;
1604
Janos Follath3fbdada2018-08-15 10:26:53 +01001605 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1606 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001607 }
1608 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 }
1614
1615 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001616 if( end - p < 2 )
1617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001618
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001619 *(p++) = (unsigned char)( psk_len >> 8 );
1620 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001621
1622 if( end < p || (size_t)( end - p ) < psk_len )
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1624
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001625 memcpy( p, psk, psk_len );
1626 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001627
1628 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1629
1630 return( 0 );
1631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001635/*
1636 * SSLv3.0 MAC functions
1637 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001638#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001639static void ssl_mac( mbedtls_md_context_t *md_ctx,
1640 const unsigned char *secret,
1641 const unsigned char *buf, size_t len,
1642 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001643 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001644{
1645 unsigned char header[11];
1646 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001647 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1649 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001650
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001651 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001653 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001654 else
Paul Bakker68884e32013-01-07 18:20:04 +01001655 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 memcpy( header, ctr, 8 );
1658 header[ 8] = (unsigned char) type;
1659 header[ 9] = (unsigned char)( len >> 8 );
1660 header[10] = (unsigned char)( len );
1661
Paul Bakker68884e32013-01-07 18:20:04 +01001662 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 mbedtls_md_starts( md_ctx );
1664 mbedtls_md_update( md_ctx, secret, md_size );
1665 mbedtls_md_update( md_ctx, padding, padlen );
1666 mbedtls_md_update( md_ctx, header, 11 );
1667 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001668 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Paul Bakker68884e32013-01-07 18:20:04 +01001670 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 mbedtls_md_starts( md_ctx );
1672 mbedtls_md_update( md_ctx, secret, md_size );
1673 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674 mbedtls_md_update( md_ctx, out, md_size );
1675 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001678
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001679/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001680 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001681#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001682 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1683 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1684 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1685/* This function makes sure every byte in the memory region is accessed
1686 * (in ascending addresses order) */
1687static void ssl_read_memory( unsigned char *p, size_t len )
1688{
1689 unsigned char acc = 0;
1690 volatile unsigned char force;
1691
1692 for( ; len != 0; p++, len-- )
1693 acc ^= *p;
1694
1695 force = acc;
1696 (void) force;
1697}
1698#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1699
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001700/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001702 */
Hanno Becker3307b532017-12-27 21:37:21 +00001703
Hanno Beckera5a2b082019-05-15 14:03:01 +01001704#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001705/* This functions transforms a DTLS plaintext fragment and a record content
1706 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001707 *
1708 * struct {
1709 * opaque content[DTLSPlaintext.length];
1710 * ContentType real_type;
1711 * uint8 zeros[length_of_padding];
1712 * } DTLSInnerPlaintext;
1713 *
1714 * Input:
1715 * - `content`: The beginning of the buffer holding the
1716 * plaintext to be wrapped.
1717 * - `*content_size`: The length of the plaintext in Bytes.
1718 * - `max_len`: The number of Bytes available starting from
1719 * `content`. This must be `>= *content_size`.
1720 * - `rec_type`: The desired record content type.
1721 *
1722 * Output:
1723 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1724 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1725 *
1726 * Returns:
1727 * - `0` on success.
1728 * - A negative error code if `max_len` didn't offer enough space
1729 * for the expansion.
1730 */
1731static int ssl_cid_build_inner_plaintext( unsigned char *content,
1732 size_t *content_size,
1733 size_t remaining,
1734 uint8_t rec_type )
1735{
1736 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001737 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1738 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1739 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001740
1741 /* Write real content type */
1742 if( remaining == 0 )
1743 return( -1 );
1744 content[ len ] = rec_type;
1745 len++;
1746 remaining--;
1747
1748 if( remaining < pad )
1749 return( -1 );
1750 memset( content + len, 0, pad );
1751 len += pad;
1752 remaining -= pad;
1753
1754 *content_size = len;
1755 return( 0 );
1756}
1757
Hanno Becker7dc25772019-05-20 15:08:01 +01001758/* This function parses a DTLSInnerPlaintext structure.
1759 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001760static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1761 size_t *content_size,
1762 uint8_t *rec_type )
1763{
1764 size_t remaining = *content_size;
1765
1766 /* Determine length of padding by skipping zeroes from the back. */
1767 do
1768 {
1769 if( remaining == 0 )
1770 return( -1 );
1771 remaining--;
1772 } while( content[ remaining ] == 0 );
1773
1774 *content_size = remaining;
1775 *rec_type = content[ remaining ];
1776
1777 return( 0 );
1778}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001779#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001780
Hanno Becker99abf512019-05-20 14:50:53 +01001781/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001782 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001783static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001784 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001785 mbedtls_record *rec )
1786{
Hanno Becker99abf512019-05-20 14:50:53 +01001787 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001788 *
1789 * additional_data = seq_num + TLSCompressed.type +
1790 * TLSCompressed.version + TLSCompressed.length;
1791 *
Hanno Becker99abf512019-05-20 14:50:53 +01001792 * For the CID extension, this is extended as follows
1793 * (quoting draft-ietf-tls-dtls-connection-id-05,
1794 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001795 *
1796 * additional_data = seq_num + DTLSPlaintext.type +
1797 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001798 * cid +
1799 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001800 * length_of_DTLSInnerPlaintext;
1801 */
1802
Hanno Becker3307b532017-12-27 21:37:21 +00001803 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1804 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001805 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001806
Hanno Beckera5a2b082019-05-15 14:03:01 +01001807#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001808 if( rec->cid_len != 0 )
1809 {
1810 memcpy( add_data + 11, rec->cid, rec->cid_len );
1811 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1812 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1813 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1814 *add_data_len = 13 + 1 + rec->cid_len;
1815 }
1816 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001817#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001818 {
1819 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1820 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1821 *add_data_len = 13;
1822 }
Hanno Becker3307b532017-12-27 21:37:21 +00001823}
1824
Hanno Becker611a83b2018-01-03 14:27:32 +00001825int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1826 mbedtls_ssl_transform *transform,
1827 mbedtls_record *rec,
1828 int (*f_rng)(void *, unsigned char *, size_t),
1829 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001832 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001833 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001834 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001835 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001836 size_t post_avail;
1837
1838 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001839#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001840 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001841 ((void) ssl);
1842#endif
1843
1844 /* The PRNG is used for dynamic IV generation that's used
1845 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1846#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1847 ( defined(MBEDTLS_AES_C) || \
1848 defined(MBEDTLS_ARIA_C) || \
1849 defined(MBEDTLS_CAMELLIA_C) ) && \
1850 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1851 ((void) f_rng);
1852 ((void) p_rng);
1853#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Hanno Becker3307b532017-12-27 21:37:21 +00001857 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001858 {
Hanno Becker3307b532017-12-27 21:37:21 +00001859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1860 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1861 }
Hanno Becker505089d2019-05-01 09:45:57 +01001862 if( rec == NULL
1863 || rec->buf == NULL
1864 || rec->buf_len < rec->data_offset
1865 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001866#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001867 || rec->cid_len != 0
1868#endif
1869 )
Hanno Becker3307b532017-12-27 21:37:21 +00001870 {
1871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001873 }
1874
Hanno Becker3307b532017-12-27 21:37:21 +00001875 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001876 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001878 data, rec->data_len );
1879
1880 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1881
1882 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1883 {
1884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1885 (unsigned) rec->data_len,
1886 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1888 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001889
Hanno Beckera5a2b082019-05-15 14:03:01 +01001890#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001891 /*
1892 * Add CID information
1893 */
1894 rec->cid_len = transform->out_cid_len;
1895 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1896 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001897
1898 if( rec->cid_len != 0 )
1899 {
1900 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001901 * Wrap plaintext into DTLSInnerPlaintext structure.
1902 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001903 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001904 * Note that this changes `rec->data_len`, and hence
1905 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001906 */
1907 if( ssl_cid_build_inner_plaintext( data,
1908 &rec->data_len,
1909 post_avail,
1910 rec->type ) != 0 )
1911 {
1912 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1913 }
1914
1915 rec->type = MBEDTLS_SSL_MSG_CID;
1916 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001917#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001918
Hanno Becker92c930f2019-04-29 17:31:37 +01001919 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1920
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001922 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001924#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 if( mode == MBEDTLS_MODE_STREAM ||
1926 ( mode == MBEDTLS_MODE_CBC
1927#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001928 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001929#endif
1930 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 {
Hanno Becker3307b532017-12-27 21:37:21 +00001932 if( post_avail < transform->maclen )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1935 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1936 }
1937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001939 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001940 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001941 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001942 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1943 data, rec->data_len, rec->ctr, rec->type, mac );
1944 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001945 }
1946 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1949 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001950 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001951 {
Hanno Becker992b6872017-11-09 18:57:39 +00001952 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1953
Hanno Beckere83efe62019-04-29 13:52:53 +01001954 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001955
Hanno Becker3307b532017-12-27 21:37:21 +00001956 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001957 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001958 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1959 data, rec->data_len );
1960 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1961 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1962
1963 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001964 }
1965 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001966#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 }
1971
Hanno Becker3307b532017-12-27 21:37:21 +00001972 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1973 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001974
Hanno Becker3307b532017-12-27 21:37:21 +00001975 rec->data_len += transform->maclen;
1976 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001977 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001978 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001979#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001981 /*
1982 * Encrypt
1983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1985 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001987 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001988 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001990 "including %d bytes of padding",
1991 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001992
Hanno Becker3307b532017-12-27 21:37:21 +00001993 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1994 transform->iv_enc, transform->ivlen,
1995 data, rec->data_len,
1996 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001999 return( ret );
2000 }
2001
Hanno Becker3307b532017-12-27 21:37:21 +00002002 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 }
Paul Bakker68884e32013-01-07 18:20:04 +01002008 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002010
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002011#if defined(MBEDTLS_GCM_C) || \
2012 defined(MBEDTLS_CCM_C) || \
2013 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002015 mode == MBEDTLS_MODE_CCM ||
2016 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002017 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002018 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002019 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002020 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002021
Hanno Becker3307b532017-12-27 21:37:21 +00002022 /* Check that there's space for both the authentication tag
2023 * and the explicit IV before and after the record content. */
2024 if( post_avail < transform->taglen ||
2025 rec->data_offset < explicit_iv_len )
2026 {
2027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2028 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2029 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002030
Paul Bakker68884e32013-01-07 18:20:04 +01002031 /*
2032 * Generate IV
2033 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002034 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2035 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002036 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002038 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2039 explicit_iv_len );
2040 /* Prefix record content with explicit IV. */
2041 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 }
2043 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2044 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002045 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046 unsigned char i;
2047
2048 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2049
2050 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002051 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002052 }
2053 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002054 {
2055 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002058 }
2059
Hanno Beckere83efe62019-04-29 13:52:53 +01002060 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002061
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002062 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2063 iv, transform->ivlen );
2064 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002065 data - explicit_iv_len, explicit_iv_len );
2066 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002067 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002069 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002070 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002071
Paul Bakker68884e32013-01-07 18:20:04 +01002072 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002073 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002074 */
Hanno Becker3307b532017-12-27 21:37:21 +00002075
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002076 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002077 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002078 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002079 data, rec->data_len, /* source */
2080 data, &rec->data_len, /* destination */
2081 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002084 return( ret );
2085 }
2086
Hanno Becker3307b532017-12-27 21:37:21 +00002087 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2088 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002089
Hanno Becker3307b532017-12-27 21:37:21 +00002090 rec->data_len += transform->taglen + explicit_iv_len;
2091 rec->data_offset -= explicit_iv_len;
2092 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2097#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002098 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002101 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002102 size_t padlen, i;
2103 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002104
Hanno Becker3307b532017-12-27 21:37:21 +00002105 /* Currently we're always using minimal padding
2106 * (up to 255 bytes would be allowed). */
2107 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2108 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 padlen = 0;
2110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 /* Check there's enough space in the buffer for the padding. */
2112 if( post_avail < padlen + 1 )
2113 {
2114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2115 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2116 }
2117
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002119 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Hanno Becker3307b532017-12-27 21:37:21 +00002121 rec->data_len += padlen + 1;
2122 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002125 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002126 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2127 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002128 */
Hanno Becker3307b532017-12-27 21:37:21 +00002129 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002130 {
Hanno Becker3307b532017-12-27 21:37:21 +00002131 if( f_rng == NULL )
2132 {
2133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2134 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2135 }
2136
2137 if( rec->data_offset < transform->ivlen )
2138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2140 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2141 }
2142
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002143 /*
2144 * Generate IV
2145 */
Hanno Becker3307b532017-12-27 21:37:21 +00002146 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002147 if( ret != 0 )
2148 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149
Hanno Becker3307b532017-12-27 21:37:21 +00002150 memcpy( data - transform->ivlen, transform->iv_enc,
2151 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002157 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002158 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002159 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Hanno Becker3307b532017-12-27 21:37:21 +00002161 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2162 transform->iv_enc,
2163 transform->ivlen,
2164 data, rec->data_len,
2165 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002168 return( ret );
2169 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002170
Hanno Becker3307b532017-12-27 21:37:21 +00002171 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002178 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002179 {
2180 /*
2181 * Save IV in SSL3 and TLS1
2182 */
Hanno Becker3307b532017-12-27 21:37:21 +00002183 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2184 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 }
Hanno Becker3307b532017-12-27 21:37:21 +00002186 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002187#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002188 {
2189 data -= transform->ivlen;
2190 rec->data_offset -= transform->ivlen;
2191 rec->data_len += transform->ivlen;
2192 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002195 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002196 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002197 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2198
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002199 /*
2200 * MAC(MAC_write_key, seq_num +
2201 * TLSCipherText.type +
2202 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002203 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002204 * IV + // except for TLS 1.0
2205 * ENC(content + padding + padding_length));
2206 */
Hanno Becker3307b532017-12-27 21:37:21 +00002207
2208 if( post_avail < transform->maclen)
2209 {
2210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2211 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2212 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002213
Hanno Beckere83efe62019-04-29 13:52:53 +01002214 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002217 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002218 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002219
Hanno Becker3307b532017-12-27 21:37:21 +00002220 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002221 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002222 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2223 data, rec->data_len );
2224 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2225 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002226
Hanno Becker3307b532017-12-27 21:37:21 +00002227 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002228
Hanno Becker3307b532017-12-27 21:37:21 +00002229 rec->data_len += transform->maclen;
2230 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002231 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002235 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002237 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002243 /* Make extra sure authentication was performed, exactly once */
2244 if( auth_done != 1 )
2245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002248 }
2249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
2252 return( 0 );
2253}
2254
Hanno Becker611a83b2018-01-03 14:27:32 +00002255int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2256 mbedtls_ssl_transform *transform,
2257 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002258{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002259 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002261 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002262#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002263 size_t padlen = 0, correct = 1;
2264#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002265 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002266 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002267 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002268
Hanno Becker611a83b2018-01-03 14:27:32 +00002269#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002270 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002271 ((void) ssl);
2272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002275 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002276 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2279 }
2280 if( rec == NULL ||
2281 rec->buf == NULL ||
2282 rec->buf_len < rec->data_offset ||
2283 rec->buf_len - rec->data_offset < rec->data_len )
2284 {
2285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002287 }
2288
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289 data = rec->buf + rec->data_offset;
2290 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Hanno Beckera5a2b082019-05-15 14:03:01 +01002292#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002293 /*
2294 * Match record's CID with incoming CID.
2295 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002296 if( rec->cid_len != transform->in_cid_len ||
2297 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2298 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002299 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002300 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002301#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2304 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002305 {
2306 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002307 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2308 transform->iv_dec,
2309 transform->ivlen,
2310 data, rec->data_len,
2311 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002314 return( ret );
2315 }
2316
Hanno Becker4c6876b2017-12-27 21:28:58 +00002317 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
Paul Bakker68884e32013-01-07 18:20:04 +01002323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325#if defined(MBEDTLS_GCM_C) || \
2326 defined(MBEDTLS_CCM_C) || \
2327 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002329 mode == MBEDTLS_MODE_CCM ||
2330 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002331 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002332 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002333 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002334
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 /*
2336 * Compute and update sizes
2337 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002338 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002341 "+ taglen (%d)", rec->data_len,
2342 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002344 }
Paul Bakker68884e32013-01-07 18:20:04 +01002345
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346 /*
2347 * Prepare IV
2348 */
2349 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2350 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002351 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002353 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002354
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355 }
2356 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2357 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002358 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 unsigned char i;
2360
2361 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2362
2363 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002364 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 }
2366 else
2367 {
2368 /* Reminder if we ever add an AEAD mode with a different size */
2369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2371 }
2372
Hanno Becker4c6876b2017-12-27 21:28:58 +00002373 data += explicit_iv_len;
2374 rec->data_offset += explicit_iv_len;
2375 rec->data_len -= explicit_iv_len + transform->taglen;
2376
Hanno Beckere83efe62019-04-29 13:52:53 +01002377 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002378 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002379 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002380
2381 memcpy( transform->iv_dec + transform->fixed_ivlen,
2382 data - explicit_iv_len, explicit_iv_len );
2383
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002384 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002386 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002387
Paul Bakker68884e32013-01-07 18:20:04 +01002388
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002389 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002390 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002391 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002392 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2393 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002394 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002395 data, rec->data_len,
2396 data, &olen,
2397 data + rec->data_len,
2398 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2403 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002404
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002405 return( ret );
2406 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002407 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002408
Hanno Becker4c6876b2017-12-27 21:28:58 +00002409 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002413 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2417#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002418 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002421 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 /*
Paul Bakker45829992013-01-03 14:52:21 +01002424 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002427 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2428 {
2429 /* The ciphertext is prefixed with the CBC IV. */
2430 minlen += transform->ivlen;
2431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#endif
Paul Bakker45829992013-01-03 14:52:21 +01002433
Hanno Becker4c6876b2017-12-27 21:28:58 +00002434 /* Size considerations:
2435 *
2436 * - The CBC cipher text must not be empty and hence
2437 * at least of size transform->ivlen.
2438 *
2439 * Together with the potential IV-prefix, this explains
2440 * the first of the two checks below.
2441 *
2442 * - The record must contain a MAC, either in plain or
2443 * encrypted, depending on whether Encrypt-then-MAC
2444 * is used or not.
2445 * - If it is, the message contains the IV-prefix,
2446 * the CBC ciphertext, and the MAC.
2447 * - If it is not, the padded plaintext, and hence
2448 * the CBC ciphertext, has at least length maclen + 1
2449 * because there is at least the padding length byte.
2450 *
2451 * As the CBC ciphertext is not empty, both cases give the
2452 * lower bound minlen + maclen + 1 on the record size, which
2453 * we test for in the second check below.
2454 */
2455 if( rec->data_len < minlen + transform->ivlen ||
2456 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002459 "+ 1 ) ( + expl IV )", rec->data_len,
2460 transform->ivlen,
2461 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002463 }
2464
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002465 /*
2466 * Authenticate before decrypt if enabled
2467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002469 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002470 {
Hanno Becker992b6872017-11-09 18:57:39 +00002471 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002474
Hanno Becker4c6876b2017-12-27 21:28:58 +00002475 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2476 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002477
Hanno Beckere83efe62019-04-29 13:52:53 +01002478 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002479
Hanno Beckere83efe62019-04-29 13:52:53 +01002480 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2481 add_data_len );
2482 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2483 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002484 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2485 data, rec->data_len );
2486 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2487 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002488
Hanno Becker4c6876b2017-12-27 21:28:58 +00002489 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2490 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002491 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002492 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
Hanno Becker4c6876b2017-12-27 21:28:58 +00002494 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2495 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002500 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002503
2504 /*
2505 * Check length sanity
2506 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002507 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002510 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002512 }
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002515 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002516 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002517 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002518 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002519 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002520 /* This is safe because data_len >= minlen + maclen + 1 initially,
2521 * and at this point we have at most subtracted maclen (note that
2522 * minlen == transform->ivlen here). */
2523 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002524
Hanno Becker4c6876b2017-12-27 21:28:58 +00002525 data += transform->ivlen;
2526 rec->data_offset += transform->ivlen;
2527 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2532 transform->iv_dec, transform->ivlen,
2533 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002536 return( ret );
2537 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002538
Hanno Becker4c6876b2017-12-27 21:28:58 +00002539 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002543 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002547 {
2548 /*
2549 * Save IV in SSL3 and TLS1
2550 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002551 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2552 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002554#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Hanno Becker4c6876b2017-12-27 21:28:58 +00002556 /* Safe since data_len >= minlen + maclen + 1, so after having
2557 * subtracted at most minlen and maclen up to this point,
2558 * data_len > 0. */
2559 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( auth_done == 1 )
2562 {
2563 correct *= ( rec->data_len >= padlen + 1 );
2564 padlen *= ( rec->data_len >= padlen + 1 );
2565 }
2566 else
Paul Bakker45829992013-01-03 14:52:21 +01002567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002569 if( rec->data_len < transform->maclen + padlen + 1 )
2570 {
2571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2572 rec->data_len,
2573 transform->maclen,
2574 padlen + 1 ) );
2575 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002576#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577
2578 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2579 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002580 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Hanno Becker4c6876b2017-12-27 21:28:58 +00002582 padlen++;
2583
2584 /* Regardless of the validity of the padding,
2585 * we have data_len >= padlen here. */
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_SSL_DEBUG_ALL)
2593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002594 "should be no more than %d",
2595 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002596#endif
Paul Bakker45829992013-01-03 14:52:21 +01002597 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599 }
2600 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2602#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2603 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002606 /* The padding check involves a series of up to 256
2607 * consecutive memory reads at the end of the record
2608 * plaintext buffer. In order to hide the length and
2609 * validity of the padding, always perform exactly
2610 * `min(256,plaintext_len)` reads (but take into account
2611 * only the last `padlen` bytes for the padding check). */
2612 size_t pad_count = 0;
2613 size_t real_count = 0;
2614 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002615
Hanno Becker4c6876b2017-12-27 21:28:58 +00002616 /* Index of first padding byte; it has been ensured above
2617 * that the subtraction is safe. */
2618 size_t const padding_idx = rec->data_len - padlen;
2619 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2620 size_t const start_idx = rec->data_len - num_checks;
2621 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002622
Hanno Becker4c6876b2017-12-27 21:28:58 +00002623 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002624 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002625 real_count |= ( idx >= padding_idx );
2626 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002627 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002631 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002633#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002634 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002636 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2638 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002642 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 /* If the padding was found to be invalid, padlen == 0
2645 * and the subtraction is safe. If the padding was found valid,
2646 * padlen hasn't been changed and the previous assertion
2647 * data_len >= padlen still holds. */
2648 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002650 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002652 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2655 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002658#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002660 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002661#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
2663 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002664 * Authenticate if not done yet.
2665 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002667#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002668 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002669 {
Hanno Becker992b6872017-11-09 18:57:39 +00002670 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002671
Hanno Becker4c6876b2017-12-27 21:28:58 +00002672 /* If the initial value of padlen was such that
2673 * data_len < maclen + padlen + 1, then padlen
2674 * got reset to 1, and the initial check
2675 * data_len >= minlen + maclen + 1
2676 * guarantees that at this point we still
2677 * have at least data_len >= maclen.
2678 *
2679 * If the initial value of padlen was such that
2680 * data_len >= maclen + padlen + 1, then we have
2681 * subtracted either padlen + 1 (if the padding was correct)
2682 * or 0 (if the padding was incorrect) since then,
2683 * hence data_len >= maclen in any case.
2684 */
2685 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Hanno Beckere83efe62019-04-29 13:52:53 +01002687 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002690 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002691 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002692 ssl_mac( &transform->md_ctx_dec,
2693 transform->mac_dec,
2694 data, rec->data_len,
2695 rec->ctr, rec->type,
2696 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 }
2698 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2700#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2701 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002702 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002703 {
2704 /*
2705 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002706 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002707 *
2708 * Known timing attacks:
2709 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2710 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002711 * To compensate for different timings for the MAC calculation
2712 * depending on how much padding was removed (which is determined
2713 * by padlen), process extra_run more blocks through the hash
2714 * function.
2715 *
2716 * The formula in the paper is
2717 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2718 * where L1 is the size of the header plus the decrypted message
2719 * plus CBC padding and L2 is the size of the header plus the
2720 * decrypted message. This is for an underlying hash function
2721 * with 64-byte blocks.
2722 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2723 * correctly. We round down instead of up, so -56 is the correct
2724 * value for our calculations instead of -55.
2725 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002726 * Repeat the formula rather than defining a block_size variable.
2727 * This avoids requiring division by a variable at runtime
2728 * (which would be marginally less efficient and would require
2729 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002730 */
2731 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002732 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002733
2734 /*
2735 * The next two sizes are the minimum and maximum values of
2736 * in_msglen over all padlen values.
2737 *
2738 * They're independent of padlen, since we previously did
2739 * in_msglen -= padlen.
2740 *
2741 * Note that max_len + maclen is never more than the buffer
2742 * length, as we previously did in_msglen -= maclen too.
2743 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002744 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002745 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2746
Hanno Becker4c6876b2017-12-27 21:28:58 +00002747 memset( tmp, 0, sizeof( tmp ) );
2748
2749 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002750 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002751#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2752 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002753 case MBEDTLS_MD_MD5:
2754 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002755 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002757 extra_run =
2758 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2759 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002760 break;
2761#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002762#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002763 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002764 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002765 extra_run =
2766 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2767 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002768 break;
2769#endif
2770 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2773 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002774
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002775 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002776
Hanno Beckere83efe62019-04-29 13:52:53 +01002777 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2778 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002779 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2780 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002781 /* Make sure we access everything even when padlen > 0. This
2782 * makes the synchronisation requirements for just-in-time
2783 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002784 ssl_read_memory( data + rec->data_len, padlen );
2785 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002786
2787 /* Call mbedtls_md_process at least once due to cache attacks
2788 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002789 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002790 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002791
Hanno Becker4c6876b2017-12-27 21:28:58 +00002792 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002793
2794 /* Make sure we access all the memory that could contain the MAC,
2795 * before we check it in the next code block. This makes the
2796 * synchronisation requirements for just-in-time Prime+Probe
2797 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002798 ssl_read_memory( data + min_len,
2799 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002800 }
2801 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2803 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002809#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002810 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2811 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002812#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2815 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#if defined(MBEDTLS_SSL_DEBUG_ALL)
2818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002819#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002820 correct = 0;
2821 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002822 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002823 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002824
2825 /*
2826 * Finally check the correct flag
2827 */
2828 if( correct == 0 )
2829 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002830#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002831
2832 /* Make extra sure authentication was performed, exactly once */
2833 if( auth_done != 1 )
2834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Hanno Beckera5a2b082019-05-15 14:03:01 +01002839#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002840 if( rec->cid_len != 0 )
2841 {
2842 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2843 &rec->type );
2844 if( ret != 0 )
2845 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2846 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002847#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
2851 return( 0 );
2852}
2853
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002854#undef MAC_NONE
2855#undef MAC_PLAINTEXT
2856#undef MAC_CIPHERTEXT
2857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002859/*
2860 * Compression/decompression functions
2861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002863{
2864 int ret;
2865 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002866 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002867 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002868 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002871
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002872 if( len_pre == 0 )
2873 return( 0 );
2874
Paul Bakker2770fbd2012-07-03 13:30:23 +00002875 memcpy( msg_pre, ssl->out_msg, len_pre );
2876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002878 ssl->out_msglen ) );
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881 ssl->out_msg, ssl->out_msglen );
2882
Paul Bakker48916f92012-09-16 19:57:18 +00002883 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2884 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2885 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002886 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887
Paul Bakker48916f92012-09-16 19:57:18 +00002888 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002889 if( ret != Z_OK )
2890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2892 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893 }
2894
Angus Grattond8213d02016-05-25 20:56:48 +10002895 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002896 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 ssl->out_msglen ) );
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->out_msg, ssl->out_msglen );
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905
2906 return( 0 );
2907}
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910{
2911 int ret;
2912 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002913 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002915 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002919 if( len_pre == 0 )
2920 return( 0 );
2921
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922 memcpy( msg_pre, ssl->in_msg, len_pre );
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002925 ssl->in_msglen ) );
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 ssl->in_msg, ssl->in_msglen );
2929
Paul Bakker48916f92012-09-16 19:57:18 +00002930 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2931 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2932 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002933 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002934 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935
Paul Bakker48916f92012-09-16 19:57:18 +00002936 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937 if( ret != Z_OK )
2938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2940 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941 }
2942
Angus Grattond8213d02016-05-25 20:56:48 +10002943 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002944 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 ssl->in_msglen ) );
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 ssl->in_msg, ssl->in_msglen );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953
2954 return( 0 );
2955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2959static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_PROTO_DTLS)
2962static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002963{
2964 /* If renegotiation is not enforced, retransmit until we would reach max
2965 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002966 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002967 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002968 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002969 unsigned char doublings = 1;
2970
2971 while( ratio != 0 )
2972 {
2973 ++doublings;
2974 ratio >>= 1;
2975 }
2976
2977 if( ++ssl->renego_records_seen > doublings )
2978 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002980 return( 0 );
2981 }
2982 }
2983
2984 return( ssl_write_hello_request( ssl ) );
2985}
2986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002988
Paul Bakker5121ce52009-01-03 21:22:43 +00002989/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002990 * Fill the input message buffer by appending data to it.
2991 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002992 *
2993 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2994 * available (from this read and/or a previous one). Otherwise, an error code
2995 * is returned (possibly EOF or WANT_READ).
2996 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002997 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2998 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2999 * since we always read a whole datagram at once.
3000 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003001 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003002 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003005{
Paul Bakker23986e52011-04-24 08:57:21 +00003006 int ret;
3007 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003011 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003014 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003016 }
3017
Angus Grattond8213d02016-05-25 20:56:48 +10003018 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003022 }
3023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003027 uint32_t timeout;
3028
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003029 /* Just to be sure */
3030 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3031 {
3032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3033 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3035 }
3036
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003037 /*
3038 * The point is, we need to always read a full datagram at once, so we
3039 * sometimes read more then requested, and handle the additional data.
3040 * It could be the rest of the current record (while fetching the
3041 * header) and/or some other records in the same datagram.
3042 */
3043
3044 /*
3045 * Move to the next record in the already read datagram if applicable
3046 */
3047 if( ssl->next_record_offset != 0 )
3048 {
3049 if( ssl->in_left < ssl->next_record_offset )
3050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003053 }
3054
3055 ssl->in_left -= ssl->next_record_offset;
3056
3057 if( ssl->in_left != 0 )
3058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003060 ssl->next_record_offset ) );
3061 memmove( ssl->in_hdr,
3062 ssl->in_hdr + ssl->next_record_offset,
3063 ssl->in_left );
3064 }
3065
3066 ssl->next_record_offset = 0;
3067 }
3068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003071
3072 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003073 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003074 */
3075 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003078 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003079 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080
3081 /*
3082 * A record can't be split accross datagrams. If we need to read but
3083 * are not at the beginning of a new record, the caller did something
3084 * wrong.
3085 */
3086 if( ssl->in_left != 0 )
3087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3089 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003090 }
3091
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003092 /*
3093 * Don't even try to read if time's out already.
3094 * This avoids by-passing the timer when repeatedly receiving messages
3095 * that will end up being dropped.
3096 */
3097 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003098 {
3099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003100 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003101 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003102 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003103 {
Angus Grattond8213d02016-05-25 20:56:48 +10003104 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003107 timeout = ssl->handshake->retransmit_timeout;
3108 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003109 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003112
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003113 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003114 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3115 timeout );
3116 else
3117 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120
3121 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003123 }
3124
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003125 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003128 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003131 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003132 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003135 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003136 }
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003141 return( ret );
3142 }
3143
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003144 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003145 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003147 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003149 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003150 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003153 return( ret );
3154 }
3155
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003156 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003157 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003159 }
3160
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 if( ret < 0 )
3162 return( ret );
3163
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003164 ssl->in_left = ret;
3165 }
3166 else
3167#endif
3168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003170 ssl->in_left, nb_want ) );
3171
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003172 while( ssl->in_left < nb_want )
3173 {
3174 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003175
3176 if( ssl_check_timer( ssl ) != 0 )
3177 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3178 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003179 {
3180 if( ssl->f_recv_timeout != NULL )
3181 {
3182 ret = ssl->f_recv_timeout( ssl->p_bio,
3183 ssl->in_hdr + ssl->in_left, len,
3184 ssl->conf->read_timeout );
3185 }
3186 else
3187 {
3188 ret = ssl->f_recv( ssl->p_bio,
3189 ssl->in_hdr + ssl->in_left, len );
3190 }
3191 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003194 ssl->in_left, nb_want ) );
3195 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003196
3197 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003199
3200 if( ret < 0 )
3201 return( ret );
3202
mohammad160352aecb92018-03-28 23:41:40 -07003203 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003204 {
Darryl Green11999bb2018-03-13 15:22:58 +00003205 MBEDTLS_SSL_DEBUG_MSG( 1,
3206 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003207 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3209 }
3210
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003211 ssl->in_left += ret;
3212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 }
3214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003216
3217 return( 0 );
3218}
3219
3220/*
3221 * Flush any data not yet written
3222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003224{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003225 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003226 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003230 if( ssl->f_send == NULL )
3231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003233 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003235 }
3236
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003237 /* Avoid incrementing counter if data is flushed */
3238 if( ssl->out_left == 0 )
3239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003241 return( 0 );
3242 }
3243
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 while( ssl->out_left > 0 )
3245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003247 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
Hanno Becker2b1e3542018-08-06 11:19:13 +01003249 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003250 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
3254 if( ret <= 0 )
3255 return( ret );
3256
mohammad160352aecb92018-03-28 23:41:40 -07003257 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003258 {
Darryl Green11999bb2018-03-13 15:22:58 +00003259 MBEDTLS_SSL_DEBUG_MSG( 1,
3260 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003261 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3263 }
3264
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 ssl->out_left -= ret;
3266 }
3267
Hanno Becker2b1e3542018-08-06 11:19:13 +01003268#if defined(MBEDTLS_SSL_PROTO_DTLS)
3269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003270 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003271 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003272 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003273 else
3274#endif
3275 {
3276 ssl->out_hdr = ssl->out_buf + 8;
3277 }
3278 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003281
3282 return( 0 );
3283}
3284
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003285/*
3286 * Functions to handle the DTLS retransmission state machine
3287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003289/*
3290 * Append current handshake message to current outgoing flight
3291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3296 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3297 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003298
3299 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003300 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003301 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003304 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305 }
3306
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003307 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003311 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003312 }
3313
3314 /* Copy current handshake message with headers */
3315 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3316 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003317 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318 msg->next = NULL;
3319
3320 /* Append to the current flight */
3321 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003322 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323 else
3324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326 while( cur->next != NULL )
3327 cur = cur->next;
3328 cur->next = msg;
3329 }
3330
Hanno Becker3b235902018-08-06 09:54:53 +01003331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332 return( 0 );
3333}
3334
3335/*
3336 * Free the current flight of handshake messages
3337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 mbedtls_ssl_flight_item *cur = flight;
3341 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342
3343 while( cur != NULL )
3344 {
3345 next = cur->next;
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 mbedtls_free( cur->p );
3348 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349
3350 cur = next;
3351 }
3352}
3353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3355static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003356#endif
3357
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003359 * Swap transform_out and out_ctr with the alternative ones
3360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003364 unsigned char tmp_out_ctr[8];
3365
3366 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003369 return;
3370 }
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003374 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003375 tmp_transform = ssl->transform_out;
3376 ssl->transform_out = ssl->handshake->alt_transform_out;
3377 ssl->handshake->alt_transform_out = tmp_transform;
3378
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003379 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003380 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3381 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003382 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003383
3384 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003385 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3388 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3393 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003394 }
3395 }
3396#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003397}
3398
3399/*
3400 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003401 */
3402int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3403{
3404 int ret = 0;
3405
3406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3407
3408 ret = mbedtls_ssl_flight_transmit( ssl );
3409
3410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3411
3412 return( ret );
3413}
3414
3415/*
3416 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003417 *
3418 * Need to remember the current message in case flush_output returns
3419 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003420 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003421 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003422int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003423{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003424 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003428 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003430
3431 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003432 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433 ssl_swap_epochs( ssl );
3434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003437
3438 while( ssl->handshake->cur_msg != NULL )
3439 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003440 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003441 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003442
Hanno Beckere1dcb032018-08-17 16:47:58 +01003443 int const is_finished =
3444 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3445 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3446
Hanno Becker04da1892018-08-14 13:22:10 +01003447 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3448 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3449
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003450 /* Swap epochs before sending Finished: we can't do it after
3451 * sending ChangeCipherSpec, in case write returns WANT_READ.
3452 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003453 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003454 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003456 ssl_swap_epochs( ssl );
3457 }
3458
Hanno Becker67bc7c32018-08-06 11:33:50 +01003459 ret = ssl_get_remaining_payload_in_datagram( ssl );
3460 if( ret < 0 )
3461 return( ret );
3462 max_frag_len = (size_t) ret;
3463
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003464 /* CCS is copied as is, while HS messages may need fragmentation */
3465 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3466 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003467 if( max_frag_len == 0 )
3468 {
3469 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3470 return( ret );
3471
3472 continue;
3473 }
3474
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003475 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003476 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003477 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003478
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003479 /* Update position inside current message */
3480 ssl->handshake->cur_msg_p += cur->len;
3481 }
3482 else
3483 {
3484 const unsigned char * const p = ssl->handshake->cur_msg_p;
3485 const size_t hs_len = cur->len - 12;
3486 const size_t frag_off = p - ( cur->p + 12 );
3487 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003488 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003489
Hanno Beckere1dcb032018-08-17 16:47:58 +01003490 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003491 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003492 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003494
Hanno Becker67bc7c32018-08-06 11:33:50 +01003495 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3496 return( ret );
3497
3498 continue;
3499 }
3500 max_hs_frag_len = max_frag_len - 12;
3501
3502 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3503 max_hs_frag_len : rem_len;
3504
3505 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003506 {
3507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003508 (unsigned) cur_hs_frag_len,
3509 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003510 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003511
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003512 /* Messages are stored with handshake headers as if not fragmented,
3513 * copy beginning of headers then fill fragmentation fields.
3514 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3515 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003516
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003517 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3518 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3519 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3520
Hanno Becker67bc7c32018-08-06 11:33:50 +01003521 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3522 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3523 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003524
3525 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3526
Hanno Becker3f7b9732018-08-28 09:53:25 +01003527 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003528 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3529 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003530 ssl->out_msgtype = cur->type;
3531
3532 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003533 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003534 }
3535
3536 /* If done with the current message move to the next one if any */
3537 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3538 {
3539 if( cur->next != NULL )
3540 {
3541 ssl->handshake->cur_msg = cur->next;
3542 ssl->handshake->cur_msg_p = cur->next->p + 12;
3543 }
3544 else
3545 {
3546 ssl->handshake->cur_msg = NULL;
3547 ssl->handshake->cur_msg_p = NULL;
3548 }
3549 }
3550
3551 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003552 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003555 return( ret );
3556 }
3557 }
3558
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3560 return( ret );
3561
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003562 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3564 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003565 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003568 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3569 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003570
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003572
3573 return( 0 );
3574}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003575
3576/*
3577 * To be called when the last message of an incoming flight is received.
3578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003580{
3581 /* We won't need to resend that one any more */
3582 ssl_flight_free( ssl->handshake->flight );
3583 ssl->handshake->flight = NULL;
3584 ssl->handshake->cur_msg = NULL;
3585
3586 /* The next incoming flight will start with this msg_seq */
3587 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3588
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003589 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003590 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003591
Hanno Becker0271f962018-08-16 13:23:47 +01003592 /* Clear future message buffering structure. */
3593 ssl_buffering_free( ssl );
3594
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003595 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003596 ssl_set_timer( ssl, 0 );
3597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3599 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003602 }
3603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003606
3607/*
3608 * To be called when the last message of an outgoing flight is send.
3609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003611{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003612 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003613 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3616 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003619 }
3620 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003624
Paul Bakker5121ce52009-01-03 21:22:43 +00003625/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003626 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003628
3629/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003630 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003631 *
3632 * - fill in handshake headers
3633 * - update handshake checksum
3634 * - DTLS: save message for resending
3635 * - then pass to the record layer
3636 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003637 * DTLS: except for HelloRequest, messages are only queued, and will only be
3638 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003639 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003640 * Inputs:
3641 * - ssl->out_msglen: 4 + actual handshake message len
3642 * (4 is the size of handshake headers for TLS)
3643 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3644 * - ssl->out_msg + 4: the handshake message body
3645 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003646 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003647 * - ssl->out_msglen: the length of the record contents
3648 * (including handshake headers but excluding record headers)
3649 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003651int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003652{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003653 int ret;
3654 const size_t hs_len = ssl->out_msglen - 4;
3655 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3658
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003659 /*
3660 * Sanity checks
3661 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003662 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003663 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3664 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003665 /* In SSLv3, the client might send a NoCertificate alert. */
3666#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3667 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3668 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3669 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3670#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3671 {
3672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3674 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003676
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003677 /* Whenever we send anything different from a
3678 * HelloRequest we should be in a handshake - double check. */
3679 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3680 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003681 ssl->handshake == NULL )
3682 {
3683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3684 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003688 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003689 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003691 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003694 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003695#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003696
Hanno Beckerb50a2532018-08-06 11:52:54 +01003697 /* Double-check that we did not exceed the bounds
3698 * of the outgoing record buffer.
3699 * This should never fail as the various message
3700 * writing functions must obey the bounds of the
3701 * outgoing record buffer, but better be safe.
3702 *
3703 * Note: We deliberately do not check for the MTU or MFL here.
3704 */
3705 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3706 {
3707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3708 "size %u, maximum %u",
3709 (unsigned) ssl->out_msglen,
3710 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3712 }
3713
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003714 /*
3715 * Fill handshake headers
3716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003719 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3720 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3721 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003723 /*
3724 * DTLS has additional fields in the Handshake layer,
3725 * between the length field and the actual payload:
3726 * uint16 message_seq;
3727 * uint24 fragment_offset;
3728 * uint24 fragment_length;
3729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003731 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003732 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003733 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003734 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003735 {
3736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3737 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003738 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003739 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3741 }
3742
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003743 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003744 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003745
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003746 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003747 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003748 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003749 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3750 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3751 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003752 }
3753 else
3754 {
3755 ssl->out_msg[4] = 0;
3756 ssl->out_msg[5] = 0;
3757 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003758
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003759 /* Handshake hashes are computed without fragmentation,
3760 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003761 memset( ssl->out_msg + 6, 0x00, 3 );
3762 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003765
Hanno Becker0207e532018-08-28 10:28:28 +01003766 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003767 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3768 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 }
3770
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003771 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003774 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3775 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003776 {
3777 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003780 return( ret );
3781 }
3782 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003783 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003784#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003785 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003786 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003787 {
3788 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3789 return( ret );
3790 }
3791 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003792
3793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3794
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003795 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003796}
3797
3798/*
3799 * Record layer functions
3800 */
3801
3802/*
3803 * Write current record.
3804 *
3805 * Uses:
3806 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3807 * - ssl->out_msglen: length of the record content (excl headers)
3808 * - ssl->out_msg: record content
3809 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003810int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003811{
3812 int ret, done = 0;
3813 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003815
3816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003819 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003821 {
3822 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003825 return( ret );
3826 }
3827
3828 len = ssl->out_msglen;
3829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3833 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 ret = mbedtls_ssl_hw_record_write( ssl );
3838 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3841 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003842 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003843
3844 if( ret == 0 )
3845 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003846 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003848 if( !done )
3849 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003850 unsigned i;
3851 size_t protected_record_size;
3852
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003853 /* Skip writing the record content type to after the encryption,
3854 * as it may change when using the CID extension. */
3855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003857 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003858
Hanno Becker19859472018-08-06 09:40:20 +01003859 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003860 ssl->out_len[0] = (unsigned char)( len >> 8 );
3861 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003862
Paul Bakker48916f92012-09-16 19:57:18 +00003863 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003864 {
Hanno Becker3307b532017-12-27 21:37:21 +00003865 mbedtls_record rec;
3866
3867 rec.buf = ssl->out_iv;
3868 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3869 ( ssl->out_iv - ssl->out_buf );
3870 rec.data_len = ssl->out_msglen;
3871 rec.data_offset = ssl->out_msg - rec.buf;
3872
3873 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3874 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3875 ssl->conf->transport, rec.ver );
3876 rec.type = ssl->out_msgtype;
3877
Hanno Beckera5a2b082019-05-15 14:03:01 +01003878#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003879 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003880 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003881#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003882
Hanno Becker611a83b2018-01-03 14:27:32 +00003883 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003884 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003887 return( ret );
3888 }
3889
Hanno Becker3307b532017-12-27 21:37:21 +00003890 if( rec.data_offset != 0 )
3891 {
3892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3894 }
3895
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003896 /* Update the record content type and CID. */
3897 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003898#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003899 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003900#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003901 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003902 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3903 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003904 }
3905
Hanno Becker43395762019-05-03 14:46:38 +01003906 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003907
3908#if defined(MBEDTLS_SSL_PROTO_DTLS)
3909 /* In case of DTLS, double-check that we don't exceed
3910 * the remaining space in the datagram. */
3911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3912 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003913 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003914 if( ret < 0 )
3915 return( ret );
3916
3917 if( protected_record_size > (size_t) ret )
3918 {
3919 /* Should never happen */
3920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3921 }
3922 }
3923#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003924
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003925 /* Now write the potentially updated record content type. */
3926 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003929 "version = [%d:%d], msglen = %d",
3930 ssl->out_hdr[0], ssl->out_hdr[1],
3931 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003934 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003935
3936 ssl->out_left += protected_record_size;
3937 ssl->out_hdr += protected_record_size;
3938 ssl_update_out_pointers( ssl, ssl->transform_out );
3939
Hanno Becker04484622018-08-06 09:49:38 +01003940 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3941 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3942 break;
3943
3944 /* The loop goes to its end iff the counter is wrapping */
3945 if( i == ssl_ep_len( ssl ) )
3946 {
3947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3948 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003950 }
3951
Hanno Becker67bc7c32018-08-06 11:33:50 +01003952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003953 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3954 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003955 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003956 size_t remaining;
3957 ret = ssl_get_remaining_payload_in_datagram( ssl );
3958 if( ret < 0 )
3959 {
3960 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3961 ret );
3962 return( ret );
3963 }
3964
3965 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003966 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003967 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003968 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003969 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003970 else
3971 {
Hanno Becker513815a2018-08-20 11:56:09 +01003972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003973 }
3974 }
3975#endif /* MBEDTLS_SSL_PROTO_DTLS */
3976
3977 if( ( flush == SSL_FORCE_FLUSH ) &&
3978 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981 return( ret );
3982 }
3983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003985
3986 return( 0 );
3987}
3988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003990
3991static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3992{
3993 if( ssl->in_msglen < ssl->in_hslen ||
3994 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3995 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3996 {
3997 return( 1 );
3998 }
3999 return( 0 );
4000}
Hanno Becker44650b72018-08-16 12:51:11 +01004001
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004002static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004003{
4004 return( ( ssl->in_msg[9] << 16 ) |
4005 ( ssl->in_msg[10] << 8 ) |
4006 ssl->in_msg[11] );
4007}
4008
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004009static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004010{
4011 return( ( ssl->in_msg[6] << 16 ) |
4012 ( ssl->in_msg[7] << 8 ) |
4013 ssl->in_msg[8] );
4014}
4015
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004016static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004017{
4018 uint32_t msg_len, frag_off, frag_len;
4019
4020 msg_len = ssl_get_hs_total_len( ssl );
4021 frag_off = ssl_get_hs_frag_off( ssl );
4022 frag_len = ssl_get_hs_frag_len( ssl );
4023
4024 if( frag_off > msg_len )
4025 return( -1 );
4026
4027 if( frag_len > msg_len - frag_off )
4028 return( -1 );
4029
4030 if( frag_len + 12 > ssl->in_msglen )
4031 return( -1 );
4032
4033 return( 0 );
4034}
4035
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004036/*
4037 * Mark bits in bitmask (used for DTLS HS reassembly)
4038 */
4039static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4040{
4041 unsigned int start_bits, end_bits;
4042
4043 start_bits = 8 - ( offset % 8 );
4044 if( start_bits != 8 )
4045 {
4046 size_t first_byte_idx = offset / 8;
4047
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004048 /* Special case */
4049 if( len <= start_bits )
4050 {
4051 for( ; len != 0; len-- )
4052 mask[first_byte_idx] |= 1 << ( start_bits - len );
4053
4054 /* Avoid potential issues with offset or len becoming invalid */
4055 return;
4056 }
4057
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004058 offset += start_bits; /* Now offset % 8 == 0 */
4059 len -= start_bits;
4060
4061 for( ; start_bits != 0; start_bits-- )
4062 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4063 }
4064
4065 end_bits = len % 8;
4066 if( end_bits != 0 )
4067 {
4068 size_t last_byte_idx = ( offset + len ) / 8;
4069
4070 len -= end_bits; /* Now len % 8 == 0 */
4071
4072 for( ; end_bits != 0; end_bits-- )
4073 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4074 }
4075
4076 memset( mask + offset / 8, 0xFF, len / 8 );
4077}
4078
4079/*
4080 * Check that bitmask is full
4081 */
4082static int ssl_bitmask_check( unsigned char *mask, size_t len )
4083{
4084 size_t i;
4085
4086 for( i = 0; i < len / 8; i++ )
4087 if( mask[i] != 0xFF )
4088 return( -1 );
4089
4090 for( i = 0; i < len % 8; i++ )
4091 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4092 return( -1 );
4093
4094 return( 0 );
4095}
4096
Hanno Becker56e205e2018-08-16 09:06:12 +01004097/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004098static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004099 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004100{
Hanno Becker56e205e2018-08-16 09:06:12 +01004101 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004102
Hanno Becker56e205e2018-08-16 09:06:12 +01004103 alloc_len = 12; /* Handshake header */
4104 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004105
Hanno Beckerd07df862018-08-16 09:14:58 +01004106 if( add_bitmap )
4107 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004108
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004109 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004110}
Hanno Becker56e205e2018-08-16 09:06:12 +01004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004113
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004114static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004115{
4116 return( ( ssl->in_msg[1] << 16 ) |
4117 ( ssl->in_msg[2] << 8 ) |
4118 ssl->in_msg[3] );
4119}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004120
Simon Butcher99000142016-10-13 17:21:01 +01004121int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004126 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004128 }
4129
Hanno Becker12555c62018-08-16 12:47:53 +01004130 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004133 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004134 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004137 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004138 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004139 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004140 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004141
Hanno Becker44650b72018-08-16 12:51:11 +01004142 if( ssl_check_hs_header( ssl ) != 0 )
4143 {
4144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4146 }
4147
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004148 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004149 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4150 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4151 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4152 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004153 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004154 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4155 {
4156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4157 recv_msg_seq,
4158 ssl->handshake->in_msg_seq ) );
4159 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4160 }
4161
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004162 /* Retransmit only on last message from previous flight, to avoid
4163 * too many retransmissions.
4164 * Besides, No sane server ever retransmits HelloVerifyRequest */
4165 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004169 "message_seq = %d, start_of_flight = %d",
4170 recv_msg_seq,
4171 ssl->handshake->in_flight_start_seq ) );
4172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004176 return( ret );
4177 }
4178 }
4179 else
4180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004182 "message_seq = %d, expected = %d",
4183 recv_msg_seq,
4184 ssl->handshake->in_msg_seq ) );
4185 }
4186
Hanno Becker90333da2017-10-10 11:27:13 +01004187 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004188 }
4189 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004190
Hanno Becker6d97ef52018-08-16 13:09:04 +01004191 /* Message reassembly is handled alongside buffering of future
4192 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004193 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004194 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004195 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004198 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004199 }
4200 }
4201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004203 /* With TLS we don't handle fragmentation (for now) */
4204 if( ssl->in_msglen < ssl->in_hslen )
4205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4207 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004208 }
4209
Simon Butcher99000142016-10-13 17:21:01 +01004210 return( 0 );
4211}
4212
4213void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4214{
Hanno Becker0271f962018-08-16 13:23:47 +01004215 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004216
Hanno Becker0271f962018-08-16 13:23:47 +01004217 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004218 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004219 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004220 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004221
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004222 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004225 ssl->handshake != NULL )
4226 {
Hanno Becker0271f962018-08-16 13:23:47 +01004227 unsigned offset;
4228 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004229
Hanno Becker0271f962018-08-16 13:23:47 +01004230 /* Increment handshake sequence number */
4231 hs->in_msg_seq++;
4232
4233 /*
4234 * Clear up handshake buffering and reassembly structure.
4235 */
4236
4237 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004238 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004239
4240 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004241 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4242 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004243 offset++, hs_buf++ )
4244 {
4245 *hs_buf = *(hs_buf + 1);
4246 }
4247
4248 /* Create a fresh last entry */
4249 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004250 }
4251#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004252}
4253
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004254/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004255 * DTLS anti-replay: RFC 6347 4.1.2.6
4256 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004257 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4258 * Bit n is set iff record number in_window_top - n has been seen.
4259 *
4260 * Usually, in_window_top is the last record number seen and the lsb of
4261 * in_window is set. The only exception is the initial state (record number 0
4262 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4265static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004266{
4267 ssl->in_window_top = 0;
4268 ssl->in_window = 0;
4269}
4270
4271static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4272{
4273 return( ( (uint64_t) buf[0] << 40 ) |
4274 ( (uint64_t) buf[1] << 32 ) |
4275 ( (uint64_t) buf[2] << 24 ) |
4276 ( (uint64_t) buf[3] << 16 ) |
4277 ( (uint64_t) buf[4] << 8 ) |
4278 ( (uint64_t) buf[5] ) );
4279}
4280
4281/*
4282 * Return 0 if sequence number is acceptable, -1 otherwise
4283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004285{
4286 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4287 uint64_t bit;
4288
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004289 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004290 return( 0 );
4291
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004292 if( rec_seqnum > ssl->in_window_top )
4293 return( 0 );
4294
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004295 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004296
4297 if( bit >= 64 )
4298 return( -1 );
4299
4300 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4301 return( -1 );
4302
4303 return( 0 );
4304}
4305
4306/*
4307 * Update replay window on new validated record
4308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004310{
4311 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4312
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004313 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004314 return;
4315
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004316 if( rec_seqnum > ssl->in_window_top )
4317 {
4318 /* Update window_top and the contents of the window */
4319 uint64_t shift = rec_seqnum - ssl->in_window_top;
4320
4321 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004322 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004323 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004324 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004325 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004326 ssl->in_window |= 1;
4327 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328
4329 ssl->in_window_top = rec_seqnum;
4330 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004331 else
4332 {
4333 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004334 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004335
4336 if( bit < 64 ) /* Always true, but be extra sure */
4337 ssl->in_window |= (uint64_t) 1 << bit;
4338 }
4339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004341
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004342#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004343/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004344static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4345
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004346/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004347 * Without any SSL context, check if a datagram looks like a ClientHello with
4348 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004349 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004350 *
4351 * - if cookie is valid, return 0
4352 * - if ClientHello looks superficially valid but cookie is not,
4353 * fill obuf and set olen, then
4354 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4355 * - otherwise return a specific error code
4356 */
4357static int ssl_check_dtls_clihlo_cookie(
4358 mbedtls_ssl_cookie_write_t *f_cookie_write,
4359 mbedtls_ssl_cookie_check_t *f_cookie_check,
4360 void *p_cookie,
4361 const unsigned char *cli_id, size_t cli_id_len,
4362 const unsigned char *in, size_t in_len,
4363 unsigned char *obuf, size_t buf_len, size_t *olen )
4364{
4365 size_t sid_len, cookie_len;
4366 unsigned char *p;
4367
4368 if( f_cookie_write == NULL || f_cookie_check == NULL )
4369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4370
4371 /*
4372 * Structure of ClientHello with record and handshake headers,
4373 * and expected values. We don't need to check a lot, more checks will be
4374 * done when actually parsing the ClientHello - skipping those checks
4375 * avoids code duplication and does not make cookie forging any easier.
4376 *
4377 * 0-0 ContentType type; copied, must be handshake
4378 * 1-2 ProtocolVersion version; copied
4379 * 3-4 uint16 epoch; copied, must be 0
4380 * 5-10 uint48 sequence_number; copied
4381 * 11-12 uint16 length; (ignored)
4382 *
4383 * 13-13 HandshakeType msg_type; (ignored)
4384 * 14-16 uint24 length; (ignored)
4385 * 17-18 uint16 message_seq; copied
4386 * 19-21 uint24 fragment_offset; copied, must be 0
4387 * 22-24 uint24 fragment_length; (ignored)
4388 *
4389 * 25-26 ProtocolVersion client_version; (ignored)
4390 * 27-58 Random random; (ignored)
4391 * 59-xx SessionID session_id; 1 byte len + sid_len content
4392 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4393 * ...
4394 *
4395 * Minimum length is 61 bytes.
4396 */
4397 if( in_len < 61 ||
4398 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4399 in[3] != 0 || in[4] != 0 ||
4400 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4401 {
4402 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4403 }
4404
4405 sid_len = in[59];
4406 if( sid_len > in_len - 61 )
4407 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4408
4409 cookie_len = in[60 + sid_len];
4410 if( cookie_len > in_len - 60 )
4411 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4412
4413 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4414 cli_id, cli_id_len ) == 0 )
4415 {
4416 /* Valid cookie */
4417 return( 0 );
4418 }
4419
4420 /*
4421 * If we get here, we've got an invalid cookie, let's prepare HVR.
4422 *
4423 * 0-0 ContentType type; copied
4424 * 1-2 ProtocolVersion version; copied
4425 * 3-4 uint16 epoch; copied
4426 * 5-10 uint48 sequence_number; copied
4427 * 11-12 uint16 length; olen - 13
4428 *
4429 * 13-13 HandshakeType msg_type; hello_verify_request
4430 * 14-16 uint24 length; olen - 25
4431 * 17-18 uint16 message_seq; copied
4432 * 19-21 uint24 fragment_offset; copied
4433 * 22-24 uint24 fragment_length; olen - 25
4434 *
4435 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4436 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4437 *
4438 * Minimum length is 28.
4439 */
4440 if( buf_len < 28 )
4441 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4442
4443 /* Copy most fields and adapt others */
4444 memcpy( obuf, in, 25 );
4445 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4446 obuf[25] = 0xfe;
4447 obuf[26] = 0xff;
4448
4449 /* Generate and write actual cookie */
4450 p = obuf + 28;
4451 if( f_cookie_write( p_cookie,
4452 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4453 {
4454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4455 }
4456
4457 *olen = p - obuf;
4458
4459 /* Go back and fill length fields */
4460 obuf[27] = (unsigned char)( *olen - 28 );
4461
4462 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4463 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4464 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4465
4466 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4467 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4468
4469 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4470}
4471
4472/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004473 * Handle possible client reconnect with the same UDP quadruplet
4474 * (RFC 6347 Section 4.2.8).
4475 *
4476 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4477 * that looks like a ClientHello.
4478 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004479 * - if the input looks like a ClientHello without cookies,
4480 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004481 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004482 * - if the input looks like a ClientHello with a valid cookie,
4483 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004484 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004485 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004486 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004487 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004488 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4489 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004490 */
4491static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4492{
4493 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004494 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004495
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004496 ret = ssl_check_dtls_clihlo_cookie(
4497 ssl->conf->f_cookie_write,
4498 ssl->conf->f_cookie_check,
4499 ssl->conf->p_cookie,
4500 ssl->cli_id, ssl->cli_id_len,
4501 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004502 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004503
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004504 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4505
4506 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004507 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004508 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004509 * If the error is permanent we'll catch it later,
4510 * if it's not, then hopefully it'll work next time. */
4511 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4512
4513 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004514 }
4515
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004518 /* Got a valid cookie, partially reset context */
4519 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4520 {
4521 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4522 return( ret );
4523 }
4524
4525 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004526 }
4527
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 return( ret );
4529}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004530#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531
Hanno Becker46483f12019-05-03 13:25:54 +01004532static int ssl_check_record_type( uint8_t record_type )
4533{
4534 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4535 record_type != MBEDTLS_SSL_MSG_ALERT &&
4536 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4537 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4538 {
4539 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4540 }
4541
4542 return( 0 );
4543}
4544
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004545/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004546 * ContentType type;
4547 * ProtocolVersion version;
4548 * uint16 epoch; // DTLS only
4549 * uint48 sequence_number; // DTLS only
4550 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004551 *
4552 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004553 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004554 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4555 *
4556 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004557 * 1. proceed with the record if this function returns 0
4558 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4559 * 3. return CLIENT_RECONNECT if this function return that value
4560 * 4. drop the whole datagram if this function returns anything else.
4561 * Point 2 is needed when the peer is resending, and we have already received
4562 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004565{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004566 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004567 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004568
Hanno Becker8b09b732019-05-08 12:03:28 +01004569 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004570
Paul Bakker5121ce52009-01-03 21:22:43 +00004571 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004572 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004574 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004575#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b09b732019-05-08 12:03:28 +01004576 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4577 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4578 ssl->conf->cid_len != 0 )
4579 {
4580 /* Shift pointers to account for record header including CID
4581 * struct {
4582 * ContentType special_type = tls12_cid;
4583 * ProtocolVersion version;
4584 * uint16 epoch;
4585 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004586 * opaque cid[cid_length]; // Additional field compared to
4587 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004588 * uint16 length;
4589 * opaque enc_content[DTLSCiphertext.length];
4590 * } DTLSCiphertext;
4591 */
4592
4593 /* So far, we only support static CID lengths
4594 * fixed in the configuration. */
4595 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4596 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4597 }
4598 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004599#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004600 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004603
4604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004605 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4606 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004607 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4608#endif /* MBEDTLS_SSL_PROTO_DTLS */
4609 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4610 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004613 }
4614
4615 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004616 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4619 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 }
4621
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004622 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4625 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 }
4627
Hanno Becker8b09b732019-05-08 12:03:28 +01004628 /* Now that the total length of the record header is known, ensure
4629 * that the current datagram is large enough to hold it.
4630 * This would fail, for example, if we received a datagram of
4631 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4632 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4633 if( ret != 0 )
4634 {
4635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4636 return( ret );
4637 }
4638 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4639
4640 /* Parse and validate record length
4641 * This must happen after the CID parsing because
4642 * its position in the record header depends on
4643 * the presence of a CID. */
4644
4645 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004646 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004647 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004651 }
4652
Hanno Becker8b09b732019-05-08 12:03:28 +01004653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004654 "version = [%d:%d], msglen = %d",
4655 ssl->in_msgtype,
4656 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004657
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004658 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004659 * DTLS-related tests.
4660 * Check epoch before checking length constraint because
4661 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4662 * message gets duplicated before the corresponding Finished message,
4663 * the second ChangeCipherSpec should be discarded because it belongs
4664 * to an old epoch, but not because its length is shorter than
4665 * the minimum record length for packets using the new record transform.
4666 * Note that these two kinds of failures are handled differently,
4667 * as an unexpected record is silently skipped but an invalid
4668 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004669 */
4670#if defined(MBEDTLS_SSL_PROTO_DTLS)
4671 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4672 {
4673 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4674
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004675 /* Check epoch (and sequence number) with DTLS */
4676 if( rec_epoch != ssl->in_epoch )
4677 {
4678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4679 "expected %d, received %d",
4680 ssl->in_epoch, rec_epoch ) );
4681
4682#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4683 /*
4684 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4685 * access the first byte of record content (handshake type), as we
4686 * have an active transform (possibly iv_len != 0), so use the
4687 * fact that the record header len is 13 instead.
4688 */
4689 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4690 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4691 rec_epoch == 0 &&
4692 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4693 ssl->in_left > 13 &&
4694 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4695 {
4696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4697 "from the same port" ) );
4698 return( ssl_handle_possible_reconnect( ssl ) );
4699 }
4700 else
4701#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004702 {
4703 /* Consider buffering the record. */
4704 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4705 {
4706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4707 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4708 }
4709
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004710 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004711 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 }
4713
4714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4715 /* Replay detection only works for the current epoch */
4716 if( rec_epoch == ssl->in_epoch &&
4717 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4718 {
4719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4720 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4721 }
4722#endif
4723 }
4724#endif /* MBEDTLS_SSL_PROTO_DTLS */
4725
Hanno Becker52c6dc62017-05-26 16:07:36 +01004726
4727 /* Check length against bounds of the current transform and version */
4728 if( ssl->transform_in == NULL )
4729 {
4730 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004731 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004732 {
4733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4734 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4735 }
4736 }
4737 else
4738 {
4739 if( ssl->in_msglen < ssl->transform_in->minlen )
4740 {
4741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4742 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4743 }
4744
4745#if defined(MBEDTLS_SSL_PROTO_SSL3)
4746 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004747 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004748 {
4749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4750 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4751 }
4752#endif
4753#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4754 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4755 /*
4756 * TLS encrypted messages can have up to 256 bytes of padding
4757 */
4758 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4759 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004760 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004761 {
4762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4763 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4764 }
4765#endif
4766 }
4767
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004768 return( 0 );
4769}
Paul Bakker5121ce52009-01-03 21:22:43 +00004770
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004771/*
4772 * If applicable, decrypt (and decompress) record content
4773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004775{
4776 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004779 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4782 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 ret = mbedtls_ssl_hw_record_read( ssl );
4787 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4790 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004791 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004792
4793 if( ret == 0 )
4794 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004797 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004798 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004799 mbedtls_record rec;
4800
4801 rec.buf = ssl->in_iv;
4802 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4803 - ( ssl->in_iv - ssl->in_buf );
4804 rec.data_len = ssl->in_msglen;
4805 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004806#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004807 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004808 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004809#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004810
4811 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4812 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4813 ssl->conf->transport, rec.ver );
4814 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004815 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4816 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004819
Hanno Beckera5a2b082019-05-15 14:03:01 +01004820#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004821 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4822 ssl->conf->ignore_unexpected_cid
4823 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4824 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004826 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004827 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004828#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004829
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 return( ret );
4831 }
4832
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004833 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004834 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004835 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4836 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004838
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004839 /* The record content type may change during decryption,
4840 * so re-read it. */
4841 ssl->in_msgtype = rec.type;
4842 /* Also update the input buffer, because unfortunately
4843 * the server-side ssl_parse_client_hello() reparses the
4844 * record header when receiving a ClientHello initiating
4845 * a renegotiation. */
4846 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004847 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004848 ssl->in_msglen = rec.data_len;
4849 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4850 ssl->in_len[1] = (unsigned char)( rec.data_len );
4851
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4853 ssl->in_msg, ssl->in_msglen );
4854
Hanno Beckera5a2b082019-05-15 14:03:01 +01004855#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004856 /* We have already checked the record content type
4857 * in ssl_parse_record_header(), failing or silently
4858 * dropping the record in the case of an unknown type.
4859 *
4860 * Since with the use of CIDs, the record content type
4861 * might change during decryption, re-check the record
4862 * content type, but treat a failure as fatal this time. */
4863 if( ssl_check_record_type( ssl->in_msgtype ) )
4864 {
4865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004869
Angus Grattond8213d02016-05-25 20:56:48 +10004870 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4873 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004874 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004875 else if( ssl->in_msglen == 0 )
4876 {
4877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4878 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4879 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4880 {
4881 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4883 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4884 }
4885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4886
4887 ssl->nb_zero++;
4888
4889 /*
4890 * Three or more empty messages may be a DoS attack
4891 * (excessive CPU consumption).
4892 */
4893 if( ssl->nb_zero > 3 )
4894 {
4895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004896 "messages, possible DoS attack" ) );
4897 /* Treat the records as if they were not properly authenticated,
4898 * thereby failing the connection if we see more than allowed
4899 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004900 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4901 }
4902 }
4903 else
4904 ssl->nb_zero = 0;
4905
4906#if defined(MBEDTLS_SSL_PROTO_DTLS)
4907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4908 {
4909 ; /* in_ctr read from peer, not maintained internally */
4910 }
4911 else
4912#endif
4913 {
4914 unsigned i;
4915 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4916 if( ++ssl->in_ctr[i - 1] != 0 )
4917 break;
4918
4919 /* The loop goes to its end iff the counter is wrapping */
4920 if( i == ssl_ep_len( ssl ) )
4921 {
4922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4923 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4924 }
4925 }
4926
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 }
4928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004930 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004932 {
4933 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004936 return( ret );
4937 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004938 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004942 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004945 }
4946#endif
4947
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004948 return( 0 );
4949}
4950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004952
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004953/*
4954 * Read a record.
4955 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004956 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4957 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4958 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004959 */
Hanno Becker1097b342018-08-15 14:09:41 +01004960
4961/* Helper functions for mbedtls_ssl_read_record(). */
4962static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004963static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4964static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004965
Hanno Becker327c93b2018-08-15 13:56:18 +01004966int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004967 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004968{
4969 int ret;
4970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004972
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004973 if( ssl->keep_current_message == 0 )
4974 {
4975 do {
Simon Butcher99000142016-10-13 17:21:01 +01004976
Hanno Becker26994592018-08-15 14:14:59 +01004977 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004978 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004979 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004980
Hanno Beckere74d5562018-08-15 14:26:08 +01004981 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004982 {
Hanno Becker40f50842018-08-15 14:48:01 +01004983#if defined(MBEDTLS_SSL_PROTO_DTLS)
4984 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004985
Hanno Becker40f50842018-08-15 14:48:01 +01004986 /* We only check for buffered messages if the
4987 * current datagram is fully consumed. */
4988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004989 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004990 {
Hanno Becker40f50842018-08-15 14:48:01 +01004991 if( ssl_load_buffered_message( ssl ) == 0 )
4992 have_buffered = 1;
4993 }
4994
4995 if( have_buffered == 0 )
4996#endif /* MBEDTLS_SSL_PROTO_DTLS */
4997 {
4998 ret = ssl_get_next_record( ssl );
4999 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5000 continue;
5001
5002 if( ret != 0 )
5003 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005004 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005005 return( ret );
5006 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005007 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005008 }
5009
5010 ret = mbedtls_ssl_handle_message_type( ssl );
5011
Hanno Becker40f50842018-08-15 14:48:01 +01005012#if defined(MBEDTLS_SSL_PROTO_DTLS)
5013 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5014 {
5015 /* Buffer future message */
5016 ret = ssl_buffer_message( ssl );
5017 if( ret != 0 )
5018 return( ret );
5019
5020 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5021 }
5022#endif /* MBEDTLS_SSL_PROTO_DTLS */
5023
Hanno Becker90333da2017-10-10 11:27:13 +01005024 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5025 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005026
5027 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005028 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005029 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005030 return( ret );
5031 }
5032
Hanno Becker327c93b2018-08-15 13:56:18 +01005033 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005034 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005035 {
5036 mbedtls_ssl_update_handshake_status( ssl );
5037 }
Simon Butcher99000142016-10-13 17:21:01 +01005038 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005039 else
Simon Butcher99000142016-10-13 17:21:01 +01005040 {
Hanno Becker02f59072018-08-15 14:00:24 +01005041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005042 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005043 }
5044
5045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5046
5047 return( 0 );
5048}
5049
Hanno Becker40f50842018-08-15 14:48:01 +01005050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005051static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005052{
Hanno Becker40f50842018-08-15 14:48:01 +01005053 if( ssl->in_left > ssl->next_record_offset )
5054 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005055
Hanno Becker40f50842018-08-15 14:48:01 +01005056 return( 0 );
5057}
5058
5059static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5060{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005061 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005062 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005063 int ret = 0;
5064
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005065 if( hs == NULL )
5066 return( -1 );
5067
Hanno Beckere00ae372018-08-20 09:39:42 +01005068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5069
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005070 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5071 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5072 {
5073 /* Check if we have seen a ChangeCipherSpec before.
5074 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005075 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 {
5077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5078 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005079 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005080 }
5081
Hanno Becker39b8bc92018-08-28 17:17:13 +01005082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005083 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5084 ssl->in_msglen = 1;
5085 ssl->in_msg[0] = 1;
5086
5087 /* As long as they are equal, the exact value doesn't matter. */
5088 ssl->in_left = 0;
5089 ssl->next_record_offset = 0;
5090
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005091 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005092 goto exit;
5093 }
Hanno Becker37f95322018-08-16 13:55:32 +01005094
Hanno Beckerb8f50142018-08-28 10:01:34 +01005095#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005096 /* Debug only */
5097 {
5098 unsigned offset;
5099 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5100 {
5101 hs_buf = &hs->buffering.hs[offset];
5102 if( hs_buf->is_valid == 1 )
5103 {
5104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5105 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005106 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005107 }
5108 }
5109 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005110#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005111
5112 /* Check if we have buffered and/or fully reassembled the
5113 * next handshake message. */
5114 hs_buf = &hs->buffering.hs[0];
5115 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5116 {
5117 /* Synthesize a record containing the buffered HS message. */
5118 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5119 ( hs_buf->data[2] << 8 ) |
5120 hs_buf->data[3];
5121
5122 /* Double-check that we haven't accidentally buffered
5123 * a message that doesn't fit into the input buffer. */
5124 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5125 {
5126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5128 }
5129
5130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5131 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5132 hs_buf->data, msg_len + 12 );
5133
5134 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5135 ssl->in_hslen = msg_len + 12;
5136 ssl->in_msglen = msg_len + 12;
5137 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5138
5139 ret = 0;
5140 goto exit;
5141 }
5142 else
5143 {
5144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5145 hs->in_msg_seq ) );
5146 }
5147
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005148 ret = -1;
5149
5150exit:
5151
5152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5153 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005154}
5155
Hanno Beckera02b0b42018-08-21 17:20:27 +01005156static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5157 size_t desired )
5158{
5159 int offset;
5160 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5162 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005163
Hanno Becker01315ea2018-08-21 17:22:17 +01005164 /* Get rid of future records epoch first, if such exist. */
5165 ssl_free_buffered_record( ssl );
5166
5167 /* Check if we have enough space available now. */
5168 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5169 hs->buffering.total_bytes_buffered ) )
5170 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005172 return( 0 );
5173 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005174
Hanno Becker4f432ad2018-08-28 10:02:32 +01005175 /* We don't have enough space to buffer the next expected handshake
5176 * message. Remove buffers used for future messages to gain space,
5177 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005178 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5179 offset >= 0; offset-- )
5180 {
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5182 offset ) );
5183
Hanno Beckerb309b922018-08-23 13:18:05 +01005184 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005185
5186 /* Check if we have enough space available now. */
5187 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5188 hs->buffering.total_bytes_buffered ) )
5189 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191 return( 0 );
5192 }
5193 }
5194
5195 return( -1 );
5196}
5197
Hanno Becker40f50842018-08-15 14:48:01 +01005198static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5199{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005200 int ret = 0;
5201 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5202
5203 if( hs == NULL )
5204 return( 0 );
5205
5206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5207
5208 switch( ssl->in_msgtype )
5209 {
5210 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005212
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005213 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005214 break;
5215
5216 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005217 {
5218 unsigned recv_msg_seq_offset;
5219 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5220 mbedtls_ssl_hs_buffer *hs_buf;
5221 size_t msg_len = ssl->in_hslen - 12;
5222
5223 /* We should never receive an old handshake
5224 * message - double-check nonetheless. */
5225 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5226 {
5227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5228 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5229 }
5230
5231 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5232 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5233 {
5234 /* Silently ignore -- message too far in the future */
5235 MBEDTLS_SSL_DEBUG_MSG( 2,
5236 ( "Ignore future HS message with sequence number %u, "
5237 "buffering window %u - %u",
5238 recv_msg_seq, ssl->handshake->in_msg_seq,
5239 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5240
5241 goto exit;
5242 }
5243
5244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5245 recv_msg_seq, recv_msg_seq_offset ) );
5246
5247 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5248
5249 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005250 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005251 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005252 size_t reassembly_buf_sz;
5253
Hanno Becker37f95322018-08-16 13:55:32 +01005254 hs_buf->is_fragmented =
5255 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5256
5257 /* We copy the message back into the input buffer
5258 * after reassembly, so check that it's not too large.
5259 * This is an implementation-specific limitation
5260 * and not one from the standard, hence it is not
5261 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005262 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005263 {
5264 /* Ignore message */
5265 goto exit;
5266 }
5267
Hanno Beckere0b150f2018-08-21 15:51:03 +01005268 /* Check if we have enough space to buffer the message. */
5269 if( hs->buffering.total_bytes_buffered >
5270 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5271 {
5272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5274 }
5275
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005276 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5277 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005278
5279 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5280 hs->buffering.total_bytes_buffered ) )
5281 {
5282 if( recv_msg_seq_offset > 0 )
5283 {
5284 /* If we can't buffer a future message because
5285 * of space limitations -- ignore. */
5286 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",
5287 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5288 (unsigned) hs->buffering.total_bytes_buffered ) );
5289 goto exit;
5290 }
Hanno Beckere1801392018-08-21 16:51:05 +01005291 else
5292 {
5293 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",
5294 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5295 (unsigned) hs->buffering.total_bytes_buffered ) );
5296 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005297
Hanno Beckera02b0b42018-08-21 17:20:27 +01005298 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005299 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005300 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",
5301 (unsigned) msg_len,
5302 (unsigned) reassembly_buf_sz,
5303 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005304 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005305 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5306 goto exit;
5307 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005308 }
5309
5310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5311 msg_len ) );
5312
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005313 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5314 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005315 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005316 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005317 goto exit;
5318 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005319 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005320
5321 /* Prepare final header: copy msg_type, length and message_seq,
5322 * then add standardised fragment_offset and fragment_length */
5323 memcpy( hs_buf->data, ssl->in_msg, 6 );
5324 memset( hs_buf->data + 6, 0, 3 );
5325 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5326
5327 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005328
5329 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005330 }
5331 else
5332 {
5333 /* Make sure msg_type and length are consistent */
5334 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5335 {
5336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5337 /* Ignore */
5338 goto exit;
5339 }
5340 }
5341
Hanno Becker4422bbb2018-08-20 09:40:19 +01005342 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005343 {
5344 size_t frag_len, frag_off;
5345 unsigned char * const msg = hs_buf->data + 12;
5346
5347 /*
5348 * Check and copy current fragment
5349 */
5350
5351 /* Validation of header fields already done in
5352 * mbedtls_ssl_prepare_handshake_record(). */
5353 frag_off = ssl_get_hs_frag_off( ssl );
5354 frag_len = ssl_get_hs_frag_len( ssl );
5355
5356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5357 frag_off, frag_len ) );
5358 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5359
5360 if( hs_buf->is_fragmented )
5361 {
5362 unsigned char * const bitmask = msg + msg_len;
5363 ssl_bitmask_set( bitmask, frag_off, frag_len );
5364 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5365 msg_len ) == 0 );
5366 }
5367 else
5368 {
5369 hs_buf->is_complete = 1;
5370 }
5371
5372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5373 hs_buf->is_complete ? "" : "not yet " ) );
5374 }
5375
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005376 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005377 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005378
5379 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005380 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005381 break;
5382 }
5383
5384exit:
5385
5386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5387 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005388}
5389#endif /* MBEDTLS_SSL_PROTO_DTLS */
5390
Hanno Becker1097b342018-08-15 14:09:41 +01005391static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005392{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005393 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005394 * Consume last content-layer message and potentially
5395 * update in_msglen which keeps track of the contents'
5396 * consumption state.
5397 *
5398 * (1) Handshake messages:
5399 * Remove last handshake message, move content
5400 * and adapt in_msglen.
5401 *
5402 * (2) Alert messages:
5403 * Consume whole record content, in_msglen = 0.
5404 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005405 * (3) Change cipher spec:
5406 * Consume whole record content, in_msglen = 0.
5407 *
5408 * (4) Application data:
5409 * Don't do anything - the record layer provides
5410 * the application data as a stream transport
5411 * and consumes through mbedtls_ssl_read only.
5412 *
5413 */
5414
5415 /* Case (1): Handshake messages */
5416 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005417 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005418 /* Hard assertion to be sure that no application data
5419 * is in flight, as corrupting ssl->in_msglen during
5420 * ssl->in_offt != NULL is fatal. */
5421 if( ssl->in_offt != NULL )
5422 {
5423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5425 }
5426
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005427 /*
5428 * Get next Handshake message in the current record
5429 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005430
Hanno Becker4a810fb2017-05-24 16:27:30 +01005431 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005432 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005433 * current handshake content: If DTLS handshake
5434 * fragmentation is used, that's the fragment
5435 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005436 * size here is faulty and should be changed at
5437 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005438 * (2) While it doesn't seem to cause problems, one
5439 * has to be very careful not to assume that in_hslen
5440 * is always <= in_msglen in a sensible communication.
5441 * Again, it's wrong for DTLS handshake fragmentation.
5442 * The following check is therefore mandatory, and
5443 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005444 * Additionally, ssl->in_hslen might be arbitrarily out of
5445 * bounds after handling a DTLS message with an unexpected
5446 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005447 */
5448 if( ssl->in_hslen < ssl->in_msglen )
5449 {
5450 ssl->in_msglen -= ssl->in_hslen;
5451 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5452 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005453
Hanno Becker4a810fb2017-05-24 16:27:30 +01005454 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5455 ssl->in_msg, ssl->in_msglen );
5456 }
5457 else
5458 {
5459 ssl->in_msglen = 0;
5460 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005461
Hanno Becker4a810fb2017-05-24 16:27:30 +01005462 ssl->in_hslen = 0;
5463 }
5464 /* Case (4): Application data */
5465 else if( ssl->in_offt != NULL )
5466 {
5467 return( 0 );
5468 }
5469 /* Everything else (CCS & Alerts) */
5470 else
5471 {
5472 ssl->in_msglen = 0;
5473 }
5474
Hanno Becker1097b342018-08-15 14:09:41 +01005475 return( 0 );
5476}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005477
Hanno Beckere74d5562018-08-15 14:26:08 +01005478static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5479{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005480 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005481 return( 1 );
5482
5483 return( 0 );
5484}
5485
Hanno Becker5f066e72018-08-16 14:56:31 +01005486#if defined(MBEDTLS_SSL_PROTO_DTLS)
5487
5488static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5489{
5490 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5491 if( hs == NULL )
5492 return;
5493
Hanno Becker01315ea2018-08-21 17:22:17 +01005494 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005495 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005496 hs->buffering.total_bytes_buffered -=
5497 hs->buffering.future_record.len;
5498
5499 mbedtls_free( hs->buffering.future_record.data );
5500 hs->buffering.future_record.data = NULL;
5501 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005502}
5503
5504static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5505{
5506 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5507 unsigned char * rec;
5508 size_t rec_len;
5509 unsigned rec_epoch;
5510
5511 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5512 return( 0 );
5513
5514 if( hs == NULL )
5515 return( 0 );
5516
Hanno Becker5f066e72018-08-16 14:56:31 +01005517 rec = hs->buffering.future_record.data;
5518 rec_len = hs->buffering.future_record.len;
5519 rec_epoch = hs->buffering.future_record.epoch;
5520
5521 if( rec == NULL )
5522 return( 0 );
5523
Hanno Becker4cb782d2018-08-20 11:19:05 +01005524 /* Only consider loading future records if the
5525 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005526 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005527 return( 0 );
5528
Hanno Becker5f066e72018-08-16 14:56:31 +01005529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5530
5531 if( rec_epoch != ssl->in_epoch )
5532 {
5533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5534 goto exit;
5535 }
5536
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5538
5539 /* Double-check that the record is not too large */
5540 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5541 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5542 {
5543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5545 }
5546
5547 memcpy( ssl->in_hdr, rec, rec_len );
5548 ssl->in_left = rec_len;
5549 ssl->next_record_offset = 0;
5550
5551 ssl_free_buffered_record( ssl );
5552
5553exit:
5554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5555 return( 0 );
5556}
5557
5558static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5559{
5560 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5561 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005562 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005563
5564 /* Don't buffer future records outside handshakes. */
5565 if( hs == NULL )
5566 return( 0 );
5567
5568 /* Only buffer handshake records (we are only interested
5569 * in Finished messages). */
5570 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5571 return( 0 );
5572
5573 /* Don't buffer more than one future epoch record. */
5574 if( hs->buffering.future_record.data != NULL )
5575 return( 0 );
5576
Hanno Becker01315ea2018-08-21 17:22:17 +01005577 /* Don't buffer record if there's not enough buffering space remaining. */
5578 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5579 hs->buffering.total_bytes_buffered ) )
5580 {
5581 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",
5582 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5583 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005584 return( 0 );
5585 }
5586
Hanno Becker5f066e72018-08-16 14:56:31 +01005587 /* Buffer record */
5588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5589 ssl->in_epoch + 1 ) );
5590 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5591 rec_hdr_len + ssl->in_msglen );
5592
5593 /* ssl_parse_record_header() only considers records
5594 * of the next epoch as candidates for buffering. */
5595 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005596 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005597
5598 hs->buffering.future_record.data =
5599 mbedtls_calloc( 1, hs->buffering.future_record.len );
5600 if( hs->buffering.future_record.data == NULL )
5601 {
5602 /* If we run out of RAM trying to buffer a
5603 * record from the next epoch, just ignore. */
5604 return( 0 );
5605 }
5606
Hanno Becker01315ea2018-08-21 17:22:17 +01005607 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005608
Hanno Becker01315ea2018-08-21 17:22:17 +01005609 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005610 return( 0 );
5611}
5612
5613#endif /* MBEDTLS_SSL_PROTO_DTLS */
5614
Hanno Beckere74d5562018-08-15 14:26:08 +01005615static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005616{
5617 int ret;
5618
Hanno Becker5f066e72018-08-16 14:56:31 +01005619#if defined(MBEDTLS_SSL_PROTO_DTLS)
5620 /* We might have buffered a future record; if so,
5621 * and if the epoch matches now, load it.
5622 * On success, this call will set ssl->in_left to
5623 * the length of the buffered record, so that
5624 * the calls to ssl_fetch_input() below will
5625 * essentially be no-ops. */
5626 ret = ssl_load_buffered_record( ssl );
5627 if( ret != 0 )
5628 return( ret );
5629#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005630
Hanno Becker8b09b732019-05-08 12:03:28 +01005631 /* Reset in pointers to default state for TLS/DTLS records,
5632 * assuming no CID and no offset between record content and
5633 * record plaintext. */
5634 ssl_update_in_pointers( ssl );
5635
5636 /* Ensure that we have enough space available for the default form
5637 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5638 * with no space for CIDs counted in). */
5639 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5640 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005643 return( ret );
5644 }
5645
5646 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5650 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005651 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005652 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5653 {
5654 ret = ssl_buffer_future_record( ssl );
5655 if( ret != 0 )
5656 return( ret );
5657
5658 /* Fall through to handling of unexpected records */
5659 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5660 }
5661
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005662 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5663 {
5664 /* Skip unexpected record (but not whole datagram) */
5665 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005666 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005667
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5669 "(header)" ) );
5670 }
5671 else
5672 {
5673 /* Skip invalid record and the rest of the datagram */
5674 ssl->next_record_offset = 0;
5675 ssl->in_left = 0;
5676
5677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5678 "(header)" ) );
5679 }
5680
5681 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005682 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005683 }
5684#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005685 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005686 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005687
5688 /*
5689 * Read and optionally decrypt the message contents
5690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005692 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005695 return( ret );
5696 }
5697
5698 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005700 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005701 {
Hanno Becker43395762019-05-03 14:46:38 +01005702 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005703 if( ssl->next_record_offset < ssl->in_left )
5704 {
5705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5706 }
5707 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 else
5709#endif
5710 ssl->in_left = 0;
5711
5712 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005716 {
5717 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005718 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005719 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005720 /* Except when waiting for Finished as a bad mac here
5721 * probably means something went wrong in the handshake
5722 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5723 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5724 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5725 {
5726#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5727 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5728 {
5729 mbedtls_ssl_send_alert_message( ssl,
5730 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5731 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5732 }
5733#endif
5734 return( ret );
5735 }
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005738 if( ssl->conf->badmac_limit != 0 &&
5739 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5742 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005743 }
5744#endif
5745
Hanno Becker4a810fb2017-05-24 16:27:30 +01005746 /* As above, invalid records cause
5747 * dismissal of the whole datagram. */
5748
5749 ssl->next_record_offset = 0;
5750 ssl->in_left = 0;
5751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005753 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754 }
5755
5756 return( ret );
5757 }
5758 else
5759#endif
5760 {
5761 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5763 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 mbedtls_ssl_send_alert_message( ssl,
5766 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5767 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005768 }
5769#endif
5770 return( ret );
5771 }
5772 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005773
Simon Butcher99000142016-10-13 17:21:01 +01005774 return( 0 );
5775}
5776
5777int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5778{
5779 int ret;
5780
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005781 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005782 * Handle particular types of records
5783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 {
Simon Butcher99000142016-10-13 17:21:01 +01005786 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5787 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005788 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005790 }
5791
Hanno Beckere678eaa2018-08-21 14:57:46 +01005792 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005793 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005794 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005795 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5797 ssl->in_msglen ) );
5798 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005799 }
5800
Hanno Beckere678eaa2018-08-21 14:57:46 +01005801 if( ssl->in_msg[0] != 1 )
5802 {
5803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5804 ssl->in_msg[0] ) );
5805 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5806 }
5807
5808#if defined(MBEDTLS_SSL_PROTO_DTLS)
5809 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5810 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5811 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5812 {
5813 if( ssl->handshake == NULL )
5814 {
5815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5816 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5817 }
5818
5819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5820 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5821 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005822#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005823 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005826 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005827 if( ssl->in_msglen != 2 )
5828 {
5829 /* Note: Standard allows for more than one 2 byte alert
5830 to be packed in a single message, but Mbed TLS doesn't
5831 currently support this. */
5832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5833 ssl->in_msglen ) );
5834 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5835 }
5836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 ssl->in_msg[0], ssl->in_msg[1] ) );
5839
5840 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005841 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005846 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 }
5849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5851 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5854 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005856
5857#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5858 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5859 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5860 {
Hanno Becker90333da2017-10-10 11:27:13 +01005861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005862 /* Will be handled when trying to parse ServerHello */
5863 return( 0 );
5864 }
5865#endif
5866
5867#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5868 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5869 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5870 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5871 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5872 {
5873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5874 /* Will be handled in mbedtls_ssl_parse_certificate() */
5875 return( 0 );
5876 }
5877#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5878
5879 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005880 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 }
5882
Hanno Beckerc76c6192017-06-06 10:03:17 +01005883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005885 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005886 /* Drop unexpected ApplicationData records,
5887 * except at the beginning of renegotiations */
5888 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5889 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5890#if defined(MBEDTLS_SSL_RENEGOTIATION)
5891 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5892 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005893#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005894 )
5895 {
5896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5897 return( MBEDTLS_ERR_SSL_NON_FATAL );
5898 }
5899
5900 if( ssl->handshake != NULL &&
5901 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5902 {
5903 ssl_handshake_wrapup_free_hs_transform( ssl );
5904 }
5905 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005906#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005907
Paul Bakker5121ce52009-01-03 21:22:43 +00005908 return( 0 );
5909}
5910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005912{
5913 int ret;
5914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5916 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5917 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005918 {
5919 return( ret );
5920 }
5921
5922 return( 0 );
5923}
5924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005926 unsigned char level,
5927 unsigned char message )
5928{
5929 int ret;
5930
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005931 if( ssl == NULL || ssl->conf == NULL )
5932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005938 ssl->out_msglen = 2;
5939 ssl->out_msg[0] = level;
5940 ssl->out_msg[1] = message;
5941
Hanno Becker67bc7c32018-08-06 11:33:50 +01005942 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005945 return( ret );
5946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005948
5949 return( 0 );
5950}
5951
Paul Bakker5121ce52009-01-03 21:22:43 +00005952/*
5953 * Handshake functions
5954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5956 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5957 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5958 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5959 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5960 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5961 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005962/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005964{
Hanno Becker8759e162017-12-27 21:34:08 +00005965 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5970 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005971 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5972 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005975 ssl->state++;
5976 return( 0 );
5977 }
5978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005981}
5982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005984{
Hanno Becker8759e162017-12-27 21:34:08 +00005985 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5990 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5992 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005995 ssl->state++;
5996 return( 0 );
5997 }
5998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6000 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006001}
Gilles Peskinef9828522017-05-03 12:28:43 +02006002
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006003#else
Gilles Peskinef9828522017-05-03 12:28:43 +02006004/* Some certificate support -> implement write and parse */
6005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006011 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6016 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006017 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6018 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006021 ssl->state++;
6022 return( 0 );
6023 }
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006026 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 {
6028 if( ssl->client_auth == 0 )
6029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 ssl->state++;
6032 return( 0 );
6033 }
6034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 /*
6037 * If using SSLv3 and got no cert, send an Alert message
6038 * (otherwise an empty Certificate message will be sent).
6039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6041 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 {
6043 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6045 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6046 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006049 goto write_msg;
6050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#endif /* MBEDTLS_SSL_CLI_C */
6054#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006055 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6060 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 }
6062 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
6067 /*
6068 * 0 . 0 handshake type
6069 * 1 . 3 handshake length
6070 * 4 . 6 length of all certs
6071 * 7 . 9 length of cert. 1
6072 * 10 . n-1 peer certificate
6073 * n . n+2 length of cert. 2
6074 * n+3 . ... upper level cert, etc.
6075 */
6076 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006078
Paul Bakker29087132010-03-21 21:03:34 +00006079 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 {
6081 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006082 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006085 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006087 }
6088
6089 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6090 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6091 ssl->out_msg[i + 2] = (unsigned char)( n );
6092
6093 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6094 i += n; crt = crt->next;
6095 }
6096
6097 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6098 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6099 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6100
6101 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6103 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006104
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006105#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006106write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006108
6109 ssl->state++;
6110
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006111 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 return( ret );
6115 }
6116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006118
Paul Bakkered27a042013-04-18 22:46:23 +02006119 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006120}
6121
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006122/*
6123 * Once the certificate message is read, parse it into a cert chain and
6124 * perform basic checks, but leave actual verification to the caller
6125 */
6126static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006127{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006128 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006129 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006130 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132#if defined(MBEDTLS_SSL_SRV_C)
6133#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 /*
6135 * Check if the client sent an empty certificate
6136 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006137 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00006140 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6142 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6143 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006146
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006147 /* The client was asked for a certificate but didn't send
6148 one. The client should know what's going on, so we
6149 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006150 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006151 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006152 }
6153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6157 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006158 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6162 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6163 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6164 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006167
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006168 /* The client was asked for a certificate but didn't send
6169 one. The client should know what's going on, so we
6170 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006171 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006172 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006173 }
6174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6176 MBEDTLS_SSL_PROTO_TLS1_2 */
6177#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006182 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6183 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006185 }
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6188 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006191 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6192 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 }
6195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006197
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006200 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006201 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006202
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006203 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006207 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6208 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210 }
6211
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006212 /* In case we tried to reuse a session but it failed */
6213 if( ssl->session_negotiate->peer_cert != NULL )
6214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6216 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006217 }
6218
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006219 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006224 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6225 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006226 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006227 }
6228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006230
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006231 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006232
6233 while( i < ssl->in_hslen )
6234 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006235 if ( i + 3 > ssl->in_hslen ) {
6236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6238 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6239 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 if( ssl->in_msg[i] != 0 )
6242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006244 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6245 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 }
6248
6249 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6250 | (unsigned int) ssl->in_msg[i + 2];
6251 i += 3;
6252
6253 if( n < 128 || i + n > ssl->in_hslen )
6254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006256 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6257 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006259 }
6260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006262 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006263 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006264 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006265 case 0: /*ok*/
6266 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6267 /* Ignore certificate with an unknown algorithm: maybe a
6268 prior certificate was already trusted. */
6269 break;
6270
6271 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6272 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6273 goto crt_parse_der_failed;
6274
6275 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6276 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6277 goto crt_parse_der_failed;
6278
6279 default:
6280 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6281 crt_parse_der_failed:
6282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 return( ret );
6285 }
6286
6287 i += n;
6288 }
6289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006292 /*
6293 * On client, make sure the server cert doesn't change during renego to
6294 * avoid "triple handshake" attack: https://secure-resumption.com/
6295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006299 {
6300 if( ssl->session->peer_cert == NULL )
6301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006303 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6304 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006306 }
6307
6308 if( ssl->session->peer_cert->raw.len !=
6309 ssl->session_negotiate->peer_cert->raw.len ||
6310 memcmp( ssl->session->peer_cert->raw.p,
6311 ssl->session_negotiate->peer_cert->raw.p,
6312 ssl->session->peer_cert->raw.len ) != 0 )
6313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006315 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6316 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006318 }
6319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006321
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006322 return( 0 );
6323}
6324
6325int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6326{
6327 int ret;
6328 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006329 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006330#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6331 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6332 ? ssl->handshake->sni_authmode
6333 : ssl->conf->authmode;
6334#else
6335 const int authmode = ssl->conf->authmode;
6336#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006337 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006338
6339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6340
6341 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6342 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6343 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6344 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6345 {
6346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6347 ssl->state++;
6348 return( 0 );
6349 }
6350
6351#if defined(MBEDTLS_SSL_SRV_C)
6352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6353 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6354 {
6355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6356 ssl->state++;
6357 return( 0 );
6358 }
6359
6360 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6361 authmode == MBEDTLS_SSL_VERIFY_NONE )
6362 {
6363 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006365
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006366 ssl->state++;
6367 return( 0 );
6368 }
6369#endif
6370
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006371#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6372 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006373 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006374 {
6375 goto crt_verify;
6376 }
6377#endif
6378
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006379 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006380 {
6381 /* mbedtls_ssl_read_record may have sent an alert already. We
6382 let it decide whether to alert. */
6383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6384 return( ret );
6385 }
6386
6387 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6388 {
6389#if defined(MBEDTLS_SSL_SRV_C)
6390 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6391 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6392 {
6393 ret = 0;
6394 }
6395#endif
6396
6397 ssl->state++;
6398 return( ret );
6399 }
6400
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006401#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6402 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006403 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006404
6405crt_verify:
6406 if( ssl->handshake->ecrs_enabled)
6407 rs_ctx = &ssl->handshake->ecrs_ctx;
6408#endif
6409
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006410 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006412 mbedtls_x509_crt *ca_chain;
6413 mbedtls_x509_crl *ca_crl;
6414
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006415#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006416 if( ssl->handshake->sni_ca_chain != NULL )
6417 {
6418 ca_chain = ssl->handshake->sni_ca_chain;
6419 ca_crl = ssl->handshake->sni_ca_crl;
6420 }
6421 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006422#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006423 {
6424 ca_chain = ssl->conf->ca_chain;
6425 ca_crl = ssl->conf->ca_crl;
6426 }
6427
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006428 /*
6429 * Main check: verify certificate
6430 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006431 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006432 ssl->session_negotiate->peer_cert,
6433 ca_chain, ca_crl,
6434 ssl->conf->cert_profile,
6435 ssl->hostname,
6436 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006437 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006438
6439 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006442 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006443
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006444#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6445 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006446 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006447#endif
6448
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006449 /*
6450 * Secondary checks: always done, but change 'ret' only if it was 0
6451 */
6452
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006453#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006456
6457 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006459 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006460 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006461 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006464 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006466 }
6467 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006468#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006471 ciphersuite_info,
6472 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006473 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006476 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006478 }
6479
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006480 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6481 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6482 * with details encoded in the verification flags. All other kinds
6483 * of error codes, including those from the user provided f_vrfy
6484 * functions, are treated as fatal and lead to a failure of
6485 * ssl_parse_certificate even if verification was optional. */
6486 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6487 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6488 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6489 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006490 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006491 }
6492
6493 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6494 {
6495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6496 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6497 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006498
6499 if( ret != 0 )
6500 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006501 uint8_t alert;
6502
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006503 /* The certificate may have been rejected for several reasons.
6504 Pick one and send the corresponding alert. Which alert to send
6505 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006506 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6507 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6508 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6509 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6510 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6511 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6512 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6513 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6515 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6519 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6521 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6523 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6525 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006526 else
6527 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006528 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6529 alert );
6530 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006531
Hanno Beckere6706e62017-05-15 16:05:15 +01006532#if defined(MBEDTLS_DEBUG_C)
6533 if( ssl->session_negotiate->verify_result != 0 )
6534 {
6535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6536 ssl->session_negotiate->verify_result ) );
6537 }
6538 else
6539 {
6540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6541 }
6542#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 }
6544
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006545 ssl->state++;
6546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006548
6549 return( ret );
6550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6552 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6553 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6554 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6555 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6556 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6557 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560{
6561 int ret;
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006566 ssl->out_msglen = 1;
6567 ssl->out_msg[0] = 1;
6568
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 ssl->state++;
6570
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006571 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006572 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006574 return( ret );
6575 }
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006578
6579 return( 0 );
6580}
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006583{
6584 int ret;
6585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006587
Hanno Becker327c93b2018-08-15 13:56:18 +01006588 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 return( ret );
6592 }
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006597 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6598 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006600 }
6601
Hanno Beckere678eaa2018-08-21 14:57:46 +01006602 /* CCS records are only accepted if they have length 1 and content '1',
6603 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006604
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006605 /*
6606 * Switch to our negotiated transform and session parameters for inbound
6607 * data.
6608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006610 ssl->transform_in = ssl->transform_negotiate;
6611 ssl->session_in = ssl->session_negotiate;
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006614 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006617 ssl_dtls_replay_reset( ssl );
6618#endif
6619
6620 /* Increment epoch */
6621 if( ++ssl->in_epoch == 0 )
6622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006624 /* This is highly unlikely to happen for legitimate reasons, so
6625 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006627 }
6628 }
6629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006631 memset( ssl->in_ctr, 0, 8 );
6632
Hanno Beckerf5970a02019-05-08 09:38:41 +01006633 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6636 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006641 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6642 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006644 }
6645 }
6646#endif
6647
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 ssl->state++;
6649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651
6652 return( 0 );
6653}
6654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6656 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006657{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006658 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6661 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6662 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006663 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006664 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6667#if defined(MBEDTLS_SHA512_C)
6668 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006669 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6670 else
6671#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672#if defined(MBEDTLS_SHA256_C)
6673 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006674 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006675 else
6676#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006680 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006681 }
Paul Bakker380da532012-04-18 16:10:25 +00006682}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6687 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006688 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6689 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6692#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006693 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006696 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006699}
6700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006702 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6705 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006706 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6707 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6710#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006711 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006714 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006717}
6718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6720 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6721static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006722 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006723{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006724 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6725 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006726}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006727#endif
Paul Bakker380da532012-04-18 16:10:25 +00006728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6730#if defined(MBEDTLS_SHA256_C)
6731static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006732 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006733{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006734 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006735}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006736#endif
Paul Bakker380da532012-04-18 16:10:25 +00006737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#if defined(MBEDTLS_SHA512_C)
6739static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006740 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006741{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006742 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006743}
Paul Bakker769075d2012-11-24 11:26:46 +01006744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006748static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006750{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006751 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006752 mbedtls_md5_context md5;
6753 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006754
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 unsigned char padbuf[48];
6756 unsigned char md5sum[16];
6757 unsigned char sha1sum[20];
6758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006760 if( !session )
6761 session = ssl->session;
6762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006764
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006765 mbedtls_md5_init( &md5 );
6766 mbedtls_sha1_init( &sha1 );
6767
6768 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6769 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006770
6771 /*
6772 * SSLv3:
6773 * hash =
6774 * MD5( master + pad2 +
6775 * MD5( handshake + sender + master + pad1 ) )
6776 * + SHA1( master + pad2 +
6777 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 */
6779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006781 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6782 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006786 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6787 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006788#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006791 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
Paul Bakker1ef83d62012-04-11 12:09:53 +00006793 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006794
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006795 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6796 mbedtls_md5_update_ret( &md5, session->master, 48 );
6797 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6798 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006799
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006800 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6801 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6802 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6803 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006804
Paul Bakker1ef83d62012-04-11 12:09:53 +00006805 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006807 mbedtls_md5_starts_ret( &md5 );
6808 mbedtls_md5_update_ret( &md5, session->master, 48 );
6809 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6810 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6811 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006813 mbedtls_sha1_starts_ret( &sha1 );
6814 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6815 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6816 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6817 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006821 mbedtls_md5_free( &md5 );
6822 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006824 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6825 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6826 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006833static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006835{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006836 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006837 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006838 mbedtls_md5_context md5;
6839 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006840 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006843 if( !session )
6844 session = ssl->session;
6845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006848 mbedtls_md5_init( &md5 );
6849 mbedtls_sha1_init( &sha1 );
6850
6851 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6852 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Paul Bakker1ef83d62012-04-11 12:09:53 +00006854 /*
6855 * TLSv1:
6856 * hash = PRF( master, finished_label,
6857 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6858 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006861 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6862 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006863#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006866 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6867 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006868#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006871 ? "client finished"
6872 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006873
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006874 mbedtls_md5_finish_ret( &md5, padbuf );
6875 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006876
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006877 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006878 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006881
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006882 mbedtls_md5_free( &md5 );
6883 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006884
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006885 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6892#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006893static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006895{
6896 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006897 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006898 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006899 unsigned char padbuf[32];
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006902 if( !session )
6903 session = ssl->session;
6904
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006905 mbedtls_sha256_init( &sha256 );
6906
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006908
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006909 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006910
6911 /*
6912 * TLSv1.2:
6913 * hash = PRF( master, finished_label,
6914 * Hash( handshake ) )[0.11]
6915 */
6916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917#if !defined(MBEDTLS_SHA256_ALT)
6918 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006919 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006920#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006923 ? "client finished"
6924 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006925
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006926 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006927
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006928 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006929 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006932
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006933 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006934
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006935 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006942static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006944{
6945 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006946 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006947 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006948 unsigned char padbuf[48];
6949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006951 if( !session )
6952 session = ssl->session;
6953
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006954 mbedtls_sha512_init( &sha512 );
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006957
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006958 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006959
6960 /*
6961 * TLSv1.2:
6962 * hash = PRF( master, finished_label,
6963 * Hash( handshake ) )[0.11]
6964 */
6965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006967 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6968 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006969#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006972 ? "client finished"
6973 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006974
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006975 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006976
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006977 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006978 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006981
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006982 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006983
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006984 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#endif /* MBEDTLS_SHA512_C */
6989#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006994
6995 /*
6996 * Free our handshake params
6997 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006998 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007000 ssl->handshake = NULL;
7001
7002 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007003 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007004 */
7005 if( ssl->transform )
7006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 mbedtls_ssl_transform_free( ssl->transform );
7008 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007009 }
7010 ssl->transform = ssl->transform_negotiate;
7011 ssl->transform_negotiate = NULL;
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007014}
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007017{
7018 int resume = ssl->handshake->resume;
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022#if defined(MBEDTLS_SSL_RENEGOTIATION)
7023 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007026 ssl->renego_records_seen = 0;
7027 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007028#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007029
7030 /*
7031 * Free the previous session and switch in the current one
7032 */
Paul Bakker0a597072012-09-25 21:55:46 +00007033 if( ssl->session )
7034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007036 /* RFC 7366 3.1: keep the EtM state */
7037 ssl->session_negotiate->encrypt_then_mac =
7038 ssl->session->encrypt_then_mac;
7039#endif
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 mbedtls_ssl_session_free( ssl->session );
7042 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007043 }
7044 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007045 ssl->session_negotiate = NULL;
7046
Paul Bakker0a597072012-09-25 21:55:46 +00007047 /*
7048 * Add cache entry
7049 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007050 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007051 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007052 resume == 0 )
7053 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007054 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007056 }
Paul Bakker0a597072012-09-25 21:55:46 +00007057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007060 ssl->handshake->flight != NULL )
7061 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007062 /* Cancel handshake timer */
7063 ssl_set_timer( ssl, 0 );
7064
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007065 /* Keep last flight around in case we need to resend it:
7066 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007068 }
7069 else
7070#endif
7071 ssl_handshake_wrapup_free_hs_transform( ssl );
7072
Paul Bakker48916f92012-09-16 19:57:18 +00007073 ssl->state++;
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007076}
7077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007079{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007080 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007083
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007084 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007085
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007086 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007087
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007088 /*
7089 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7090 * may define some other value. Currently (early 2016), no defined
7091 * ciphersuite does this (and this is unlikely to change as activity has
7092 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007097 ssl->verify_data_len = hash_len;
7098 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007099#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007100
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7103 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007104
7105 /*
7106 * In case of session resuming, invert the client and server
7107 * ChangeCipherSpec messages order.
7108 */
Paul Bakker0a597072012-09-25 21:55:46 +00007109 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007112 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007116 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 }
7120 else
7121 ssl->state++;
7122
Paul Bakker48916f92012-09-16 19:57:18 +00007123 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007124 * Switch to our negotiated transform and session parameters for outbound
7125 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007131 {
7132 unsigned char i;
7133
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007134 /* Remember current epoch settings for resending */
7135 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007136 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007137
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007138 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007139 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007140
7141 /* Increment epoch */
7142 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007143 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007144 break;
7145
7146 /* The loop goes to its end iff the counter is wrapping */
7147 if( i == 0 )
7148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7150 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007151 }
7152 }
7153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007155 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007156
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007157 ssl->transform_out = ssl->transform_negotiate;
7158 ssl->session_out = ssl->session_negotiate;
7159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7161 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7166 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007167 }
7168 }
7169#endif
7170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007174#endif
7175
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007176 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007179 return( ret );
7180 }
7181
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007182#if defined(MBEDTLS_SSL_PROTO_DTLS)
7183 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7184 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7185 {
7186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7187 return( ret );
7188 }
7189#endif
7190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007192
7193 return( 0 );
7194}
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007197#define SSL_MAX_HASH_LEN 36
7198#else
7199#define SSL_MAX_HASH_LEN 12
7200#endif
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007203{
Paul Bakker23986e52011-04-24 08:57:21 +00007204 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007205 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007206 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007209
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007210 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007211
Hanno Becker327c93b2018-08-15 13:56:18 +01007212 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007215 return( ret );
7216 }
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007221 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7222 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007224 }
7225
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007226 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#if defined(MBEDTLS_SSL_PROTO_SSL3)
7228 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007229 hash_len = 36;
7230 else
7231#endif
7232 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7235 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007238 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7239 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007241 }
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007244 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007247 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007250 }
7251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007253 ssl->verify_data_len = hash_len;
7254 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007255#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007256
Paul Bakker0a597072012-09-25 21:55:46 +00007257 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007264 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007267 }
7268 else
7269 ssl->state++;
7270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007274#endif
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007277
7278 return( 0 );
7279}
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7286 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7287 mbedtls_md5_init( &handshake->fin_md5 );
7288 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007289 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7290 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7293#if defined(MBEDTLS_SHA256_C)
7294 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007295 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SHA512_C)
7298 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007299 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007302
7303 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007304
7305#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7306 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7307 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7308#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_DHM_C)
7311 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313#if defined(MBEDTLS_ECDH_C)
7314 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007315#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007316#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007317 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007318#if defined(MBEDTLS_SSL_CLI_C)
7319 handshake->ecjpake_cache = NULL;
7320 handshake->ecjpake_cache_len = 0;
7321#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007322#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007323
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007324#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007325 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007326#endif
7327
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007328#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7329 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7330#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007331}
7332
Hanno Becker611a83b2018-01-03 14:27:32 +00007333void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7338 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007339
Hanno Becker92231322018-01-03 15:32:51 +00007340#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 mbedtls_md_init( &transform->md_ctx_enc );
7342 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007343#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007344}
7345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007349}
7350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007352{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007353 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007354 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007356 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007358 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007359 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007360
7361 /*
7362 * Either the pointers are now NULL or cleared properly and can be freed.
7363 * Now allocate missing structures.
7364 */
7365 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007366 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007367 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007368 }
Paul Bakker48916f92012-09-16 19:57:18 +00007369
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007370 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007371 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007372 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007373 }
Paul Bakker48916f92012-09-16 19:57:18 +00007374
Paul Bakker82788fb2014-10-20 13:59:19 +02007375 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007376 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007377 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007378 }
Paul Bakker48916f92012-09-16 19:57:18 +00007379
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007380 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007381 if( ssl->handshake == NULL ||
7382 ssl->transform_negotiate == NULL ||
7383 ssl->session_negotiate == NULL )
7384 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 mbedtls_free( ssl->handshake );
7388 mbedtls_free( ssl->transform_negotiate );
7389 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007390
7391 ssl->handshake = NULL;
7392 ssl->transform_negotiate = NULL;
7393 ssl->session_negotiate = NULL;
7394
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007395 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007396 }
7397
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007398 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007400 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007401 ssl_handshake_params_init( ssl->handshake );
7402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7405 {
7406 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007407
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007408 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7409 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7410 else
7411 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007412
7413 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007414 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007415#endif
7416
Paul Bakker48916f92012-09-16 19:57:18 +00007417 return( 0 );
7418}
7419
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007420#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007421/* Dummy cookie callbacks for defaults */
7422static int ssl_cookie_write_dummy( void *ctx,
7423 unsigned char **p, unsigned char *end,
7424 const unsigned char *cli_id, size_t cli_id_len )
7425{
7426 ((void) ctx);
7427 ((void) p);
7428 ((void) end);
7429 ((void) cli_id);
7430 ((void) cli_id_len);
7431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007433}
7434
7435static int ssl_cookie_check_dummy( void *ctx,
7436 const unsigned char *cookie, size_t cookie_len,
7437 const unsigned char *cli_id, size_t cli_id_len )
7438{
7439 ((void) ctx);
7440 ((void) cookie);
7441 ((void) cookie_len);
7442 ((void) cli_id);
7443 ((void) cli_id_len);
7444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007446}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007447#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007448
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007449/* Once ssl->out_hdr as the address of the beginning of the
7450 * next outgoing record is set, deduce the other pointers.
7451 *
7452 * Note: For TLS, we save the implicit record sequence number
7453 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7454 * and the caller has to make sure there's space for this.
7455 */
7456
7457static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7458 mbedtls_ssl_transform *transform )
7459{
7460#if defined(MBEDTLS_SSL_PROTO_DTLS)
7461 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7462 {
7463 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007464#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007465 ssl->out_cid = ssl->out_ctr + 8;
7466 ssl->out_len = ssl->out_cid;
7467 if( transform != NULL )
7468 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007469#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007470 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007471#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007472 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007473 }
7474 else
7475#endif
7476 {
7477 ssl->out_ctr = ssl->out_hdr - 8;
7478 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007479#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007480 ssl->out_cid = ssl->out_len;
7481#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007482 ssl->out_iv = ssl->out_hdr + 5;
7483 }
7484
7485 /* Adjust out_msg to make space for explicit IV, if used. */
7486 if( transform != NULL &&
7487 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7488 {
7489 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7490 }
7491 else
7492 ssl->out_msg = ssl->out_iv;
7493}
7494
7495/* Once ssl->in_hdr as the address of the beginning of the
7496 * next incoming record is set, deduce the other pointers.
7497 *
7498 * Note: For TLS, we save the implicit record sequence number
7499 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7500 * and the caller has to make sure there's space for this.
7501 */
7502
Hanno Beckerf5970a02019-05-08 09:38:41 +01007503static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007504{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007505 /* This function sets the pointers to match the case
7506 * of unprotected TLS/DTLS records, with both ssl->in_iv
7507 * and ssl->in_msg pointing to the beginning of the record
7508 * content.
7509 *
7510 * When decrypting a protected record, ssl->in_msg
7511 * will be shifted to point to the beginning of the
7512 * record plaintext.
7513 */
7514
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007515#if defined(MBEDTLS_SSL_PROTO_DTLS)
7516 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7517 {
Hanno Becker70e79282019-05-03 14:34:53 +01007518 /* This sets the header pointers to match records
7519 * without CID. When we receive a record containing
7520 * a CID, the fields are shifted accordingly in
7521 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007522 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007523#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007524 ssl->in_cid = ssl->in_ctr + 8;
7525 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007526#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007527 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007528#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007529 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007530 }
7531 else
7532#endif
7533 {
7534 ssl->in_ctr = ssl->in_hdr - 8;
7535 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007536#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007537 ssl->in_cid = ssl->in_len;
7538#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007539 ssl->in_iv = ssl->in_hdr + 5;
7540 }
7541
Hanno Beckerf5970a02019-05-08 09:38:41 +01007542 /* This will be adjusted at record decryption time. */
7543 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007544}
7545
Paul Bakker5121ce52009-01-03 21:22:43 +00007546/*
7547 * Initialize an SSL context
7548 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007549void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7550{
7551 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7552}
7553
7554/*
7555 * Setup an SSL context
7556 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007557
7558static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7559{
7560 /* Set the incoming and outgoing record pointers. */
7561#if defined(MBEDTLS_SSL_PROTO_DTLS)
7562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7563 {
7564 ssl->out_hdr = ssl->out_buf;
7565 ssl->in_hdr = ssl->in_buf;
7566 }
7567 else
7568#endif /* MBEDTLS_SSL_PROTO_DTLS */
7569 {
7570 ssl->out_hdr = ssl->out_buf + 8;
7571 ssl->in_hdr = ssl->in_buf + 8;
7572 }
7573
7574 /* Derive other internal pointers. */
7575 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007576 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007577}
7578
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007579int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007580 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007581{
Paul Bakker48916f92012-09-16 19:57:18 +00007582 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007583
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007584 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007585
7586 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007587 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007588 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007589
7590 /* Set to NULL in case of an error condition */
7591 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007592
Angus Grattond8213d02016-05-25 20:56:48 +10007593 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7594 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007595 {
Angus Grattond8213d02016-05-25 20:56:48 +10007596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007597 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007598 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007599 }
7600
7601 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7602 if( ssl->out_buf == NULL )
7603 {
7604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007605 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007606 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007607 }
7608
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007609 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007610
Paul Bakker48916f92012-09-16 19:57:18 +00007611 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007612 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007613
7614 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007615
7616error:
7617 mbedtls_free( ssl->in_buf );
7618 mbedtls_free( ssl->out_buf );
7619
7620 ssl->conf = NULL;
7621
7622 ssl->in_buf = NULL;
7623 ssl->out_buf = NULL;
7624
7625 ssl->in_hdr = NULL;
7626 ssl->in_ctr = NULL;
7627 ssl->in_len = NULL;
7628 ssl->in_iv = NULL;
7629 ssl->in_msg = NULL;
7630
7631 ssl->out_hdr = NULL;
7632 ssl->out_ctr = NULL;
7633 ssl->out_len = NULL;
7634 ssl->out_iv = NULL;
7635 ssl->out_msg = NULL;
7636
k-stachowiak9f7798e2018-07-31 16:52:32 +02007637 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007638}
7639
7640/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007641 * Reset an initialized and used SSL context for re-use while retaining
7642 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007643 *
7644 * If partial is non-zero, keep data in the input buffer and client ID.
7645 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007646 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007647static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007648{
Paul Bakker48916f92012-09-16 19:57:18 +00007649 int ret;
7650
Hanno Becker7e772132018-08-10 12:38:21 +01007651#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7652 !defined(MBEDTLS_SSL_SRV_C)
7653 ((void) partial);
7654#endif
7655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007657
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007658 /* Cancel any possibly running timer */
7659 ssl_set_timer( ssl, 0 );
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661#if defined(MBEDTLS_SSL_RENEGOTIATION)
7662 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007663 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007664
7665 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7667 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007670
Paul Bakker7eb013f2011-10-06 12:37:39 +00007671 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007672 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007673
7674 ssl->in_msgtype = 0;
7675 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007677 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007678 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007679#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007681 ssl_dtls_replay_reset( ssl );
7682#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007683
7684 ssl->in_hslen = 0;
7685 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007686
7687 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007688
7689 ssl->out_msgtype = 0;
7690 ssl->out_msglen = 0;
7691 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7693 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007694 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007695#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007696
Hanno Becker19859472018-08-06 09:40:20 +01007697 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7698
Paul Bakker48916f92012-09-16 19:57:18 +00007699 ssl->transform_in = NULL;
7700 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007701
Hanno Becker78640902018-08-13 16:35:15 +01007702 ssl->session_in = NULL;
7703 ssl->session_out = NULL;
7704
Angus Grattond8213d02016-05-25 20:56:48 +10007705 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007706
7707#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007708 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007709#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7710 {
7711 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007712 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007713 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7716 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7719 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7722 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007723 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007724 }
7725#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007726
Paul Bakker48916f92012-09-16 19:57:18 +00007727 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 mbedtls_ssl_transform_free( ssl->transform );
7730 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007731 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007732 }
Paul Bakker48916f92012-09-16 19:57:18 +00007733
Paul Bakkerc0463502013-02-14 11:19:38 +01007734 if( ssl->session )
7735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 mbedtls_ssl_session_free( ssl->session );
7737 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007738 ssl->session = NULL;
7739 }
7740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007742 ssl->alpn_chosen = NULL;
7743#endif
7744
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007745#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007746#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007747 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007748#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007749 {
7750 mbedtls_free( ssl->cli_id );
7751 ssl->cli_id = NULL;
7752 ssl->cli_id_len = 0;
7753 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007754#endif
7755
Paul Bakker48916f92012-09-16 19:57:18 +00007756 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7757 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007758
7759 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007760}
7761
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007762/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007763 * Reset an initialized and used SSL context for re-use while retaining
7764 * all application-set variables, function pointers and data.
7765 */
7766int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7767{
7768 return( ssl_session_reset_int( ssl, 0 ) );
7769}
7770
7771/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 * SSL set accessors
7773 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007774void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007775{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007776 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007777}
7778
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007779void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007780{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007781 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007782}
7783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007785void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007787 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007788}
7789#endif
7790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007792void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007793{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007794 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007795}
7796#endif
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007799
Hanno Becker1841b0a2018-08-24 11:13:57 +01007800void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7801 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007802{
7803 ssl->disable_datagram_packing = !allow_packing;
7804}
7805
7806void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7807 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007808{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007809 conf->hs_timeout_min = min;
7810 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007811}
7812#endif
7813
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007814void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007815{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007816 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007817}
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007820void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007821 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007822 void *p_vrfy )
7823{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007824 conf->f_vrfy = f_vrfy;
7825 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007828
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007829void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007830 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007831 void *p_rng )
7832{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007833 conf->f_rng = f_rng;
7834 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007835}
7836
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007837void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007838 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007839 void *p_dbg )
7840{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007841 conf->f_dbg = f_dbg;
7842 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007843}
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007846 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007847 mbedtls_ssl_send_t *f_send,
7848 mbedtls_ssl_recv_t *f_recv,
7849 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007850{
7851 ssl->p_bio = p_bio;
7852 ssl->f_send = f_send;
7853 ssl->f_recv = f_recv;
7854 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007855}
7856
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007857#if defined(MBEDTLS_SSL_PROTO_DTLS)
7858void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7859{
7860 ssl->mtu = mtu;
7861}
7862#endif
7863
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007864void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007865{
7866 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007867}
7868
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007869void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7870 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007871 mbedtls_ssl_set_timer_t *f_set_timer,
7872 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007873{
7874 ssl->p_timer = p_timer;
7875 ssl->f_set_timer = f_set_timer;
7876 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007877
7878 /* Make sure we start with no timer running */
7879 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007880}
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007883void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007884 void *p_cache,
7885 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7886 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007887{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007888 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007889 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007890 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_CLI_C)
7895int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007896{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007897 int ret;
7898
7899 if( ssl == NULL ||
7900 session == NULL ||
7901 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007902 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007905 }
7906
7907 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7908 return( ret );
7909
Paul Bakker0a597072012-09-25 21:55:46 +00007910 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007911
7912 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007915
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007916void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007917 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007918{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007919 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7920 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7921 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7922 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007923}
7924
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007925void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007926 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007927 int major, int minor )
7928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007930 return;
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007933 return;
7934
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007935 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007936}
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007939void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007940 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007941{
7942 conf->cert_profile = profile;
7943}
7944
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007945/* Append a new keycert entry to a (possibly empty) list */
7946static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7947 mbedtls_x509_crt *cert,
7948 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007949{
niisato8ee24222018-06-25 19:05:48 +09007950 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007951
niisato8ee24222018-06-25 19:05:48 +09007952 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7953 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007954 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007955
niisato8ee24222018-06-25 19:05:48 +09007956 new_cert->cert = cert;
7957 new_cert->key = key;
7958 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007959
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007960 /* Update head is the list was null, else add to the end */
7961 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007962 {
niisato8ee24222018-06-25 19:05:48 +09007963 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007964 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007965 else
7966 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007967 mbedtls_ssl_key_cert *cur = *head;
7968 while( cur->next != NULL )
7969 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007970 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007971 }
7972
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007973 return( 0 );
7974}
7975
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007976int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007977 mbedtls_x509_crt *own_cert,
7978 mbedtls_pk_context *pk_key )
7979{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007980 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007981}
7982
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007983void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007984 mbedtls_x509_crt *ca_chain,
7985 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007986{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007987 conf->ca_chain = ca_chain;
7988 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007991
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007992#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7993int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7994 mbedtls_x509_crt *own_cert,
7995 mbedtls_pk_context *pk_key )
7996{
7997 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7998 own_cert, pk_key ) );
7999}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008000
8001void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8002 mbedtls_x509_crt *ca_chain,
8003 mbedtls_x509_crl *ca_crl )
8004{
8005 ssl->handshake->sni_ca_chain = ca_chain;
8006 ssl->handshake->sni_ca_crl = ca_crl;
8007}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008008
8009void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8010 int authmode )
8011{
8012 ssl->handshake->sni_authmode = authmode;
8013}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008014#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8015
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008016#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008017/*
8018 * Set EC J-PAKE password for current handshake
8019 */
8020int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8021 const unsigned char *pw,
8022 size_t pw_len )
8023{
8024 mbedtls_ecjpake_role role;
8025
Janos Follath8eb64132016-06-03 15:40:57 +01008026 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8028
8029 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8030 role = MBEDTLS_ECJPAKE_SERVER;
8031 else
8032 role = MBEDTLS_ECJPAKE_CLIENT;
8033
8034 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8035 role,
8036 MBEDTLS_MD_SHA256,
8037 MBEDTLS_ECP_DP_SECP256R1,
8038 pw, pw_len ) );
8039}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008040#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008043int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008044 const unsigned char *psk, size_t psk_len,
8045 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008046{
Paul Bakker6db455e2013-09-18 17:29:31 +02008047 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008052
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008053 /* Identity len will be encoded on two bytes */
8054 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008055 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008056 {
8057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8058 }
8059
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008060 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008061 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008062 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008063
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008064 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008065 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008066 conf->psk_len = 0;
8067 }
8068 if( conf->psk_identity != NULL )
8069 {
8070 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008071 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008072 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008073 }
8074
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008075 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8076 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008077 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008078 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008079 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008080 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008081 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008082 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008083 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008084
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008085 conf->psk_len = psk_len;
8086 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008087
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008088 memcpy( conf->psk, psk, conf->psk_len );
8089 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008090
8091 return( 0 );
8092}
8093
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008094int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8095 const unsigned char *psk, size_t psk_len )
8096{
8097 if( psk == NULL || ssl->handshake == NULL )
8098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8099
8100 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8102
8103 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008104 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008105 mbedtls_platform_zeroize( ssl->handshake->psk,
8106 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008107 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008108 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008109 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008110
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008111 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008113
8114 ssl->handshake->psk_len = psk_len;
8115 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8116
8117 return( 0 );
8118}
8119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008120void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008122 size_t),
8123 void *p_psk )
8124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008125 conf->f_psk = f_psk;
8126 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008129
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008130#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008131
8132#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008133int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008134{
8135 int ret;
8136
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008137 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8138 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8139 {
8140 mbedtls_mpi_free( &conf->dhm_P );
8141 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008142 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008144
8145 return( 0 );
8146}
Hanno Becker470a8c42017-10-04 15:28:46 +01008147#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008148
Hanno Beckera90658f2017-10-04 15:29:08 +01008149int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8150 const unsigned char *dhm_P, size_t P_len,
8151 const unsigned char *dhm_G, size_t G_len )
8152{
8153 int ret;
8154
8155 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8156 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8157 {
8158 mbedtls_mpi_free( &conf->dhm_P );
8159 mbedtls_mpi_free( &conf->dhm_G );
8160 return( ret );
8161 }
8162
8163 return( 0 );
8164}
Paul Bakker5121ce52009-01-03 21:22:43 +00008165
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008166int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008167{
8168 int ret;
8169
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008170 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8171 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8172 {
8173 mbedtls_mpi_free( &conf->dhm_P );
8174 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008175 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008176 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008177
8178 return( 0 );
8179}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008180#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008181
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008182#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8183/*
8184 * Set the minimum length for Diffie-Hellman parameters
8185 */
8186void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8187 unsigned int bitlen )
8188{
8189 conf->dhm_min_bitlen = bitlen;
8190}
8191#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8192
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008193#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008194/*
8195 * Set allowed/preferred hashes for handshake signatures
8196 */
8197void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8198 const int *hashes )
8199{
8200 conf->sig_hashes = hashes;
8201}
Hanno Becker947194e2017-04-07 13:25:49 +01008202#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008203
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008204#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008205/*
8206 * Set the allowed elliptic curves
8207 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008208void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008209 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008210{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008211 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008212}
Hanno Becker947194e2017-04-07 13:25:49 +01008213#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008214
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008215#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008217{
Hanno Becker947194e2017-04-07 13:25:49 +01008218 /* Initialize to suppress unnecessary compiler warning */
8219 size_t hostname_len = 0;
8220
8221 /* Check if new hostname is valid before
8222 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008223 if( hostname != NULL )
8224 {
8225 hostname_len = strlen( hostname );
8226
8227 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8229 }
8230
8231 /* Now it's clear that we will overwrite the old hostname,
8232 * so we can free it safely */
8233
8234 if( ssl->hostname != NULL )
8235 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008236 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008237 mbedtls_free( ssl->hostname );
8238 }
8239
8240 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008241
Paul Bakker5121ce52009-01-03 21:22:43 +00008242 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008243 {
8244 ssl->hostname = NULL;
8245 }
8246 else
8247 {
8248 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008249 if( ssl->hostname == NULL )
8250 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008251
Hanno Becker947194e2017-04-07 13:25:49 +01008252 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008253
Hanno Becker947194e2017-04-07 13:25:49 +01008254 ssl->hostname[hostname_len] = '\0';
8255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008256
8257 return( 0 );
8258}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008259#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008260
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008261#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008262void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008264 const unsigned char *, size_t),
8265 void *p_sni )
8266{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008267 conf->f_sni = f_sni;
8268 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008273int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008274{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008275 size_t cur_len, tot_len;
8276 const char **p;
8277
8278 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008279 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8280 * MUST NOT be truncated."
8281 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008282 */
8283 tot_len = 0;
8284 for( p = protos; *p != NULL; p++ )
8285 {
8286 cur_len = strlen( *p );
8287 tot_len += cur_len;
8288
8289 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008291 }
8292
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008293 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008294
8295 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008296}
8297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008299{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008300 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008303
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008304void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008305{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008306 conf->max_major_ver = major;
8307 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008308}
8309
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008310void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008311{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008312 conf->min_major_ver = major;
8313 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008314}
8315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008317void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008318{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008319 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008320}
8321#endif
8322
Janos Follath088ce432017-04-10 12:42:31 +01008323#if defined(MBEDTLS_SSL_SRV_C)
8324void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8325 char cert_req_ca_list )
8326{
8327 conf->cert_req_ca_list = cert_req_ca_list;
8328}
8329#endif
8330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008332void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008334 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008335}
8336#endif
8337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008339void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008340{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008341 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008342}
8343#endif
8344
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008345#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008346void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008347{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008348 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008349}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008350#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008353int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008356 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008359 }
8360
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008361 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008362
8363 return( 0 );
8364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008368void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008369{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008370 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008375void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008376{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008377 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008378}
8379#endif
8380
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008381void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008382{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008383 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008384}
8385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008387void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008388{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008389 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008390}
8391
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008392void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008393{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008394 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008395}
8396
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008397void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008398 const unsigned char period[8] )
8399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008405#if defined(MBEDTLS_SSL_CLI_C)
8406void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008407{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008408 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008409}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008410#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008411
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008412#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008413void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8414 mbedtls_ssl_ticket_write_t *f_ticket_write,
8415 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8416 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008417{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008418 conf->f_ticket_write = f_ticket_write;
8419 conf->f_ticket_parse = f_ticket_parse;
8420 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008421}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008424
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008425#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8426void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8427 mbedtls_ssl_export_keys_t *f_export_keys,
8428 void *p_export_keys )
8429{
8430 conf->f_export_keys = f_export_keys;
8431 conf->p_export_keys = p_export_keys;
8432}
8433#endif
8434
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008435#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008436void mbedtls_ssl_conf_async_private_cb(
8437 mbedtls_ssl_config *conf,
8438 mbedtls_ssl_async_sign_t *f_async_sign,
8439 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8440 mbedtls_ssl_async_resume_t *f_async_resume,
8441 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008442 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008443{
8444 conf->f_async_sign_start = f_async_sign;
8445 conf->f_async_decrypt_start = f_async_decrypt;
8446 conf->f_async_resume = f_async_resume;
8447 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008448 conf->p_async_config_data = async_config_data;
8449}
8450
Gilles Peskine8f97af72018-04-26 11:46:10 +02008451void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8452{
8453 return( conf->p_async_config_data );
8454}
8455
Gilles Peskine1febfef2018-04-30 11:54:39 +02008456void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008457{
8458 if( ssl->handshake == NULL )
8459 return( NULL );
8460 else
8461 return( ssl->handshake->user_async_ctx );
8462}
8463
Gilles Peskine1febfef2018-04-30 11:54:39 +02008464void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008465 void *ctx )
8466{
8467 if( ssl->handshake != NULL )
8468 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008469}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008470#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008471
Paul Bakker5121ce52009-01-03 21:22:43 +00008472/*
8473 * SSL get accessors
8474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008476{
8477 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8478}
8479
Hanno Becker8b170a02017-10-10 11:51:19 +01008480int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8481{
8482 /*
8483 * Case A: We're currently holding back
8484 * a message for further processing.
8485 */
8486
8487 if( ssl->keep_current_message == 1 )
8488 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008490 return( 1 );
8491 }
8492
8493 /*
8494 * Case B: Further records are pending in the current datagram.
8495 */
8496
8497#if defined(MBEDTLS_SSL_PROTO_DTLS)
8498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8499 ssl->in_left > ssl->next_record_offset )
8500 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008502 return( 1 );
8503 }
8504#endif /* MBEDTLS_SSL_PROTO_DTLS */
8505
8506 /*
8507 * Case C: A handshake message is being processed.
8508 */
8509
Hanno Becker8b170a02017-10-10 11:51:19 +01008510 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8511 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008513 return( 1 );
8514 }
8515
8516 /*
8517 * Case D: An application data message is being processed
8518 */
8519 if( ssl->in_offt != NULL )
8520 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008522 return( 1 );
8523 }
8524
8525 /*
8526 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008527 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008528 * we implement support for multiple alerts in single records.
8529 */
8530
8531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8532 return( 0 );
8533}
8534
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008535uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008536{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008537 if( ssl->session != NULL )
8538 return( ssl->session->verify_result );
8539
8540 if( ssl->session_negotiate != NULL )
8541 return( ssl->session_negotiate->verify_result );
8542
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008543 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008544}
8545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008547{
Paul Bakker926c8e42013-03-06 10:23:34 +01008548 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008549 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008552}
8553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008557 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008558 {
8559 switch( ssl->minor_ver )
8560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008562 return( "DTLSv1.0" );
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008565 return( "DTLSv1.2" );
8566
8567 default:
8568 return( "unknown (DTLS)" );
8569 }
8570 }
8571#endif
8572
Paul Bakker43ca69c2011-01-15 17:35:19 +00008573 switch( ssl->minor_ver )
8574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008576 return( "SSLv3.0" );
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008579 return( "TLSv1.0" );
8580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008582 return( "TLSv1.1" );
8583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008585 return( "TLSv1.2" );
8586
Paul Bakker43ca69c2011-01-15 17:35:19 +00008587 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008588 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008589 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008590}
8591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008593{
Hanno Becker3136ede2018-08-17 15:28:19 +01008594 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008596 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008597
Hanno Becker43395762019-05-03 14:46:38 +01008598 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8599
Hanno Becker78640902018-08-13 16:35:15 +01008600 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008601 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603#if defined(MBEDTLS_ZLIB_SUPPORT)
8604 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8605 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008606#endif
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610 case MBEDTLS_MODE_GCM:
8611 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008612 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008614 transform_expansion = transform->minlen;
8615 break;
8616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008618
8619 block_size = mbedtls_cipher_get_block_size(
8620 &transform->cipher_ctx_enc );
8621
Hanno Becker3136ede2018-08-17 15:28:19 +01008622 /* Expansion due to the addition of the MAC. */
8623 transform_expansion += transform->maclen;
8624
8625 /* Expansion due to the addition of CBC padding;
8626 * Theoretically up to 256 bytes, but we never use
8627 * more than the block size of the underlying cipher. */
8628 transform_expansion += block_size;
8629
8630 /* For TLS 1.1 or higher, an explicit IV is added
8631 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008632#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8633 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008634 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008635#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008636
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008637 break;
8638
8639 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008642 }
8643
Hanno Beckera5a2b082019-05-15 14:03:01 +01008644#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008645 if( transform->out_cid_len != 0 )
8646 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008647#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008648
Hanno Becker43395762019-05-03 14:46:38 +01008649 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008650}
8651
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008652#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8653size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8654{
8655 size_t max_len;
8656
8657 /*
8658 * Assume mfl_code is correct since it was checked when set
8659 */
Angus Grattond8213d02016-05-25 20:56:48 +10008660 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008661
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008662 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008663 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008664 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008665 {
Angus Grattond8213d02016-05-25 20:56:48 +10008666 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008667 }
8668
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008669 /* During a handshake, use the value being negotiated */
8670 if( ssl->session_negotiate != NULL &&
8671 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8672 {
8673 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8674 }
8675
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008676 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008677}
8678#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8679
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008680#if defined(MBEDTLS_SSL_PROTO_DTLS)
8681static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8682{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008683 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8685 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8686 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8687 return ( 0 );
8688
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008689 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8690 return( ssl->mtu );
8691
8692 if( ssl->mtu == 0 )
8693 return( ssl->handshake->mtu );
8694
8695 return( ssl->mtu < ssl->handshake->mtu ?
8696 ssl->mtu : ssl->handshake->mtu );
8697}
8698#endif /* MBEDTLS_SSL_PROTO_DTLS */
8699
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008700int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8701{
8702 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8703
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008704#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8705 !defined(MBEDTLS_SSL_PROTO_DTLS)
8706 (void) ssl;
8707#endif
8708
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008709#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8710 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8711
8712 if( max_len > mfl )
8713 max_len = mfl;
8714#endif
8715
8716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008717 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008718 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008719 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008720 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8721 const size_t overhead = (size_t) ret;
8722
8723 if( ret < 0 )
8724 return( ret );
8725
8726 if( mtu <= overhead )
8727 {
8728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8729 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8730 }
8731
8732 if( max_len > mtu - overhead )
8733 max_len = mtu - overhead;
8734 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008735#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008736
Hanno Becker0defedb2018-08-10 12:35:02 +01008737#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8738 !defined(MBEDTLS_SSL_PROTO_DTLS)
8739 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008740#endif
8741
8742 return( (int) max_len );
8743}
8744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745#if defined(MBEDTLS_X509_CRT_PARSE_C)
8746const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008747{
8748 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008749 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008750
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008751 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755#if defined(MBEDTLS_SSL_CLI_C)
8756int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008757{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008758 if( ssl == NULL ||
8759 dst == NULL ||
8760 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008761 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008764 }
8765
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008766 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008769
Paul Bakker5121ce52009-01-03 21:22:43 +00008770/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008771 * Serialize a session in the following format:
8772 * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
8773 * n . n+2 peer_cert length = m (0 if no certificate)
8774 * n+3 . n+2+m peer cert ASN.1
8775 */
8776int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8777 unsigned char *buf,
8778 size_t buf_len,
8779 size_t *olen )
8780{
8781 unsigned char *p = buf;
8782 size_t left = buf_len;
8783#if defined(MBEDTLS_X509_CRT_PARSE_C)
8784 size_t cert_len;
8785#endif /* MBEDTLS_X509_CRT_PARSE_C */
8786
8787 if( left < sizeof( mbedtls_ssl_session ) )
8788 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
8789
8790 memcpy( p, session, sizeof( mbedtls_ssl_session ) );
8791 p += sizeof( mbedtls_ssl_session );
8792 left -= sizeof( mbedtls_ssl_session );
8793
8794#if defined(MBEDTLS_X509_CRT_PARSE_C)
8795 if( session->peer_cert == NULL )
8796 cert_len = 0;
8797 else
8798 cert_len = session->peer_cert->raw.len;
8799
8800 if( left < 3 + cert_len )
8801 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
8802
8803 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
8804 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
8805 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
8806
8807 if( session->peer_cert != NULL )
8808 memcpy( p, session->peer_cert->raw.p, cert_len );
8809
8810 p += cert_len;
8811#endif /* MBEDTLS_X509_CRT_PARSE_C */
8812
8813 *olen = p - buf;
8814
8815 return( 0 );
8816}
8817
8818/*
8819 * Unserialise session, see mbedtls_ssl_session_save()
8820 */
8821int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
8822 const unsigned char *buf,
8823 size_t len )
8824{
8825 const unsigned char *p = buf;
8826 const unsigned char * const end = buf + len;
8827#if defined(MBEDTLS_X509_CRT_PARSE_C)
8828 size_t cert_len;
8829#endif /* MBEDTLS_X509_CRT_PARSE_C */
8830
8831 if( sizeof( mbedtls_ssl_session ) > (size_t)( end - p ) )
8832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8833
8834 memcpy( session, p, sizeof( mbedtls_ssl_session ) );
8835 p += sizeof( mbedtls_ssl_session );
8836
8837#if defined(MBEDTLS_X509_CRT_PARSE_C)
8838 if( 3 > (size_t)( end - p ) )
8839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8840
8841 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8842 p += 3;
8843
8844 if( cert_len == 0 )
8845 {
8846 session->peer_cert = NULL;
8847 }
8848 else
8849 {
8850 int ret;
8851
8852 if( cert_len > (size_t)( end - p ) )
8853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8854
8855 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8856
8857 if( session->peer_cert == NULL )
8858 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8859
8860 mbedtls_x509_crt_init( session->peer_cert );
8861
8862 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8863 p, cert_len ) ) != 0 )
8864 {
8865 mbedtls_x509_crt_free( session->peer_cert );
8866 mbedtls_free( session->peer_cert );
8867 session->peer_cert = NULL;
8868 return( ret );
8869 }
8870
8871 p += cert_len;
8872 }
8873#endif /* MBEDTLS_X509_CRT_PARSE_C */
8874
8875 if( p != end )
8876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8877
8878 return( 0 );
8879}
8880
8881/*
Paul Bakker1961b702013-01-25 14:49:24 +01008882 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008884int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008887
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008888 if( ssl == NULL || ssl->conf == NULL )
8889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008898#endif
8899
Paul Bakker1961b702013-01-25 14:49:24 +01008900 return( ret );
8901}
8902
8903/*
8904 * Perform the SSL handshake
8905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008907{
8908 int ret = 0;
8909
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008910 if( ssl == NULL || ssl->conf == NULL )
8911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008917 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008918
8919 if( ret != 0 )
8920 break;
8921 }
8922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008924
8925 return( ret );
8926}
8927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928#if defined(MBEDTLS_SSL_RENEGOTIATION)
8929#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008930/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008931 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008934{
8935 int ret;
8936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008938
8939 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8941 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008942
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008943 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008944 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008946 return( ret );
8947 }
8948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008950
8951 return( 0 );
8952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008954
8955/*
8956 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957 * - any side: calling mbedtls_ssl_renegotiate(),
8958 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8959 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008960 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008961 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008965{
8966 int ret;
8967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008969
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008970 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8971 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008972
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008973 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8974 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008976 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008978 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008980 ssl->handshake->out_msg_seq = 1;
8981 else
8982 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008983 }
8984#endif
8985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8987 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008992 return( ret );
8993 }
8994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008996
8997 return( 0 );
8998}
8999
9000/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009001 * Renegotiate current connection on client,
9002 * or request renegotiation on server
9003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009007
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009008 if( ssl == NULL || ssl->conf == NULL )
9009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009012 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009013 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009019
9020 /* Did we already try/start sending HelloRequest? */
9021 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009023
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009024 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009029 /*
9030 * On client, either start the renegotiation process or,
9031 * if already in progress, continue the handshake
9032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009037
9038 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009041 return( ret );
9042 }
9043 }
9044 else
9045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009049 return( ret );
9050 }
9051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009053
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009054 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009055}
9056
9057/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009058 * Check record counters and renegotiate if they're above the limit.
9059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009061{
Andres AG2196c7f2016-12-15 17:01:16 +00009062 size_t ep_len = ssl_ep_len( ssl );
9063 int in_ctr_cmp;
9064 int out_ctr_cmp;
9065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9067 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009068 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009069 {
9070 return( 0 );
9071 }
9072
Andres AG2196c7f2016-12-15 17:01:16 +00009073 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9074 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009075 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009076 ssl->conf->renego_period + ep_len, 8 - ep_len );
9077
9078 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009079 {
9080 return( 0 );
9081 }
9082
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009087
9088/*
9089 * Receive application data decrypted from the SSL layer
9090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009092{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009093 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009094 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009095
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009096 if( ssl == NULL || ssl->conf == NULL )
9097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009102 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009105 return( ret );
9106
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009107 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009109 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009110 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009111 return( ret );
9112 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009113 }
9114#endif
9115
Hanno Becker4a810fb2017-05-24 16:27:30 +01009116 /*
9117 * Check if renegotiation is necessary and/or handshake is
9118 * in process. If yes, perform/continue, and fall through
9119 * if an unexpected packet is received while the client
9120 * is waiting for the ServerHello.
9121 *
9122 * (There is no equivalent to the last condition on
9123 * the server-side as it is not treated as within
9124 * a handshake while waiting for the ClientHello
9125 * after a renegotiation request.)
9126 */
9127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009129 ret = ssl_check_ctr_renegotiate( ssl );
9130 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9131 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009134 return( ret );
9135 }
9136#endif
9137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009141 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9142 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009145 return( ret );
9146 }
9147 }
9148
Hanno Beckere41158b2017-10-23 13:30:32 +01009149 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009150 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009151 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009152 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009153 if( ssl->f_get_timer != NULL &&
9154 ssl->f_get_timer( ssl->p_timer ) == -1 )
9155 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009156 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009157 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009158
Hanno Becker327c93b2018-08-15 13:56:18 +01009159 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009160 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009161 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9162 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009163
Hanno Becker4a810fb2017-05-24 16:27:30 +01009164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9165 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009166 }
9167
9168 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009170 {
9171 /*
9172 * OpenSSL sends empty messages to randomize the IV
9173 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009174 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009177 return( 0 );
9178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009180 return( ret );
9181 }
9182 }
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009187
Hanno Becker4a810fb2017-05-24 16:27:30 +01009188 /*
9189 * - For client-side, expect SERVER_HELLO_REQUEST.
9190 * - For server-side, expect CLIENT_HELLO.
9191 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9192 */
9193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009197 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009200
9201 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009203 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009204 {
9205 continue;
9206 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009209 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009210#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009211
Hanno Becker4a810fb2017-05-24 16:27:30 +01009212#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009213 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009217
9218 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009220 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009221 {
9222 continue;
9223 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009226 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009227#endif /* MBEDTLS_SSL_SRV_C */
9228
Hanno Becker21df7f92017-10-17 11:03:26 +01009229#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009230 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009231 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9232 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9233 ssl->conf->allow_legacy_renegotiation ==
9234 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9235 {
9236 /*
9237 * Accept renegotiation request
9238 */
Paul Bakker48916f92012-09-16 19:57:18 +00009239
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009240 /* DTLS clients need to know renego is server-initiated */
9241#if defined(MBEDTLS_SSL_PROTO_DTLS)
9242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9243 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9244 {
9245 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9246 }
9247#endif
9248 ret = ssl_start_renegotiation( ssl );
9249 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9250 ret != 0 )
9251 {
9252 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9253 return( ret );
9254 }
9255 }
9256 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009257#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009258 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009259 /*
9260 * Refuse renegotiation
9261 */
9262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265#if defined(MBEDTLS_SSL_PROTO_SSL3)
9266 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009267 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009268 /* SSLv3 does not have a "no_renegotiation" warning, so
9269 we send a fatal alert and abort the connection. */
9270 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9271 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9272 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009273 }
9274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9276#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9277 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9278 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9281 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9282 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009283 {
9284 return( ret );
9285 }
Paul Bakker48916f92012-09-16 19:57:18 +00009286 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009287 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9289 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009293 }
Paul Bakker48916f92012-09-16 19:57:18 +00009294 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009295
Hanno Becker90333da2017-10-10 11:27:13 +01009296 /* At this point, we don't know whether the renegotiation has been
9297 * completed or not. The cases to consider are the following:
9298 * 1) The renegotiation is complete. In this case, no new record
9299 * has been read yet.
9300 * 2) The renegotiation is incomplete because the client received
9301 * an application data record while awaiting the ServerHello.
9302 * 3) The renegotiation is incomplete because the client received
9303 * a non-handshake, non-application data message while awaiting
9304 * the ServerHello.
9305 * In each of these case, looping will be the proper action:
9306 * - For 1), the next iteration will read a new record and check
9307 * if it's application data.
9308 * - For 2), the loop condition isn't satisfied as application data
9309 * is present, hence continue is the same as break
9310 * - For 3), the loop condition is satisfied and read_record
9311 * will re-deliver the message that was held back by the client
9312 * when expecting the ServerHello.
9313 */
9314 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009315 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009316#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009318 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009319 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009320 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009321 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009324 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009326 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009327 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9332 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009335 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009336 }
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9341 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009342 }
9343
9344 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009345
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009346 /* We're going to return something now, cancel timer,
9347 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009349 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009350
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009352 /* If we requested renego but received AppData, resend HelloRequest.
9353 * Do it now, after setting in_offt, to avoid taking this branch
9354 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009356 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009358 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009359 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009362 return( ret );
9363 }
9364 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009366#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009367 }
9368
9369 n = ( len < ssl->in_msglen )
9370 ? len : ssl->in_msglen;
9371
9372 memcpy( buf, ssl->in_offt, n );
9373 ssl->in_msglen -= n;
9374
9375 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009376 {
9377 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009378 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009379 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009381 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009382 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009383 /* more data available */
9384 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009388
Paul Bakker23986e52011-04-24 08:57:21 +00009389 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009390}
9391
9392/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009393 * Send application data to be encrypted by the SSL layer, taking care of max
9394 * fragment length and buffer size.
9395 *
9396 * According to RFC 5246 Section 6.2.1:
9397 *
9398 * Zero-length fragments of Application data MAY be sent as they are
9399 * potentially useful as a traffic analysis countermeasure.
9400 *
9401 * Therefore, it is possible that the input message length is 0 and the
9402 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009403 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009404static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009405 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009406{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009407 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9408 const size_t max_len = (size_t) ret;
9409
9410 if( ret < 0 )
9411 {
9412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9413 return( ret );
9414 }
9415
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009416 if( len > max_len )
9417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009422 "maximum fragment length: %d > %d",
9423 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009425 }
9426 else
9427#endif
9428 len = max_len;
9429 }
Paul Bakker887bd502011-06-08 13:10:54 +00009430
Paul Bakker5121ce52009-01-03 21:22:43 +00009431 if( ssl->out_left != 0 )
9432 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009433 /*
9434 * The user has previously tried to send the data and
9435 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9436 * written. In this case, we expect the high-level write function
9437 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009442 return( ret );
9443 }
9444 }
Paul Bakker887bd502011-06-08 13:10:54 +00009445 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009446 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009447 /*
9448 * The user is trying to send a message the first time, so we need to
9449 * copy the data into the internal buffers and setup the data structure
9450 * to keep track of partial writes
9451 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009452 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009454 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009455
Hanno Becker67bc7c32018-08-06 11:33:50 +01009456 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009459 return( ret );
9460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009461 }
9462
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009463 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009464}
9465
9466/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009467 * Write application data, doing 1/n-1 splitting if necessary.
9468 *
9469 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009470 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009471 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009474static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009475 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009476{
9477 int ret;
9478
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009479 if( ssl->conf->cbc_record_splitting ==
9480 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009481 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9483 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9484 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009485 {
9486 return( ssl_write_real( ssl, buf, len ) );
9487 }
9488
9489 if( ssl->split_done == 0 )
9490 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009491 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009492 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009493 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009494 }
9495
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009496 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9497 return( ret );
9498 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009499
9500 return( ret + 1 );
9501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009503
9504/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009505 * Write application data (public-facing wrapper)
9506 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009507int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009508{
9509 int ret;
9510
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009512
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009513 if( ssl == NULL || ssl->conf == NULL )
9514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9515
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009516#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009517 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9518 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009519 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009520 return( ret );
9521 }
9522#endif
9523
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009524 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009525 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009526 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009527 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009529 return( ret );
9530 }
9531 }
9532
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009533#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009534 ret = ssl_write_split( ssl, buf, len );
9535#else
9536 ret = ssl_write_real( ssl, buf, len );
9537#endif
9538
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009540
9541 return( ret );
9542}
9543
9544/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009545 * Notify the peer that the connection is being closed
9546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009548{
9549 int ret;
9550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009551 if( ssl == NULL || ssl->conf == NULL )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009555
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009556 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9562 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9563 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009566 return( ret );
9567 }
9568 }
9569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009571
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009572 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009573}
9574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009576{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009577 if( transform == NULL )
9578 return;
9579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009581 deflateEnd( &transform->ctx_deflate );
9582 inflateEnd( &transform->ctx_inflate );
9583#endif
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9586 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009587
Hanno Becker92231322018-01-03 15:32:51 +00009588#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 mbedtls_md_free( &transform->md_ctx_enc );
9590 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009591#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009592
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009593 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009594}
9595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596#if defined(MBEDTLS_X509_CRT_PARSE_C)
9597static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009600
9601 while( cur != NULL )
9602 {
9603 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009605 cur = next;
9606 }
9607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009609
Hanno Becker0271f962018-08-16 13:23:47 +01009610#if defined(MBEDTLS_SSL_PROTO_DTLS)
9611
9612static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9613{
9614 unsigned offset;
9615 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9616
9617 if( hs == NULL )
9618 return;
9619
Hanno Becker283f5ef2018-08-24 09:34:47 +01009620 ssl_free_buffered_record( ssl );
9621
Hanno Becker0271f962018-08-16 13:23:47 +01009622 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009623 ssl_buffering_free_slot( ssl, offset );
9624}
9625
9626static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9627 uint8_t slot )
9628{
9629 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9630 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009631
9632 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9633 return;
9634
Hanno Beckere605b192018-08-21 15:59:07 +01009635 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009636 {
Hanno Beckere605b192018-08-21 15:59:07 +01009637 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009638 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009639 mbedtls_free( hs_buf->data );
9640 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009641 }
9642}
9643
9644#endif /* MBEDTLS_SSL_PROTO_DTLS */
9645
Gilles Peskine9b562d52018-04-25 20:32:43 +02009646void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009647{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009648 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9649
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009650 if( handshake == NULL )
9651 return;
9652
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009653#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9654 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9655 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009656 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009657 handshake->async_in_progress = 0;
9658 }
9659#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9660
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009661#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9662 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9663 mbedtls_md5_free( &handshake->fin_md5 );
9664 mbedtls_sha1_free( &handshake->fin_sha1 );
9665#endif
9666#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9667#if defined(MBEDTLS_SHA256_C)
9668 mbedtls_sha256_free( &handshake->fin_sha256 );
9669#endif
9670#if defined(MBEDTLS_SHA512_C)
9671 mbedtls_sha512_free( &handshake->fin_sha512 );
9672#endif
9673#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675#if defined(MBEDTLS_DHM_C)
9676 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#if defined(MBEDTLS_ECDH_C)
9679 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009680#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009681#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009682 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009683#if defined(MBEDTLS_SSL_CLI_C)
9684 mbedtls_free( handshake->ecjpake_cache );
9685 handshake->ecjpake_cache = NULL;
9686 handshake->ecjpake_cache_len = 0;
9687#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009688#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009689
Janos Follath4ae5c292016-02-10 11:27:43 +00009690#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9691 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009692 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009694#endif
9695
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009696#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9697 if( handshake->psk != NULL )
9698 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009699 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009700 mbedtls_free( handshake->psk );
9701 }
9702#endif
9703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9705 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009706 /*
9707 * Free only the linked list wrapper, not the keys themselves
9708 * since the belong to the SNI callback
9709 */
9710 if( handshake->sni_key_cert != NULL )
9711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009713
9714 while( cur != NULL )
9715 {
9716 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009718 cur = next;
9719 }
9720 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009722
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009723#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009724 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009725#endif
9726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727#if defined(MBEDTLS_SSL_PROTO_DTLS)
9728 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009729 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009730 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009731#endif
9732
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009733 mbedtls_platform_zeroize( handshake,
9734 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009735}
9736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009738{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009739 if( session == NULL )
9740 return;
9741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009743 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 mbedtls_x509_crt_free( session->peer_cert );
9746 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009747 }
Paul Bakkered27a042013-04-18 22:46:23 +02009748#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009749
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009750#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009752#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009753
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009754 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009755}
9756
Paul Bakker5121ce52009-01-03 21:22:43 +00009757/*
9758 * Free an SSL context
9759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009761{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009762 if( ssl == NULL )
9763 return;
9764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009766
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009767 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009768 {
Angus Grattond8213d02016-05-25 20:56:48 +10009769 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009771 }
9772
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009773 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009774 {
Angus Grattond8213d02016-05-25 20:56:48 +10009775 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009777 }
9778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009780 if( ssl->compress_buf != NULL )
9781 {
Angus Grattond8213d02016-05-25 20:56:48 +10009782 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009784 }
9785#endif
9786
Paul Bakker48916f92012-09-16 19:57:18 +00009787 if( ssl->transform )
9788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 mbedtls_ssl_transform_free( ssl->transform );
9790 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009791 }
9792
9793 if( ssl->handshake )
9794 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009795 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9797 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 mbedtls_free( ssl->handshake );
9800 mbedtls_free( ssl->transform_negotiate );
9801 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009802 }
9803
Paul Bakkerc0463502013-02-14 11:19:38 +01009804 if( ssl->session )
9805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 mbedtls_ssl_session_free( ssl->session );
9807 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009808 }
9809
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009810#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009811 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009812 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009813 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009815 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9819 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9822 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009823 }
9824#endif
9825
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009826#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009828#endif
9829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009831
Paul Bakker86f04f42013-02-14 11:20:09 +01009832 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009833 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009834}
9835
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009836/*
9837 * Initialze mbedtls_ssl_config
9838 */
9839void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9840{
9841 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9842}
9843
Simon Butcherc97b6972015-12-27 23:48:17 +00009844#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009845static int ssl_preset_default_hashes[] = {
9846#if defined(MBEDTLS_SHA512_C)
9847 MBEDTLS_MD_SHA512,
9848 MBEDTLS_MD_SHA384,
9849#endif
9850#if defined(MBEDTLS_SHA256_C)
9851 MBEDTLS_MD_SHA256,
9852 MBEDTLS_MD_SHA224,
9853#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009854#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009855 MBEDTLS_MD_SHA1,
9856#endif
9857 MBEDTLS_MD_NONE
9858};
Simon Butcherc97b6972015-12-27 23:48:17 +00009859#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009860
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009861static int ssl_preset_suiteb_ciphersuites[] = {
9862 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9863 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9864 0
9865};
9866
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009867#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009868static int ssl_preset_suiteb_hashes[] = {
9869 MBEDTLS_MD_SHA256,
9870 MBEDTLS_MD_SHA384,
9871 MBEDTLS_MD_NONE
9872};
9873#endif
9874
9875#if defined(MBEDTLS_ECP_C)
9876static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9877 MBEDTLS_ECP_DP_SECP256R1,
9878 MBEDTLS_ECP_DP_SECP384R1,
9879 MBEDTLS_ECP_DP_NONE
9880};
9881#endif
9882
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009883/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009884 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009885 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009886int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009887 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009888{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009889#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009890 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009891#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009892
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009893 /* Use the functions here so that they are covered in tests,
9894 * but otherwise access member directly for efficiency */
9895 mbedtls_ssl_conf_endpoint( conf, endpoint );
9896 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009897
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009898 /*
9899 * Things that are common to all presets
9900 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009901#if defined(MBEDTLS_SSL_CLI_C)
9902 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9903 {
9904 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9905#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9906 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9907#endif
9908 }
9909#endif
9910
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009911#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009912 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009913#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009914
9915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9916 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9917#endif
9918
9919#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9920 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9921#endif
9922
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009923#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9924 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9925#endif
9926
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009927#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009928 conf->f_cookie_write = ssl_cookie_write_dummy;
9929 conf->f_cookie_check = ssl_cookie_check_dummy;
9930#endif
9931
9932#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9933 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9934#endif
9935
Janos Follath088ce432017-04-10 12:42:31 +01009936#if defined(MBEDTLS_SSL_SRV_C)
9937 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9938#endif
9939
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009940#if defined(MBEDTLS_SSL_PROTO_DTLS)
9941 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9942 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9943#endif
9944
9945#if defined(MBEDTLS_SSL_RENEGOTIATION)
9946 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009947 memset( conf->renego_period, 0x00, 2 );
9948 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009949#endif
9950
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009951#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9952 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9953 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009954 const unsigned char dhm_p[] =
9955 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9956 const unsigned char dhm_g[] =
9957 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9958
Hanno Beckera90658f2017-10-04 15:29:08 +01009959 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9960 dhm_p, sizeof( dhm_p ),
9961 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009962 {
9963 return( ret );
9964 }
9965 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009966#endif
9967
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009968 /*
9969 * Preset-specific defaults
9970 */
9971 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009972 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009973 /*
9974 * NSA Suite B
9975 */
9976 case MBEDTLS_SSL_PRESET_SUITEB:
9977 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9978 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9979 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9980 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9981
9982 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9983 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9984 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9985 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9986 ssl_preset_suiteb_ciphersuites;
9987
9988#if defined(MBEDTLS_X509_CRT_PARSE_C)
9989 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009990#endif
9991
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009992#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009993 conf->sig_hashes = ssl_preset_suiteb_hashes;
9994#endif
9995
9996#if defined(MBEDTLS_ECP_C)
9997 conf->curve_list = ssl_preset_suiteb_curves;
9998#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009999 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010000
10001 /*
10002 * Default
10003 */
10004 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010005 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10006 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10007 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10008 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10009 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10010 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10011 MBEDTLS_SSL_MIN_MINOR_VERSION :
10012 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010013 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10014 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10015
10016#if defined(MBEDTLS_SSL_PROTO_DTLS)
10017 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10018 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10019#endif
10020
10021 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10022 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10023 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10024 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10025 mbedtls_ssl_list_ciphersuites();
10026
10027#if defined(MBEDTLS_X509_CRT_PARSE_C)
10028 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10029#endif
10030
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010031#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010032 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010033#endif
10034
10035#if defined(MBEDTLS_ECP_C)
10036 conf->curve_list = mbedtls_ecp_grp_id_list();
10037#endif
10038
10039#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10040 conf->dhm_min_bitlen = 1024;
10041#endif
10042 }
10043
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010044 return( 0 );
10045}
10046
10047/*
10048 * Free mbedtls_ssl_config
10049 */
10050void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10051{
10052#if defined(MBEDTLS_DHM_C)
10053 mbedtls_mpi_free( &conf->dhm_P );
10054 mbedtls_mpi_free( &conf->dhm_G );
10055#endif
10056
10057#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10058 if( conf->psk != NULL )
10059 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010060 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010061 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010062 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010063 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010064 }
10065
10066 if( conf->psk_identity != NULL )
10067 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010068 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010069 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010070 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010071 conf->psk_identity_len = 0;
10072 }
10073#endif
10074
10075#if defined(MBEDTLS_X509_CRT_PARSE_C)
10076 ssl_key_cert_free( conf->key_cert );
10077#endif
10078
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010079 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010080}
10081
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010082#if defined(MBEDTLS_PK_C) && \
10083 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010084/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089#if defined(MBEDTLS_RSA_C)
10090 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10091 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093#if defined(MBEDTLS_ECDSA_C)
10094 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10095 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010098}
10099
Hanno Becker7e5437a2017-04-28 17:15:26 +010010100unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10101{
10102 switch( type ) {
10103 case MBEDTLS_PK_RSA:
10104 return( MBEDTLS_SSL_SIG_RSA );
10105 case MBEDTLS_PK_ECDSA:
10106 case MBEDTLS_PK_ECKEY:
10107 return( MBEDTLS_SSL_SIG_ECDSA );
10108 default:
10109 return( MBEDTLS_SSL_SIG_ANON );
10110 }
10111}
10112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010114{
10115 switch( sig )
10116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117#if defined(MBEDTLS_RSA_C)
10118 case MBEDTLS_SSL_SIG_RSA:
10119 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121#if defined(MBEDTLS_ECDSA_C)
10122 case MBEDTLS_SSL_SIG_ECDSA:
10123 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010124#endif
10125 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010127 }
10128}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010129#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010130
Hanno Becker7e5437a2017-04-28 17:15:26 +010010131#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10132 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10133
10134/* Find an entry in a signature-hash set matching a given hash algorithm. */
10135mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10136 mbedtls_pk_type_t sig_alg )
10137{
10138 switch( sig_alg )
10139 {
10140 case MBEDTLS_PK_RSA:
10141 return( set->rsa );
10142 case MBEDTLS_PK_ECDSA:
10143 return( set->ecdsa );
10144 default:
10145 return( MBEDTLS_MD_NONE );
10146 }
10147}
10148
10149/* Add a signature-hash-pair to a signature-hash set */
10150void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10151 mbedtls_pk_type_t sig_alg,
10152 mbedtls_md_type_t md_alg )
10153{
10154 switch( sig_alg )
10155 {
10156 case MBEDTLS_PK_RSA:
10157 if( set->rsa == MBEDTLS_MD_NONE )
10158 set->rsa = md_alg;
10159 break;
10160
10161 case MBEDTLS_PK_ECDSA:
10162 if( set->ecdsa == MBEDTLS_MD_NONE )
10163 set->ecdsa = md_alg;
10164 break;
10165
10166 default:
10167 break;
10168 }
10169}
10170
10171/* Allow exactly one hash algorithm for each signature. */
10172void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10173 mbedtls_md_type_t md_alg )
10174{
10175 set->rsa = md_alg;
10176 set->ecdsa = md_alg;
10177}
10178
10179#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10180 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10181
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010182/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010183 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010186{
10187 switch( hash )
10188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189#if defined(MBEDTLS_MD5_C)
10190 case MBEDTLS_SSL_HASH_MD5:
10191 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193#if defined(MBEDTLS_SHA1_C)
10194 case MBEDTLS_SSL_HASH_SHA1:
10195 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197#if defined(MBEDTLS_SHA256_C)
10198 case MBEDTLS_SSL_HASH_SHA224:
10199 return( MBEDTLS_MD_SHA224 );
10200 case MBEDTLS_SSL_HASH_SHA256:
10201 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203#if defined(MBEDTLS_SHA512_C)
10204 case MBEDTLS_SSL_HASH_SHA384:
10205 return( MBEDTLS_MD_SHA384 );
10206 case MBEDTLS_SSL_HASH_SHA512:
10207 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010208#endif
10209 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010211 }
10212}
10213
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010214/*
10215 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10216 */
10217unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10218{
10219 switch( md )
10220 {
10221#if defined(MBEDTLS_MD5_C)
10222 case MBEDTLS_MD_MD5:
10223 return( MBEDTLS_SSL_HASH_MD5 );
10224#endif
10225#if defined(MBEDTLS_SHA1_C)
10226 case MBEDTLS_MD_SHA1:
10227 return( MBEDTLS_SSL_HASH_SHA1 );
10228#endif
10229#if defined(MBEDTLS_SHA256_C)
10230 case MBEDTLS_MD_SHA224:
10231 return( MBEDTLS_SSL_HASH_SHA224 );
10232 case MBEDTLS_MD_SHA256:
10233 return( MBEDTLS_SSL_HASH_SHA256 );
10234#endif
10235#if defined(MBEDTLS_SHA512_C)
10236 case MBEDTLS_MD_SHA384:
10237 return( MBEDTLS_SSL_HASH_SHA384 );
10238 case MBEDTLS_MD_SHA512:
10239 return( MBEDTLS_SSL_HASH_SHA512 );
10240#endif
10241 default:
10242 return( MBEDTLS_SSL_HASH_NONE );
10243 }
10244}
10245
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010246#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010247/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010248 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010249 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010250 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010251int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010254
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010255 if( ssl->conf->curve_list == NULL )
10256 return( -1 );
10257
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010258 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010259 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010260 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010261
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010262 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010263}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010264#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010265
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010266#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010267/*
10268 * Check if a hash proposed by the peer is in our list.
10269 * Return 0 if we're willing to use it, -1 otherwise.
10270 */
10271int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10272 mbedtls_md_type_t md )
10273{
10274 const int *cur;
10275
10276 if( ssl->conf->sig_hashes == NULL )
10277 return( -1 );
10278
10279 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10280 if( *cur == (int) md )
10281 return( 0 );
10282
10283 return( -1 );
10284}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010285#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287#if defined(MBEDTLS_X509_CRT_PARSE_C)
10288int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10289 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010290 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010291 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010292{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010293 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010295 int usage = 0;
10296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010298 const char *ext_oid;
10299 size_t ext_len;
10300#endif
10301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10303 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010304 ((void) cert);
10305 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010306 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010307#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10310 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010311 {
10312 /* Server part of the key exchange */
10313 switch( ciphersuite->key_exchange )
10314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 case MBEDTLS_KEY_EXCHANGE_RSA:
10316 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010317 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010318 break;
10319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10321 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10322 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10323 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010324 break;
10325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10327 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010328 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010329 break;
10330
10331 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 case MBEDTLS_KEY_EXCHANGE_NONE:
10333 case MBEDTLS_KEY_EXCHANGE_PSK:
10334 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10335 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010336 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010337 usage = 0;
10338 }
10339 }
10340 else
10341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10343 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010344 }
10345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010346 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010347 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010348 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010349 ret = -1;
10350 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010351#else
10352 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10356 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10359 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010360 }
10361 else
10362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10364 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010365 }
10366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010368 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010369 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010370 ret = -1;
10371 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010373
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010374 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010375}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010377
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010378/*
10379 * Convert version numbers to/from wire format
10380 * and, for DTLS, to/from TLS equivalent.
10381 *
10382 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010383 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010384 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10385 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010388 unsigned char ver[2] )
10389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390#if defined(MBEDTLS_SSL_PROTO_DTLS)
10391 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010394 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10395
10396 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10397 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10398 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010399 else
10400#else
10401 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010402#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010403 {
10404 ver[0] = (unsigned char) major;
10405 ver[1] = (unsigned char) minor;
10406 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010407}
10408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010410 const unsigned char ver[2] )
10411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412#if defined(MBEDTLS_SSL_PROTO_DTLS)
10413 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010414 {
10415 *major = 255 - ver[0] + 2;
10416 *minor = 255 - ver[1] + 1;
10417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010419 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10420 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010421 else
10422#else
10423 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010424#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010425 {
10426 *major = ver[0];
10427 *minor = ver[1];
10428 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010429}
10430
Simon Butcher99000142016-10-13 17:21:01 +010010431int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10432{
10433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10434 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10435 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10436
10437 switch( md )
10438 {
10439#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10440#if defined(MBEDTLS_MD5_C)
10441 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010442 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010443#endif
10444#if defined(MBEDTLS_SHA1_C)
10445 case MBEDTLS_SSL_HASH_SHA1:
10446 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10447 break;
10448#endif
10449#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10450#if defined(MBEDTLS_SHA512_C)
10451 case MBEDTLS_SSL_HASH_SHA384:
10452 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10453 break;
10454#endif
10455#if defined(MBEDTLS_SHA256_C)
10456 case MBEDTLS_SSL_HASH_SHA256:
10457 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10458 break;
10459#endif
10460 default:
10461 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10462 }
10463
10464 return 0;
10465#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10466 (void) ssl;
10467 (void) md;
10468
10469 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10470#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10471}
10472
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010473#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10474 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10475int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10476 unsigned char *output,
10477 unsigned char *data, size_t data_len )
10478{
10479 int ret = 0;
10480 mbedtls_md5_context mbedtls_md5;
10481 mbedtls_sha1_context mbedtls_sha1;
10482
10483 mbedtls_md5_init( &mbedtls_md5 );
10484 mbedtls_sha1_init( &mbedtls_sha1 );
10485
10486 /*
10487 * digitally-signed struct {
10488 * opaque md5_hash[16];
10489 * opaque sha_hash[20];
10490 * };
10491 *
10492 * md5_hash
10493 * MD5(ClientHello.random + ServerHello.random
10494 * + ServerParams);
10495 * sha_hash
10496 * SHA(ClientHello.random + ServerHello.random
10497 * + ServerParams);
10498 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010499 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010500 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010502 goto exit;
10503 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010504 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010505 ssl->handshake->randbytes, 64 ) ) != 0 )
10506 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010508 goto exit;
10509 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010510 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010511 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010513 goto exit;
10514 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010515 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010516 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010518 goto exit;
10519 }
10520
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010521 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010522 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010524 goto exit;
10525 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010526 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010527 ssl->handshake->randbytes, 64 ) ) != 0 )
10528 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010530 goto exit;
10531 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010532 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010533 data_len ) ) != 0 )
10534 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010536 goto exit;
10537 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010538 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010539 output + 16 ) ) != 0 )
10540 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010542 goto exit;
10543 }
10544
10545exit:
10546 mbedtls_md5_free( &mbedtls_md5 );
10547 mbedtls_sha1_free( &mbedtls_sha1 );
10548
10549 if( ret != 0 )
10550 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10551 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10552
10553 return( ret );
10554
10555}
10556#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10557 MBEDTLS_SSL_PROTO_TLS1_1 */
10558
10559#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10560 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10561int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010562 unsigned char *hash, size_t *hashlen,
10563 unsigned char *data, size_t data_len,
10564 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010565{
10566 int ret = 0;
10567 mbedtls_md_context_t ctx;
10568 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010569 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010570
10571 mbedtls_md_init( &ctx );
10572
10573 /*
10574 * digitally-signed struct {
10575 * opaque client_random[32];
10576 * opaque server_random[32];
10577 * ServerDHParams params;
10578 * };
10579 */
10580 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10581 {
10582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10583 goto exit;
10584 }
10585 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10586 {
10587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10588 goto exit;
10589 }
10590 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10591 {
10592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10593 goto exit;
10594 }
10595 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10596 {
10597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10598 goto exit;
10599 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010600 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010601 {
10602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10603 goto exit;
10604 }
10605
10606exit:
10607 mbedtls_md_free( &ctx );
10608
10609 if( ret != 0 )
10610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10611 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10612
10613 return( ret );
10614}
10615#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10616 MBEDTLS_SSL_PROTO_TLS1_2 */
10617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618#endif /* MBEDTLS_SSL_TLS_C */