blob: fc261c4fe243352d5b751a1e070116620c66dd1d [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 Beckerba8cd672019-04-23 12:31:42 +0100111#if defined(MBEDTLS_SSL_CID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100112/* Top-level Connection ID API */
113
Hanno Becker07489862019-04-25 16:01:49 +0100114/* WARNING: The CID feature isn't fully implemented yet
115 * and will not be used. */
Hanno Beckereec2be92019-05-03 13:06:44 +0100116int mbedtls_ssl_conf_cid_len( mbedtls_ssl_config *conf,
117 size_t len )
118{
119 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
121
122 conf->cid_len = len;
123 return( 0 );
124}
125
126/* WARNING: The CID feature isn't fully implemented yet
127 * and will not be used. */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100128int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
129 int enable,
130 unsigned char const *own_cid,
131 size_t own_cid_len )
132{
Hanno Becker78c43022019-05-03 14:38:32 +0100133 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
135
Hanno Becker07489862019-04-25 16:01:49 +0100136 ssl->negotiate_cid = enable;
137 if( enable == MBEDTLS_SSL_CID_DISABLED )
138 {
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
140 return( 0 );
141 }
142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100143 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100144
Hanno Beckereec2be92019-05-03 13:06:44 +0100145 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100146 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
148 (unsigned) own_cid_len,
149 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
151 }
152
153 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100154 /* Truncation is not an issue here because
155 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
156 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100157
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100158 return( 0 );
159}
160
Hanno Becker2de89fa2019-04-26 17:08:02 +0100161/* WARNING: The CID feature isn't fully implemented yet
162 * and will not be used. */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100169
Hanno Becker78c43022019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
171 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
172 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100174 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100175
Hanno Beckercb063f52019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
186 *peer_cid_len = ssl->transform_in->out_cid_len;
187 memcpy( peer_cid, ssl->transform_in->out_cid,
188 ssl->transform_in->out_cid_len );
189
190 *enabled = MBEDTLS_SSL_CID_ENABLED;
191
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100192 return( 0 );
193}
Hanno Beckerba8cd672019-04-23 12:31:42 +0100194#endif /* MBEDTLS_SSL_CID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100195
Hanno Beckerd5847772018-08-28 10:09:23 +0100196/* Forward declarations for functions related to message buffering. */
197static void ssl_buffering_free( mbedtls_ssl_context *ssl );
198static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
199 uint8_t slot );
200static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
201static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
202static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
203static int ssl_buffer_message( mbedtls_ssl_context *ssl );
204static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100205static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100206
Hanno Beckera67dee22018-08-22 10:05:20 +0100207static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100208static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100209{
Hanno Becker11682cc2018-08-22 14:41:02 +0100210 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100211
212 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100213 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100214
215 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
216}
217
Hanno Becker67bc7c32018-08-06 11:33:50 +0100218static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
219{
Hanno Becker11682cc2018-08-22 14:41:02 +0100220 size_t const bytes_written = ssl->out_left;
221 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100222
223 /* Double-check that the write-index hasn't gone
224 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100225 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100226 {
227 /* Should never happen... */
228 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
229 }
230
231 return( (int) ( mtu - bytes_written ) );
232}
233
234static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
235{
236 int ret;
237 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400238 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100239
240#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
241 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
242
243 if( max_len > mfl )
244 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100245
246 /* By the standard (RFC 6066 Sect. 4), the MFL extension
247 * only limits the maximum record payload size, so in theory
248 * we would be allowed to pack multiple records of payload size
249 * MFL into a single datagram. However, this would mean that there's
250 * no way to explicitly communicate MTU restrictions to the peer.
251 *
252 * The following reduction of max_len makes sure that we never
253 * write datagrams larger than MFL + Record Expansion Overhead.
254 */
255 if( max_len <= ssl->out_left )
256 return( 0 );
257
258 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100259#endif
260
261 ret = ssl_get_remaining_space_in_datagram( ssl );
262 if( ret < 0 )
263 return( ret );
264 remaining = (size_t) ret;
265
266 ret = mbedtls_ssl_get_record_expansion( ssl );
267 if( ret < 0 )
268 return( ret );
269 expansion = (size_t) ret;
270
271 if( remaining <= expansion )
272 return( 0 );
273
274 remaining -= expansion;
275 if( remaining >= max_len )
276 remaining = max_len;
277
278 return( (int) remaining );
279}
280
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281/*
282 * Double the retransmit timeout value, within the allowed range,
283 * returning -1 if the maximum value has already been reached.
284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200286{
287 uint32_t new_timeout;
288
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200289 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290 return( -1 );
291
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200292 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
293 * in the following way: after the initial transmission and a first
294 * retransmission, back off to a temporary estimated MTU of 508 bytes.
295 * This value is guaranteed to be deliverable (if not guaranteed to be
296 * delivered) of any compliant IPv4 (and IPv6) network, and should work
297 * on most non-IP stacks too. */
298 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400299 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200300 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
302 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200303
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 new_timeout = 2 * ssl->handshake->retransmit_timeout;
305
306 /* Avoid arithmetic overflow and range overflow */
307 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200308 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200309 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200310 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200311 }
312
313 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315 ssl->handshake->retransmit_timeout ) );
316
317 return( 0 );
318}
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200321{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324 ssl->handshake->retransmit_timeout ) );
325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200329/*
330 * Convert max_fragment_length codes to length.
331 * RFC 6066 says:
332 * enum{
333 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
334 * } MaxFragmentLength;
335 * and we add 0 -> extension unused
336 */
Angus Grattond8213d02016-05-25 20:56:48 +1000337static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200338{
Angus Grattond8213d02016-05-25 20:56:48 +1000339 switch( mfl )
340 {
341 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
342 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
343 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
344 return 512;
345 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
346 return 1024;
347 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
348 return 2048;
349 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
350 return 4096;
351 default:
352 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
353 }
354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200356
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200357#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_ssl_session_free( dst );
361 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200364 if( src->peer_cert != NULL )
365 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200366 int ret;
367
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200368 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200369 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200375 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 dst->peer_cert = NULL;
379 return( ret );
380 }
381 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200383
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200384#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385 if( src->ticket != NULL )
386 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200387 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200388 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200389 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390
391 memcpy( dst->ticket, src->ticket, src->ticket_len );
392 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394
395 return( 0 );
396}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200397#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
400int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200401 const unsigned char *key_enc, const unsigned char *key_dec,
402 size_t keylen,
403 const unsigned char *iv_enc, const unsigned char *iv_dec,
404 size_t ivlen,
405 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200406 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
408int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
409int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
410int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
411int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
412#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000413
Paul Bakker5121ce52009-01-03 21:22:43 +0000414/*
415 * Key material generation
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200418static int ssl3_prf( const unsigned char *secret, size_t slen,
419 const char *label,
420 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000421 unsigned char *dstbuf, size_t dlen )
422{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100423 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000424 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200425 mbedtls_md5_context md5;
426 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000427 unsigned char padding[16];
428 unsigned char sha1sum[20];
429 ((void)label);
430
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200431 mbedtls_md5_init( &md5 );
432 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200433
Paul Bakker5f70b252012-09-13 14:23:06 +0000434 /*
435 * SSLv3:
436 * block =
437 * MD5( secret + SHA1( 'A' + secret + random ) ) +
438 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
439 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
440 * ...
441 */
442 for( i = 0; i < dlen / 16; i++ )
443 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200444 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000445
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100446 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100447 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100448 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100449 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100450 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100451 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100452 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100453 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100457 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000465 }
466
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200468 mbedtls_md5_free( &md5 );
469 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000470
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500471 mbedtls_platform_zeroize( padding, sizeof( padding ) );
472 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000473
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200479static int tls1_prf( const unsigned char *secret, size_t slen,
480 const char *label,
481 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000482 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
Paul Bakker23986e52011-04-24 08:57:21 +0000484 size_t nb, hs;
485 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200486 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 unsigned char tmp[128];
488 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 const mbedtls_md_info_t *md_info;
490 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100491 int ret;
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 hs = ( slen + 1 ) / 2;
499 S1 = secret;
500 S2 = secret + slen - hs;
501
502 nb = strlen( label );
503 memcpy( tmp + 20, label, nb );
504 memcpy( tmp + 20 + nb, random, rlen );
505 nb += rlen;
506
507 /*
508 * First compute P_md5(secret,label+random)[0..dlen]
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
511 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100514 return( ret );
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
517 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
518 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 for( i = 0; i < dlen; i += 16 )
521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_md_hmac_reset ( &md_ctx );
523 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
524 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_md_hmac_reset ( &md_ctx );
527 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
528 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
531
532 for( j = 0; j < k; j++ )
533 dstbuf[i + j] = h_i[j];
534 }
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100537
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 /*
539 * XOR out with P_sha1(secret,label+random)[0..dlen]
540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100545 return( ret );
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
548 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
549 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 for( i = 0; i < dlen; i += 20 )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_md_hmac_reset ( &md_ctx );
554 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
555 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_md_hmac_reset ( &md_ctx );
558 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
559 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
562
563 for( j = 0; j < k; j++ )
564 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
565 }
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100568
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500569 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
570 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 return( 0 );
573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
577static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100578 const unsigned char *secret, size_t slen,
579 const char *label,
580 const unsigned char *random, size_t rlen,
581 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000582{
583 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100584 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000585 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
587 const mbedtls_md_info_t *md_info;
588 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100589 int ret;
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100597
598 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000600
601 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100602 memcpy( tmp + md_len, label, nb );
603 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000604 nb += rlen;
605
606 /*
607 * Compute P_<hash>(secret, label + random)[0..dlen]
608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100610 return( ret );
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
613 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
614 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100615
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100616 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_md_hmac_reset ( &md_ctx );
619 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
620 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_reset ( &md_ctx );
623 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
624 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000625
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627
628 for( j = 0; j < k; j++ )
629 dstbuf[i + j] = h_i[j];
630 }
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100633
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500634 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
635 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
637 return( 0 );
638}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100641static int tls_prf_sha256( const unsigned char *secret, size_t slen,
642 const char *label,
643 const unsigned char *random, size_t rlen,
644 unsigned char *dstbuf, size_t dlen )
645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100647 label, random, rlen, dstbuf, dlen ) );
648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200652static int tls_prf_sha384( const unsigned char *secret, size_t slen,
653 const char *label,
654 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000655 unsigned char *dstbuf, size_t dlen )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100658 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SHA512_C */
661#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
666 defined(MBEDTLS_SSL_PROTO_TLS1_1)
667static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668#endif
Paul Bakker380da532012-04-18 16:10:25 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SSL_PROTO_SSL3)
671static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
672static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200673#endif
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
676static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
677static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678#endif
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
681#if defined(MBEDTLS_SHA256_C)
682static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
683static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
684static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_SHA512_C)
688static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
689static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
690static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000695{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200696 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 unsigned char keyblk[256];
699 unsigned char *key1;
700 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100701 unsigned char *mac_enc;
702 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000703 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200704 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000705 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000706 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 const mbedtls_cipher_info_t *cipher_info;
708 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_ssl_session *session = ssl->session_negotiate;
711 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
712 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Hanno Becker3307b532017-12-27 21:37:21 +0000716#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
717 transform->encrypt_then_mac = session->encrypt_then_mac;
718#endif
719 transform->minor_ver = ssl->minor_ver;
720
Hanno Becker8759e162017-12-27 21:34:08 +0000721 ciphersuite_info = handshake->ciphersuite_info;
722 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100723 if( cipher_info == NULL )
724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000726 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100728 }
729
Hanno Becker8759e162017-12-27 21:34:08 +0000730 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100731 if( md_info == NULL )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000734 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100736 }
737
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100738#if defined(MBEDTLS_SSL_CID)
739 /* Copy own and peer's CID if the use of the CID
740 * extension has been negotiated. */
741 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
742 {
743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100744
745 /* Uncomment this once CID-parsing and support for a change
746 * record content type during record decryption are added. */
747 /* transform->in_cid_len = ssl->own_cid_len; */
748 /* transform->out_cid_len = ssl->handshake->peer_cid_len; */
749 /* memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); */
750 /* memcpy( transform->out_cid, ssl->handshake->peer_cid, */
751 /* ssl->handshake->peer_cid_len ); */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100752
753 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
754 transform->out_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100755 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100756 transform->in_cid_len );
757 }
758#endif /* MBEDTLS_SSL_CID */
759
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000761 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_SSL_PROTO_SSL3)
764 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765 {
Paul Bakker48916f92012-09-16 19:57:18 +0000766 handshake->tls_prf = ssl3_prf;
767 handshake->calc_verify = ssl_calc_verify_ssl;
768 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000769 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200770 else
771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
773 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000774 {
Paul Bakker48916f92012-09-16 19:57:18 +0000775 handshake->tls_prf = tls1_prf;
776 handshake->calc_verify = ssl_calc_verify_tls;
777 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000778 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200779 else
780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
782#if defined(MBEDTLS_SHA512_C)
783 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker8759e162017-12-27 21:34:08 +0000784 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 {
Paul Bakker48916f92012-09-16 19:57:18 +0000786 handshake->tls_prf = tls_prf_sha384;
787 handshake->calc_verify = ssl_calc_verify_tls_sha384;
788 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000790 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SHA256_C)
793 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000794 {
Paul Bakker48916f92012-09-16 19:57:18 +0000795 handshake->tls_prf = tls_prf_sha256;
796 handshake->calc_verify = ssl_calc_verify_tls_sha256;
797 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000798 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200799 else
800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200805 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 * SSLv3:
809 * master =
810 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
811 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
812 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200813 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200814 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 * master = PRF( premaster, "master secret", randbytes )[0..47]
816 */
Paul Bakker0a597072012-09-25 21:55:46 +0000817 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000820 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
823 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200824 {
825 unsigned char session_hash[48];
826 size_t hash_len;
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200829
830 ssl->handshake->calc_verify( ssl, session_hash );
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
833 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#if defined(MBEDTLS_SHA512_C)
Hanno Becker8759e162017-12-27 21:34:08 +0000836 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200837 {
838 hash_len = 48;
839 }
840 else
841#endif
842 hash_len = 32;
843 }
844 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200846 hash_len = 36;
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200849
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100850 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
851 "extended master secret",
852 session_hash, hash_len,
853 session->master, 48 );
854 if( ret != 0 )
855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100857 return( ret );
858 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200859
860 }
861 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200862#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100863 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
864 "master secret",
865 handshake->randbytes, 64,
866 session->master, 48 );
867 if( ret != 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100870 return( ret );
871 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200872
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500873 mbedtls_platform_zeroize( handshake->premaster,
874 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 }
876 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
879 /*
880 * Swap the client and server random values.
881 */
Paul Bakker48916f92012-09-16 19:57:18 +0000882 memcpy( tmp, handshake->randbytes, 64 );
883 memcpy( handshake->randbytes, tmp + 32, 32 );
884 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500885 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
887 /*
888 * SSLv3:
889 * key block =
890 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
891 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
892 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
893 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
894 * ...
895 *
896 * TLSv1:
897 * key block = PRF( master, "key expansion", randbytes )
898 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100899 ret = handshake->tls_prf( session->master, 48, "key expansion",
900 handshake->randbytes, 64, keyblk, 256 );
901 if( ret != 0 )
902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100904 return( ret );
905 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
908 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
909 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
910 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
911 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500913 mbedtls_platform_zeroize( handshake->randbytes,
914 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
916 /*
917 * Determine the appropriate key, IV and MAC length.
918 */
Paul Bakker68884e32013-01-07 18:20:04 +0100919
Hanno Beckere7f2df02017-12-27 08:17:40 +0000920 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200921
Hanno Beckerf1229442018-01-03 15:32:31 +0000922#if defined(MBEDTLS_GCM_C) || \
923 defined(MBEDTLS_CCM_C) || \
924 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200926 cipher_info->mode == MBEDTLS_MODE_CCM ||
927 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 {
Hanno Becker8759e162017-12-27 21:34:08 +0000929 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200930
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000932 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000933 transform->taglen =
934 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200935
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200936 /* All modes haves 96-bit IVs;
937 * GCM and CCM has 4 implicit and 8 explicit bytes
938 * ChachaPoly has all 12 bytes implicit
939 */
Paul Bakker68884e32013-01-07 18:20:04 +0100940 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200941 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
942 transform->fixed_ivlen = 12;
943 else
944 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200945
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200946 /* Minimum length of encrypted record */
947 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000948 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100949 }
950 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000951#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
952#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
953 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
954 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100955 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200956 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
958 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200961 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100962 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200964 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000965 mac_key_len = mbedtls_md_get_size( md_info );
966 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200969 /*
970 * If HMAC is to be truncated, we shall keep the leftmost bytes,
971 * (rfc 6066 page 13 or rfc 2104 section 4),
972 * so we only need to adjust the length here.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000977
978#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
979 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000980 * HMAC implementation which also truncates the key
981 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000982 mac_key_len = transform->maclen;
983#endif
984 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200986
987 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100988 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200990 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200992 transform->minlen = transform->maclen;
993 else
Paul Bakker68884e32013-01-07 18:20:04 +0100994 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200995 /*
996 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100997 * 1. if EtM is in use: one block plus MAC
998 * otherwise: * first multiple of blocklen greater than maclen
999 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1002 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001003 {
1004 transform->minlen = transform->maclen
1005 + cipher_info->block_size;
1006 }
1007 else
1008#endif
1009 {
1010 transform->minlen = transform->maclen
1011 + cipher_info->block_size
1012 - transform->maclen % cipher_info->block_size;
1013 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1016 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1017 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001018 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001019 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1022 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1023 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001024 {
1025 transform->minlen += transform->ivlen;
1026 }
1027 else
1028#endif
1029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001032 }
Paul Bakker68884e32013-01-07 18:20:04 +01001033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001035 else
1036#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1037 {
1038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Hanno Beckere7f2df02017-12-27 08:17:40 +00001042 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1043 (unsigned) keylen,
1044 (unsigned) transform->minlen,
1045 (unsigned) transform->ivlen,
1046 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
1048 /*
1049 * Finally setup the cipher contexts, IVs and MAC secrets.
1050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001052 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001054 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001055 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Paul Bakker68884e32013-01-07 18:20:04 +01001057 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001058 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001060 /*
1061 * This is not used in TLS v1.1.
1062 */
Paul Bakker48916f92012-09-16 19:57:18 +00001063 iv_copy_len = ( transform->fixed_ivlen ) ?
1064 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001065 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1066 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001067 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 }
1069 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#endif /* MBEDTLS_SSL_CLI_C */
1071#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001072 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001074 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001075 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Hanno Becker81c7b182017-11-09 18:39:33 +00001077 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001078 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001080 /*
1081 * This is not used in TLS v1.1.
1082 */
Paul Bakker48916f92012-09-16 19:57:18 +00001083 iv_copy_len = ( transform->fixed_ivlen ) ?
1084 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001085 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1086 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Hanno Becker92231322018-01-03 15:32:51 +00001096#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SSL_PROTO_SSL3)
1098 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001099 {
Hanno Becker92231322018-01-03 15:32:51 +00001100 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001104 }
1105
Hanno Becker81c7b182017-11-09 18:39:33 +00001106 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1107 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001108 }
1109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1111#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1112 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1113 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001114 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001115 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1116 For AEAD-based ciphersuites, there is nothing to do here. */
1117 if( mac_key_len != 0 )
1118 {
1119 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1120 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1121 }
Paul Bakker68884e32013-01-07 18:20:04 +01001122 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001123 else
1124#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001128 }
Hanno Becker92231322018-01-03 15:32:51 +00001129#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1132 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001133 {
1134 int ret = 0;
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001137
Hanno Beckere7f2df02017-12-27 08:17:40 +00001138 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001139 transform->iv_enc, transform->iv_dec,
1140 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001141 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001142 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1145 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001146 }
1147 }
Hanno Becker92231322018-01-03 15:32:51 +00001148#else
1149 ((void) mac_dec);
1150 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001152
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001153#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1154 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001155 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001156 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1157 session->master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001158 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001159 iv_copy_len );
1160 }
1161#endif
1162
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001163 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001164 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001167 return( ret );
1168 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001169
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001170 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001171 cipher_info ) ) != 0 )
1172 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001174 return( ret );
1175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001178 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001182 return( ret );
1183 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001186 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190 return( ret );
1191 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_CIPHER_MODE_CBC)
1194 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1197 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001200 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001201 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1204 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001207 return( ret );
1208 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001212 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001215 // Initialize compression
1216 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001218 {
Paul Bakker16770332013-10-11 09:59:44 +02001219 if( ssl->compress_buf == NULL )
1220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001222 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001223 if( ssl->compress_buf == NULL )
1224 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001226 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001227 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001228 }
1229 }
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001232
Paul Bakker48916f92012-09-16 19:57:18 +00001233 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1234 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001235
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 if( deflateInit( &transform->ctx_deflate,
1237 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001238 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1241 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001242 }
1243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 return( 0 );
1249}
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_PROTO_SSL3)
1252void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001254 mbedtls_md5_context md5;
1255 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 unsigned char pad_1[48];
1257 unsigned char pad_2[48];
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001261 mbedtls_md5_init( &md5 );
1262 mbedtls_sha1_init( &sha1 );
1263
1264 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1265 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Paul Bakker380da532012-04-18 16:10:25 +00001267 memset( pad_1, 0x36, 48 );
1268 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001270 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1271 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1272 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001274 mbedtls_md5_starts_ret( &md5 );
1275 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1276 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1277 mbedtls_md5_update_ret( &md5, hash, 16 );
1278 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001280 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1281 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1282 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001283
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001284 mbedtls_sha1_starts_ret( &sha1 );
1285 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1286 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1287 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1288 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001292
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001293 mbedtls_md5_free( &md5 );
1294 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001295
Paul Bakker380da532012-04-18 16:10:25 +00001296 return;
1297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1301void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001302{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001303 mbedtls_md5_context md5;
1304 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001307
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001308 mbedtls_md5_init( &md5 );
1309 mbedtls_sha1_init( &sha1 );
1310
1311 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1312 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001313
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001314 mbedtls_md5_finish_ret( &md5, hash );
1315 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001319
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001320 mbedtls_md5_free( &md5 );
1321 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001322
Paul Bakker380da532012-04-18 16:10:25 +00001323 return;
1324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1328#if defined(MBEDTLS_SHA256_C)
1329void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001330{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001331 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001332
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001333 mbedtls_sha256_init( &sha256 );
1334
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001336
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001337 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001338 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001342
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001343 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001344
Paul Bakker380da532012-04-18 16:10:25 +00001345 return;
1346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SHA512_C)
1350void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001351{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001352 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001353
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001354 mbedtls_sha512_init( &sha512 );
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001357
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001358 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001359 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001364 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001365
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 return;
1367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#endif /* MBEDTLS_SHA512_C */
1369#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1372int 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 +02001373{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001374 unsigned char *p = ssl->handshake->premaster;
1375 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001376 const unsigned char *psk = ssl->conf->psk;
1377 size_t psk_len = ssl->conf->psk_len;
1378
1379 /* If the psk callback was called, use its result */
1380 if( ssl->handshake->psk != NULL )
1381 {
1382 psk = ssl->handshake->psk;
1383 psk_len = ssl->handshake->psk_len;
1384 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001385
1386 /*
1387 * PMS = struct {
1388 * opaque other_secret<0..2^16-1>;
1389 * opaque psk<0..2^16-1>;
1390 * };
1391 * with "other_secret" depending on the particular key exchange
1392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1394 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001395 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001396 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001398
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001399 *(p++) = (unsigned char)( psk_len >> 8 );
1400 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001401
1402 if( end < p || (size_t)( end - p ) < psk_len )
1403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1404
1405 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001406 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001407 }
1408 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1410#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1411 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001412 {
1413 /*
1414 * other_secret already set by the ClientKeyExchange message,
1415 * and is 48 bytes long
1416 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001417 if( end - p < 2 )
1418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1419
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001420 *p++ = 0;
1421 *p++ = 48;
1422 p += 48;
1423 }
1424 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1426#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1427 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001428 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001429 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001430 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001431
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001432 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001434 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001435 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001438 return( ret );
1439 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001440 *(p++) = (unsigned char)( len >> 8 );
1441 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001442 p += len;
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001445 }
1446 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1448#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1449 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001450 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001451 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001452 size_t zlen;
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001455 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001456 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001459 return( ret );
1460 }
1461
1462 *(p++) = (unsigned char)( zlen >> 8 );
1463 *(p++) = (unsigned char)( zlen );
1464 p += zlen;
1465
Janos Follath3fbdada2018-08-15 10:26:53 +01001466 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1467 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001468 }
1469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001474 }
1475
1476 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001477 if( end - p < 2 )
1478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001479
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001480 *(p++) = (unsigned char)( psk_len >> 8 );
1481 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001482
1483 if( end < p || (size_t)( end - p ) < psk_len )
1484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1485
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001486 memcpy( p, psk, psk_len );
1487 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001488
1489 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1490
1491 return( 0 );
1492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001496/*
1497 * SSLv3.0 MAC functions
1498 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001499#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001500static void ssl_mac( mbedtls_md_context_t *md_ctx,
1501 const unsigned char *secret,
1502 const unsigned char *buf, size_t len,
1503 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001504 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505{
1506 unsigned char header[11];
1507 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001508 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1510 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001511
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001512 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001514 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001515 else
Paul Bakker68884e32013-01-07 18:20:04 +01001516 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
1518 memcpy( header, ctr, 8 );
1519 header[ 8] = (unsigned char) type;
1520 header[ 9] = (unsigned char)( len >> 8 );
1521 header[10] = (unsigned char)( len );
1522
Paul Bakker68884e32013-01-07 18:20:04 +01001523 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_md_starts( md_ctx );
1525 mbedtls_md_update( md_ctx, secret, md_size );
1526 mbedtls_md_update( md_ctx, padding, padlen );
1527 mbedtls_md_update( md_ctx, header, 11 );
1528 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001529 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Paul Bakker68884e32013-01-07 18:20:04 +01001531 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_md_starts( md_ctx );
1533 mbedtls_md_update( md_ctx, secret, md_size );
1534 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001535 mbedtls_md_update( md_ctx, out, md_size );
1536 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001539
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001540/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001541 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001542#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001543 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1544 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1545 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1546/* This function makes sure every byte in the memory region is accessed
1547 * (in ascending addresses order) */
1548static void ssl_read_memory( unsigned char *p, size_t len )
1549{
1550 unsigned char acc = 0;
1551 volatile unsigned char force;
1552
1553 for( ; len != 0; p++, len-- )
1554 acc ^= *p;
1555
1556 force = acc;
1557 (void) force;
1558}
1559#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1560
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001561/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001563 */
Hanno Becker3307b532017-12-27 21:37:21 +00001564
Hanno Becker92c930f2019-04-29 17:31:37 +01001565#if defined(MBEDTLS_SSL_CID)
Hanno Becker89693692019-05-20 15:06:12 +01001566/* This functions transforms a DTLS plaintext fragment and a record content
1567 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001568 *
1569 * struct {
1570 * opaque content[DTLSPlaintext.length];
1571 * ContentType real_type;
1572 * uint8 zeros[length_of_padding];
1573 * } DTLSInnerPlaintext;
1574 *
1575 * Input:
1576 * - `content`: The beginning of the buffer holding the
1577 * plaintext to be wrapped.
1578 * - `*content_size`: The length of the plaintext in Bytes.
1579 * - `max_len`: The number of Bytes available starting from
1580 * `content`. This must be `>= *content_size`.
1581 * - `rec_type`: The desired record content type.
1582 *
1583 * Output:
1584 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1585 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1586 *
1587 * Returns:
1588 * - `0` on success.
1589 * - A negative error code if `max_len` didn't offer enough space
1590 * for the expansion.
1591 */
1592static int ssl_cid_build_inner_plaintext( unsigned char *content,
1593 size_t *content_size,
1594 size_t remaining,
1595 uint8_t rec_type )
1596{
1597 size_t len = *content_size;
1598 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
1599
1600 /* Write real content type */
1601 if( remaining == 0 )
1602 return( -1 );
1603 content[ len ] = rec_type;
1604 len++;
1605 remaining--;
1606
1607 if( remaining < pad )
1608 return( -1 );
1609 memset( content + len, 0, pad );
1610 len += pad;
1611 remaining -= pad;
1612
1613 *content_size = len;
1614 return( 0 );
1615}
1616
Hanno Becker7dc25772019-05-20 15:08:01 +01001617/* This function parses a DTLSInnerPlaintext structure.
1618 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001619static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1620 size_t *content_size,
1621 uint8_t *rec_type )
1622{
1623 size_t remaining = *content_size;
1624
1625 /* Determine length of padding by skipping zeroes from the back. */
1626 do
1627 {
1628 if( remaining == 0 )
1629 return( -1 );
1630 remaining--;
1631 } while( content[ remaining ] == 0 );
1632
1633 *content_size = remaining;
1634 *rec_type = content[ remaining ];
1635
1636 return( 0 );
1637}
1638#endif /* MBEDTLS_SSL_CID */
1639
Hanno Becker99abf512019-05-20 14:50:53 +01001640/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001641 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001642static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001643 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001644 mbedtls_record *rec )
1645{
Hanno Becker99abf512019-05-20 14:50:53 +01001646 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001647 *
1648 * additional_data = seq_num + TLSCompressed.type +
1649 * TLSCompressed.version + TLSCompressed.length;
1650 *
Hanno Becker99abf512019-05-20 14:50:53 +01001651 * For the CID extension, this is extended as follows
1652 * (quoting draft-ietf-tls-dtls-connection-id-05,
1653 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001654 *
1655 * additional_data = seq_num + DTLSPlaintext.type +
1656 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001657 * cid +
1658 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001659 * length_of_DTLSInnerPlaintext;
1660 */
1661
Hanno Becker3307b532017-12-27 21:37:21 +00001662 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1663 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001664 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001665
1666#if defined(MBEDTLS_SSL_CID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001667 if( rec->cid_len != 0 )
1668 {
1669 memcpy( add_data + 11, rec->cid, rec->cid_len );
1670 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1671 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1672 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1673 *add_data_len = 13 + 1 + rec->cid_len;
1674 }
1675 else
Hanno Beckere83efe62019-04-29 13:52:53 +01001676#endif /* MBEDTLS_SSL_CID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001677 {
1678 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1679 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1680 *add_data_len = 13;
1681 }
Hanno Becker3307b532017-12-27 21:37:21 +00001682}
1683
Hanno Becker611a83b2018-01-03 14:27:32 +00001684int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1685 mbedtls_ssl_transform *transform,
1686 mbedtls_record *rec,
1687 int (*f_rng)(void *, unsigned char *, size_t),
1688 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001691 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001692 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001693 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001694 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001695 size_t post_avail;
1696
1697 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001698#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker3307b532017-12-27 21:37:21 +00001699 ((void) ssl);
1700#endif
1701
1702 /* The PRNG is used for dynamic IV generation that's used
1703 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1704#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1705 ( defined(MBEDTLS_AES_C) || \
1706 defined(MBEDTLS_ARIA_C) || \
1707 defined(MBEDTLS_CAMELLIA_C) ) && \
1708 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1709 ((void) f_rng);
1710 ((void) p_rng);
1711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Hanno Becker3307b532017-12-27 21:37:21 +00001715 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001716 {
Hanno Becker3307b532017-12-27 21:37:21 +00001717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1719 }
Hanno Becker505089d2019-05-01 09:45:57 +01001720 if( rec == NULL
1721 || rec->buf == NULL
1722 || rec->buf_len < rec->data_offset
1723 || rec->buf_len - rec->data_offset < rec->data_len
1724#if defined(MBEDTLS_SSL_CID)
1725 || rec->cid_len != 0
1726#endif
1727 )
Hanno Becker3307b532017-12-27 21:37:21 +00001728 {
1729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001731 }
1732
Hanno Becker3307b532017-12-27 21:37:21 +00001733 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001734 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001736 data, rec->data_len );
1737
1738 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1739
1740 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1741 {
1742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1743 (unsigned) rec->data_len,
1744 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1746 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001747
Hanno Beckere83efe62019-04-29 13:52:53 +01001748#if defined(MBEDTLS_SSL_CID)
1749 /*
1750 * Add CID information
1751 */
1752 rec->cid_len = transform->out_cid_len;
1753 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1754 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001755
1756 if( rec->cid_len != 0 )
1757 {
1758 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001759 * Wrap plaintext into DTLSInnerPlaintext structure.
1760 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001761 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001762 * Note that this changes `rec->data_len`, and hence
1763 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001764 */
1765 if( ssl_cid_build_inner_plaintext( data,
1766 &rec->data_len,
1767 post_avail,
1768 rec->type ) != 0 )
1769 {
1770 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1771 }
1772
1773 rec->type = MBEDTLS_SSL_MSG_CID;
1774 }
Hanno Beckere83efe62019-04-29 13:52:53 +01001775#endif /* MBEDTLS_SSL_CID */
1776
Hanno Becker92c930f2019-04-29 17:31:37 +01001777 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1778
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001780 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001782#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 if( mode == MBEDTLS_MODE_STREAM ||
1784 ( mode == MBEDTLS_MODE_CBC
1785#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001786 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001787#endif
1788 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 {
Hanno Becker3307b532017-12-27 21:37:21 +00001790 if( post_avail < transform->maclen )
1791 {
1792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1793 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1794 }
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001797 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001799 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001800 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1801 data, rec->data_len, rec->ctr, rec->type, mac );
1802 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 }
1804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1807 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001808 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001809 {
Hanno Becker992b6872017-11-09 18:57:39 +00001810 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1811
Hanno Beckere83efe62019-04-29 13:52:53 +01001812 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001813
Hanno Becker3307b532017-12-27 21:37:21 +00001814 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001815 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001816 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1817 data, rec->data_len );
1818 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1819 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1820
1821 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001822 }
1823 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001824#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 }
1829
Hanno Becker3307b532017-12-27 21:37:21 +00001830 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1831 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001832
Hanno Becker3307b532017-12-27 21:37:21 +00001833 rec->data_len += transform->maclen;
1834 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001835 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001836 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001837#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001839 /*
1840 * Encrypt
1841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1843 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001845 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001846 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001848 "including %d bytes of padding",
1849 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Hanno Becker3307b532017-12-27 21:37:21 +00001851 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1852 transform->iv_enc, transform->ivlen,
1853 data, rec->data_len,
1854 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001857 return( ret );
1858 }
1859
Hanno Becker3307b532017-12-27 21:37:21 +00001860 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 }
Paul Bakker68884e32013-01-07 18:20:04 +01001866 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00001868
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001869#if defined(MBEDTLS_GCM_C) || \
1870 defined(MBEDTLS_CCM_C) || \
1871 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001873 mode == MBEDTLS_MODE_CCM ||
1874 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001875 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001876 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001877 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00001878 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001879
Hanno Becker3307b532017-12-27 21:37:21 +00001880 /* Check that there's space for both the authentication tag
1881 * and the explicit IV before and after the record content. */
1882 if( post_avail < transform->taglen ||
1883 rec->data_offset < explicit_iv_len )
1884 {
1885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1886 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1887 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001888
Paul Bakker68884e32013-01-07 18:20:04 +01001889 /*
1890 * Generate IV
1891 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001892 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1893 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001894 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001895 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00001896 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1897 explicit_iv_len );
1898 /* Prefix record content with explicit IV. */
1899 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001900 }
1901 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1902 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001903 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001904 unsigned char i;
1905
1906 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1907
1908 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001909 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001910 }
1911 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001912 {
1913 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001916 }
1917
Hanno Beckere83efe62019-04-29 13:52:53 +01001918 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01001919
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001920 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1921 iv, transform->ivlen );
1922 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00001923 data - explicit_iv_len, explicit_iv_len );
1924 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01001925 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00001928 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001929
Paul Bakker68884e32013-01-07 18:20:04 +01001930 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001931 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001932 */
Hanno Becker3307b532017-12-27 21:37:21 +00001933
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001934 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00001935 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01001936 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00001937 data, rec->data_len, /* source */
1938 data, &rec->data_len, /* destination */
1939 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001942 return( ret );
1943 }
1944
Hanno Becker3307b532017-12-27 21:37:21 +00001945 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1946 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001947
Hanno Becker3307b532017-12-27 21:37:21 +00001948 rec->data_len += transform->taglen + explicit_iv_len;
1949 rec->data_offset -= explicit_iv_len;
1950 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001951 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1955#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001956 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001959 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001960 size_t padlen, i;
1961 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001962
Hanno Becker3307b532017-12-27 21:37:21 +00001963 /* Currently we're always using minimal padding
1964 * (up to 255 bytes would be allowed). */
1965 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1966 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 padlen = 0;
1968
Hanno Becker3307b532017-12-27 21:37:21 +00001969 /* Check there's enough space in the buffer for the padding. */
1970 if( post_avail < padlen + 1 )
1971 {
1972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1973 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1974 }
1975
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001977 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Hanno Becker3307b532017-12-27 21:37:21 +00001979 rec->data_len += padlen + 1;
1980 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001983 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001984 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1985 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986 */
Hanno Becker3307b532017-12-27 21:37:21 +00001987 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001988 {
Hanno Becker3307b532017-12-27 21:37:21 +00001989 if( f_rng == NULL )
1990 {
1991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
1992 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1993 }
1994
1995 if( rec->data_offset < transform->ivlen )
1996 {
1997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1998 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1999 }
2000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002001 /*
2002 * Generate IV
2003 */
Hanno Becker3307b532017-12-27 21:37:21 +00002004 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002005 if( ret != 0 )
2006 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002007
Hanno Becker3307b532017-12-27 21:37:21 +00002008 memcpy( data - transform->ivlen, transform->iv_enc,
2009 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002010
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002011 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002015 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002016 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002017 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
Hanno Becker3307b532017-12-27 21:37:21 +00002019 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2020 transform->iv_enc,
2021 transform->ivlen,
2022 data, rec->data_len,
2023 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002026 return( ret );
2027 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002028
Hanno Becker3307b532017-12-27 21:37:21 +00002029 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002033 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002036 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002037 {
2038 /*
2039 * Save IV in SSL3 and TLS1
2040 */
Hanno Becker3307b532017-12-27 21:37:21 +00002041 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2042 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
Hanno Becker3307b532017-12-27 21:37:21 +00002044 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002045#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002046 {
2047 data -= transform->ivlen;
2048 rec->data_offset -= transform->ivlen;
2049 rec->data_len += transform->ivlen;
2050 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002053 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002054 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002055 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2056
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002057 /*
2058 * MAC(MAC_write_key, seq_num +
2059 * TLSCipherText.type +
2060 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002061 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002062 * IV + // except for TLS 1.0
2063 * ENC(content + padding + padding_length));
2064 */
Hanno Becker3307b532017-12-27 21:37:21 +00002065
2066 if( post_avail < transform->maclen)
2067 {
2068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2069 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2070 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002071
Hanno Beckere83efe62019-04-29 13:52:53 +01002072 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002075 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002076 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002077
Hanno Becker3307b532017-12-27 21:37:21 +00002078 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002079 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002080 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2081 data, rec->data_len );
2082 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2083 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002084
Hanno Becker3307b532017-12-27 21:37:21 +00002085 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002086
Hanno Becker3307b532017-12-27 21:37:21 +00002087 rec->data_len += transform->maclen;
2088 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002089 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002090 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002093 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002095 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002101 /* Make extra sure authentication was performed, exactly once */
2102 if( auth_done != 1 )
2103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2105 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002106 }
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
2110 return( 0 );
2111}
2112
Hanno Becker611a83b2018-01-03 14:27:32 +00002113int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2114 mbedtls_ssl_transform *transform,
2115 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002116{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002117 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002119 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002120#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002121 size_t padlen = 0, correct = 1;
2122#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002123 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002124 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002125 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002126
Hanno Becker611a83b2018-01-03 14:27:32 +00002127#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002128 ((void) ssl);
2129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002132 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2136 }
2137 if( rec == NULL ||
2138 rec->buf == NULL ||
2139 rec->buf_len < rec->data_offset ||
2140 rec->buf_len - rec->data_offset < rec->data_len )
2141 {
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002144 }
2145
Hanno Becker4c6876b2017-12-27 21:28:58 +00002146 data = rec->buf + rec->data_offset;
2147 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Hanno Beckere83efe62019-04-29 13:52:53 +01002149#if defined(MBEDTLS_SSL_CID)
2150 /*
2151 * Match record's CID with incoming CID.
2152 */
2153
Hanno Becker4c6fe122019-04-30 16:56:40 +01002154 /* Uncomment this once CID parsing is in place */
Hanno Beckere83efe62019-04-29 13:52:53 +01002155 /* if( rec->cid_len != transform->in_cid_len || */
2156 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2157 /* { */
2158 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2159 /* } */
Hanno Becker4c6fe122019-04-30 16:56:40 +01002160
2161 /* Remove this once CID parsing is in place */
Hanno Beckere83efe62019-04-29 13:52:53 +01002162 rec->cid_len = transform->in_cid_len;
2163 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2164 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2165#endif /* MBEDTLS_SSL_CID */
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2168 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002169 {
2170 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002171 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2172 transform->iv_dec,
2173 transform->ivlen,
2174 data, rec->data_len,
2175 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002178 return( ret );
2179 }
2180
Hanno Becker4c6876b2017-12-27 21:28:58 +00002181 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
Paul Bakker68884e32013-01-07 18:20:04 +01002187 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002189#if defined(MBEDTLS_GCM_C) || \
2190 defined(MBEDTLS_CCM_C) || \
2191 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002193 mode == MBEDTLS_MODE_CCM ||
2194 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002195 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002196 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002197 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002198
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002199 /*
2200 * Compute and update sizes
2201 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002202 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002205 "+ taglen (%d)", rec->data_len,
2206 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002208 }
Paul Bakker68884e32013-01-07 18:20:04 +01002209
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002210 /*
2211 * Prepare IV
2212 */
2213 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2214 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002215 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002216 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002217 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002218
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002219 }
2220 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2221 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002222 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002223 unsigned char i;
2224
2225 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2226
2227 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002228 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002229 }
2230 else
2231 {
2232 /* Reminder if we ever add an AEAD mode with a different size */
2233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2235 }
2236
Hanno Becker4c6876b2017-12-27 21:28:58 +00002237 data += explicit_iv_len;
2238 rec->data_offset += explicit_iv_len;
2239 rec->data_len -= explicit_iv_len + transform->taglen;
2240
Hanno Beckere83efe62019-04-29 13:52:53 +01002241 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002242 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002243 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002244
2245 memcpy( transform->iv_dec + transform->fixed_ivlen,
2246 data - explicit_iv_len, explicit_iv_len );
2247
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002248 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002249 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002250 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002251
Paul Bakker68884e32013-01-07 18:20:04 +01002252
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002253 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002254 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002255 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002256 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2257 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002258 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002259 data, rec->data_len,
2260 data, &olen,
2261 data + rec->data_len,
2262 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2267 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002268
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002269 return( ret );
2270 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002271 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002272
Hanno Becker4c6876b2017-12-27 21:28:58 +00002273 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002277 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2281#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002282 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002285 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002286
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 /*
Paul Bakker45829992013-01-03 14:52:21 +01002288 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002291 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2292 {
2293 /* The ciphertext is prefixed with the CBC IV. */
2294 minlen += transform->ivlen;
2295 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002296#endif
Paul Bakker45829992013-01-03 14:52:21 +01002297
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 /* Size considerations:
2299 *
2300 * - The CBC cipher text must not be empty and hence
2301 * at least of size transform->ivlen.
2302 *
2303 * Together with the potential IV-prefix, this explains
2304 * the first of the two checks below.
2305 *
2306 * - The record must contain a MAC, either in plain or
2307 * encrypted, depending on whether Encrypt-then-MAC
2308 * is used or not.
2309 * - If it is, the message contains the IV-prefix,
2310 * the CBC ciphertext, and the MAC.
2311 * - If it is not, the padded plaintext, and hence
2312 * the CBC ciphertext, has at least length maclen + 1
2313 * because there is at least the padding length byte.
2314 *
2315 * As the CBC ciphertext is not empty, both cases give the
2316 * lower bound minlen + maclen + 1 on the record size, which
2317 * we test for in the second check below.
2318 */
2319 if( rec->data_len < minlen + transform->ivlen ||
2320 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002323 "+ 1 ) ( + expl IV )", rec->data_len,
2324 transform->ivlen,
2325 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002327 }
2328
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329 /*
2330 * Authenticate before decrypt if enabled
2331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002333 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002334 {
Hanno Becker992b6872017-11-09 18:57:39 +00002335 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002338
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2340 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002341
Hanno Beckere83efe62019-04-29 13:52:53 +01002342 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002343
Hanno Beckere83efe62019-04-29 13:52:53 +01002344 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2345 add_data_len );
2346 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2347 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002348 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2349 data, rec->data_len );
2350 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2351 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002352
Hanno Becker4c6876b2017-12-27 21:28:58 +00002353 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2354 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002355 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002356 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002357
Hanno Becker4c6876b2017-12-27 21:28:58 +00002358 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2359 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002363 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002364 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002367
2368 /*
2369 * Check length sanity
2370 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002371 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002376 }
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002379 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002380 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002381 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002382 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002383 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 /* This is safe because data_len >= minlen + maclen + 1 initially,
2385 * and at this point we have at most subtracted maclen (note that
2386 * minlen == transform->ivlen here). */
2387 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002388
Hanno Becker4c6876b2017-12-27 21:28:58 +00002389 data += transform->ivlen;
2390 rec->data_offset += transform->ivlen;
2391 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002392 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002394
Hanno Becker4c6876b2017-12-27 21:28:58 +00002395 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2396 transform->iv_dec, transform->ivlen,
2397 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002400 return( ret );
2401 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002402
Hanno Becker4c6876b2017-12-27 21:28:58 +00002403 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2406 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002407 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002411 {
2412 /*
2413 * Save IV in SSL3 and TLS1
2414 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2416 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Hanno Becker4c6876b2017-12-27 21:28:58 +00002420 /* Safe since data_len >= minlen + maclen + 1, so after having
2421 * subtracted at most minlen and maclen up to this point,
2422 * data_len > 0. */
2423 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002424
Hanno Becker4c6876b2017-12-27 21:28:58 +00002425 if( auth_done == 1 )
2426 {
2427 correct *= ( rec->data_len >= padlen + 1 );
2428 padlen *= ( rec->data_len >= padlen + 1 );
2429 }
2430 else
Paul Bakker45829992013-01-03 14:52:21 +01002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002433 if( rec->data_len < transform->maclen + padlen + 1 )
2434 {
2435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2436 rec->data_len,
2437 transform->maclen,
2438 padlen + 1 ) );
2439 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002440#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002441
2442 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2443 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Hanno Becker4c6876b2017-12-27 21:28:58 +00002446 padlen++;
2447
2448 /* Regardless of the validity of the padding,
2449 * we have data_len >= padlen here. */
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002452 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002454 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_SSL_DEBUG_ALL)
2457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002458 "should be no more than %d",
2459 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002460#endif
Paul Bakker45829992013-01-03 14:52:21 +01002461 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463 }
2464 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2466#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2467 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002468 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002470 /* The padding check involves a series of up to 256
2471 * consecutive memory reads at the end of the record
2472 * plaintext buffer. In order to hide the length and
2473 * validity of the padding, always perform exactly
2474 * `min(256,plaintext_len)` reads (but take into account
2475 * only the last `padlen` bytes for the padding check). */
2476 size_t pad_count = 0;
2477 size_t real_count = 0;
2478 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002479
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 /* Index of first padding byte; it has been ensured above
2481 * that the subtraction is safe. */
2482 size_t const padding_idx = rec->data_len - padlen;
2483 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2484 size_t const start_idx = rec->data_len - num_checks;
2485 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002486
Hanno Becker4c6876b2017-12-27 21:28:58 +00002487 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002488 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002489 real_count |= ( idx >= padding_idx );
2490 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002491 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002492 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002495 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002497#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002498 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002500 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2502 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002506 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507
Hanno Becker4c6876b2017-12-27 21:28:58 +00002508 /* If the padding was found to be invalid, padlen == 0
2509 * and the subtraction is safe. If the padding was found valid,
2510 * padlen hasn't been changed and the previous assertion
2511 * data_len >= padlen still holds. */
2512 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002516 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002522#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
2527 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002528 * Authenticate if not done yet.
2529 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002531#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002532 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002533 {
Hanno Becker992b6872017-11-09 18:57:39 +00002534 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002535
Hanno Becker4c6876b2017-12-27 21:28:58 +00002536 /* If the initial value of padlen was such that
2537 * data_len < maclen + padlen + 1, then padlen
2538 * got reset to 1, and the initial check
2539 * data_len >= minlen + maclen + 1
2540 * guarantees that at this point we still
2541 * have at least data_len >= maclen.
2542 *
2543 * If the initial value of padlen was such that
2544 * data_len >= maclen + padlen + 1, then we have
2545 * subtracted either padlen + 1 (if the padding was correct)
2546 * or 0 (if the padding was incorrect) since then,
2547 * hence data_len >= maclen in any case.
2548 */
2549 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Hanno Beckere83efe62019-04-29 13:52:53 +01002551 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002554 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002555 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002556 ssl_mac( &transform->md_ctx_dec,
2557 transform->mac_dec,
2558 data, rec->data_len,
2559 rec->ctr, rec->type,
2560 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002561 }
2562 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2564#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2565 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002566 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002567 {
2568 /*
2569 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002570 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 *
2572 * Known timing attacks:
2573 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2574 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002575 * To compensate for different timings for the MAC calculation
2576 * depending on how much padding was removed (which is determined
2577 * by padlen), process extra_run more blocks through the hash
2578 * function.
2579 *
2580 * The formula in the paper is
2581 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2582 * where L1 is the size of the header plus the decrypted message
2583 * plus CBC padding and L2 is the size of the header plus the
2584 * decrypted message. This is for an underlying hash function
2585 * with 64-byte blocks.
2586 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2587 * correctly. We round down instead of up, so -56 is the correct
2588 * value for our calculations instead of -55.
2589 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002590 * Repeat the formula rather than defining a block_size variable.
2591 * This avoids requiring division by a variable at runtime
2592 * (which would be marginally less efficient and would require
2593 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002594 */
2595 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002597
2598 /*
2599 * The next two sizes are the minimum and maximum values of
2600 * in_msglen over all padlen values.
2601 *
2602 * They're independent of padlen, since we previously did
2603 * in_msglen -= padlen.
2604 *
2605 * Note that max_len + maclen is never more than the buffer
2606 * length, as we previously did in_msglen -= maclen too.
2607 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002608 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002609 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2610
Hanno Becker4c6876b2017-12-27 21:28:58 +00002611 memset( tmp, 0, sizeof( tmp ) );
2612
2613 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002614 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002615#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2616 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002617 case MBEDTLS_MD_MD5:
2618 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002619 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002620 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002621 extra_run =
2622 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2623 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002624 break;
2625#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002626#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002627 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002628 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002629 extra_run =
2630 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2631 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002632 break;
2633#endif
2634 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002636 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2637 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002638
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002639 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002640
Hanno Beckere83efe62019-04-29 13:52:53 +01002641 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2642 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002643 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2644 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002645 /* Make sure we access everything even when padlen > 0. This
2646 * makes the synchronisation requirements for just-in-time
2647 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002648 ssl_read_memory( data + rec->data_len, padlen );
2649 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002650
2651 /* Call mbedtls_md_process at least once due to cache attacks
2652 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002653 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002654 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002655
Hanno Becker4c6876b2017-12-27 21:28:58 +00002656 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002657
2658 /* Make sure we access all the memory that could contain the MAC,
2659 * before we check it in the next code block. This makes the
2660 * synchronisation requirements for just-in-time Prime+Probe
2661 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002662 ssl_read_memory( data + min_len,
2663 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002664 }
2665 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2667 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002673#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002674 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2675 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002676#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2679 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#if defined(MBEDTLS_SSL_DEBUG_ALL)
2682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002683#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002684 correct = 0;
2685 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002686 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002687 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002688
2689 /*
2690 * Finally check the correct flag
2691 */
2692 if( correct == 0 )
2693 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002694#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002695
2696 /* Make extra sure authentication was performed, exactly once */
2697 if( auth_done != 1 )
2698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2700 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Hanno Becker92c930f2019-04-29 17:31:37 +01002703#if defined(MBEDTLS_SSL_CID)
2704 if( rec->cid_len != 0 )
2705 {
2706 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2707 &rec->type );
2708 if( ret != 0 )
2709 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2710 }
2711#endif /* MBEDTLS_SSL_CID */
2712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
2715 return( 0 );
2716}
2717
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002718#undef MAC_NONE
2719#undef MAC_PLAINTEXT
2720#undef MAC_CIPHERTEXT
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002723/*
2724 * Compression/decompression functions
2725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002727{
2728 int ret;
2729 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002730 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002731 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002732 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002735
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002736 if( len_pre == 0 )
2737 return( 0 );
2738
Paul Bakker2770fbd2012-07-03 13:30:23 +00002739 memcpy( msg_pre, ssl->out_msg, len_pre );
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002742 ssl->out_msglen ) );
2743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002745 ssl->out_msg, ssl->out_msglen );
2746
Paul Bakker48916f92012-09-16 19:57:18 +00002747 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2748 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2749 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002750 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002751
Paul Bakker48916f92012-09-16 19:57:18 +00002752 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753 if( ret != Z_OK )
2754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2756 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002757 }
2758
Angus Grattond8213d02016-05-25 20:56:48 +10002759 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002760 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002763 ssl->out_msglen ) );
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766 ssl->out_msg, ssl->out_msglen );
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002769
2770 return( 0 );
2771}
2772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002774{
2775 int ret;
2776 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002777 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002778 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002779 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002782
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002783 if( len_pre == 0 )
2784 return( 0 );
2785
Paul Bakker2770fbd2012-07-03 13:30:23 +00002786 memcpy( msg_pre, ssl->in_msg, len_pre );
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002789 ssl->in_msglen ) );
2790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002792 ssl->in_msg, ssl->in_msglen );
2793
Paul Bakker48916f92012-09-16 19:57:18 +00002794 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2795 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2796 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002797 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002798 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002799
Paul Bakker48916f92012-09-16 19:57:18 +00002800 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002801 if( ret != Z_OK )
2802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2804 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002805 }
2806
Angus Grattond8213d02016-05-25 20:56:48 +10002807 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002808 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002811 ssl->in_msglen ) );
2812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002814 ssl->in_msg, ssl->in_msglen );
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002817
2818 return( 0 );
2819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2823static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_PROTO_DTLS)
2826static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002827{
2828 /* If renegotiation is not enforced, retransmit until we would reach max
2829 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002830 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002831 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002832 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002833 unsigned char doublings = 1;
2834
2835 while( ratio != 0 )
2836 {
2837 ++doublings;
2838 ratio >>= 1;
2839 }
2840
2841 if( ++ssl->renego_records_seen > doublings )
2842 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002844 return( 0 );
2845 }
2846 }
2847
2848 return( ssl_write_hello_request( ssl ) );
2849}
2850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002852
Paul Bakker5121ce52009-01-03 21:22:43 +00002853/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002854 * Fill the input message buffer by appending data to it.
2855 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002856 *
2857 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2858 * available (from this read and/or a previous one). Otherwise, an error code
2859 * is returned (possibly EOF or WANT_READ).
2860 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002861 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2862 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2863 * since we always read a whole datagram at once.
2864 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002865 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002866 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002869{
Paul Bakker23986e52011-04-24 08:57:21 +00002870 int ret;
2871 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002875 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002878 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002880 }
2881
Angus Grattond8213d02016-05-25 20:56:48 +10002882 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002886 }
2887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002889 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002891 uint32_t timeout;
2892
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002893 /* Just to be sure */
2894 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2895 {
2896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2897 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2899 }
2900
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002901 /*
2902 * The point is, we need to always read a full datagram at once, so we
2903 * sometimes read more then requested, and handle the additional data.
2904 * It could be the rest of the current record (while fetching the
2905 * header) and/or some other records in the same datagram.
2906 */
2907
2908 /*
2909 * Move to the next record in the already read datagram if applicable
2910 */
2911 if( ssl->next_record_offset != 0 )
2912 {
2913 if( ssl->in_left < ssl->next_record_offset )
2914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002917 }
2918
2919 ssl->in_left -= ssl->next_record_offset;
2920
2921 if( ssl->in_left != 0 )
2922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002924 ssl->next_record_offset ) );
2925 memmove( ssl->in_hdr,
2926 ssl->in_hdr + ssl->next_record_offset,
2927 ssl->in_left );
2928 }
2929
2930 ssl->next_record_offset = 0;
2931 }
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002935
2936 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002937 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002938 */
2939 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002942 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002943 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002944
2945 /*
2946 * A record can't be split accross datagrams. If we need to read but
2947 * are not at the beginning of a new record, the caller did something
2948 * wrong.
2949 */
2950 if( ssl->in_left != 0 )
2951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002954 }
2955
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002956 /*
2957 * Don't even try to read if time's out already.
2958 * This avoids by-passing the timer when repeatedly receiving messages
2959 * that will end up being dropped.
2960 */
2961 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002962 {
2963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002964 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002965 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002966 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002967 {
Angus Grattond8213d02016-05-25 20:56:48 +10002968 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002971 timeout = ssl->handshake->retransmit_timeout;
2972 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002973 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002976
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002977 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002978 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2979 timeout );
2980 else
2981 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002984
2985 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002987 }
2988
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002989 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002992 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002995 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002996 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002999 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003000 }
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003005 return( ret );
3006 }
3007
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003008 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003009 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003011 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003013 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003014 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003017 return( ret );
3018 }
3019
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003020 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003023 }
3024
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 if( ret < 0 )
3026 return( ret );
3027
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003028 ssl->in_left = ret;
3029 }
3030 else
3031#endif
3032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003034 ssl->in_left, nb_want ) );
3035
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003036 while( ssl->in_left < nb_want )
3037 {
3038 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003039
3040 if( ssl_check_timer( ssl ) != 0 )
3041 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3042 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003043 {
3044 if( ssl->f_recv_timeout != NULL )
3045 {
3046 ret = ssl->f_recv_timeout( ssl->p_bio,
3047 ssl->in_hdr + ssl->in_left, len,
3048 ssl->conf->read_timeout );
3049 }
3050 else
3051 {
3052 ret = ssl->f_recv( ssl->p_bio,
3053 ssl->in_hdr + ssl->in_left, len );
3054 }
3055 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003058 ssl->in_left, nb_want ) );
3059 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003060
3061 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003063
3064 if( ret < 0 )
3065 return( ret );
3066
mohammad160352aecb92018-03-28 23:41:40 -07003067 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003068 {
Darryl Green11999bb2018-03-13 15:22:58 +00003069 MBEDTLS_SSL_DEBUG_MSG( 1,
3070 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003071 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3073 }
3074
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003075 ssl->in_left += ret;
3076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 }
3078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
3081 return( 0 );
3082}
3083
3084/*
3085 * Flush any data not yet written
3086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003088{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003089 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003090 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003094 if( ssl->f_send == NULL )
3095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003097 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003099 }
3100
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003101 /* Avoid incrementing counter if data is flushed */
3102 if( ssl->out_left == 0 )
3103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003105 return( 0 );
3106 }
3107
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 while( ssl->out_left > 0 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003111 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Hanno Becker2b1e3542018-08-06 11:19:13 +01003113 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003114 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
3118 if( ret <= 0 )
3119 return( ret );
3120
mohammad160352aecb92018-03-28 23:41:40 -07003121 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003122 {
Darryl Green11999bb2018-03-13 15:22:58 +00003123 MBEDTLS_SSL_DEBUG_MSG( 1,
3124 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003125 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3127 }
3128
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 ssl->out_left -= ret;
3130 }
3131
Hanno Becker2b1e3542018-08-06 11:19:13 +01003132#if defined(MBEDTLS_SSL_PROTO_DTLS)
3133 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003134 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003135 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003136 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003137 else
3138#endif
3139 {
3140 ssl->out_hdr = ssl->out_buf + 8;
3141 }
3142 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
3146 return( 0 );
3147}
3148
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003149/*
3150 * Functions to handle the DTLS retransmission state machine
3151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003153/*
3154 * Append current handshake message to current outgoing flight
3155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3160 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3161 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003162
3163 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003164 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003165 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003168 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003169 }
3170
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003171 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003172 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003175 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003176 }
3177
3178 /* Copy current handshake message with headers */
3179 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3180 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003181 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003182 msg->next = NULL;
3183
3184 /* Append to the current flight */
3185 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003186 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003187 else
3188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003190 while( cur->next != NULL )
3191 cur = cur->next;
3192 cur->next = msg;
3193 }
3194
Hanno Becker3b235902018-08-06 09:54:53 +01003195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003196 return( 0 );
3197}
3198
3199/*
3200 * Free the current flight of handshake messages
3201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 mbedtls_ssl_flight_item *cur = flight;
3205 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206
3207 while( cur != NULL )
3208 {
3209 next = cur->next;
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 mbedtls_free( cur->p );
3212 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003213
3214 cur = next;
3215 }
3216}
3217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3219static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003220#endif
3221
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003222/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003223 * Swap transform_out and out_ctr with the alternative ones
3224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003228 unsigned char tmp_out_ctr[8];
3229
3230 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003233 return;
3234 }
3235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003237
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003238 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003239 tmp_transform = ssl->transform_out;
3240 ssl->transform_out = ssl->handshake->alt_transform_out;
3241 ssl->handshake->alt_transform_out = tmp_transform;
3242
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003243 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003244 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3245 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003246 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003247
3248 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003249 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3252 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3257 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003258 }
3259 }
3260#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261}
3262
3263/*
3264 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003265 */
3266int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3267{
3268 int ret = 0;
3269
3270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3271
3272 ret = mbedtls_ssl_flight_transmit( ssl );
3273
3274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3275
3276 return( ret );
3277}
3278
3279/*
3280 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003281 *
3282 * Need to remember the current message in case flush_output returns
3283 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003284 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003285 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003286int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003287{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003288 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003292 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003294
3295 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003296 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003297 ssl_swap_epochs( ssl );
3298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003300 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003301
3302 while( ssl->handshake->cur_msg != NULL )
3303 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003304 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003305 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003306
Hanno Beckere1dcb032018-08-17 16:47:58 +01003307 int const is_finished =
3308 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3309 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3310
Hanno Becker04da1892018-08-14 13:22:10 +01003311 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3312 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3313
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003314 /* Swap epochs before sending Finished: we can't do it after
3315 * sending ChangeCipherSpec, in case write returns WANT_READ.
3316 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003317 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003318 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003320 ssl_swap_epochs( ssl );
3321 }
3322
Hanno Becker67bc7c32018-08-06 11:33:50 +01003323 ret = ssl_get_remaining_payload_in_datagram( ssl );
3324 if( ret < 0 )
3325 return( ret );
3326 max_frag_len = (size_t) ret;
3327
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003328 /* CCS is copied as is, while HS messages may need fragmentation */
3329 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3330 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003331 if( max_frag_len == 0 )
3332 {
3333 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3334 return( ret );
3335
3336 continue;
3337 }
3338
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003339 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003340 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003341 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003343 /* Update position inside current message */
3344 ssl->handshake->cur_msg_p += cur->len;
3345 }
3346 else
3347 {
3348 const unsigned char * const p = ssl->handshake->cur_msg_p;
3349 const size_t hs_len = cur->len - 12;
3350 const size_t frag_off = p - ( cur->p + 12 );
3351 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003353
Hanno Beckere1dcb032018-08-17 16:47:58 +01003354 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003355 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003356 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003357 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358
Hanno Becker67bc7c32018-08-06 11:33:50 +01003359 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3360 return( ret );
3361
3362 continue;
3363 }
3364 max_hs_frag_len = max_frag_len - 12;
3365
3366 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3367 max_hs_frag_len : rem_len;
3368
3369 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003370 {
3371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003372 (unsigned) cur_hs_frag_len,
3373 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003374 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003375
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003376 /* Messages are stored with handshake headers as if not fragmented,
3377 * copy beginning of headers then fill fragmentation fields.
3378 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3379 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003380
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003381 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3382 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3383 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3384
Hanno Becker67bc7c32018-08-06 11:33:50 +01003385 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3386 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3387 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003388
3389 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3390
Hanno Becker3f7b9732018-08-28 09:53:25 +01003391 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003392 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3393 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003394 ssl->out_msgtype = cur->type;
3395
3396 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003397 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003398 }
3399
3400 /* If done with the current message move to the next one if any */
3401 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3402 {
3403 if( cur->next != NULL )
3404 {
3405 ssl->handshake->cur_msg = cur->next;
3406 ssl->handshake->cur_msg_p = cur->next->p + 12;
3407 }
3408 else
3409 {
3410 ssl->handshake->cur_msg = NULL;
3411 ssl->handshake->cur_msg_p = NULL;
3412 }
3413 }
3414
3415 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003416 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003419 return( ret );
3420 }
3421 }
3422
Hanno Becker67bc7c32018-08-06 11:33:50 +01003423 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3424 return( ret );
3425
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003426 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3428 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003429 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003432 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3433 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003434
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003436
3437 return( 0 );
3438}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003439
3440/*
3441 * To be called when the last message of an incoming flight is received.
3442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003444{
3445 /* We won't need to resend that one any more */
3446 ssl_flight_free( ssl->handshake->flight );
3447 ssl->handshake->flight = NULL;
3448 ssl->handshake->cur_msg = NULL;
3449
3450 /* The next incoming flight will start with this msg_seq */
3451 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3452
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003453 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003454 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003455
Hanno Becker0271f962018-08-16 13:23:47 +01003456 /* Clear future message buffering structure. */
3457 ssl_buffering_free( ssl );
3458
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003459 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003460 ssl_set_timer( ssl, 0 );
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3463 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003466 }
3467 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003469}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003470
3471/*
3472 * To be called when the last message of an outgoing flight is send.
3473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003475{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003476 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003477 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3480 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003483 }
3484 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003488
Paul Bakker5121ce52009-01-03 21:22:43 +00003489/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003490 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003492
3493/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003494 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003495 *
3496 * - fill in handshake headers
3497 * - update handshake checksum
3498 * - DTLS: save message for resending
3499 * - then pass to the record layer
3500 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003501 * DTLS: except for HelloRequest, messages are only queued, and will only be
3502 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003503 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003504 * Inputs:
3505 * - ssl->out_msglen: 4 + actual handshake message len
3506 * (4 is the size of handshake headers for TLS)
3507 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3508 * - ssl->out_msg + 4: the handshake message body
3509 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003510 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003511 * - ssl->out_msglen: the length of the record contents
3512 * (including handshake headers but excluding record headers)
3513 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003515int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003516{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003517 int ret;
3518 const size_t hs_len = ssl->out_msglen - 4;
3519 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3522
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003523 /*
3524 * Sanity checks
3525 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003526 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003527 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3528 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003529 /* In SSLv3, the client might send a NoCertificate alert. */
3530#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3531 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3532 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3533 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3534#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3535 {
3536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3538 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003541 /* Whenever we send anything different from a
3542 * HelloRequest we should be in a handshake - double check. */
3543 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3544 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003545 ssl->handshake == NULL )
3546 {
3547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003553 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003555 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003558 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003559#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003560
Hanno Beckerb50a2532018-08-06 11:52:54 +01003561 /* Double-check that we did not exceed the bounds
3562 * of the outgoing record buffer.
3563 * This should never fail as the various message
3564 * writing functions must obey the bounds of the
3565 * outgoing record buffer, but better be safe.
3566 *
3567 * Note: We deliberately do not check for the MTU or MFL here.
3568 */
3569 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3570 {
3571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3572 "size %u, maximum %u",
3573 (unsigned) ssl->out_msglen,
3574 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3576 }
3577
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003578 /*
3579 * Fill handshake headers
3580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003583 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3584 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3585 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003587 /*
3588 * DTLS has additional fields in the Handshake layer,
3589 * between the length field and the actual payload:
3590 * uint16 message_seq;
3591 * uint24 fragment_offset;
3592 * uint24 fragment_length;
3593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003596 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003597 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003598 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003599 {
3600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3601 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003602 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003603 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3605 }
3606
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003607 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003608 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003609
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003610 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003611 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003612 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003613 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3614 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3615 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003616 }
3617 else
3618 {
3619 ssl->out_msg[4] = 0;
3620 ssl->out_msg[5] = 0;
3621 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003622
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003623 /* Handshake hashes are computed without fragmentation,
3624 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003625 memset( ssl->out_msg + 6, 0x00, 3 );
3626 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003629
Hanno Becker0207e532018-08-28 10:28:28 +01003630 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003631 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3632 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 }
3634
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003635 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003637 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003638 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3639 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003640 {
3641 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003644 return( ret );
3645 }
3646 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003647 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003648#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003649 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003650 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003651 {
3652 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3653 return( ret );
3654 }
3655 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003656
3657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3658
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003659 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003660}
3661
3662/*
3663 * Record layer functions
3664 */
3665
3666/*
3667 * Write current record.
3668 *
3669 * Uses:
3670 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3671 * - ssl->out_msglen: length of the record content (excl headers)
3672 * - ssl->out_msg: record content
3673 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003674int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003675{
3676 int ret, done = 0;
3677 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003678 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003679
3680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003683 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003685 {
3686 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003689 return( ret );
3690 }
3691
3692 len = ssl->out_msglen;
3693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3697 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 ret = mbedtls_ssl_hw_record_write( ssl );
3702 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3705 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003706 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003707
3708 if( ret == 0 )
3709 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003712 if( !done )
3713 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003714 unsigned i;
3715 size_t protected_record_size;
3716
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003717 /* Skip writing the record content type to after the encryption,
3718 * as it may change when using the CID extension. */
3719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003721 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003722
Hanno Becker19859472018-08-06 09:40:20 +01003723 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003724 ssl->out_len[0] = (unsigned char)( len >> 8 );
3725 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003726
Paul Bakker48916f92012-09-16 19:57:18 +00003727 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003728 {
Hanno Becker3307b532017-12-27 21:37:21 +00003729 mbedtls_record rec;
3730
3731 rec.buf = ssl->out_iv;
3732 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3733 ( ssl->out_iv - ssl->out_buf );
3734 rec.data_len = ssl->out_msglen;
3735 rec.data_offset = ssl->out_msg - rec.buf;
3736
3737 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3738 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3739 ssl->conf->transport, rec.ver );
3740 rec.type = ssl->out_msgtype;
3741
Hanno Becker505089d2019-05-01 09:45:57 +01003742#if defined(MBEDTLS_SSL_CID)
3743 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003744 rec.cid_len = 0;
Hanno Becker505089d2019-05-01 09:45:57 +01003745#endif /* MBEDTLS_SSL_CID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003746
Hanno Becker611a83b2018-01-03 14:27:32 +00003747 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003748 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003751 return( ret );
3752 }
3753
Hanno Becker3307b532017-12-27 21:37:21 +00003754 if( rec.data_offset != 0 )
3755 {
3756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3757 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3758 }
3759
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003760 /* Update the record content type and CID. */
3761 ssl->out_msgtype = rec.type;
Hanno Becker70e79282019-05-03 14:34:53 +01003762#if defined(MBEDTLS_SSL_CID )
3763 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
3764#endif /* MBEDTLS_SSL_CID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003765 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003766 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3767 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003768 }
3769
Hanno Becker43395762019-05-03 14:46:38 +01003770 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003771
3772#if defined(MBEDTLS_SSL_PROTO_DTLS)
3773 /* In case of DTLS, double-check that we don't exceed
3774 * the remaining space in the datagram. */
3775 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3776 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003777 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003778 if( ret < 0 )
3779 return( ret );
3780
3781 if( protected_record_size > (size_t) ret )
3782 {
3783 /* Should never happen */
3784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3785 }
3786 }
3787#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003788
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003789 /* Now write the potentially updated record content type. */
3790 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003793 "version = [%d:%d], msglen = %d",
3794 ssl->out_hdr[0], ssl->out_hdr[1],
3795 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003798 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003799
3800 ssl->out_left += protected_record_size;
3801 ssl->out_hdr += protected_record_size;
3802 ssl_update_out_pointers( ssl, ssl->transform_out );
3803
Hanno Becker04484622018-08-06 09:49:38 +01003804 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3805 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3806 break;
3807
3808 /* The loop goes to its end iff the counter is wrapping */
3809 if( i == ssl_ep_len( ssl ) )
3810 {
3811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3812 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 }
3815
Hanno Becker67bc7c32018-08-06 11:33:50 +01003816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003817 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3818 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003820 size_t remaining;
3821 ret = ssl_get_remaining_payload_in_datagram( ssl );
3822 if( ret < 0 )
3823 {
3824 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3825 ret );
3826 return( ret );
3827 }
3828
3829 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003830 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003831 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003832 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003833 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003834 else
3835 {
Hanno Becker513815a2018-08-20 11:56:09 +01003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003837 }
3838 }
3839#endif /* MBEDTLS_SSL_PROTO_DTLS */
3840
3841 if( ( flush == SSL_FORCE_FLUSH ) &&
3842 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 return( ret );
3846 }
3847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849
3850 return( 0 );
3851}
3852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003854
3855static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3856{
3857 if( ssl->in_msglen < ssl->in_hslen ||
3858 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3859 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3860 {
3861 return( 1 );
3862 }
3863 return( 0 );
3864}
Hanno Becker44650b72018-08-16 12:51:11 +01003865
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003866static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003867{
3868 return( ( ssl->in_msg[9] << 16 ) |
3869 ( ssl->in_msg[10] << 8 ) |
3870 ssl->in_msg[11] );
3871}
3872
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003873static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003874{
3875 return( ( ssl->in_msg[6] << 16 ) |
3876 ( ssl->in_msg[7] << 8 ) |
3877 ssl->in_msg[8] );
3878}
3879
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003880static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003881{
3882 uint32_t msg_len, frag_off, frag_len;
3883
3884 msg_len = ssl_get_hs_total_len( ssl );
3885 frag_off = ssl_get_hs_frag_off( ssl );
3886 frag_len = ssl_get_hs_frag_len( ssl );
3887
3888 if( frag_off > msg_len )
3889 return( -1 );
3890
3891 if( frag_len > msg_len - frag_off )
3892 return( -1 );
3893
3894 if( frag_len + 12 > ssl->in_msglen )
3895 return( -1 );
3896
3897 return( 0 );
3898}
3899
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003900/*
3901 * Mark bits in bitmask (used for DTLS HS reassembly)
3902 */
3903static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3904{
3905 unsigned int start_bits, end_bits;
3906
3907 start_bits = 8 - ( offset % 8 );
3908 if( start_bits != 8 )
3909 {
3910 size_t first_byte_idx = offset / 8;
3911
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003912 /* Special case */
3913 if( len <= start_bits )
3914 {
3915 for( ; len != 0; len-- )
3916 mask[first_byte_idx] |= 1 << ( start_bits - len );
3917
3918 /* Avoid potential issues with offset or len becoming invalid */
3919 return;
3920 }
3921
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003922 offset += start_bits; /* Now offset % 8 == 0 */
3923 len -= start_bits;
3924
3925 for( ; start_bits != 0; start_bits-- )
3926 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3927 }
3928
3929 end_bits = len % 8;
3930 if( end_bits != 0 )
3931 {
3932 size_t last_byte_idx = ( offset + len ) / 8;
3933
3934 len -= end_bits; /* Now len % 8 == 0 */
3935
3936 for( ; end_bits != 0; end_bits-- )
3937 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3938 }
3939
3940 memset( mask + offset / 8, 0xFF, len / 8 );
3941}
3942
3943/*
3944 * Check that bitmask is full
3945 */
3946static int ssl_bitmask_check( unsigned char *mask, size_t len )
3947{
3948 size_t i;
3949
3950 for( i = 0; i < len / 8; i++ )
3951 if( mask[i] != 0xFF )
3952 return( -1 );
3953
3954 for( i = 0; i < len % 8; i++ )
3955 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3956 return( -1 );
3957
3958 return( 0 );
3959}
3960
Hanno Becker56e205e2018-08-16 09:06:12 +01003961/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003962static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003963 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003964{
Hanno Becker56e205e2018-08-16 09:06:12 +01003965 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003966
Hanno Becker56e205e2018-08-16 09:06:12 +01003967 alloc_len = 12; /* Handshake header */
3968 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003969
Hanno Beckerd07df862018-08-16 09:14:58 +01003970 if( add_bitmap )
3971 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003972
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003973 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003974}
Hanno Becker56e205e2018-08-16 09:06:12 +01003975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003977
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003978static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003979{
3980 return( ( ssl->in_msg[1] << 16 ) |
3981 ( ssl->in_msg[2] << 8 ) |
3982 ssl->in_msg[3] );
3983}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003984
Simon Butcher99000142016-10-13 17:21:01 +01003985int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003990 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003992 }
3993
Hanno Becker12555c62018-08-16 12:47:53 +01003994 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003997 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003998 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004002 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004003 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004004 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004005
Hanno Becker44650b72018-08-16 12:51:11 +01004006 if( ssl_check_hs_header( ssl ) != 0 )
4007 {
4008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4009 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4010 }
4011
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004012 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004013 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4014 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4015 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4016 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004017 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004018 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4019 {
4020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4021 recv_msg_seq,
4022 ssl->handshake->in_msg_seq ) );
4023 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4024 }
4025
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004026 /* Retransmit only on last message from previous flight, to avoid
4027 * too many retransmissions.
4028 * Besides, No sane server ever retransmits HelloVerifyRequest */
4029 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004033 "message_seq = %d, start_of_flight = %d",
4034 recv_msg_seq,
4035 ssl->handshake->in_flight_start_seq ) );
4036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004040 return( ret );
4041 }
4042 }
4043 else
4044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004046 "message_seq = %d, expected = %d",
4047 recv_msg_seq,
4048 ssl->handshake->in_msg_seq ) );
4049 }
4050
Hanno Becker90333da2017-10-10 11:27:13 +01004051 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004052 }
4053 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004054
Hanno Becker6d97ef52018-08-16 13:09:04 +01004055 /* Message reassembly is handled alongside buffering of future
4056 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004057 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004058 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004059 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004062 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004063 }
4064 }
4065 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004067 /* With TLS we don't handle fragmentation (for now) */
4068 if( ssl->in_msglen < ssl->in_hslen )
4069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4071 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004072 }
4073
Simon Butcher99000142016-10-13 17:21:01 +01004074 return( 0 );
4075}
4076
4077void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4078{
Hanno Becker0271f962018-08-16 13:23:47 +01004079 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004080
Hanno Becker0271f962018-08-16 13:23:47 +01004081 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004082 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004083 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004084 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004085
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004086 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004089 ssl->handshake != NULL )
4090 {
Hanno Becker0271f962018-08-16 13:23:47 +01004091 unsigned offset;
4092 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004093
Hanno Becker0271f962018-08-16 13:23:47 +01004094 /* Increment handshake sequence number */
4095 hs->in_msg_seq++;
4096
4097 /*
4098 * Clear up handshake buffering and reassembly structure.
4099 */
4100
4101 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004102 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004103
4104 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004105 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4106 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004107 offset++, hs_buf++ )
4108 {
4109 *hs_buf = *(hs_buf + 1);
4110 }
4111
4112 /* Create a fresh last entry */
4113 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004114 }
4115#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004116}
4117
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004118/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004119 * DTLS anti-replay: RFC 6347 4.1.2.6
4120 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004121 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4122 * Bit n is set iff record number in_window_top - n has been seen.
4123 *
4124 * Usually, in_window_top is the last record number seen and the lsb of
4125 * in_window is set. The only exception is the initial state (record number 0
4126 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4129static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004130{
4131 ssl->in_window_top = 0;
4132 ssl->in_window = 0;
4133}
4134
4135static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4136{
4137 return( ( (uint64_t) buf[0] << 40 ) |
4138 ( (uint64_t) buf[1] << 32 ) |
4139 ( (uint64_t) buf[2] << 24 ) |
4140 ( (uint64_t) buf[3] << 16 ) |
4141 ( (uint64_t) buf[4] << 8 ) |
4142 ( (uint64_t) buf[5] ) );
4143}
4144
4145/*
4146 * Return 0 if sequence number is acceptable, -1 otherwise
4147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004149{
4150 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4151 uint64_t bit;
4152
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004153 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004154 return( 0 );
4155
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004156 if( rec_seqnum > ssl->in_window_top )
4157 return( 0 );
4158
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004159 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004160
4161 if( bit >= 64 )
4162 return( -1 );
4163
4164 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4165 return( -1 );
4166
4167 return( 0 );
4168}
4169
4170/*
4171 * Update replay window on new validated record
4172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004174{
4175 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004177 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004178 return;
4179
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004180 if( rec_seqnum > ssl->in_window_top )
4181 {
4182 /* Update window_top and the contents of the window */
4183 uint64_t shift = rec_seqnum - ssl->in_window_top;
4184
4185 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004186 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004188 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004189 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004190 ssl->in_window |= 1;
4191 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004192
4193 ssl->in_window_top = rec_seqnum;
4194 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004195 else
4196 {
4197 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004198 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004199
4200 if( bit < 64 ) /* Always true, but be extra sure */
4201 ssl->in_window |= (uint64_t) 1 << bit;
4202 }
4203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004205
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004206#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004207/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004208static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4209
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004210/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004211 * Without any SSL context, check if a datagram looks like a ClientHello with
4212 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004213 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004214 *
4215 * - if cookie is valid, return 0
4216 * - if ClientHello looks superficially valid but cookie is not,
4217 * fill obuf and set olen, then
4218 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4219 * - otherwise return a specific error code
4220 */
4221static int ssl_check_dtls_clihlo_cookie(
4222 mbedtls_ssl_cookie_write_t *f_cookie_write,
4223 mbedtls_ssl_cookie_check_t *f_cookie_check,
4224 void *p_cookie,
4225 const unsigned char *cli_id, size_t cli_id_len,
4226 const unsigned char *in, size_t in_len,
4227 unsigned char *obuf, size_t buf_len, size_t *olen )
4228{
4229 size_t sid_len, cookie_len;
4230 unsigned char *p;
4231
4232 if( f_cookie_write == NULL || f_cookie_check == NULL )
4233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4234
4235 /*
4236 * Structure of ClientHello with record and handshake headers,
4237 * and expected values. We don't need to check a lot, more checks will be
4238 * done when actually parsing the ClientHello - skipping those checks
4239 * avoids code duplication and does not make cookie forging any easier.
4240 *
4241 * 0-0 ContentType type; copied, must be handshake
4242 * 1-2 ProtocolVersion version; copied
4243 * 3-4 uint16 epoch; copied, must be 0
4244 * 5-10 uint48 sequence_number; copied
4245 * 11-12 uint16 length; (ignored)
4246 *
4247 * 13-13 HandshakeType msg_type; (ignored)
4248 * 14-16 uint24 length; (ignored)
4249 * 17-18 uint16 message_seq; copied
4250 * 19-21 uint24 fragment_offset; copied, must be 0
4251 * 22-24 uint24 fragment_length; (ignored)
4252 *
4253 * 25-26 ProtocolVersion client_version; (ignored)
4254 * 27-58 Random random; (ignored)
4255 * 59-xx SessionID session_id; 1 byte len + sid_len content
4256 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4257 * ...
4258 *
4259 * Minimum length is 61 bytes.
4260 */
4261 if( in_len < 61 ||
4262 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4263 in[3] != 0 || in[4] != 0 ||
4264 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4265 {
4266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4267 }
4268
4269 sid_len = in[59];
4270 if( sid_len > in_len - 61 )
4271 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4272
4273 cookie_len = in[60 + sid_len];
4274 if( cookie_len > in_len - 60 )
4275 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4276
4277 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4278 cli_id, cli_id_len ) == 0 )
4279 {
4280 /* Valid cookie */
4281 return( 0 );
4282 }
4283
4284 /*
4285 * If we get here, we've got an invalid cookie, let's prepare HVR.
4286 *
4287 * 0-0 ContentType type; copied
4288 * 1-2 ProtocolVersion version; copied
4289 * 3-4 uint16 epoch; copied
4290 * 5-10 uint48 sequence_number; copied
4291 * 11-12 uint16 length; olen - 13
4292 *
4293 * 13-13 HandshakeType msg_type; hello_verify_request
4294 * 14-16 uint24 length; olen - 25
4295 * 17-18 uint16 message_seq; copied
4296 * 19-21 uint24 fragment_offset; copied
4297 * 22-24 uint24 fragment_length; olen - 25
4298 *
4299 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4300 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4301 *
4302 * Minimum length is 28.
4303 */
4304 if( buf_len < 28 )
4305 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4306
4307 /* Copy most fields and adapt others */
4308 memcpy( obuf, in, 25 );
4309 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4310 obuf[25] = 0xfe;
4311 obuf[26] = 0xff;
4312
4313 /* Generate and write actual cookie */
4314 p = obuf + 28;
4315 if( f_cookie_write( p_cookie,
4316 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4317 {
4318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4319 }
4320
4321 *olen = p - obuf;
4322
4323 /* Go back and fill length fields */
4324 obuf[27] = (unsigned char)( *olen - 28 );
4325
4326 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4327 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4328 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4329
4330 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4331 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4332
4333 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4334}
4335
4336/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004337 * Handle possible client reconnect with the same UDP quadruplet
4338 * (RFC 6347 Section 4.2.8).
4339 *
4340 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4341 * that looks like a ClientHello.
4342 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004343 * - if the input looks like a ClientHello without cookies,
4344 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004345 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004346 * - if the input looks like a ClientHello with a valid cookie,
4347 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004348 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004349 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004350 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004351 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004352 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4353 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004354 */
4355static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4356{
4357 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004358 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004359
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004360 ret = ssl_check_dtls_clihlo_cookie(
4361 ssl->conf->f_cookie_write,
4362 ssl->conf->f_cookie_check,
4363 ssl->conf->p_cookie,
4364 ssl->cli_id, ssl->cli_id_len,
4365 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004366 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004367
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004368 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4369
4370 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004371 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004372 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373 * If the error is permanent we'll catch it later,
4374 * if it's not, then hopefully it'll work next time. */
4375 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4376
4377 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004378 }
4379
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004380 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004381 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004382 /* Got a valid cookie, partially reset context */
4383 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4384 {
4385 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4386 return( ret );
4387 }
4388
4389 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004390 }
4391
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004392 return( ret );
4393}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004394#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004395
Hanno Becker46483f12019-05-03 13:25:54 +01004396static int ssl_check_record_type( uint8_t record_type )
4397{
4398 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4399 record_type != MBEDTLS_SSL_MSG_ALERT &&
4400 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4401 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4402 {
4403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4404 }
4405
4406 return( 0 );
4407}
4408
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004409/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004410 * ContentType type;
4411 * ProtocolVersion version;
4412 * uint16 epoch; // DTLS only
4413 * uint48 sequence_number; // DTLS only
4414 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004415 *
4416 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004417 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004418 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4419 *
4420 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004421 * 1. proceed with the record if this function returns 0
4422 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4423 * 3. return CLIENT_RECONNECT if this function return that value
4424 * 4. drop the whole datagram if this function returns anything else.
4425 * Point 2 is needed when the peer is resending, and we have already received
4426 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004429{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004430 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004431 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004432
Hanno Becker8b09b732019-05-08 12:03:28 +01004433 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004434
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004436 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004437
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004438 /* Check record type */
Hanno Becker8b09b732019-05-08 12:03:28 +01004439#if defined(MBEDTLS_SSL_CID)
4440 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4441 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4442 ssl->conf->cid_len != 0 )
4443 {
4444 /* Shift pointers to account for record header including CID
4445 * struct {
4446 * ContentType special_type = tls12_cid;
4447 * ProtocolVersion version;
4448 * uint16 epoch;
4449 * uint48 sequence_number;
4450 * opaque cid[cid_length]; // New field
4451 * uint16 length;
4452 * opaque enc_content[DTLSCiphertext.length];
4453 * } DTLSCiphertext;
4454 */
4455
4456 /* So far, we only support static CID lengths
4457 * fixed in the configuration. */
4458 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4459 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4460 }
4461 else
4462#endif /* MBEDTLS_SSL_CID */
Hanno Becker46483f12019-05-03 13:25:54 +01004463 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004466
4467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004468 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4469 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004470 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4471#endif /* MBEDTLS_SSL_PROTO_DTLS */
4472 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4473 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004476 }
4477
4478 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004479 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4482 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004483 }
4484
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004485 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4488 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 }
4490
Hanno Becker8b09b732019-05-08 12:03:28 +01004491 /* Now that the total length of the record header is known, ensure
4492 * that the current datagram is large enough to hold it.
4493 * This would fail, for example, if we received a datagram of
4494 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4495 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4496 if( ret != 0 )
4497 {
4498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4499 return( ret );
4500 }
4501 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4502
4503 /* Parse and validate record length
4504 * This must happen after the CID parsing because
4505 * its position in the record header depends on
4506 * the presence of a CID. */
4507
4508 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004509 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004510 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4513 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004514 }
4515
Hanno Becker8b09b732019-05-08 12:03:28 +01004516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4517 "version = [%d:%d], msglen = %d",
4518 ssl->in_msgtype,
4519 major_ver, minor_ver, ssl->in_msglen ) );
4520
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004521 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004522 * DTLS-related tests.
4523 * Check epoch before checking length constraint because
4524 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4525 * message gets duplicated before the corresponding Finished message,
4526 * the second ChangeCipherSpec should be discarded because it belongs
4527 * to an old epoch, but not because its length is shorter than
4528 * the minimum record length for packets using the new record transform.
4529 * Note that these two kinds of failures are handled differently,
4530 * as an unexpected record is silently skipped but an invalid
4531 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004532 */
4533#if defined(MBEDTLS_SSL_PROTO_DTLS)
4534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4535 {
4536 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4537
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004538 /* Check epoch (and sequence number) with DTLS */
4539 if( rec_epoch != ssl->in_epoch )
4540 {
4541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4542 "expected %d, received %d",
4543 ssl->in_epoch, rec_epoch ) );
4544
4545#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4546 /*
4547 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4548 * access the first byte of record content (handshake type), as we
4549 * have an active transform (possibly iv_len != 0), so use the
4550 * fact that the record header len is 13 instead.
4551 */
4552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4553 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4554 rec_epoch == 0 &&
4555 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4556 ssl->in_left > 13 &&
4557 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4558 {
4559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4560 "from the same port" ) );
4561 return( ssl_handle_possible_reconnect( ssl ) );
4562 }
4563 else
4564#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004565 {
4566 /* Consider buffering the record. */
4567 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4568 {
4569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4570 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4571 }
4572
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004573 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004574 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004575 }
4576
4577#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4578 /* Replay detection only works for the current epoch */
4579 if( rec_epoch == ssl->in_epoch &&
4580 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4581 {
4582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4583 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4584 }
4585#endif
4586 }
4587#endif /* MBEDTLS_SSL_PROTO_DTLS */
4588
Hanno Becker52c6dc62017-05-26 16:07:36 +01004589
4590 /* Check length against bounds of the current transform and version */
4591 if( ssl->transform_in == NULL )
4592 {
4593 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004594 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004595 {
4596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4597 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4598 }
4599 }
4600 else
4601 {
4602 if( ssl->in_msglen < ssl->transform_in->minlen )
4603 {
4604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4605 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4606 }
4607
4608#if defined(MBEDTLS_SSL_PROTO_SSL3)
4609 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004610 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004611 {
4612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4613 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4614 }
4615#endif
4616#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4617 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4618 /*
4619 * TLS encrypted messages can have up to 256 bytes of padding
4620 */
4621 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4622 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004623 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004624 {
4625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4627 }
4628#endif
4629 }
4630
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004631 return( 0 );
4632}
Paul Bakker5121ce52009-01-03 21:22:43 +00004633
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004634/*
4635 * If applicable, decrypt (and decompress) record content
4636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004638{
4639 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004642 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4645 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 ret = mbedtls_ssl_hw_record_read( ssl );
4650 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4653 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004654 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004655
4656 if( ret == 0 )
4657 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004660 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004662 mbedtls_record rec;
4663
4664 rec.buf = ssl->in_iv;
4665 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4666 - ( ssl->in_iv - ssl->in_buf );
4667 rec.data_len = ssl->in_msglen;
4668 rec.data_offset = 0;
Hanno Becker70e79282019-05-03 14:34:53 +01004669#if defined(MBEDTLS_SSL_CID )
4670 rec.cid_len = ssl->in_len - ssl->in_cid;
4671 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
4672#endif /* MBEDTLS_SSL_CID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004673
4674 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4675 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4676 ssl->conf->transport, rec.ver );
4677 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004678 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4679 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 return( ret );
4683 }
4684
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004685 if( ssl->in_msgtype != rec.type )
4686 {
4687 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4688 ssl->in_msgtype, rec.type ) );
4689 }
4690
4691 /* The record content type may change during decryption,
4692 * so re-read it. */
4693 ssl->in_msgtype = rec.type;
4694 /* Also update the input buffer, because unfortunately
4695 * the server-side ssl_parse_client_hello() reparses the
4696 * record header when receiving a ClientHello initiating
4697 * a renegotiation. */
4698 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004699 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004700 ssl->in_msglen = rec.data_len;
4701 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4702 ssl->in_len[1] = (unsigned char)( rec.data_len );
4703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 ssl->in_msg, ssl->in_msglen );
4706
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004707#if defined(MBEDTLS_SSL_CID)
4708 /* We have already checked the record content type
4709 * in ssl_parse_record_header(), failing or silently
4710 * dropping the record in the case of an unknown type.
4711 *
4712 * Since with the use of CIDs, the record content type
4713 * might change during decryption, re-check the record
4714 * content type, but treat a failure as fatal this time. */
4715 if( ssl_check_record_type( ssl->in_msgtype ) )
4716 {
4717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4718 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4719 }
4720#endif /* MBEDTLS_SSL_CID */
4721
Angus Grattond8213d02016-05-25 20:56:48 +10004722 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4725 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004726 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004727 else if( ssl->in_msglen == 0 )
4728 {
4729#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4730 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4731 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4732 {
4733 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4735 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4736 }
4737#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4738
4739 ssl->nb_zero++;
4740
4741 /*
4742 * Three or more empty messages may be a DoS attack
4743 * (excessive CPU consumption).
4744 */
4745 if( ssl->nb_zero > 3 )
4746 {
4747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004748 "messages, possible DoS attack" ) );
4749 /* Treat the records as if they were not properly authenticated,
4750 * thereby failing the connection if we see more than allowed
4751 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004752 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4753 }
4754 }
4755 else
4756 ssl->nb_zero = 0;
4757
4758#if defined(MBEDTLS_SSL_PROTO_DTLS)
4759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4760 {
4761 ; /* in_ctr read from peer, not maintained internally */
4762 }
4763 else
4764#endif
4765 {
4766 unsigned i;
4767 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4768 if( ++ssl->in_ctr[i - 1] != 0 )
4769 break;
4770
4771 /* The loop goes to its end iff the counter is wrapping */
4772 if( i == ssl_ep_len( ssl ) )
4773 {
4774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4775 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4776 }
4777 }
4778
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 }
4780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004782 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004784 {
4785 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004788 return( ret );
4789 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004797 }
4798#endif
4799
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004800 return( 0 );
4801}
4802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004804
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004805/*
4806 * Read a record.
4807 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004808 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4809 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4810 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004811 */
Hanno Becker1097b342018-08-15 14:09:41 +01004812
4813/* Helper functions for mbedtls_ssl_read_record(). */
4814static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004815static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4816static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004817
Hanno Becker327c93b2018-08-15 13:56:18 +01004818int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004819 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004820{
4821 int ret;
4822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004824
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004825 if( ssl->keep_current_message == 0 )
4826 {
4827 do {
Simon Butcher99000142016-10-13 17:21:01 +01004828
Hanno Becker26994592018-08-15 14:14:59 +01004829 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004830 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004831 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004832
Hanno Beckere74d5562018-08-15 14:26:08 +01004833 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004834 {
Hanno Becker40f50842018-08-15 14:48:01 +01004835#if defined(MBEDTLS_SSL_PROTO_DTLS)
4836 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004837
Hanno Becker40f50842018-08-15 14:48:01 +01004838 /* We only check for buffered messages if the
4839 * current datagram is fully consumed. */
4840 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004841 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004842 {
Hanno Becker40f50842018-08-15 14:48:01 +01004843 if( ssl_load_buffered_message( ssl ) == 0 )
4844 have_buffered = 1;
4845 }
4846
4847 if( have_buffered == 0 )
4848#endif /* MBEDTLS_SSL_PROTO_DTLS */
4849 {
4850 ret = ssl_get_next_record( ssl );
4851 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4852 continue;
4853
4854 if( ret != 0 )
4855 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004856 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004857 return( ret );
4858 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004859 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004860 }
4861
4862 ret = mbedtls_ssl_handle_message_type( ssl );
4863
Hanno Becker40f50842018-08-15 14:48:01 +01004864#if defined(MBEDTLS_SSL_PROTO_DTLS)
4865 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4866 {
4867 /* Buffer future message */
4868 ret = ssl_buffer_message( ssl );
4869 if( ret != 0 )
4870 return( ret );
4871
4872 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4873 }
4874#endif /* MBEDTLS_SSL_PROTO_DTLS */
4875
Hanno Becker90333da2017-10-10 11:27:13 +01004876 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4877 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004878
4879 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004880 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004881 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004882 return( ret );
4883 }
4884
Hanno Becker327c93b2018-08-15 13:56:18 +01004885 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004886 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004887 {
4888 mbedtls_ssl_update_handshake_status( ssl );
4889 }
Simon Butcher99000142016-10-13 17:21:01 +01004890 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004891 else
Simon Butcher99000142016-10-13 17:21:01 +01004892 {
Hanno Becker02f59072018-08-15 14:00:24 +01004893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004894 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004895 }
4896
4897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4898
4899 return( 0 );
4900}
4901
Hanno Becker40f50842018-08-15 14:48:01 +01004902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004903static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004904{
Hanno Becker40f50842018-08-15 14:48:01 +01004905 if( ssl->in_left > ssl->next_record_offset )
4906 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004907
Hanno Becker40f50842018-08-15 14:48:01 +01004908 return( 0 );
4909}
4910
4911static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4912{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004913 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004914 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004915 int ret = 0;
4916
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004917 if( hs == NULL )
4918 return( -1 );
4919
Hanno Beckere00ae372018-08-20 09:39:42 +01004920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4921
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004922 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4923 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4924 {
4925 /* Check if we have seen a ChangeCipherSpec before.
4926 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004927 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004928 {
4929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4930 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004931 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004932 }
4933
Hanno Becker39b8bc92018-08-28 17:17:13 +01004934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004935 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4936 ssl->in_msglen = 1;
4937 ssl->in_msg[0] = 1;
4938
4939 /* As long as they are equal, the exact value doesn't matter. */
4940 ssl->in_left = 0;
4941 ssl->next_record_offset = 0;
4942
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004943 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004944 goto exit;
4945 }
Hanno Becker37f95322018-08-16 13:55:32 +01004946
Hanno Beckerb8f50142018-08-28 10:01:34 +01004947#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004948 /* Debug only */
4949 {
4950 unsigned offset;
4951 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4952 {
4953 hs_buf = &hs->buffering.hs[offset];
4954 if( hs_buf->is_valid == 1 )
4955 {
4956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4957 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004958 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004959 }
4960 }
4961 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004962#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004963
4964 /* Check if we have buffered and/or fully reassembled the
4965 * next handshake message. */
4966 hs_buf = &hs->buffering.hs[0];
4967 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4968 {
4969 /* Synthesize a record containing the buffered HS message. */
4970 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4971 ( hs_buf->data[2] << 8 ) |
4972 hs_buf->data[3];
4973
4974 /* Double-check that we haven't accidentally buffered
4975 * a message that doesn't fit into the input buffer. */
4976 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4977 {
4978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4980 }
4981
4982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4983 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4984 hs_buf->data, msg_len + 12 );
4985
4986 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4987 ssl->in_hslen = msg_len + 12;
4988 ssl->in_msglen = msg_len + 12;
4989 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4990
4991 ret = 0;
4992 goto exit;
4993 }
4994 else
4995 {
4996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4997 hs->in_msg_seq ) );
4998 }
4999
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005000 ret = -1;
5001
5002exit:
5003
5004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5005 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005006}
5007
Hanno Beckera02b0b42018-08-21 17:20:27 +01005008static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5009 size_t desired )
5010{
5011 int offset;
5012 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5014 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005015
Hanno Becker01315ea2018-08-21 17:22:17 +01005016 /* Get rid of future records epoch first, if such exist. */
5017 ssl_free_buffered_record( ssl );
5018
5019 /* Check if we have enough space available now. */
5020 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5021 hs->buffering.total_bytes_buffered ) )
5022 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005024 return( 0 );
5025 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005026
Hanno Becker4f432ad2018-08-28 10:02:32 +01005027 /* We don't have enough space to buffer the next expected handshake
5028 * message. Remove buffers used for future messages to gain space,
5029 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005030 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5031 offset >= 0; offset-- )
5032 {
5033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5034 offset ) );
5035
Hanno Beckerb309b922018-08-23 13:18:05 +01005036 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005037
5038 /* Check if we have enough space available now. */
5039 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5040 hs->buffering.total_bytes_buffered ) )
5041 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005043 return( 0 );
5044 }
5045 }
5046
5047 return( -1 );
5048}
5049
Hanno Becker40f50842018-08-15 14:48:01 +01005050static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5051{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005052 int ret = 0;
5053 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5054
5055 if( hs == NULL )
5056 return( 0 );
5057
5058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5059
5060 switch( ssl->in_msgtype )
5061 {
5062 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005064
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005065 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005066 break;
5067
5068 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005069 {
5070 unsigned recv_msg_seq_offset;
5071 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5072 mbedtls_ssl_hs_buffer *hs_buf;
5073 size_t msg_len = ssl->in_hslen - 12;
5074
5075 /* We should never receive an old handshake
5076 * message - double-check nonetheless. */
5077 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5078 {
5079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5081 }
5082
5083 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5084 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5085 {
5086 /* Silently ignore -- message too far in the future */
5087 MBEDTLS_SSL_DEBUG_MSG( 2,
5088 ( "Ignore future HS message with sequence number %u, "
5089 "buffering window %u - %u",
5090 recv_msg_seq, ssl->handshake->in_msg_seq,
5091 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5092
5093 goto exit;
5094 }
5095
5096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5097 recv_msg_seq, recv_msg_seq_offset ) );
5098
5099 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5100
5101 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005102 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005103 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005104 size_t reassembly_buf_sz;
5105
Hanno Becker37f95322018-08-16 13:55:32 +01005106 hs_buf->is_fragmented =
5107 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5108
5109 /* We copy the message back into the input buffer
5110 * after reassembly, so check that it's not too large.
5111 * This is an implementation-specific limitation
5112 * and not one from the standard, hence it is not
5113 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005114 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005115 {
5116 /* Ignore message */
5117 goto exit;
5118 }
5119
Hanno Beckere0b150f2018-08-21 15:51:03 +01005120 /* Check if we have enough space to buffer the message. */
5121 if( hs->buffering.total_bytes_buffered >
5122 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5123 {
5124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5125 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5126 }
5127
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005128 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5129 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005130
5131 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5132 hs->buffering.total_bytes_buffered ) )
5133 {
5134 if( recv_msg_seq_offset > 0 )
5135 {
5136 /* If we can't buffer a future message because
5137 * of space limitations -- ignore. */
5138 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",
5139 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5140 (unsigned) hs->buffering.total_bytes_buffered ) );
5141 goto exit;
5142 }
Hanno Beckere1801392018-08-21 16:51:05 +01005143 else
5144 {
5145 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",
5146 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5147 (unsigned) hs->buffering.total_bytes_buffered ) );
5148 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005149
Hanno Beckera02b0b42018-08-21 17:20:27 +01005150 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005151 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005152 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",
5153 (unsigned) msg_len,
5154 (unsigned) reassembly_buf_sz,
5155 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005156 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005157 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5158 goto exit;
5159 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005160 }
5161
5162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5163 msg_len ) );
5164
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005165 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5166 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005167 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005168 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005169 goto exit;
5170 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005171 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005172
5173 /* Prepare final header: copy msg_type, length and message_seq,
5174 * then add standardised fragment_offset and fragment_length */
5175 memcpy( hs_buf->data, ssl->in_msg, 6 );
5176 memset( hs_buf->data + 6, 0, 3 );
5177 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5178
5179 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005180
5181 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005182 }
5183 else
5184 {
5185 /* Make sure msg_type and length are consistent */
5186 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5187 {
5188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5189 /* Ignore */
5190 goto exit;
5191 }
5192 }
5193
Hanno Becker4422bbb2018-08-20 09:40:19 +01005194 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005195 {
5196 size_t frag_len, frag_off;
5197 unsigned char * const msg = hs_buf->data + 12;
5198
5199 /*
5200 * Check and copy current fragment
5201 */
5202
5203 /* Validation of header fields already done in
5204 * mbedtls_ssl_prepare_handshake_record(). */
5205 frag_off = ssl_get_hs_frag_off( ssl );
5206 frag_len = ssl_get_hs_frag_len( ssl );
5207
5208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5209 frag_off, frag_len ) );
5210 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5211
5212 if( hs_buf->is_fragmented )
5213 {
5214 unsigned char * const bitmask = msg + msg_len;
5215 ssl_bitmask_set( bitmask, frag_off, frag_len );
5216 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5217 msg_len ) == 0 );
5218 }
5219 else
5220 {
5221 hs_buf->is_complete = 1;
5222 }
5223
5224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5225 hs_buf->is_complete ? "" : "not yet " ) );
5226 }
5227
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005228 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005229 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005230
5231 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005232 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005233 break;
5234 }
5235
5236exit:
5237
5238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5239 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005240}
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
Hanno Becker1097b342018-08-15 14:09:41 +01005243static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005244{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005245 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005246 * Consume last content-layer message and potentially
5247 * update in_msglen which keeps track of the contents'
5248 * consumption state.
5249 *
5250 * (1) Handshake messages:
5251 * Remove last handshake message, move content
5252 * and adapt in_msglen.
5253 *
5254 * (2) Alert messages:
5255 * Consume whole record content, in_msglen = 0.
5256 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005257 * (3) Change cipher spec:
5258 * Consume whole record content, in_msglen = 0.
5259 *
5260 * (4) Application data:
5261 * Don't do anything - the record layer provides
5262 * the application data as a stream transport
5263 * and consumes through mbedtls_ssl_read only.
5264 *
5265 */
5266
5267 /* Case (1): Handshake messages */
5268 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005269 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005270 /* Hard assertion to be sure that no application data
5271 * is in flight, as corrupting ssl->in_msglen during
5272 * ssl->in_offt != NULL is fatal. */
5273 if( ssl->in_offt != NULL )
5274 {
5275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5277 }
5278
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005279 /*
5280 * Get next Handshake message in the current record
5281 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005282
Hanno Becker4a810fb2017-05-24 16:27:30 +01005283 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005284 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005285 * current handshake content: If DTLS handshake
5286 * fragmentation is used, that's the fragment
5287 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005288 * size here is faulty and should be changed at
5289 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005290 * (2) While it doesn't seem to cause problems, one
5291 * has to be very careful not to assume that in_hslen
5292 * is always <= in_msglen in a sensible communication.
5293 * Again, it's wrong for DTLS handshake fragmentation.
5294 * The following check is therefore mandatory, and
5295 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005296 * Additionally, ssl->in_hslen might be arbitrarily out of
5297 * bounds after handling a DTLS message with an unexpected
5298 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005299 */
5300 if( ssl->in_hslen < ssl->in_msglen )
5301 {
5302 ssl->in_msglen -= ssl->in_hslen;
5303 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5304 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005305
Hanno Becker4a810fb2017-05-24 16:27:30 +01005306 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5307 ssl->in_msg, ssl->in_msglen );
5308 }
5309 else
5310 {
5311 ssl->in_msglen = 0;
5312 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005313
Hanno Becker4a810fb2017-05-24 16:27:30 +01005314 ssl->in_hslen = 0;
5315 }
5316 /* Case (4): Application data */
5317 else if( ssl->in_offt != NULL )
5318 {
5319 return( 0 );
5320 }
5321 /* Everything else (CCS & Alerts) */
5322 else
5323 {
5324 ssl->in_msglen = 0;
5325 }
5326
Hanno Becker1097b342018-08-15 14:09:41 +01005327 return( 0 );
5328}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005329
Hanno Beckere74d5562018-08-15 14:26:08 +01005330static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5331{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005332 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005333 return( 1 );
5334
5335 return( 0 );
5336}
5337
Hanno Becker5f066e72018-08-16 14:56:31 +01005338#if defined(MBEDTLS_SSL_PROTO_DTLS)
5339
5340static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5341{
5342 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5343 if( hs == NULL )
5344 return;
5345
Hanno Becker01315ea2018-08-21 17:22:17 +01005346 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005347 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005348 hs->buffering.total_bytes_buffered -=
5349 hs->buffering.future_record.len;
5350
5351 mbedtls_free( hs->buffering.future_record.data );
5352 hs->buffering.future_record.data = NULL;
5353 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005354}
5355
5356static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5357{
5358 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5359 unsigned char * rec;
5360 size_t rec_len;
5361 unsigned rec_epoch;
5362
5363 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5364 return( 0 );
5365
5366 if( hs == NULL )
5367 return( 0 );
5368
Hanno Becker5f066e72018-08-16 14:56:31 +01005369 rec = hs->buffering.future_record.data;
5370 rec_len = hs->buffering.future_record.len;
5371 rec_epoch = hs->buffering.future_record.epoch;
5372
5373 if( rec == NULL )
5374 return( 0 );
5375
Hanno Becker4cb782d2018-08-20 11:19:05 +01005376 /* Only consider loading future records if the
5377 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005378 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005379 return( 0 );
5380
Hanno Becker5f066e72018-08-16 14:56:31 +01005381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5382
5383 if( rec_epoch != ssl->in_epoch )
5384 {
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5386 goto exit;
5387 }
5388
5389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5390
5391 /* Double-check that the record is not too large */
5392 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5393 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5394 {
5395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5397 }
5398
5399 memcpy( ssl->in_hdr, rec, rec_len );
5400 ssl->in_left = rec_len;
5401 ssl->next_record_offset = 0;
5402
5403 ssl_free_buffered_record( ssl );
5404
5405exit:
5406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5407 return( 0 );
5408}
5409
5410static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5411{
5412 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5413 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005414 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005415
5416 /* Don't buffer future records outside handshakes. */
5417 if( hs == NULL )
5418 return( 0 );
5419
5420 /* Only buffer handshake records (we are only interested
5421 * in Finished messages). */
5422 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5423 return( 0 );
5424
5425 /* Don't buffer more than one future epoch record. */
5426 if( hs->buffering.future_record.data != NULL )
5427 return( 0 );
5428
Hanno Becker01315ea2018-08-21 17:22:17 +01005429 /* Don't buffer record if there's not enough buffering space remaining. */
5430 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5431 hs->buffering.total_bytes_buffered ) )
5432 {
5433 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",
5434 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5435 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005436 return( 0 );
5437 }
5438
Hanno Becker5f066e72018-08-16 14:56:31 +01005439 /* Buffer record */
5440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5441 ssl->in_epoch + 1 ) );
5442 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5443 rec_hdr_len + ssl->in_msglen );
5444
5445 /* ssl_parse_record_header() only considers records
5446 * of the next epoch as candidates for buffering. */
5447 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005448 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005449
5450 hs->buffering.future_record.data =
5451 mbedtls_calloc( 1, hs->buffering.future_record.len );
5452 if( hs->buffering.future_record.data == NULL )
5453 {
5454 /* If we run out of RAM trying to buffer a
5455 * record from the next epoch, just ignore. */
5456 return( 0 );
5457 }
5458
Hanno Becker01315ea2018-08-21 17:22:17 +01005459 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005460
Hanno Becker01315ea2018-08-21 17:22:17 +01005461 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005462 return( 0 );
5463}
5464
5465#endif /* MBEDTLS_SSL_PROTO_DTLS */
5466
Hanno Beckere74d5562018-08-15 14:26:08 +01005467static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005468{
5469 int ret;
5470
Hanno Becker5f066e72018-08-16 14:56:31 +01005471#if defined(MBEDTLS_SSL_PROTO_DTLS)
5472 /* We might have buffered a future record; if so,
5473 * and if the epoch matches now, load it.
5474 * On success, this call will set ssl->in_left to
5475 * the length of the buffered record, so that
5476 * the calls to ssl_fetch_input() below will
5477 * essentially be no-ops. */
5478 ret = ssl_load_buffered_record( ssl );
5479 if( ret != 0 )
5480 return( ret );
5481#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005482
Hanno Becker8b09b732019-05-08 12:03:28 +01005483 /* Reset in pointers to default state for TLS/DTLS records,
5484 * assuming no CID and no offset between record content and
5485 * record plaintext. */
5486 ssl_update_in_pointers( ssl );
5487
5488 /* Ensure that we have enough space available for the default form
5489 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5490 * with no space for CIDs counted in). */
5491 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5492 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005495 return( ret );
5496 }
5497
5498 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5502 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005503 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005504 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5505 {
5506 ret = ssl_buffer_future_record( ssl );
5507 if( ret != 0 )
5508 return( ret );
5509
5510 /* Fall through to handling of unexpected records */
5511 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5512 }
5513
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005514 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5515 {
5516 /* Skip unexpected record (but not whole datagram) */
5517 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005518 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005519
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5521 "(header)" ) );
5522 }
5523 else
5524 {
5525 /* Skip invalid record and the rest of the datagram */
5526 ssl->next_record_offset = 0;
5527 ssl->in_left = 0;
5528
5529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5530 "(header)" ) );
5531 }
5532
5533 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005534 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005535 }
5536#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005537 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005538 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005539
5540 /*
5541 * Read and optionally decrypt the message contents
5542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005544 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005547 return( ret );
5548 }
5549
5550 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005553 {
Hanno Becker43395762019-05-03 14:46:38 +01005554 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005555 if( ssl->next_record_offset < ssl->in_left )
5556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5558 }
5559 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005560 else
5561#endif
5562 ssl->in_left = 0;
5563
5564 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005568 {
5569 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005570 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005571 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005572 /* Except when waiting for Finished as a bad mac here
5573 * probably means something went wrong in the handshake
5574 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5575 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5576 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5577 {
5578#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5579 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5580 {
5581 mbedtls_ssl_send_alert_message( ssl,
5582 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5583 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5584 }
5585#endif
5586 return( ret );
5587 }
5588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005590 if( ssl->conf->badmac_limit != 0 &&
5591 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5594 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005595 }
5596#endif
5597
Hanno Becker4a810fb2017-05-24 16:27:30 +01005598 /* As above, invalid records cause
5599 * dismissal of the whole datagram. */
5600
5601 ssl->next_record_offset = 0;
5602 ssl->in_left = 0;
5603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005605 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005606 }
5607
5608 return( ret );
5609 }
5610 else
5611#endif
5612 {
5613 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5615 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 mbedtls_ssl_send_alert_message( ssl,
5618 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5619 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005620 }
5621#endif
5622 return( ret );
5623 }
5624 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005625
Simon Butcher99000142016-10-13 17:21:01 +01005626 return( 0 );
5627}
5628
5629int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5630{
5631 int ret;
5632
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005633 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005634 * Handle particular types of records
5635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005637 {
Simon Butcher99000142016-10-13 17:21:01 +01005638 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5639 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005640 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 }
5643
Hanno Beckere678eaa2018-08-21 14:57:46 +01005644 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005645 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005646 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005647 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5649 ssl->in_msglen ) );
5650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005651 }
5652
Hanno Beckere678eaa2018-08-21 14:57:46 +01005653 if( ssl->in_msg[0] != 1 )
5654 {
5655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5656 ssl->in_msg[0] ) );
5657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5658 }
5659
5660#if defined(MBEDTLS_SSL_PROTO_DTLS)
5661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5662 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5663 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5664 {
5665 if( ssl->handshake == NULL )
5666 {
5667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5668 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5669 }
5670
5671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5672 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5673 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005674#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005675 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005679 if( ssl->in_msglen != 2 )
5680 {
5681 /* Note: Standard allows for more than one 2 byte alert
5682 to be packed in a single message, but Mbed TLS doesn't
5683 currently support this. */
5684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5685 ssl->in_msglen ) );
5686 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5687 }
5688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 ssl->in_msg[0], ssl->in_msg[1] ) );
5691
5692 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005693 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005698 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 }
5701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5703 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5706 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005708
5709#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5710 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5711 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5712 {
Hanno Becker90333da2017-10-10 11:27:13 +01005713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005714 /* Will be handled when trying to parse ServerHello */
5715 return( 0 );
5716 }
5717#endif
5718
5719#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5720 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5721 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5722 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5723 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5724 {
5725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5726 /* Will be handled in mbedtls_ssl_parse_certificate() */
5727 return( 0 );
5728 }
5729#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5730
5731 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005732 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 }
5734
Hanno Beckerc76c6192017-06-06 10:03:17 +01005735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker74dd3a72019-05-03 16:54:26 +01005736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005737 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005738 /* Drop unexpected ApplicationData records,
5739 * except at the beginning of renegotiations */
5740 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5741 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5742#if defined(MBEDTLS_SSL_RENEGOTIATION)
5743 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5744 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005745#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005746 )
5747 {
5748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5749 return( MBEDTLS_ERR_SSL_NON_FATAL );
5750 }
5751
5752 if( ssl->handshake != NULL &&
5753 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5754 {
5755 ssl_handshake_wrapup_free_hs_transform( ssl );
5756 }
5757 }
5758#endif /* MBEDTLS_SSL_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005759
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 return( 0 );
5761}
5762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005764{
5765 int ret;
5766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5769 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005770 {
5771 return( ret );
5772 }
5773
5774 return( 0 );
5775}
5776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005778 unsigned char level,
5779 unsigned char message )
5780{
5781 int ret;
5782
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005783 if( ssl == NULL || ssl->conf == NULL )
5784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005790 ssl->out_msglen = 2;
5791 ssl->out_msg[0] = level;
5792 ssl->out_msg[1] = message;
5793
Hanno Becker67bc7c32018-08-06 11:33:50 +01005794 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005797 return( ret );
5798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005800
5801 return( 0 );
5802}
5803
Paul Bakker5121ce52009-01-03 21:22:43 +00005804/*
5805 * Handshake functions
5806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5808 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5809 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5810 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5811 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5812 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5813 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005814/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005816{
Hanno Becker8759e162017-12-27 21:34:08 +00005817 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5822 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005823 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5824 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005827 ssl->state++;
5828 return( 0 );
5829 }
5830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005833}
5834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005836{
Hanno Becker8759e162017-12-27 21:34:08 +00005837 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5842 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005843 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5844 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005847 ssl->state++;
5848 return( 0 );
5849 }
5850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005853}
Gilles Peskinef9828522017-05-03 12:28:43 +02005854
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005855#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005856/* Some certificate support -> implement write and parse */
5857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005861 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005863 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5868 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005869 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5870 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005873 ssl->state++;
5874 return( 0 );
5875 }
5876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005878 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 {
5880 if( ssl->client_auth == 0 )
5881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 ssl->state++;
5884 return( 0 );
5885 }
5886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005888 /*
5889 * If using SSLv3 and got no cert, send an Alert message
5890 * (otherwise an empty Certificate message will be sent).
5891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5893 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005894 {
5895 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5897 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5898 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005901 goto write_msg;
5902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005904 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905#endif /* MBEDTLS_SSL_CLI_C */
5906#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005907 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5912 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 }
5914 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005915#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005918
5919 /*
5920 * 0 . 0 handshake type
5921 * 1 . 3 handshake length
5922 * 4 . 6 length of all certs
5923 * 7 . 9 length of cert. 1
5924 * 10 . n-1 peer certificate
5925 * n . n+2 length of cert. 2
5926 * n+3 . ... upper level cert, etc.
5927 */
5928 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
Paul Bakker29087132010-03-21 21:03:34 +00005931 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005932 {
5933 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005934 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005937 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 }
5940
5941 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5942 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5943 ssl->out_msg[i + 2] = (unsigned char)( n );
5944
5945 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5946 i += n; crt = crt->next;
5947 }
5948
5949 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5950 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5951 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5952
5953 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5955 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005956
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005957#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005958write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005959#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005960
5961 ssl->state++;
5962
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005963 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005964 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005966 return( ret );
5967 }
5968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005970
Paul Bakkered27a042013-04-18 22:46:23 +02005971 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005972}
5973
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005974/*
5975 * Once the certificate message is read, parse it into a cert chain and
5976 * perform basic checks, but leave actual verification to the caller
5977 */
5978static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005979{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005980 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005981 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005982 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if defined(MBEDTLS_SSL_SRV_C)
5985#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005986 /*
5987 * Check if the client sent an empty certificate
5988 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005991 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005992 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5994 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5995 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005998
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005999 /* The client was asked for a certificate but didn't send
6000 one. The client should know what's going on, so we
6001 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006002 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006003 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006004 }
6005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6009 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006010 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6014 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6015 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6016 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006019
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006020 /* The client was asked for a certificate but didn't send
6021 one. The client should know what's going on, so we
6022 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01006023 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006024 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006025 }
6026 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6028 MBEDTLS_SSL_PROTO_TLS1_2 */
6029#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006034 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6035 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 }
6038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6040 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006043 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6044 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 }
6047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006049
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006053 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006054
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006055 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006059 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6060 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 }
6063
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006064 /* In case we tried to reuse a session but it failed */
6065 if( ssl->session_negotiate->peer_cert != NULL )
6066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
6068 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02006069 }
6070
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006071 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006076 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6077 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006078 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 }
6080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006082
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006083 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00006084
6085 while( i < ssl->in_hslen )
6086 {
Philippe Antoine747fd532018-05-30 09:13:21 +02006087 if ( i + 3 > ssl->in_hslen ) {
6088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6089 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6090 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6091 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6092 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 if( ssl->in_msg[i] != 0 )
6094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006096 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6097 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 }
6100
6101 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6102 | (unsigned int) ssl->in_msg[i + 2];
6103 i += 3;
6104
6105 if( n < 128 || i + n > ssl->in_hslen )
6106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6109 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 }
6112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02006114 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006115 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006117 case 0: /*ok*/
6118 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6119 /* Ignore certificate with an unknown algorithm: maybe a
6120 prior certificate was already trusted. */
6121 break;
6122
6123 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6124 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6125 goto crt_parse_der_failed;
6126
6127 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6128 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6129 goto crt_parse_der_failed;
6130
6131 default:
6132 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6133 crt_parse_der_failed:
6134 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 return( ret );
6137 }
6138
6139 i += n;
6140 }
6141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00006143
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006144 /*
6145 * On client, make sure the server cert doesn't change during renego to
6146 * avoid "triple handshake" attack: https://secure-resumption.com/
6147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006149 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006151 {
6152 if( ssl->session->peer_cert == NULL )
6153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006155 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6156 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006158 }
6159
6160 if( ssl->session->peer_cert->raw.len !=
6161 ssl->session_negotiate->peer_cert->raw.len ||
6162 memcmp( ssl->session->peer_cert->raw.p,
6163 ssl->session_negotiate->peer_cert->raw.p,
6164 ssl->session->peer_cert->raw.len ) != 0 )
6165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006167 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6168 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006170 }
6171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01006173
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006174 return( 0 );
6175}
6176
6177int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6178{
6179 int ret;
6180 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006181 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006182#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6183 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6184 ? ssl->handshake->sni_authmode
6185 : ssl->conf->authmode;
6186#else
6187 const int authmode = ssl->conf->authmode;
6188#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006189 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006190
6191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6192
6193 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6194 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6195 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6196 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6197 {
6198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6199 ssl->state++;
6200 return( 0 );
6201 }
6202
6203#if defined(MBEDTLS_SSL_SRV_C)
6204 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6205 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6208 ssl->state++;
6209 return( 0 );
6210 }
6211
6212 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6213 authmode == MBEDTLS_SSL_VERIFY_NONE )
6214 {
6215 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006217
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006218 ssl->state++;
6219 return( 0 );
6220 }
6221#endif
6222
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006223#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6224 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006225 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006226 {
6227 goto crt_verify;
6228 }
6229#endif
6230
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006231 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006232 {
6233 /* mbedtls_ssl_read_record may have sent an alert already. We
6234 let it decide whether to alert. */
6235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6236 return( ret );
6237 }
6238
6239 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6240 {
6241#if defined(MBEDTLS_SSL_SRV_C)
6242 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
6243 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6244 {
6245 ret = 0;
6246 }
6247#endif
6248
6249 ssl->state++;
6250 return( ret );
6251 }
6252
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006253#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6254 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006255 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006256
6257crt_verify:
6258 if( ssl->handshake->ecrs_enabled)
6259 rs_ctx = &ssl->handshake->ecrs_ctx;
6260#endif
6261
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006262 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006264 mbedtls_x509_crt *ca_chain;
6265 mbedtls_x509_crl *ca_crl;
6266
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006267#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006268 if( ssl->handshake->sni_ca_chain != NULL )
6269 {
6270 ca_chain = ssl->handshake->sni_ca_chain;
6271 ca_crl = ssl->handshake->sni_ca_crl;
6272 }
6273 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006274#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006275 {
6276 ca_chain = ssl->conf->ca_chain;
6277 ca_crl = ssl->conf->ca_crl;
6278 }
6279
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006280 /*
6281 * Main check: verify certificate
6282 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006283 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006284 ssl->session_negotiate->peer_cert,
6285 ca_chain, ca_crl,
6286 ssl->conf->cert_profile,
6287 ssl->hostname,
6288 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006289 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290
6291 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006294 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006295
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006296#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6297 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006298 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006299#endif
6300
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006301 /*
6302 * Secondary checks: always done, but change 'ret' only if it was 0
6303 */
6304
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006305#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006308
6309 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006311 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006312 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006313 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006316 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006318 }
6319 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006320#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006323 ciphersuite_info,
6324 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006325 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006328 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006330 }
6331
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006332 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6333 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6334 * with details encoded in the verification flags. All other kinds
6335 * of error codes, including those from the user provided f_vrfy
6336 * functions, are treated as fatal and lead to a failure of
6337 * ssl_parse_certificate even if verification was optional. */
6338 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6339 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6340 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6341 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006342 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006343 }
6344
6345 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6346 {
6347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6348 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6349 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006350
6351 if( ret != 0 )
6352 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006353 uint8_t alert;
6354
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006355 /* The certificate may have been rejected for several reasons.
6356 Pick one and send the corresponding alert. Which alert to send
6357 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006358 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6359 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6360 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6361 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6362 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6363 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6364 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6365 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6366 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6367 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6368 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6369 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6370 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6371 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6372 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6373 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6374 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6375 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6376 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6377 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006378 else
6379 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6381 alert );
6382 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006383
Hanno Beckere6706e62017-05-15 16:05:15 +01006384#if defined(MBEDTLS_DEBUG_C)
6385 if( ssl->session_negotiate->verify_result != 0 )
6386 {
6387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6388 ssl->session_negotiate->verify_result ) );
6389 }
6390 else
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6393 }
6394#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006395 }
6396
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006397 ssl->state++;
6398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
6401 return( ret );
6402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6404 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6405 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6406 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6407 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6408 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6409 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006412{
6413 int ret;
6414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 ssl->out_msglen = 1;
6419 ssl->out_msg[0] = 1;
6420
Paul Bakker5121ce52009-01-03 21:22:43 +00006421 ssl->state++;
6422
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006423 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006424 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006426 return( ret );
6427 }
6428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006430
6431 return( 0 );
6432}
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435{
6436 int ret;
6437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439
Hanno Becker327c93b2018-08-15 13:56:18 +01006440 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006443 return( ret );
6444 }
6445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006449 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6450 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 }
6453
Hanno Beckere678eaa2018-08-21 14:57:46 +01006454 /* CCS records are only accepted if they have length 1 and content '1',
6455 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006456
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006457 /*
6458 * Switch to our negotiated transform and session parameters for inbound
6459 * data.
6460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006462 ssl->transform_in = ssl->transform_negotiate;
6463 ssl->session_in = ssl->session_negotiate;
6464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006469 ssl_dtls_replay_reset( ssl );
6470#endif
6471
6472 /* Increment epoch */
6473 if( ++ssl->in_epoch == 0 )
6474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006476 /* This is highly unlikely to happen for legitimate reasons, so
6477 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006479 }
6480 }
6481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006483 memset( ssl->in_ctr, 0, 8 );
6484
Hanno Beckerf5970a02019-05-08 09:38:41 +01006485 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6488 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6494 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006496 }
6497 }
6498#endif
6499
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 ssl->state++;
6501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006503
6504 return( 0 );
6505}
6506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6508 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006509{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006510 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6513 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6514 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006515 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006516 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6519#if defined(MBEDTLS_SHA512_C)
6520 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006521 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6522 else
6523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524#if defined(MBEDTLS_SHA256_C)
6525 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006526 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006527 else
6528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006532 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006533 }
Paul Bakker380da532012-04-18 16:10:25 +00006534}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6539 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006540 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6541 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6544#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006545 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006546#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006548 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006551}
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006554 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6557 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006558 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6559 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006560#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6562#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006563 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006566 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006569}
6570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6572 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6573static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006574 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006575{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006576 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6577 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006578}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006579#endif
Paul Bakker380da532012-04-18 16:10:25 +00006580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6582#if defined(MBEDTLS_SHA256_C)
6583static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006584 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006585{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006586 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006587}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006588#endif
Paul Bakker380da532012-04-18 16:10:25 +00006589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590#if defined(MBEDTLS_SHA512_C)
6591static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006592 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006593{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006594 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006595}
Paul Bakker769075d2012-11-24 11:26:46 +01006596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006600static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006602{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006603 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006604 mbedtls_md5_context md5;
6605 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006606
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 unsigned char padbuf[48];
6608 unsigned char md5sum[16];
6609 unsigned char sha1sum[20];
6610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006612 if( !session )
6613 session = ssl->session;
6614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006616
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006617 mbedtls_md5_init( &md5 );
6618 mbedtls_sha1_init( &sha1 );
6619
6620 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6621 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622
6623 /*
6624 * SSLv3:
6625 * hash =
6626 * MD5( master + pad2 +
6627 * MD5( handshake + sender + master + pad1 ) )
6628 * + SHA1( master + pad2 +
6629 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006630 */
6631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006633 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6634 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006635#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006638 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6639 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006640#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006643 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006644
Paul Bakker1ef83d62012-04-11 12:09:53 +00006645 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006646
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006647 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6648 mbedtls_md5_update_ret( &md5, session->master, 48 );
6649 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6650 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006652 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6653 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6654 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6655 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
Paul Bakker1ef83d62012-04-11 12:09:53 +00006657 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006659 mbedtls_md5_starts_ret( &md5 );
6660 mbedtls_md5_update_ret( &md5, session->master, 48 );
6661 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6662 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6663 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006664
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006665 mbedtls_sha1_starts_ret( &sha1 );
6666 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6667 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6668 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6669 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006672
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006673 mbedtls_md5_free( &md5 );
6674 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006676 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6677 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6678 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006685static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006688 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006689 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006690 mbedtls_md5_context md5;
6691 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006692 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006695 if( !session )
6696 session = ssl->session;
6697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006699
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006700 mbedtls_md5_init( &md5 );
6701 mbedtls_sha1_init( &sha1 );
6702
6703 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6704 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006705
Paul Bakker1ef83d62012-04-11 12:09:53 +00006706 /*
6707 * TLSv1:
6708 * hash = PRF( master, finished_label,
6709 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6710 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006713 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6714 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006715#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006718 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6719 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006720#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006723 ? "client finished"
6724 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006725
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006726 mbedtls_md5_finish_ret( &md5, padbuf );
6727 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006728
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006729 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006730 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006733
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006734 mbedtls_md5_free( &md5 );
6735 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006736
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006737 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6744#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006745static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006747{
6748 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006749 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006750 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006751 unsigned char padbuf[32];
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006754 if( !session )
6755 session = ssl->session;
6756
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006757 mbedtls_sha256_init( &sha256 );
6758
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006760
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006761 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006762
6763 /*
6764 * TLSv1.2:
6765 * hash = PRF( master, finished_label,
6766 * Hash( handshake ) )[0.11]
6767 */
6768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769#if !defined(MBEDTLS_SHA256_ALT)
6770 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006771 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006772#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006775 ? "client finished"
6776 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006777
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006778 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006779
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006780 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006781 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006784
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006785 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006786
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006787 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006794static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006796{
6797 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006798 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006799 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006800 unsigned char padbuf[48];
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006803 if( !session )
6804 session = ssl->session;
6805
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006806 mbedtls_sha512_init( &sha512 );
6807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006809
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006810 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006811
6812 /*
6813 * TLSv1.2:
6814 * hash = PRF( master, finished_label,
6815 * Hash( handshake ) )[0.11]
6816 */
6817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006819 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6820 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006821#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006824 ? "client finished"
6825 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006826
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006827 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006828
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006829 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006830 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006833
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006834 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006835
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006836 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#endif /* MBEDTLS_SHA512_C */
6841#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006846
6847 /*
6848 * Free our handshake params
6849 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006850 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006852 ssl->handshake = NULL;
6853
6854 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006855 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006856 */
6857 if( ssl->transform )
6858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 mbedtls_ssl_transform_free( ssl->transform );
6860 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006861 }
6862 ssl->transform = ssl->transform_negotiate;
6863 ssl->transform_negotiate = NULL;
6864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006866}
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006869{
6870 int resume = ssl->handshake->resume;
6871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874#if defined(MBEDTLS_SSL_RENEGOTIATION)
6875 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006878 ssl->renego_records_seen = 0;
6879 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006880#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006881
6882 /*
6883 * Free the previous session and switch in the current one
6884 */
Paul Bakker0a597072012-09-25 21:55:46 +00006885 if( ssl->session )
6886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006888 /* RFC 7366 3.1: keep the EtM state */
6889 ssl->session_negotiate->encrypt_then_mac =
6890 ssl->session->encrypt_then_mac;
6891#endif
6892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 mbedtls_ssl_session_free( ssl->session );
6894 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006895 }
6896 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006897 ssl->session_negotiate = NULL;
6898
Paul Bakker0a597072012-09-25 21:55:46 +00006899 /*
6900 * Add cache entry
6901 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006902 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006903 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006904 resume == 0 )
6905 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006906 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006908 }
Paul Bakker0a597072012-09-25 21:55:46 +00006909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006912 ssl->handshake->flight != NULL )
6913 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006914 /* Cancel handshake timer */
6915 ssl_set_timer( ssl, 0 );
6916
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006917 /* Keep last flight around in case we need to resend it:
6918 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006920 }
6921 else
6922#endif
6923 ssl_handshake_wrapup_free_hs_transform( ssl );
6924
Paul Bakker48916f92012-09-16 19:57:18 +00006925 ssl->state++;
6926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006928}
6929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006932 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006935
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006936 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006937
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006938 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006939
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006940 /*
6941 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6942 * may define some other value. Currently (early 2016), no defined
6943 * ciphersuite does this (and this is unlikely to change as activity has
6944 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006949 ssl->verify_data_len = hash_len;
6950 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006951#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006952
Paul Bakker5121ce52009-01-03 21:22:43 +00006953 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6955 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006956
6957 /*
6958 * In case of session resuming, invert the client and server
6959 * ChangeCipherSpec messages order.
6960 */
Paul Bakker0a597072012-09-25 21:55:46 +00006961 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006964 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006970#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 }
6972 else
6973 ssl->state++;
6974
Paul Bakker48916f92012-09-16 19:57:18 +00006975 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006976 * Switch to our negotiated transform and session parameters for outbound
6977 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006983 {
6984 unsigned char i;
6985
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006986 /* Remember current epoch settings for resending */
6987 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006988 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006989
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006990 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006991 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006992
6993 /* Increment epoch */
6994 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006995 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006996 break;
6997
6998 /* The loop goes to its end iff the counter is wrapping */
6999 if( i == 0 )
7000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7002 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007003 }
7004 }
7005 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007007 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007008
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007009 ssl->transform_out = ssl->transform_negotiate;
7010 ssl->session_out = ssl->session_negotiate;
7011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7013 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7018 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007019 }
7020 }
7021#endif
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007026#endif
7027
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007028 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007031 return( ret );
7032 }
7033
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007034#if defined(MBEDTLS_SSL_PROTO_DTLS)
7035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7036 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7037 {
7038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7039 return( ret );
7040 }
7041#endif
7042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
7045 return( 0 );
7046}
7047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007049#define SSL_MAX_HASH_LEN 36
7050#else
7051#define SSL_MAX_HASH_LEN 12
7052#endif
7053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007055{
Paul Bakker23986e52011-04-24 08:57:21 +00007056 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007057 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007058 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007061
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007062 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Hanno Becker327c93b2018-08-15 13:56:18 +01007064 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007067 return( ret );
7068 }
7069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007073 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7074 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076 }
7077
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007078 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#if defined(MBEDTLS_SSL_PROTO_SSL3)
7080 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007081 hash_len = 36;
7082 else
7083#endif
7084 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7087 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007090 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7091 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007093 }
7094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007096 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7100 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 }
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007105 ssl->verify_data_len = hash_len;
7106 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007107#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007108
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_CLIENT_CHANGE_CIPHER_SPEC;
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_HANDSHAKE_WRAPUP;
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007124 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007126#endif
7127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007129
7130 return( 0 );
7131}
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7138 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7139 mbedtls_md5_init( &handshake->fin_md5 );
7140 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007141 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7142 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7145#if defined(MBEDTLS_SHA256_C)
7146 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007147 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SHA512_C)
7150 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007151 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007154
7155 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007156
7157#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7158 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7159 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7160#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if defined(MBEDTLS_DHM_C)
7163 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#if defined(MBEDTLS_ECDH_C)
7166 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007167#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007168#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007169 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007170#if defined(MBEDTLS_SSL_CLI_C)
7171 handshake->ecjpake_cache = NULL;
7172 handshake->ecjpake_cache_len = 0;
7173#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007174#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007175
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007176#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007177 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007178#endif
7179
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007180#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7181 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7182#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007183}
7184
Hanno Becker611a83b2018-01-03 14:27:32 +00007185void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7190 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007191
Hanno Becker92231322018-01-03 15:32:51 +00007192#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 mbedtls_md_init( &transform->md_ctx_enc );
7194 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007195#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007196}
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007201}
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007204{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007205 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007206 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007208 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007210 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007211 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007212
7213 /*
7214 * Either the pointers are now NULL or cleared properly and can be freed.
7215 * Now allocate missing structures.
7216 */
7217 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007218 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007219 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007220 }
Paul Bakker48916f92012-09-16 19:57:18 +00007221
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007222 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007223 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007224 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007225 }
Paul Bakker48916f92012-09-16 19:57:18 +00007226
Paul Bakker82788fb2014-10-20 13:59:19 +02007227 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007228 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007229 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007230 }
Paul Bakker48916f92012-09-16 19:57:18 +00007231
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007232 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007233 if( ssl->handshake == NULL ||
7234 ssl->transform_negotiate == NULL ||
7235 ssl->session_negotiate == NULL )
7236 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 mbedtls_free( ssl->handshake );
7240 mbedtls_free( ssl->transform_negotiate );
7241 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007242
7243 ssl->handshake = NULL;
7244 ssl->transform_negotiate = NULL;
7245 ssl->session_negotiate = NULL;
7246
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007247 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007248 }
7249
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007250 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007252 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007253 ssl_handshake_params_init( ssl->handshake );
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7257 {
7258 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007259
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7261 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7262 else
7263 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007264
7265 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007266 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007267#endif
7268
Paul Bakker48916f92012-09-16 19:57:18 +00007269 return( 0 );
7270}
7271
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007272#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007273/* Dummy cookie callbacks for defaults */
7274static int ssl_cookie_write_dummy( void *ctx,
7275 unsigned char **p, unsigned char *end,
7276 const unsigned char *cli_id, size_t cli_id_len )
7277{
7278 ((void) ctx);
7279 ((void) p);
7280 ((void) end);
7281 ((void) cli_id);
7282 ((void) cli_id_len);
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007285}
7286
7287static int ssl_cookie_check_dummy( void *ctx,
7288 const unsigned char *cookie, size_t cookie_len,
7289 const unsigned char *cli_id, size_t cli_id_len )
7290{
7291 ((void) ctx);
7292 ((void) cookie);
7293 ((void) cookie_len);
7294 ((void) cli_id);
7295 ((void) cli_id_len);
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007298}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007299#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007300
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007301/* Once ssl->out_hdr as the address of the beginning of the
7302 * next outgoing record is set, deduce the other pointers.
7303 *
7304 * Note: For TLS, we save the implicit record sequence number
7305 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7306 * and the caller has to make sure there's space for this.
7307 */
7308
7309static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7310 mbedtls_ssl_transform *transform )
7311{
7312#if defined(MBEDTLS_SSL_PROTO_DTLS)
7313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7314 {
7315 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Becker70e79282019-05-03 14:34:53 +01007316#if defined(MBEDTLS_SSL_CID)
7317 ssl->out_cid = ssl->out_ctr + 8;
7318 ssl->out_len = ssl->out_cid;
7319 if( transform != NULL )
7320 ssl->out_len += transform->out_cid_len;
7321#else /* MBEDTLS_SSL_CID */
7322 ssl->out_len = ssl->out_ctr + 8;
7323#endif /* MBEDTLS_SSL_CID */
7324 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007325 }
7326 else
7327#endif
7328 {
7329 ssl->out_ctr = ssl->out_hdr - 8;
7330 ssl->out_len = ssl->out_hdr + 3;
7331 ssl->out_iv = ssl->out_hdr + 5;
7332 }
7333
7334 /* Adjust out_msg to make space for explicit IV, if used. */
7335 if( transform != NULL &&
7336 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7337 {
7338 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7339 }
7340 else
7341 ssl->out_msg = ssl->out_iv;
7342}
7343
7344/* Once ssl->in_hdr as the address of the beginning of the
7345 * next incoming record is set, deduce the other pointers.
7346 *
7347 * Note: For TLS, we save the implicit record sequence number
7348 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7349 * and the caller has to make sure there's space for this.
7350 */
7351
Hanno Beckerf5970a02019-05-08 09:38:41 +01007352static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007353{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007354 /* This function sets the pointers to match the case
7355 * of unprotected TLS/DTLS records, with both ssl->in_iv
7356 * and ssl->in_msg pointing to the beginning of the record
7357 * content.
7358 *
7359 * When decrypting a protected record, ssl->in_msg
7360 * will be shifted to point to the beginning of the
7361 * record plaintext.
7362 */
7363
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007364#if defined(MBEDTLS_SSL_PROTO_DTLS)
7365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7366 {
Hanno Becker70e79282019-05-03 14:34:53 +01007367 /* This sets the header pointers to match records
7368 * without CID. When we receive a record containing
7369 * a CID, the fields are shifted accordingly in
7370 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007371 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Becker70e79282019-05-03 14:34:53 +01007372#if defined(MBEDTLS_SSL_CID)
7373 ssl->in_cid = ssl->in_ctr + 8;
7374 ssl->in_len = ssl->in_cid; /* Default: no CID */
7375#else /* MBEDTLS_SSL_CID */
7376 ssl->in_len = ssl->in_ctr + 8;
7377#endif /* MBEDTLS_SSL_CID */
7378 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007379 }
7380 else
7381#endif
7382 {
7383 ssl->in_ctr = ssl->in_hdr - 8;
7384 ssl->in_len = ssl->in_hdr + 3;
7385 ssl->in_iv = ssl->in_hdr + 5;
7386 }
7387
Hanno Beckerf5970a02019-05-08 09:38:41 +01007388 /* This will be adjusted at record decryption time. */
7389 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007390}
7391
Paul Bakker5121ce52009-01-03 21:22:43 +00007392/*
7393 * Initialize an SSL context
7394 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007395void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7396{
7397 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7398}
7399
7400/*
7401 * Setup an SSL context
7402 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007403
7404static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7405{
7406 /* Set the incoming and outgoing record pointers. */
7407#if defined(MBEDTLS_SSL_PROTO_DTLS)
7408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7409 {
7410 ssl->out_hdr = ssl->out_buf;
7411 ssl->in_hdr = ssl->in_buf;
7412 }
7413 else
7414#endif /* MBEDTLS_SSL_PROTO_DTLS */
7415 {
7416 ssl->out_hdr = ssl->out_buf + 8;
7417 ssl->in_hdr = ssl->in_buf + 8;
7418 }
7419
7420 /* Derive other internal pointers. */
7421 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007422 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007423}
7424
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007425int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007426 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007427{
Paul Bakker48916f92012-09-16 19:57:18 +00007428 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007429
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007430 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007431
7432 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007433 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007434 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007435
7436 /* Set to NULL in case of an error condition */
7437 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007438
Angus Grattond8213d02016-05-25 20:56:48 +10007439 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7440 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 {
Angus Grattond8213d02016-05-25 20:56:48 +10007442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007443 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007444 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007445 }
7446
7447 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7448 if( ssl->out_buf == NULL )
7449 {
7450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007451 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007452 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007453 }
7454
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007455 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007456
Paul Bakker48916f92012-09-16 19:57:18 +00007457 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007458 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
7460 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007461
7462error:
7463 mbedtls_free( ssl->in_buf );
7464 mbedtls_free( ssl->out_buf );
7465
7466 ssl->conf = NULL;
7467
7468 ssl->in_buf = NULL;
7469 ssl->out_buf = NULL;
7470
7471 ssl->in_hdr = NULL;
7472 ssl->in_ctr = NULL;
7473 ssl->in_len = NULL;
7474 ssl->in_iv = NULL;
7475 ssl->in_msg = NULL;
7476
7477 ssl->out_hdr = NULL;
7478 ssl->out_ctr = NULL;
7479 ssl->out_len = NULL;
7480 ssl->out_iv = NULL;
7481 ssl->out_msg = NULL;
7482
k-stachowiak9f7798e2018-07-31 16:52:32 +02007483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484}
7485
7486/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007487 * Reset an initialized and used SSL context for re-use while retaining
7488 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007489 *
7490 * If partial is non-zero, keep data in the input buffer and client ID.
7491 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007492 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007493static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007494{
Paul Bakker48916f92012-09-16 19:57:18 +00007495 int ret;
7496
Hanno Becker7e772132018-08-10 12:38:21 +01007497#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7498 !defined(MBEDTLS_SSL_SRV_C)
7499 ((void) partial);
7500#endif
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007503
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007504 /* Cancel any possibly running timer */
7505 ssl_set_timer( ssl, 0 );
7506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_SSL_RENEGOTIATION)
7508 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007509 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007510
7511 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7513 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007516
Paul Bakker7eb013f2011-10-06 12:37:39 +00007517 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007518 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007519
7520 ssl->in_msgtype = 0;
7521 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007523 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007524 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007527 ssl_dtls_replay_reset( ssl );
7528#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007529
7530 ssl->in_hslen = 0;
7531 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007532
7533 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007534
7535 ssl->out_msgtype = 0;
7536 ssl->out_msglen = 0;
7537 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7539 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007540 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007541#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007542
Hanno Becker19859472018-08-06 09:40:20 +01007543 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7544
Paul Bakker48916f92012-09-16 19:57:18 +00007545 ssl->transform_in = NULL;
7546 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007547
Hanno Becker78640902018-08-13 16:35:15 +01007548 ssl->session_in = NULL;
7549 ssl->session_out = NULL;
7550
Angus Grattond8213d02016-05-25 20:56:48 +10007551 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007552
7553#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007554 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007555#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7556 {
7557 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007558 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007559 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7562 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7565 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7568 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007569 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007570 }
7571#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007572
Paul Bakker48916f92012-09-16 19:57:18 +00007573 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 mbedtls_ssl_transform_free( ssl->transform );
7576 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007577 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007578 }
Paul Bakker48916f92012-09-16 19:57:18 +00007579
Paul Bakkerc0463502013-02-14 11:19:38 +01007580 if( ssl->session )
7581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 mbedtls_ssl_session_free( ssl->session );
7583 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007584 ssl->session = NULL;
7585 }
7586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007588 ssl->alpn_chosen = NULL;
7589#endif
7590
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007591#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007592#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007593 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007594#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007595 {
7596 mbedtls_free( ssl->cli_id );
7597 ssl->cli_id = NULL;
7598 ssl->cli_id_len = 0;
7599 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007600#endif
7601
Paul Bakker48916f92012-09-16 19:57:18 +00007602 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7603 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007604
7605 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007606}
7607
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007608/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007609 * Reset an initialized and used SSL context for re-use while retaining
7610 * all application-set variables, function pointers and data.
7611 */
7612int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7613{
7614 return( ssl_session_reset_int( ssl, 0 ) );
7615}
7616
7617/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007618 * SSL set accessors
7619 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007620void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007622 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007623}
7624
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007625void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007626{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007627 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007628}
7629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007631void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007633 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007634}
7635#endif
7636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007638void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007640 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007641}
7642#endif
7643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007645
Hanno Becker1841b0a2018-08-24 11:13:57 +01007646void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7647 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007648{
7649 ssl->disable_datagram_packing = !allow_packing;
7650}
7651
7652void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7653 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007655 conf->hs_timeout_min = min;
7656 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007657}
7658#endif
7659
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007660void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007662 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007663}
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007666void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007667 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007668 void *p_vrfy )
7669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007670 conf->f_vrfy = f_vrfy;
7671 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007674
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007675void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007676 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007677 void *p_rng )
7678{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007679 conf->f_rng = f_rng;
7680 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007681}
7682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007683void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007684 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007685 void *p_dbg )
7686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007687 conf->f_dbg = f_dbg;
7688 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007689}
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007692 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007693 mbedtls_ssl_send_t *f_send,
7694 mbedtls_ssl_recv_t *f_recv,
7695 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007696{
7697 ssl->p_bio = p_bio;
7698 ssl->f_send = f_send;
7699 ssl->f_recv = f_recv;
7700 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007701}
7702
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007703#if defined(MBEDTLS_SSL_PROTO_DTLS)
7704void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7705{
7706 ssl->mtu = mtu;
7707}
7708#endif
7709
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007710void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007711{
7712 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007713}
7714
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007715void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7716 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007717 mbedtls_ssl_set_timer_t *f_set_timer,
7718 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007719{
7720 ssl->p_timer = p_timer;
7721 ssl->f_set_timer = f_set_timer;
7722 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007723
7724 /* Make sure we start with no timer running */
7725 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007726}
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007729void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007730 void *p_cache,
7731 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7732 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007733{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007734 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007735 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007736 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#if defined(MBEDTLS_SSL_CLI_C)
7741int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007742{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007743 int ret;
7744
7745 if( ssl == NULL ||
7746 session == NULL ||
7747 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007748 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007751 }
7752
7753 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7754 return( ret );
7755
Paul Bakker0a597072012-09-25 21:55:46 +00007756 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007757
7758 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007761
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007762void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007763 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007764{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007765 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7766 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7767 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7768 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007769}
7770
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007771void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007772 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007773 int major, int minor )
7774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007776 return;
7777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007779 return;
7780
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007781 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007782}
7783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007785void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007786 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007787{
7788 conf->cert_profile = profile;
7789}
7790
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007791/* Append a new keycert entry to a (possibly empty) list */
7792static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7793 mbedtls_x509_crt *cert,
7794 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007795{
niisato8ee24222018-06-25 19:05:48 +09007796 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007797
niisato8ee24222018-06-25 19:05:48 +09007798 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7799 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007800 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007801
niisato8ee24222018-06-25 19:05:48 +09007802 new_cert->cert = cert;
7803 new_cert->key = key;
7804 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007805
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007806 /* Update head is the list was null, else add to the end */
7807 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007808 {
niisato8ee24222018-06-25 19:05:48 +09007809 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007810 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007811 else
7812 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007813 mbedtls_ssl_key_cert *cur = *head;
7814 while( cur->next != NULL )
7815 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007816 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007817 }
7818
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007819 return( 0 );
7820}
7821
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007822int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007823 mbedtls_x509_crt *own_cert,
7824 mbedtls_pk_context *pk_key )
7825{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007826 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007827}
7828
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007829void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007830 mbedtls_x509_crt *ca_chain,
7831 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007832{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007833 conf->ca_chain = ca_chain;
7834 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007837
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007838#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7839int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7840 mbedtls_x509_crt *own_cert,
7841 mbedtls_pk_context *pk_key )
7842{
7843 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7844 own_cert, pk_key ) );
7845}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007846
7847void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7848 mbedtls_x509_crt *ca_chain,
7849 mbedtls_x509_crl *ca_crl )
7850{
7851 ssl->handshake->sni_ca_chain = ca_chain;
7852 ssl->handshake->sni_ca_crl = ca_crl;
7853}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007854
7855void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7856 int authmode )
7857{
7858 ssl->handshake->sni_authmode = authmode;
7859}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007860#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7861
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007862#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007863/*
7864 * Set EC J-PAKE password for current handshake
7865 */
7866int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7867 const unsigned char *pw,
7868 size_t pw_len )
7869{
7870 mbedtls_ecjpake_role role;
7871
Janos Follath8eb64132016-06-03 15:40:57 +01007872 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007873 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7874
7875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7876 role = MBEDTLS_ECJPAKE_SERVER;
7877 else
7878 role = MBEDTLS_ECJPAKE_CLIENT;
7879
7880 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7881 role,
7882 MBEDTLS_MD_SHA256,
7883 MBEDTLS_ECP_DP_SECP256R1,
7884 pw, pw_len ) );
7885}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007886#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007889int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007890 const unsigned char *psk, size_t psk_len,
7891 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007892{
Paul Bakker6db455e2013-09-18 17:29:31 +02007893 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007898
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007899 /* Identity len will be encoded on two bytes */
7900 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007901 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007902 {
7903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7904 }
7905
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007906 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007907 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007908 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007909
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007910 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007911 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007912 conf->psk_len = 0;
7913 }
7914 if( conf->psk_identity != NULL )
7915 {
7916 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007917 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007918 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007919 }
7920
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007921 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7922 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007923 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007924 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007925 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007926 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007927 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007928 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007929 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007930
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007931 conf->psk_len = psk_len;
7932 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007933
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007934 memcpy( conf->psk, psk, conf->psk_len );
7935 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007936
7937 return( 0 );
7938}
7939
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007940int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7941 const unsigned char *psk, size_t psk_len )
7942{
7943 if( psk == NULL || ssl->handshake == NULL )
7944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7945
7946 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7948
7949 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007950 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007951 mbedtls_platform_zeroize( ssl->handshake->psk,
7952 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007953 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007954 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007955 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007956
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007957 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007958 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007959
7960 ssl->handshake->psk_len = psk_len;
7961 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7962
7963 return( 0 );
7964}
7965
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007966void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007968 size_t),
7969 void *p_psk )
7970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007971 conf->f_psk = f_psk;
7972 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007975
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007976#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007977
7978#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007979int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007980{
7981 int ret;
7982
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007983 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7984 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7985 {
7986 mbedtls_mpi_free( &conf->dhm_P );
7987 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007988 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007990
7991 return( 0 );
7992}
Hanno Becker470a8c42017-10-04 15:28:46 +01007993#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007994
Hanno Beckera90658f2017-10-04 15:29:08 +01007995int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7996 const unsigned char *dhm_P, size_t P_len,
7997 const unsigned char *dhm_G, size_t G_len )
7998{
7999 int ret;
8000
8001 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8002 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8003 {
8004 mbedtls_mpi_free( &conf->dhm_P );
8005 mbedtls_mpi_free( &conf->dhm_G );
8006 return( ret );
8007 }
8008
8009 return( 0 );
8010}
Paul Bakker5121ce52009-01-03 21:22:43 +00008011
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008012int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008013{
8014 int ret;
8015
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008016 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8017 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8018 {
8019 mbedtls_mpi_free( &conf->dhm_P );
8020 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008021 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008022 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008023
8024 return( 0 );
8025}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008026#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008027
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008028#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8029/*
8030 * Set the minimum length for Diffie-Hellman parameters
8031 */
8032void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8033 unsigned int bitlen )
8034{
8035 conf->dhm_min_bitlen = bitlen;
8036}
8037#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8038
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008039#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008040/*
8041 * Set allowed/preferred hashes for handshake signatures
8042 */
8043void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8044 const int *hashes )
8045{
8046 conf->sig_hashes = hashes;
8047}
Hanno Becker947194e2017-04-07 13:25:49 +01008048#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008049
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008050#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008051/*
8052 * Set the allowed elliptic curves
8053 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008054void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008055 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008057 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008058}
Hanno Becker947194e2017-04-07 13:25:49 +01008059#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008060
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008061#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008063{
Hanno Becker947194e2017-04-07 13:25:49 +01008064 /* Initialize to suppress unnecessary compiler warning */
8065 size_t hostname_len = 0;
8066
8067 /* Check if new hostname is valid before
8068 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008069 if( hostname != NULL )
8070 {
8071 hostname_len = strlen( hostname );
8072
8073 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8075 }
8076
8077 /* Now it's clear that we will overwrite the old hostname,
8078 * so we can free it safely */
8079
8080 if( ssl->hostname != NULL )
8081 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008082 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008083 mbedtls_free( ssl->hostname );
8084 }
8085
8086 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008087
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008089 {
8090 ssl->hostname = NULL;
8091 }
8092 else
8093 {
8094 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008095 if( ssl->hostname == NULL )
8096 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008097
Hanno Becker947194e2017-04-07 13:25:49 +01008098 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008099
Hanno Becker947194e2017-04-07 13:25:49 +01008100 ssl->hostname[hostname_len] = '\0';
8101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008102
8103 return( 0 );
8104}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008105#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008106
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008107#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008108void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008110 const unsigned char *, size_t),
8111 void *p_sni )
8112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008113 conf->f_sni = f_sni;
8114 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008119int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008120{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008121 size_t cur_len, tot_len;
8122 const char **p;
8123
8124 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008125 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8126 * MUST NOT be truncated."
8127 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008128 */
8129 tot_len = 0;
8130 for( p = protos; *p != NULL; p++ )
8131 {
8132 cur_len = strlen( *p );
8133 tot_len += cur_len;
8134
8135 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008137 }
8138
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008139 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008140
8141 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008142}
8143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008145{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008146 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008149
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008150void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008152 conf->max_major_ver = major;
8153 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008154}
8155
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008156void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008158 conf->min_major_ver = major;
8159 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008160}
8161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008163void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008164{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008165 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008166}
8167#endif
8168
Janos Follath088ce432017-04-10 12:42:31 +01008169#if defined(MBEDTLS_SSL_SRV_C)
8170void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8171 char cert_req_ca_list )
8172{
8173 conf->cert_req_ca_list = cert_req_ca_list;
8174}
8175#endif
8176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008178void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008180 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008181}
8182#endif
8183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008185void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008187 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008188}
8189#endif
8190
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008191#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008192void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008193{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008194 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008195}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008196#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008199int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008202 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008205 }
8206
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008207 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008208
8209 return( 0 );
8210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008214void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008215{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008216 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008221void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008222{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008223 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008224}
8225#endif
8226
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008227void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008228{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008229 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008230}
8231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008233void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008234{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008235 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008236}
8237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008238void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008240 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008241}
8242
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008243void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008244 const unsigned char period[8] )
8245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008246 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008251#if defined(MBEDTLS_SSL_CLI_C)
8252void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008253{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008254 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008255}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008256#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008257
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008258#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008259void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8260 mbedtls_ssl_ticket_write_t *f_ticket_write,
8261 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8262 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008263{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008264 conf->f_ticket_write = f_ticket_write;
8265 conf->f_ticket_parse = f_ticket_parse;
8266 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008267}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008270
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008271#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8272void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8273 mbedtls_ssl_export_keys_t *f_export_keys,
8274 void *p_export_keys )
8275{
8276 conf->f_export_keys = f_export_keys;
8277 conf->p_export_keys = p_export_keys;
8278}
8279#endif
8280
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008281#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008282void mbedtls_ssl_conf_async_private_cb(
8283 mbedtls_ssl_config *conf,
8284 mbedtls_ssl_async_sign_t *f_async_sign,
8285 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8286 mbedtls_ssl_async_resume_t *f_async_resume,
8287 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008288 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008289{
8290 conf->f_async_sign_start = f_async_sign;
8291 conf->f_async_decrypt_start = f_async_decrypt;
8292 conf->f_async_resume = f_async_resume;
8293 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008294 conf->p_async_config_data = async_config_data;
8295}
8296
Gilles Peskine8f97af72018-04-26 11:46:10 +02008297void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8298{
8299 return( conf->p_async_config_data );
8300}
8301
Gilles Peskine1febfef2018-04-30 11:54:39 +02008302void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008303{
8304 if( ssl->handshake == NULL )
8305 return( NULL );
8306 else
8307 return( ssl->handshake->user_async_ctx );
8308}
8309
Gilles Peskine1febfef2018-04-30 11:54:39 +02008310void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008311 void *ctx )
8312{
8313 if( ssl->handshake != NULL )
8314 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008315}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008316#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008317
Paul Bakker5121ce52009-01-03 21:22:43 +00008318/*
8319 * SSL get accessors
8320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008322{
8323 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8324}
8325
Hanno Becker8b170a02017-10-10 11:51:19 +01008326int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8327{
8328 /*
8329 * Case A: We're currently holding back
8330 * a message for further processing.
8331 */
8332
8333 if( ssl->keep_current_message == 1 )
8334 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008336 return( 1 );
8337 }
8338
8339 /*
8340 * Case B: Further records are pending in the current datagram.
8341 */
8342
8343#if defined(MBEDTLS_SSL_PROTO_DTLS)
8344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8345 ssl->in_left > ssl->next_record_offset )
8346 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008348 return( 1 );
8349 }
8350#endif /* MBEDTLS_SSL_PROTO_DTLS */
8351
8352 /*
8353 * Case C: A handshake message is being processed.
8354 */
8355
Hanno Becker8b170a02017-10-10 11:51:19 +01008356 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8357 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008359 return( 1 );
8360 }
8361
8362 /*
8363 * Case D: An application data message is being processed
8364 */
8365 if( ssl->in_offt != NULL )
8366 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008368 return( 1 );
8369 }
8370
8371 /*
8372 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008373 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008374 * we implement support for multiple alerts in single records.
8375 */
8376
8377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8378 return( 0 );
8379}
8380
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008381uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008382{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008383 if( ssl->session != NULL )
8384 return( ssl->session->verify_result );
8385
8386 if( ssl->session_negotiate != NULL )
8387 return( ssl->session_negotiate->verify_result );
8388
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008389 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008390}
8391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008393{
Paul Bakker926c8e42013-03-06 10:23:34 +01008394 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008395 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008398}
8399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008404 {
8405 switch( ssl->minor_ver )
8406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008408 return( "DTLSv1.0" );
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008411 return( "DTLSv1.2" );
8412
8413 default:
8414 return( "unknown (DTLS)" );
8415 }
8416 }
8417#endif
8418
Paul Bakker43ca69c2011-01-15 17:35:19 +00008419 switch( ssl->minor_ver )
8420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008422 return( "SSLv3.0" );
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008425 return( "TLSv1.0" );
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008428 return( "TLSv1.1" );
8429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008431 return( "TLSv1.2" );
8432
Paul Bakker43ca69c2011-01-15 17:35:19 +00008433 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008434 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008435 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008436}
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008439{
Hanno Becker3136ede2018-08-17 15:28:19 +01008440 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008442 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008443
Hanno Becker43395762019-05-03 14:46:38 +01008444 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8445
Hanno Becker78640902018-08-13 16:35:15 +01008446 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008447 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_ZLIB_SUPPORT)
8450 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8451 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008452#endif
8453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 case MBEDTLS_MODE_GCM:
8457 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008458 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008460 transform_expansion = transform->minlen;
8461 break;
8462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008464
8465 block_size = mbedtls_cipher_get_block_size(
8466 &transform->cipher_ctx_enc );
8467
Hanno Becker3136ede2018-08-17 15:28:19 +01008468 /* Expansion due to the addition of the MAC. */
8469 transform_expansion += transform->maclen;
8470
8471 /* Expansion due to the addition of CBC padding;
8472 * Theoretically up to 256 bytes, but we never use
8473 * more than the block size of the underlying cipher. */
8474 transform_expansion += block_size;
8475
8476 /* For TLS 1.1 or higher, an explicit IV is added
8477 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008478#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8479 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008480 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008481#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008482
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008483 break;
8484
8485 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008488 }
8489
Hanno Beckeradd01902019-05-08 15:40:11 +01008490#if defined(MBEDTLS_SSL_CID)
8491 if( transform->out_cid_len != 0 )
8492 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
8493#endif /* MBEDTLS_SSL_CID */
8494
Hanno Becker43395762019-05-03 14:46:38 +01008495 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008496}
8497
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008498#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8499size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8500{
8501 size_t max_len;
8502
8503 /*
8504 * Assume mfl_code is correct since it was checked when set
8505 */
Angus Grattond8213d02016-05-25 20:56:48 +10008506 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008507
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008508 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008509 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008510 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008511 {
Angus Grattond8213d02016-05-25 20:56:48 +10008512 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008513 }
8514
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008515 /* During a handshake, use the value being negotiated */
8516 if( ssl->session_negotiate != NULL &&
8517 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8518 {
8519 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8520 }
8521
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008522 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008523}
8524#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8525
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008526#if defined(MBEDTLS_SSL_PROTO_DTLS)
8527static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8528{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008529 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8531 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8532 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8533 return ( 0 );
8534
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008535 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8536 return( ssl->mtu );
8537
8538 if( ssl->mtu == 0 )
8539 return( ssl->handshake->mtu );
8540
8541 return( ssl->mtu < ssl->handshake->mtu ?
8542 ssl->mtu : ssl->handshake->mtu );
8543}
8544#endif /* MBEDTLS_SSL_PROTO_DTLS */
8545
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008546int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8547{
8548 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8549
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008550#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8551 !defined(MBEDTLS_SSL_PROTO_DTLS)
8552 (void) ssl;
8553#endif
8554
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008555#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8556 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8557
8558 if( max_len > mfl )
8559 max_len = mfl;
8560#endif
8561
8562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008563 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008564 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008565 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008566 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8567 const size_t overhead = (size_t) ret;
8568
8569 if( ret < 0 )
8570 return( ret );
8571
8572 if( mtu <= overhead )
8573 {
8574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8575 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8576 }
8577
8578 if( max_len > mtu - overhead )
8579 max_len = mtu - overhead;
8580 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008581#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008582
Hanno Becker0defedb2018-08-10 12:35:02 +01008583#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8584 !defined(MBEDTLS_SSL_PROTO_DTLS)
8585 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008586#endif
8587
8588 return( (int) max_len );
8589}
8590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591#if defined(MBEDTLS_X509_CRT_PARSE_C)
8592const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008593{
8594 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008595 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008596
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008597 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#if defined(MBEDTLS_SSL_CLI_C)
8602int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008603{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008604 if( ssl == NULL ||
8605 dst == NULL ||
8606 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008607 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008610 }
8611
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008612 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008615
Paul Bakker5121ce52009-01-03 21:22:43 +00008616/*
Paul Bakker1961b702013-01-25 14:49:24 +01008617 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008622
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008623 if( ssl == NULL || ssl->conf == NULL )
8624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008633#endif
8634
Paul Bakker1961b702013-01-25 14:49:24 +01008635 return( ret );
8636}
8637
8638/*
8639 * Perform the SSL handshake
8640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008642{
8643 int ret = 0;
8644
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008645 if( ssl == NULL || ssl->conf == NULL )
8646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008653
8654 if( ret != 0 )
8655 break;
8656 }
8657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008659
8660 return( ret );
8661}
8662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#if defined(MBEDTLS_SSL_RENEGOTIATION)
8664#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008665/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008666 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008669{
8670 int ret;
8671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008673
8674 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8676 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008677
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008678 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008679 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008681 return( ret );
8682 }
8683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008685
8686 return( 0 );
8687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008689
8690/*
8691 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 * - any side: calling mbedtls_ssl_renegotiate(),
8693 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8694 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008695 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008696 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008700{
8701 int ret;
8702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008704
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008705 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8706 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008707
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008708 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8709 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008713 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008714 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008715 ssl->handshake->out_msg_seq = 1;
8716 else
8717 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008718 }
8719#endif
8720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8722 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008727 return( ret );
8728 }
8729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008731
8732 return( 0 );
8733}
8734
8735/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008736 * Renegotiate current connection on client,
8737 * or request renegotiation on server
8738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008742
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008743 if( ssl == NULL || ssl->conf == NULL )
8744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008747 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008748 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008754
8755 /* Did we already try/start sending HelloRequest? */
8756 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008758
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008759 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008760 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008764 /*
8765 * On client, either start the renegotiation process or,
8766 * if already in progress, continue the handshake
8767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008772
8773 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008776 return( ret );
8777 }
8778 }
8779 else
8780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008784 return( ret );
8785 }
8786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008788
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008789 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008790}
8791
8792/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008793 * Check record counters and renegotiate if they're above the limit.
8794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008796{
Andres AG2196c7f2016-12-15 17:01:16 +00008797 size_t ep_len = ssl_ep_len( ssl );
8798 int in_ctr_cmp;
8799 int out_ctr_cmp;
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8802 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008803 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008804 {
8805 return( 0 );
8806 }
8807
Andres AG2196c7f2016-12-15 17:01:16 +00008808 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8809 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008810 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008811 ssl->conf->renego_period + ep_len, 8 - ep_len );
8812
8813 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008814 {
8815 return( 0 );
8816 }
8817
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008820}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008822
8823/*
8824 * Receive application data decrypted from the SSL layer
8825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008827{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008828 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008829 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008830
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008831 if( ssl == NULL || ssl->conf == NULL )
8832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008837 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008840 return( ret );
8841
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008842 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008844 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008845 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008846 return( ret );
8847 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008848 }
8849#endif
8850
Hanno Becker4a810fb2017-05-24 16:27:30 +01008851 /*
8852 * Check if renegotiation is necessary and/or handshake is
8853 * in process. If yes, perform/continue, and fall through
8854 * if an unexpected packet is received while the client
8855 * is waiting for the ServerHello.
8856 *
8857 * (There is no equivalent to the last condition on
8858 * the server-side as it is not treated as within
8859 * a handshake while waiting for the ClientHello
8860 * after a renegotiation request.)
8861 */
8862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008864 ret = ssl_check_ctr_renegotiate( ssl );
8865 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8866 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008869 return( ret );
8870 }
8871#endif
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008876 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8877 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008880 return( ret );
8881 }
8882 }
8883
Hanno Beckere41158b2017-10-23 13:30:32 +01008884 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008885 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008886 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008887 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008888 if( ssl->f_get_timer != NULL &&
8889 ssl->f_get_timer( ssl->p_timer ) == -1 )
8890 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008891 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008892 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008893
Hanno Becker327c93b2018-08-15 13:56:18 +01008894 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008895 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008896 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8897 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008898
Hanno Becker4a810fb2017-05-24 16:27:30 +01008899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8900 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008901 }
8902
8903 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008904 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008905 {
8906 /*
8907 * OpenSSL sends empty messages to randomize the IV
8908 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008909 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008912 return( 0 );
8913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008915 return( ret );
8916 }
8917 }
8918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008922
Hanno Becker4a810fb2017-05-24 16:27:30 +01008923 /*
8924 * - For client-side, expect SERVER_HELLO_REQUEST.
8925 * - For server-side, expect CLIENT_HELLO.
8926 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8927 */
8928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008930 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008932 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008935
8936 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008938 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008939 {
8940 continue;
8941 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008944 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008945#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008946
Hanno Becker4a810fb2017-05-24 16:27:30 +01008947#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008948 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008952
8953 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008956 {
8957 continue;
8958 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008961 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008962#endif /* MBEDTLS_SSL_SRV_C */
8963
Hanno Becker21df7f92017-10-17 11:03:26 +01008964#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008965 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008966 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8967 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8968 ssl->conf->allow_legacy_renegotiation ==
8969 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8970 {
8971 /*
8972 * Accept renegotiation request
8973 */
Paul Bakker48916f92012-09-16 19:57:18 +00008974
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008975 /* DTLS clients need to know renego is server-initiated */
8976#if defined(MBEDTLS_SSL_PROTO_DTLS)
8977 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8978 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8979 {
8980 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8981 }
8982#endif
8983 ret = ssl_start_renegotiation( ssl );
8984 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8985 ret != 0 )
8986 {
8987 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8988 return( ret );
8989 }
8990 }
8991 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008992#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008993 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008994 /*
8995 * Refuse renegotiation
8996 */
8997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000#if defined(MBEDTLS_SSL_PROTO_SSL3)
9001 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009002 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009003 /* SSLv3 does not have a "no_renegotiation" warning, so
9004 we send a fatal alert and abort the connection. */
9005 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9006 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9007 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009008 }
9009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9011#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9012 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9013 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9016 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9017 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009018 {
9019 return( ret );
9020 }
Paul Bakker48916f92012-09-16 19:57:18 +00009021 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009022 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9024 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009028 }
Paul Bakker48916f92012-09-16 19:57:18 +00009029 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009030
Hanno Becker90333da2017-10-10 11:27:13 +01009031 /* At this point, we don't know whether the renegotiation has been
9032 * completed or not. The cases to consider are the following:
9033 * 1) The renegotiation is complete. In this case, no new record
9034 * has been read yet.
9035 * 2) The renegotiation is incomplete because the client received
9036 * an application data record while awaiting the ServerHello.
9037 * 3) The renegotiation is incomplete because the client received
9038 * a non-handshake, non-application data message while awaiting
9039 * the ServerHello.
9040 * In each of these case, looping will be the proper action:
9041 * - For 1), the next iteration will read a new record and check
9042 * if it's application data.
9043 * - For 2), the loop condition isn't satisfied as application data
9044 * is present, hence continue is the same as break
9045 * - For 3), the loop condition is satisfied and read_record
9046 * will re-deliver the message that was held back by the client
9047 * when expecting the ServerHello.
9048 */
9049 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009050 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009051#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009053 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009054 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009055 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009056 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009059 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009061 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009062 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009063 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9067 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009070 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009071 }
9072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9076 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009077 }
9078
9079 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009080
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009081 /* We're going to return something now, cancel timer,
9082 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009084 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009085
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009086#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009087 /* If we requested renego but received AppData, resend HelloRequest.
9088 * Do it now, after setting in_offt, to avoid taking this branch
9089 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009091 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009093 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009094 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009097 return( ret );
9098 }
9099 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009101#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009102 }
9103
9104 n = ( len < ssl->in_msglen )
9105 ? len : ssl->in_msglen;
9106
9107 memcpy( buf, ssl->in_offt, n );
9108 ssl->in_msglen -= n;
9109
9110 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009111 {
9112 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009113 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009114 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009116 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009117 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009118 /* more data available */
9119 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009120 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009123
Paul Bakker23986e52011-04-24 08:57:21 +00009124 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009125}
9126
9127/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009128 * Send application data to be encrypted by the SSL layer, taking care of max
9129 * fragment length and buffer size.
9130 *
9131 * According to RFC 5246 Section 6.2.1:
9132 *
9133 * Zero-length fragments of Application data MAY be sent as they are
9134 * potentially useful as a traffic analysis countermeasure.
9135 *
9136 * Therefore, it is possible that the input message length is 0 and the
9137 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009138 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009139static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009140 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009141{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009142 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9143 const size_t max_len = (size_t) ret;
9144
9145 if( ret < 0 )
9146 {
9147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9148 return( ret );
9149 }
9150
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009151 if( len > max_len )
9152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009157 "maximum fragment length: %d > %d",
9158 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009160 }
9161 else
9162#endif
9163 len = max_len;
9164 }
Paul Bakker887bd502011-06-08 13:10:54 +00009165
Paul Bakker5121ce52009-01-03 21:22:43 +00009166 if( ssl->out_left != 0 )
9167 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009168 /*
9169 * The user has previously tried to send the data and
9170 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9171 * written. In this case, we expect the high-level write function
9172 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009177 return( ret );
9178 }
9179 }
Paul Bakker887bd502011-06-08 13:10:54 +00009180 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009181 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009182 /*
9183 * The user is trying to send a message the first time, so we need to
9184 * copy the data into the internal buffers and setup the data structure
9185 * to keep track of partial writes
9186 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009187 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009189 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009190
Hanno Becker67bc7c32018-08-06 11:33:50 +01009191 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009194 return( ret );
9195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009196 }
9197
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009198 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009199}
9200
9201/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009202 * Write application data, doing 1/n-1 splitting if necessary.
9203 *
9204 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009205 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009206 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009209static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009210 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009211{
9212 int ret;
9213
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009214 if( ssl->conf->cbc_record_splitting ==
9215 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009216 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9218 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9219 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009220 {
9221 return( ssl_write_real( ssl, buf, len ) );
9222 }
9223
9224 if( ssl->split_done == 0 )
9225 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009226 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009227 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009228 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009229 }
9230
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009231 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9232 return( ret );
9233 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009234
9235 return( ret + 1 );
9236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009238
9239/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009240 * Write application data (public-facing wrapper)
9241 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009242int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009243{
9244 int ret;
9245
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009247
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009248 if( ssl == NULL || ssl->conf == NULL )
9249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9250
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009251#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009252 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9253 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009254 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009255 return( ret );
9256 }
9257#endif
9258
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009259 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009260 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009261 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009262 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009264 return( ret );
9265 }
9266 }
9267
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009268#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009269 ret = ssl_write_split( ssl, buf, len );
9270#else
9271 ret = ssl_write_real( ssl, buf, len );
9272#endif
9273
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009275
9276 return( ret );
9277}
9278
9279/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009280 * Notify the peer that the connection is being closed
9281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009283{
9284 int ret;
9285
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009286 if( ssl == NULL || ssl->conf == NULL )
9287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009290
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009291 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9297 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9298 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009301 return( ret );
9302 }
9303 }
9304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009306
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009307 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009308}
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009311{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009312 if( transform == NULL )
9313 return;
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009316 deflateEnd( &transform->ctx_deflate );
9317 inflateEnd( &transform->ctx_inflate );
9318#endif
9319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9321 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009322
Hanno Becker92231322018-01-03 15:32:51 +00009323#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 mbedtls_md_free( &transform->md_ctx_enc );
9325 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00009326#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009327
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009328 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009329}
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331#if defined(MBEDTLS_X509_CRT_PARSE_C)
9332static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009335
9336 while( cur != NULL )
9337 {
9338 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009340 cur = next;
9341 }
9342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009344
Hanno Becker0271f962018-08-16 13:23:47 +01009345#if defined(MBEDTLS_SSL_PROTO_DTLS)
9346
9347static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9348{
9349 unsigned offset;
9350 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9351
9352 if( hs == NULL )
9353 return;
9354
Hanno Becker283f5ef2018-08-24 09:34:47 +01009355 ssl_free_buffered_record( ssl );
9356
Hanno Becker0271f962018-08-16 13:23:47 +01009357 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009358 ssl_buffering_free_slot( ssl, offset );
9359}
9360
9361static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9362 uint8_t slot )
9363{
9364 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9365 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009366
9367 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9368 return;
9369
Hanno Beckere605b192018-08-21 15:59:07 +01009370 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009371 {
Hanno Beckere605b192018-08-21 15:59:07 +01009372 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009373 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009374 mbedtls_free( hs_buf->data );
9375 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009376 }
9377}
9378
9379#endif /* MBEDTLS_SSL_PROTO_DTLS */
9380
Gilles Peskine9b562d52018-04-25 20:32:43 +02009381void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009382{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009383 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9384
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009385 if( handshake == NULL )
9386 return;
9387
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009388#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9389 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9390 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009391 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009392 handshake->async_in_progress = 0;
9393 }
9394#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9395
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009396#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9397 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9398 mbedtls_md5_free( &handshake->fin_md5 );
9399 mbedtls_sha1_free( &handshake->fin_sha1 );
9400#endif
9401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9402#if defined(MBEDTLS_SHA256_C)
9403 mbedtls_sha256_free( &handshake->fin_sha256 );
9404#endif
9405#if defined(MBEDTLS_SHA512_C)
9406 mbedtls_sha512_free( &handshake->fin_sha512 );
9407#endif
9408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410#if defined(MBEDTLS_DHM_C)
9411 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#if defined(MBEDTLS_ECDH_C)
9414 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009415#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009416#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009417 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009418#if defined(MBEDTLS_SSL_CLI_C)
9419 mbedtls_free( handshake->ecjpake_cache );
9420 handshake->ecjpake_cache = NULL;
9421 handshake->ecjpake_cache_len = 0;
9422#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009423#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009424
Janos Follath4ae5c292016-02-10 11:27:43 +00009425#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9426 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009427 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009429#endif
9430
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009431#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9432 if( handshake->psk != NULL )
9433 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009434 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009435 mbedtls_free( handshake->psk );
9436 }
9437#endif
9438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9440 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009441 /*
9442 * Free only the linked list wrapper, not the keys themselves
9443 * since the belong to the SNI callback
9444 */
9445 if( handshake->sni_key_cert != NULL )
9446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009448
9449 while( cur != NULL )
9450 {
9451 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009453 cur = next;
9454 }
9455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009457
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009458#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009459 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009460#endif
9461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462#if defined(MBEDTLS_SSL_PROTO_DTLS)
9463 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009464 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009465 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009466#endif
9467
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009468 mbedtls_platform_zeroize( handshake,
9469 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009470}
9471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009473{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009474 if( session == NULL )
9475 return;
9476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009478 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 mbedtls_x509_crt_free( session->peer_cert );
9481 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009482 }
Paul Bakkered27a042013-04-18 22:46:23 +02009483#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009484
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009485#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009487#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009489 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009490}
9491
Paul Bakker5121ce52009-01-03 21:22:43 +00009492/*
9493 * Free an SSL context
9494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009496{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009497 if( ssl == NULL )
9498 return;
9499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009501
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009502 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009503 {
Angus Grattond8213d02016-05-25 20:56:48 +10009504 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009506 }
9507
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009508 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009509 {
Angus Grattond8213d02016-05-25 20:56:48 +10009510 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009512 }
9513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009515 if( ssl->compress_buf != NULL )
9516 {
Angus Grattond8213d02016-05-25 20:56:48 +10009517 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009519 }
9520#endif
9521
Paul Bakker48916f92012-09-16 19:57:18 +00009522 if( ssl->transform )
9523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 mbedtls_ssl_transform_free( ssl->transform );
9525 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009526 }
9527
9528 if( ssl->handshake )
9529 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009530 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9532 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 mbedtls_free( ssl->handshake );
9535 mbedtls_free( ssl->transform_negotiate );
9536 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009537 }
9538
Paul Bakkerc0463502013-02-14 11:19:38 +01009539 if( ssl->session )
9540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 mbedtls_ssl_session_free( ssl->session );
9542 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009543 }
9544
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009545#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009546 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009547 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009548 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009550 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009551#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9554 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9557 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009558 }
9559#endif
9560
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009561#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009563#endif
9564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009566
Paul Bakker86f04f42013-02-14 11:20:09 +01009567 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009568 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009569}
9570
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009571/*
9572 * Initialze mbedtls_ssl_config
9573 */
9574void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9575{
9576 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9577}
9578
Simon Butcherc97b6972015-12-27 23:48:17 +00009579#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009580static int ssl_preset_default_hashes[] = {
9581#if defined(MBEDTLS_SHA512_C)
9582 MBEDTLS_MD_SHA512,
9583 MBEDTLS_MD_SHA384,
9584#endif
9585#if defined(MBEDTLS_SHA256_C)
9586 MBEDTLS_MD_SHA256,
9587 MBEDTLS_MD_SHA224,
9588#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009589#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009590 MBEDTLS_MD_SHA1,
9591#endif
9592 MBEDTLS_MD_NONE
9593};
Simon Butcherc97b6972015-12-27 23:48:17 +00009594#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009595
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009596static int ssl_preset_suiteb_ciphersuites[] = {
9597 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9598 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9599 0
9600};
9601
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009602#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009603static int ssl_preset_suiteb_hashes[] = {
9604 MBEDTLS_MD_SHA256,
9605 MBEDTLS_MD_SHA384,
9606 MBEDTLS_MD_NONE
9607};
9608#endif
9609
9610#if defined(MBEDTLS_ECP_C)
9611static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9612 MBEDTLS_ECP_DP_SECP256R1,
9613 MBEDTLS_ECP_DP_SECP384R1,
9614 MBEDTLS_ECP_DP_NONE
9615};
9616#endif
9617
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009618/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009619 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009620 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009621int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009622 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009623{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009624#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009625 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009626#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009627
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009628 /* Use the functions here so that they are covered in tests,
9629 * but otherwise access member directly for efficiency */
9630 mbedtls_ssl_conf_endpoint( conf, endpoint );
9631 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009632
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009633 /*
9634 * Things that are common to all presets
9635 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009636#if defined(MBEDTLS_SSL_CLI_C)
9637 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9638 {
9639 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9640#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9641 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9642#endif
9643 }
9644#endif
9645
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009646#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009647 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009648#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009649
9650#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9651 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9652#endif
9653
9654#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9655 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9656#endif
9657
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009658#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9659 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9660#endif
9661
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009662#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009663 conf->f_cookie_write = ssl_cookie_write_dummy;
9664 conf->f_cookie_check = ssl_cookie_check_dummy;
9665#endif
9666
9667#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9668 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9669#endif
9670
Janos Follath088ce432017-04-10 12:42:31 +01009671#if defined(MBEDTLS_SSL_SRV_C)
9672 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9673#endif
9674
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009675#if defined(MBEDTLS_SSL_PROTO_DTLS)
9676 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9677 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9678#endif
9679
9680#if defined(MBEDTLS_SSL_RENEGOTIATION)
9681 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009682 memset( conf->renego_period, 0x00, 2 );
9683 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009684#endif
9685
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009686#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9687 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9688 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009689 const unsigned char dhm_p[] =
9690 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9691 const unsigned char dhm_g[] =
9692 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9693
Hanno Beckera90658f2017-10-04 15:29:08 +01009694 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9695 dhm_p, sizeof( dhm_p ),
9696 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009697 {
9698 return( ret );
9699 }
9700 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009701#endif
9702
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009703 /*
9704 * Preset-specific defaults
9705 */
9706 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009707 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009708 /*
9709 * NSA Suite B
9710 */
9711 case MBEDTLS_SSL_PRESET_SUITEB:
9712 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9713 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9714 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9715 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9716
9717 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9718 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9719 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9720 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9721 ssl_preset_suiteb_ciphersuites;
9722
9723#if defined(MBEDTLS_X509_CRT_PARSE_C)
9724 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009725#endif
9726
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009727#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009728 conf->sig_hashes = ssl_preset_suiteb_hashes;
9729#endif
9730
9731#if defined(MBEDTLS_ECP_C)
9732 conf->curve_list = ssl_preset_suiteb_curves;
9733#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009734 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009735
9736 /*
9737 * Default
9738 */
9739 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009740 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9741 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9742 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9743 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9744 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9745 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9746 MBEDTLS_SSL_MIN_MINOR_VERSION :
9747 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009748 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9749 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9750
9751#if defined(MBEDTLS_SSL_PROTO_DTLS)
9752 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9753 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9754#endif
9755
9756 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9757 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9758 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9759 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9760 mbedtls_ssl_list_ciphersuites();
9761
9762#if defined(MBEDTLS_X509_CRT_PARSE_C)
9763 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9764#endif
9765
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009766#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009767 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009768#endif
9769
9770#if defined(MBEDTLS_ECP_C)
9771 conf->curve_list = mbedtls_ecp_grp_id_list();
9772#endif
9773
9774#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9775 conf->dhm_min_bitlen = 1024;
9776#endif
9777 }
9778
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009779 return( 0 );
9780}
9781
9782/*
9783 * Free mbedtls_ssl_config
9784 */
9785void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9786{
9787#if defined(MBEDTLS_DHM_C)
9788 mbedtls_mpi_free( &conf->dhm_P );
9789 mbedtls_mpi_free( &conf->dhm_G );
9790#endif
9791
9792#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9793 if( conf->psk != NULL )
9794 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009795 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009796 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009797 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009798 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009799 }
9800
9801 if( conf->psk_identity != NULL )
9802 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009803 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009804 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009805 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009806 conf->psk_identity_len = 0;
9807 }
9808#endif
9809
9810#if defined(MBEDTLS_X509_CRT_PARSE_C)
9811 ssl_key_cert_free( conf->key_cert );
9812#endif
9813
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009814 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009815}
9816
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009817#if defined(MBEDTLS_PK_C) && \
9818 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009819/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824#if defined(MBEDTLS_RSA_C)
9825 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9826 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828#if defined(MBEDTLS_ECDSA_C)
9829 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9830 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009831#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009833}
9834
Hanno Becker7e5437a2017-04-28 17:15:26 +01009835unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9836{
9837 switch( type ) {
9838 case MBEDTLS_PK_RSA:
9839 return( MBEDTLS_SSL_SIG_RSA );
9840 case MBEDTLS_PK_ECDSA:
9841 case MBEDTLS_PK_ECKEY:
9842 return( MBEDTLS_SSL_SIG_ECDSA );
9843 default:
9844 return( MBEDTLS_SSL_SIG_ANON );
9845 }
9846}
9847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009849{
9850 switch( sig )
9851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#if defined(MBEDTLS_RSA_C)
9853 case MBEDTLS_SSL_SIG_RSA:
9854 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856#if defined(MBEDTLS_ECDSA_C)
9857 case MBEDTLS_SSL_SIG_ECDSA:
9858 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009859#endif
9860 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009862 }
9863}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009864#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009865
Hanno Becker7e5437a2017-04-28 17:15:26 +01009866#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9867 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9868
9869/* Find an entry in a signature-hash set matching a given hash algorithm. */
9870mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9871 mbedtls_pk_type_t sig_alg )
9872{
9873 switch( sig_alg )
9874 {
9875 case MBEDTLS_PK_RSA:
9876 return( set->rsa );
9877 case MBEDTLS_PK_ECDSA:
9878 return( set->ecdsa );
9879 default:
9880 return( MBEDTLS_MD_NONE );
9881 }
9882}
9883
9884/* Add a signature-hash-pair to a signature-hash set */
9885void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9886 mbedtls_pk_type_t sig_alg,
9887 mbedtls_md_type_t md_alg )
9888{
9889 switch( sig_alg )
9890 {
9891 case MBEDTLS_PK_RSA:
9892 if( set->rsa == MBEDTLS_MD_NONE )
9893 set->rsa = md_alg;
9894 break;
9895
9896 case MBEDTLS_PK_ECDSA:
9897 if( set->ecdsa == MBEDTLS_MD_NONE )
9898 set->ecdsa = md_alg;
9899 break;
9900
9901 default:
9902 break;
9903 }
9904}
9905
9906/* Allow exactly one hash algorithm for each signature. */
9907void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9908 mbedtls_md_type_t md_alg )
9909{
9910 set->rsa = md_alg;
9911 set->ecdsa = md_alg;
9912}
9913
9914#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9915 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9916
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009917/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009918 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009921{
9922 switch( hash )
9923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924#if defined(MBEDTLS_MD5_C)
9925 case MBEDTLS_SSL_HASH_MD5:
9926 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928#if defined(MBEDTLS_SHA1_C)
9929 case MBEDTLS_SSL_HASH_SHA1:
9930 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#if defined(MBEDTLS_SHA256_C)
9933 case MBEDTLS_SSL_HASH_SHA224:
9934 return( MBEDTLS_MD_SHA224 );
9935 case MBEDTLS_SSL_HASH_SHA256:
9936 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938#if defined(MBEDTLS_SHA512_C)
9939 case MBEDTLS_SSL_HASH_SHA384:
9940 return( MBEDTLS_MD_SHA384 );
9941 case MBEDTLS_SSL_HASH_SHA512:
9942 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009943#endif
9944 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009946 }
9947}
9948
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009949/*
9950 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9951 */
9952unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9953{
9954 switch( md )
9955 {
9956#if defined(MBEDTLS_MD5_C)
9957 case MBEDTLS_MD_MD5:
9958 return( MBEDTLS_SSL_HASH_MD5 );
9959#endif
9960#if defined(MBEDTLS_SHA1_C)
9961 case MBEDTLS_MD_SHA1:
9962 return( MBEDTLS_SSL_HASH_SHA1 );
9963#endif
9964#if defined(MBEDTLS_SHA256_C)
9965 case MBEDTLS_MD_SHA224:
9966 return( MBEDTLS_SSL_HASH_SHA224 );
9967 case MBEDTLS_MD_SHA256:
9968 return( MBEDTLS_SSL_HASH_SHA256 );
9969#endif
9970#if defined(MBEDTLS_SHA512_C)
9971 case MBEDTLS_MD_SHA384:
9972 return( MBEDTLS_SSL_HASH_SHA384 );
9973 case MBEDTLS_MD_SHA512:
9974 return( MBEDTLS_SSL_HASH_SHA512 );
9975#endif
9976 default:
9977 return( MBEDTLS_SSL_HASH_NONE );
9978 }
9979}
9980
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009981#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009982/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009983 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009984 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009985 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009986int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009989
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009990 if( ssl->conf->curve_list == NULL )
9991 return( -1 );
9992
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009993 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009994 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009995 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009996
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009997 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009998}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009999#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010000
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010001#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010002/*
10003 * Check if a hash proposed by the peer is in our list.
10004 * Return 0 if we're willing to use it, -1 otherwise.
10005 */
10006int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10007 mbedtls_md_type_t md )
10008{
10009 const int *cur;
10010
10011 if( ssl->conf->sig_hashes == NULL )
10012 return( -1 );
10013
10014 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10015 if( *cur == (int) md )
10016 return( 0 );
10017
10018 return( -1 );
10019}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010020#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022#if defined(MBEDTLS_X509_CRT_PARSE_C)
10023int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10024 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010025 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010026 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010027{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010028 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010030 int usage = 0;
10031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010033 const char *ext_oid;
10034 size_t ext_len;
10035#endif
10036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10038 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010039 ((void) cert);
10040 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010041 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010042#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10045 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010046 {
10047 /* Server part of the key exchange */
10048 switch( ciphersuite->key_exchange )
10049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 case MBEDTLS_KEY_EXCHANGE_RSA:
10051 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010052 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010053 break;
10054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10056 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10057 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10058 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010059 break;
10060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10062 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010063 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010064 break;
10065
10066 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 case MBEDTLS_KEY_EXCHANGE_NONE:
10068 case MBEDTLS_KEY_EXCHANGE_PSK:
10069 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10070 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010071 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010072 usage = 0;
10073 }
10074 }
10075 else
10076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10078 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010079 }
10080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010082 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010083 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010084 ret = -1;
10085 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010086#else
10087 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10091 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10094 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010095 }
10096 else
10097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10099 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010100 }
10101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010103 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010104 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010105 ret = -1;
10106 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010108
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010109 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010112
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010113/*
10114 * Convert version numbers to/from wire format
10115 * and, for DTLS, to/from TLS equivalent.
10116 *
10117 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010118 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010119 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10120 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010123 unsigned char ver[2] )
10124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125#if defined(MBEDTLS_SSL_PROTO_DTLS)
10126 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010129 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10130
10131 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10132 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10133 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010134 else
10135#else
10136 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010137#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010138 {
10139 ver[0] = (unsigned char) major;
10140 ver[1] = (unsigned char) minor;
10141 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010142}
10143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010145 const unsigned char ver[2] )
10146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010147#if defined(MBEDTLS_SSL_PROTO_DTLS)
10148 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010149 {
10150 *major = 255 - ver[0] + 2;
10151 *minor = 255 - ver[1] + 1;
10152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010154 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10155 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010156 else
10157#else
10158 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010159#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010160 {
10161 *major = ver[0];
10162 *minor = ver[1];
10163 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010164}
10165
Simon Butcher99000142016-10-13 17:21:01 +010010166int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10167{
10168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10169 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10170 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10171
10172 switch( md )
10173 {
10174#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10175#if defined(MBEDTLS_MD5_C)
10176 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010177 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010178#endif
10179#if defined(MBEDTLS_SHA1_C)
10180 case MBEDTLS_SSL_HASH_SHA1:
10181 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10182 break;
10183#endif
10184#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10185#if defined(MBEDTLS_SHA512_C)
10186 case MBEDTLS_SSL_HASH_SHA384:
10187 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10188 break;
10189#endif
10190#if defined(MBEDTLS_SHA256_C)
10191 case MBEDTLS_SSL_HASH_SHA256:
10192 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10193 break;
10194#endif
10195 default:
10196 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10197 }
10198
10199 return 0;
10200#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10201 (void) ssl;
10202 (void) md;
10203
10204 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10205#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10206}
10207
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010208#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10209 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10210int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10211 unsigned char *output,
10212 unsigned char *data, size_t data_len )
10213{
10214 int ret = 0;
10215 mbedtls_md5_context mbedtls_md5;
10216 mbedtls_sha1_context mbedtls_sha1;
10217
10218 mbedtls_md5_init( &mbedtls_md5 );
10219 mbedtls_sha1_init( &mbedtls_sha1 );
10220
10221 /*
10222 * digitally-signed struct {
10223 * opaque md5_hash[16];
10224 * opaque sha_hash[20];
10225 * };
10226 *
10227 * md5_hash
10228 * MD5(ClientHello.random + ServerHello.random
10229 * + ServerParams);
10230 * sha_hash
10231 * SHA(ClientHello.random + ServerHello.random
10232 * + ServerParams);
10233 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010234 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010235 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010237 goto exit;
10238 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010239 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010240 ssl->handshake->randbytes, 64 ) ) != 0 )
10241 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010243 goto exit;
10244 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010245 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010246 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010248 goto exit;
10249 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010250 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010251 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010253 goto exit;
10254 }
10255
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010256 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010257 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010259 goto exit;
10260 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010261 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010262 ssl->handshake->randbytes, 64 ) ) != 0 )
10263 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010265 goto exit;
10266 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010267 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010268 data_len ) ) != 0 )
10269 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010271 goto exit;
10272 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010273 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010274 output + 16 ) ) != 0 )
10275 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010277 goto exit;
10278 }
10279
10280exit:
10281 mbedtls_md5_free( &mbedtls_md5 );
10282 mbedtls_sha1_free( &mbedtls_sha1 );
10283
10284 if( ret != 0 )
10285 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10286 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10287
10288 return( ret );
10289
10290}
10291#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10292 MBEDTLS_SSL_PROTO_TLS1_1 */
10293
10294#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10295 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10296int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010297 unsigned char *hash, size_t *hashlen,
10298 unsigned char *data, size_t data_len,
10299 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010300{
10301 int ret = 0;
10302 mbedtls_md_context_t ctx;
10303 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010304 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010305
10306 mbedtls_md_init( &ctx );
10307
10308 /*
10309 * digitally-signed struct {
10310 * opaque client_random[32];
10311 * opaque server_random[32];
10312 * ServerDHParams params;
10313 * };
10314 */
10315 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10316 {
10317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10318 goto exit;
10319 }
10320 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10321 {
10322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10323 goto exit;
10324 }
10325 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10326 {
10327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10328 goto exit;
10329 }
10330 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10331 {
10332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10333 goto exit;
10334 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010335 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010336 {
10337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10338 goto exit;
10339 }
10340
10341exit:
10342 mbedtls_md_free( &ctx );
10343
10344 if( ret != 0 )
10345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10346 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10347
10348 return( ret );
10349}
10350#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10351 MBEDTLS_SSL_PROTO_TLS1_2 */
10352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353#endif /* MBEDTLS_SSL_TLS_C */