blob: b6e2c0edb2e5dfad912cbf47b8c9309c0b252a84 [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 Becker12555c62018-08-16 12:47:53 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *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 );
104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
105 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
112static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
113{
114 uint16_t mtu = ssl->conf->mtu;
115
116 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
117 return( (int) mtu );
118
119 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
120}
121
Hanno Becker67bc7c32018-08-06 11:33:50 +0100122static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
123{
124 size_t const bytes_written = ssl->out_left;
125 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
126
127 /* Double-check that the write-index hasn't gone
128 * past what we can transmit in a single datagram. */
129 if( bytes_written > (size_t) mtu )
130 {
131 /* Should never happen... */
132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
133 }
134
135 return( (int) ( mtu - bytes_written ) );
136}
137
138static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
139{
140 int ret;
141 size_t remaining, expansion;
142 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
143
144#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
145 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
146
147 if( max_len > mfl )
148 max_len = mfl;
149#endif
150
151 ret = ssl_get_remaining_space_in_datagram( ssl );
152 if( ret < 0 )
153 return( ret );
154 remaining = (size_t) ret;
155
156 ret = mbedtls_ssl_get_record_expansion( ssl );
157 if( ret < 0 )
158 return( ret );
159 expansion = (size_t) ret;
160
161 if( remaining <= expansion )
162 return( 0 );
163
164 remaining -= expansion;
165 if( remaining >= max_len )
166 remaining = max_len;
167
168 return( (int) remaining );
169}
170
Hanno Becker0271f962018-08-16 13:23:47 +0100171static void ssl_buffering_free( mbedtls_ssl_context *ssl );
172
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200173/*
174 * Double the retransmit timeout value, within the allowed range,
175 * returning -1 if the maximum value has already been reached.
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200178{
179 uint32_t new_timeout;
180
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200181 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200182 return( -1 );
183
184 new_timeout = 2 * ssl->handshake->retransmit_timeout;
185
186 /* Avoid arithmetic overflow and range overflow */
187 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200188 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200189 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200190 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200191 }
192
193 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200195 ssl->handshake->retransmit_timeout ) );
196
197 return( 0 );
198}
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200201{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200202 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200204 ssl->handshake->retransmit_timeout ) );
205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200209/*
210 * Convert max_fragment_length codes to length.
211 * RFC 6066 says:
212 * enum{
213 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
214 * } MaxFragmentLength;
215 * and we add 0 -> extension unused
216 */
Angus Grattond8213d02016-05-25 20:56:48 +1000217static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200218{
Angus Grattond8213d02016-05-25 20:56:48 +1000219 switch( mfl )
220 {
221 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
222 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
223 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
224 return 512;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
226 return 1024;
227 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
228 return 2048;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
230 return 4096;
231 default:
232 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
233 }
234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200236
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200237#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_ssl_session_free( dst );
241 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200244 if( src->peer_cert != NULL )
245 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200246 int ret;
247
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200248 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200249 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200250 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200255 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
259 return( ret );
260 }
261 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200264#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265 if( src->ticket != NULL )
266 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200267 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200268 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200269 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200270
271 memcpy( dst->ticket, src->ticket, src->ticket_len );
272 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200273#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200274
275 return( 0 );
276}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200277#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
280int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200281 const unsigned char *key_enc, const unsigned char *key_dec,
282 size_t keylen,
283 const unsigned char *iv_enc, const unsigned char *iv_dec,
284 size_t ivlen,
285 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200286 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
288int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
289int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
290int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
291int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
292#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294/*
295 * Key material generation
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200298static int ssl3_prf( const unsigned char *secret, size_t slen,
299 const char *label,
300 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000301 unsigned char *dstbuf, size_t dlen )
302{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100303 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000304 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200305 mbedtls_md5_context md5;
306 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000307 unsigned char padding[16];
308 unsigned char sha1sum[20];
309 ((void)label);
310
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200311 mbedtls_md5_init( &md5 );
312 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200313
Paul Bakker5f70b252012-09-13 14:23:06 +0000314 /*
315 * SSLv3:
316 * block =
317 * MD5( secret + SHA1( 'A' + secret + random ) ) +
318 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
319 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
320 * ...
321 */
322 for( i = 0; i < dlen / 16; i++ )
323 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200324 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000325
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100326 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100327 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100328 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100329 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100330 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100331 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100332 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100333 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100334 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100335 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000336
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100337 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100338 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100340 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100343 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100344 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000345 }
346
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100347exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200348 mbedtls_md5_free( &md5 );
349 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000350
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500351 mbedtls_platform_zeroize( padding, sizeof( padding ) );
352 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000353
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100354 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int tls1_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000362 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000363{
Paul Bakker23986e52011-04-24 08:57:21 +0000364 size_t nb, hs;
365 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200366 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 unsigned char tmp[128];
368 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 const mbedtls_md_info_t *md_info;
370 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100371 int ret;
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378 hs = ( slen + 1 ) / 2;
379 S1 = secret;
380 S2 = secret + slen - hs;
381
382 nb = strlen( label );
383 memcpy( tmp + 20, label, nb );
384 memcpy( tmp + 20 + nb, random, rlen );
385 nb += rlen;
386
387 /*
388 * First compute P_md5(secret,label+random)[0..dlen]
389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100394 return( ret );
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
397 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
398 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 for( i = 0; i < dlen; i += 16 )
401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_md_hmac_reset ( &md_ctx );
403 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
404 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_md_hmac_reset ( &md_ctx );
407 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
408 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
411
412 for( j = 0; j < k; j++ )
413 dstbuf[i + j] = h_i[j];
414 }
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100417
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 /*
419 * XOR out with P_sha1(secret,label+random)[0..dlen]
420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100425 return( ret );
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
428 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
429 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431 for( i = 0; i < dlen; i += 20 )
432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_md_hmac_reset ( &md_ctx );
434 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
435 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_md_hmac_reset ( &md_ctx );
438 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
439 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
442
443 for( j = 0; j < k; j++ )
444 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
445 }
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100448
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500449 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
450 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452 return( 0 );
453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
457static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000462{
463 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100464 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000465 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
467 const mbedtls_md_info_t *md_info;
468 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100469 int ret;
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100477
478 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000480
481 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100482 memcpy( tmp + md_len, label, nb );
483 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000484 nb += rlen;
485
486 /*
487 * Compute P_<hash>(secret, label + random)[0..dlen]
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100490 return( ret );
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
493 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
494 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100495
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100496 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
500 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_hmac_reset ( &md_ctx );
503 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
504 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000505
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100506 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000507
508 for( j = 0; j < k; j++ )
509 dstbuf[i + j] = h_i[j];
510 }
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100513
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500514 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
515 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000516
517 return( 0 );
518}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100521static int tls_prf_sha256( const unsigned char *secret, size_t slen,
522 const char *label,
523 const unsigned char *random, size_t rlen,
524 unsigned char *dstbuf, size_t dlen )
525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100527 label, random, rlen, dstbuf, dlen ) );
528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200532static int tls_prf_sha384( const unsigned char *secret, size_t slen,
533 const char *label,
534 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000535 unsigned char *dstbuf, size_t dlen )
536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100538 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#endif /* MBEDTLS_SHA512_C */
541#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
546 defined(MBEDTLS_SSL_PROTO_TLS1_1)
547static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200548#endif
Paul Bakker380da532012-04-18 16:10:25 +0000549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_SSL_PROTO_SSL3)
551static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
552static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200553#endif
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
556static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
557static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200558#endif
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
561#if defined(MBEDTLS_SHA256_C)
562static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
563static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
564static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200565#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SHA512_C)
568static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
569static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
570static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200576 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 unsigned char keyblk[256];
579 unsigned char *key1;
580 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100581 unsigned char *mac_enc;
582 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000583 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200584 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_cipher_info_t *cipher_info;
586 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_ssl_session *session = ssl->session_negotiate;
589 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
590 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100595 if( cipher_info == NULL )
596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100598 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100600 }
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100603 if( md_info == NULL )
604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100606 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100608 }
609
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000611 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_SSL_PROTO_SSL3)
614 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000615 {
Paul Bakker48916f92012-09-16 19:57:18 +0000616 handshake->tls_prf = ssl3_prf;
617 handshake->calc_verify = ssl_calc_verify_ssl;
618 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000619 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200620 else
621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
623 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000624 {
Paul Bakker48916f92012-09-16 19:57:18 +0000625 handshake->tls_prf = tls1_prf;
626 handshake->calc_verify = ssl_calc_verify_tls;
627 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000628 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200629 else
630#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
632#if defined(MBEDTLS_SHA512_C)
633 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
634 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000635 {
Paul Bakker48916f92012-09-16 19:57:18 +0000636 handshake->tls_prf = tls_prf_sha384;
637 handshake->calc_verify = ssl_calc_verify_tls_sha384;
638 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000639 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000640 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200641#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#if defined(MBEDTLS_SHA256_C)
643 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000644 {
Paul Bakker48916f92012-09-16 19:57:18 +0000645 handshake->tls_prf = tls_prf_sha256;
646 handshake->calc_verify = ssl_calc_verify_tls_sha256;
647 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000648 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200649 else
650#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200655 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656
657 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 * SSLv3:
659 * master =
660 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
661 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
662 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200663 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200664 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 * master = PRF( premaster, "master secret", randbytes )[0..47]
666 */
Paul Bakker0a597072012-09-25 21:55:46 +0000667 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000670 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
673 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200674 {
675 unsigned char session_hash[48];
676 size_t hash_len;
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200679
680 ssl->handshake->calc_verify( ssl, session_hash );
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
683 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200686 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200688 {
689 hash_len = 48;
690 }
691 else
692#endif
693 hash_len = 32;
694 }
695 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200697 hash_len = 36;
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200700
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100701 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
702 "extended master secret",
703 session_hash, hash_len,
704 session->master, 48 );
705 if( ret != 0 )
706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100708 return( ret );
709 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200710
711 }
712 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200713#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100714 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
715 "master secret",
716 handshake->randbytes, 64,
717 session->master, 48 );
718 if( ret != 0 )
719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100721 return( ret );
722 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200723
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500724 mbedtls_platform_zeroize( handshake->premaster,
725 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 }
727 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
730 /*
731 * Swap the client and server random values.
732 */
Paul Bakker48916f92012-09-16 19:57:18 +0000733 memcpy( tmp, handshake->randbytes, 64 );
734 memcpy( handshake->randbytes, tmp + 32, 32 );
735 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500736 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
738 /*
739 * SSLv3:
740 * key block =
741 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
742 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
743 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
744 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
745 * ...
746 *
747 * TLSv1:
748 * key block = PRF( master, "key expansion", randbytes )
749 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100750 ret = handshake->tls_prf( session->master, 48, "key expansion",
751 handshake->randbytes, 64, keyblk, 256 );
752 if( ret != 0 )
753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100755 return( ret );
756 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
759 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
760 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
761 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
762 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500764 mbedtls_platform_zeroize( handshake->randbytes,
765 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
767 /*
768 * Determine the appropriate key, IV and MAC length.
769 */
Paul Bakker68884e32013-01-07 18:20:04 +0100770
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200771 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200774 cipher_info->mode == MBEDTLS_MODE_CCM ||
775 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200777 size_t taglen, explicit_ivlen;
778
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200779 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000780 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200781
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200782 /* All modes haves 96-bit IVs;
783 * GCM and CCM has 4 implicit and 8 explicit bytes
784 * ChachaPoly has all 12 bytes implicit
785 */
Paul Bakker68884e32013-01-07 18:20:04 +0100786 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200787 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
788 transform->fixed_ivlen = 12;
789 else
790 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200791
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200792 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
793 taglen = transform->ciphersuite_info->flags &
794 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
795
796
797 /* Minimum length of encrypted record */
798 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
799 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100800 }
801 else
802 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200803 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
805 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200808 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100809 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200811 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000812 mac_key_len = mbedtls_md_get_size( md_info );
813 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200816 /*
817 * If HMAC is to be truncated, we shall keep the leftmost bytes,
818 * (rfc 6066 page 13 or rfc 2104 section 4),
819 * so we only need to adjust the length here.
820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000824
825#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
826 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000827 * HMAC implementation which also truncates the key
828 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000829 mac_key_len = transform->maclen;
830#endif
831 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200833
834 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100835 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200837 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200839 transform->minlen = transform->maclen;
840 else
Paul Bakker68884e32013-01-07 18:20:04 +0100841 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200842 /*
843 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100844 * 1. if EtM is in use: one block plus MAC
845 * otherwise: * first multiple of blocklen greater than maclen
846 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
849 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100850 {
851 transform->minlen = transform->maclen
852 + cipher_info->block_size;
853 }
854 else
855#endif
856 {
857 transform->minlen = transform->maclen
858 + cipher_info->block_size
859 - transform->maclen % cipher_info->block_size;
860 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
863 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
864 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200865 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100866 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
869 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
870 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200871 {
872 transform->minlen += transform->ivlen;
873 }
874 else
875#endif
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200879 }
Paul Bakker68884e32013-01-07 18:20:04 +0100880 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 }
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000884 transform->keylen, transform->minlen, transform->ivlen,
885 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
887 /*
888 * Finally setup the cipher contexts, IVs and MAC secrets.
889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200891 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000892 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000893 key1 = keyblk + mac_key_len * 2;
894 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
Paul Bakker68884e32013-01-07 18:20:04 +0100896 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000897 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000899 /*
900 * This is not used in TLS v1.1.
901 */
Paul Bakker48916f92012-09-16 19:57:18 +0000902 iv_copy_len = ( transform->fixed_ivlen ) ?
903 transform->fixed_ivlen : transform->ivlen;
904 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
905 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000906 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000907 }
908 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_SSL_CLI_C */
910#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000913 key1 = keyblk + mac_key_len * 2 + transform->keylen;
914 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Hanno Becker81c7b182017-11-09 18:39:33 +0000916 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100917 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000919 /*
920 * This is not used in TLS v1.1.
921 */
Paul Bakker48916f92012-09-16 19:57:18 +0000922 iv_copy_len = ( transform->fixed_ivlen ) ?
923 transform->fixed_ivlen : transform->ivlen;
924 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
925 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000926 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100928 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100933 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_SSL_PROTO_SSL3)
936 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100937 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000938 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100942 }
943
Hanno Becker81c7b182017-11-09 18:39:33 +0000944 memcpy( transform->mac_enc, mac_enc, mac_key_len );
945 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100946 }
947 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_SSL_PROTO_SSL3 */
949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
950 defined(MBEDTLS_SSL_PROTO_TLS1_2)
951 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100952 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100953 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
954 For AEAD-based ciphersuites, there is nothing to do here. */
955 if( mac_key_len != 0 )
956 {
957 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
958 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
959 }
Paul Bakker68884e32013-01-07 18:20:04 +0100960 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200961 else
962#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200966 }
Paul Bakker68884e32013-01-07 18:20:04 +0100967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
969 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000970 {
971 int ret = 0;
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100976 transform->iv_enc, transform->iv_dec,
977 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100978 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
982 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000983 }
984 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000986
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200987#if defined(MBEDTLS_SSL_EXPORT_KEYS)
988 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100989 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200990 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
991 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000992 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100993 iv_copy_len );
994 }
995#endif
996
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200997 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200998 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001001 return( ret );
1002 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001003
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001004 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001005 cipher_info ) ) != 0 )
1006 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001008 return( ret );
1009 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001012 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001016 return( ret );
1017 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001020 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001024 return( ret );
1025 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_CIPHER_MODE_CBC)
1028 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1031 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001034 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001035 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1038 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001041 return( ret );
1042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001046 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001049 // Initialize compression
1050 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001052 {
Paul Bakker16770332013-10-11 09:59:44 +02001053 if( ssl->compress_buf == NULL )
1054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001056 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001057 if( ssl->compress_buf == NULL )
1058 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001060 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001061 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001062 }
1063 }
1064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001066
Paul Bakker48916f92012-09-16 19:57:18 +00001067 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1068 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001069
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001070 if( deflateInit( &transform->ctx_deflate,
1071 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001072 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1075 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076 }
1077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
1082 return( 0 );
1083}
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SSL_PROTO_SSL3)
1086void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001087{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001088 mbedtls_md5_context md5;
1089 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 unsigned char pad_1[48];
1091 unsigned char pad_2[48];
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001095 mbedtls_md5_init( &md5 );
1096 mbedtls_sha1_init( &sha1 );
1097
1098 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1099 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001100
Paul Bakker380da532012-04-18 16:10:25 +00001101 memset( pad_1, 0x36, 48 );
1102 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001104 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1105 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1106 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001108 mbedtls_md5_starts_ret( &md5 );
1109 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1110 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1111 mbedtls_md5_update_ret( &md5, hash, 16 );
1112 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001114 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1115 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1116 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001117
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001118 mbedtls_sha1_starts_ret( &sha1 );
1119 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1120 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1121 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1122 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001126
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001127 mbedtls_md5_free( &md5 );
1128 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001129
Paul Bakker380da532012-04-18 16:10:25 +00001130 return;
1131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1135void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001136{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001137 mbedtls_md5_context md5;
1138 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001141
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001142 mbedtls_md5_init( &md5 );
1143 mbedtls_sha1_init( &sha1 );
1144
1145 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1146 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001147
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001148 mbedtls_md5_finish_ret( &md5, hash );
1149 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001153
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001154 mbedtls_md5_free( &md5 );
1155 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001156
Paul Bakker380da532012-04-18 16:10:25 +00001157 return;
1158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1162#if defined(MBEDTLS_SHA256_C)
1163void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001164{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001165 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001166
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001167 mbedtls_sha256_init( &sha256 );
1168
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001170
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001171 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001172 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001176
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001177 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001178
Paul Bakker380da532012-04-18 16:10:25 +00001179 return;
1180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#if defined(MBEDTLS_SHA512_C)
1184void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001185{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001186 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001187
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001188 mbedtls_sha512_init( &sha512 );
1189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001191
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001192 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001193 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001198 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001199
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 return;
1201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#endif /* MBEDTLS_SHA512_C */
1203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1206int 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 +02001207{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001208 unsigned char *p = ssl->handshake->premaster;
1209 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001210 const unsigned char *psk = ssl->conf->psk;
1211 size_t psk_len = ssl->conf->psk_len;
1212
1213 /* If the psk callback was called, use its result */
1214 if( ssl->handshake->psk != NULL )
1215 {
1216 psk = ssl->handshake->psk;
1217 psk_len = ssl->handshake->psk_len;
1218 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001219
1220 /*
1221 * PMS = struct {
1222 * opaque other_secret<0..2^16-1>;
1223 * opaque psk<0..2^16-1>;
1224 * };
1225 * with "other_secret" depending on the particular key exchange
1226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1228 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001229 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001230 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001233 *(p++) = (unsigned char)( psk_len >> 8 );
1234 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001235
1236 if( end < p || (size_t)( end - p ) < psk_len )
1237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1238
1239 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001240 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001241 }
1242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1244#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1245 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001246 {
1247 /*
1248 * other_secret already set by the ClientKeyExchange message,
1249 * and is 48 bytes long
1250 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001251 if( end - p < 2 )
1252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1253
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001254 *p++ = 0;
1255 *p++ = 48;
1256 p += 48;
1257 }
1258 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1260#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1261 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001262 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001263 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001264 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001266 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001268 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001269 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001272 return( ret );
1273 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001274 *(p++) = (unsigned char)( len >> 8 );
1275 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001276 p += len;
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 }
1280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1282#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1283 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001284 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001285 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001286 size_t zlen;
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001289 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001290 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001293 return( ret );
1294 }
1295
1296 *(p++) = (unsigned char)( zlen >> 8 );
1297 *(p++) = (unsigned char)( zlen );
1298 p += zlen;
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001301 }
1302 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001307 }
1308
1309 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001310 if( end - p < 2 )
1311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001312
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001313 *(p++) = (unsigned char)( psk_len >> 8 );
1314 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001315
1316 if( end < p || (size_t)( end - p ) < psk_len )
1317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1318
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001319 memcpy( p, psk, psk_len );
1320 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001321
1322 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1323
1324 return( 0 );
1325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001329/*
1330 * SSLv3.0 MAC functions
1331 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001332#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001333static void ssl_mac( mbedtls_md_context_t *md_ctx,
1334 const unsigned char *secret,
1335 const unsigned char *buf, size_t len,
1336 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001337 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001338{
1339 unsigned char header[11];
1340 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001341 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1343 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001344
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001345 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001347 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001348 else
Paul Bakker68884e32013-01-07 18:20:04 +01001349 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
1351 memcpy( header, ctr, 8 );
1352 header[ 8] = (unsigned char) type;
1353 header[ 9] = (unsigned char)( len >> 8 );
1354 header[10] = (unsigned char)( len );
1355
Paul Bakker68884e32013-01-07 18:20:04 +01001356 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_md_starts( md_ctx );
1358 mbedtls_md_update( md_ctx, secret, md_size );
1359 mbedtls_md_update( md_ctx, padding, padlen );
1360 mbedtls_md_update( md_ctx, header, 11 );
1361 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001362 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Paul Bakker68884e32013-01-07 18:20:04 +01001364 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_md_starts( md_ctx );
1366 mbedtls_md_update( md_ctx, secret, md_size );
1367 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001368 mbedtls_md_update( md_ctx, out, md_size );
1369 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001370}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1374 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001375 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001376#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001377#endif
1378
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001379/* The function below is only used in the Lucky 13 counter-measure in
1380 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1381#if defined(SSL_SOME_MODES_USE_MAC) && \
1382 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1383 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1384 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1385/* This function makes sure every byte in the memory region is accessed
1386 * (in ascending addresses order) */
1387static void ssl_read_memory( unsigned char *p, size_t len )
1388{
1389 unsigned char acc = 0;
1390 volatile unsigned char force;
1391
1392 for( ; len != 0; p++, len-- )
1393 acc ^= *p;
1394
1395 force = acc;
1396 (void) force;
1397}
1398#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1399
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001400/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001406 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001410 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001414 }
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001419 ssl->out_msg, ssl->out_msglen );
1420
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001422 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001424#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 if( mode == MBEDTLS_MODE_STREAM ||
1426 ( mode == MBEDTLS_MODE_CBC
1427#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1428 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001429#endif
1430 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_PROTO_SSL3)
1433 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001434 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001435 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001436
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001437 ssl_mac( &ssl->transform_out->md_ctx_enc,
1438 ssl->transform_out->mac_enc,
1439 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001440 ssl->out_ctr, ssl->out_msgtype,
1441 mac );
1442
1443 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001444 }
1445 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1448 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1449 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001450 {
Hanno Becker992b6872017-11-09 18:57:39 +00001451 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1454 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1455 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1456 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001457 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001458 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001460
1461 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001462 }
1463 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001464#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001468 }
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 ssl->out_msg + ssl->out_msglen,
1472 ssl->transform_out->maclen );
1473
1474 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001475 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001476 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001477#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001479 /*
1480 * Encrypt
1481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1483 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001485 int ret;
1486 size_t olen = 0;
1487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 "including %d bytes of padding",
1490 ssl->out_msglen, 0 ) );
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001493 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001494 ssl->transform_out->ivlen,
1495 ssl->out_msg, ssl->out_msglen,
1496 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001499 return( ret );
1500 }
1501
1502 if( ssl->out_msglen != olen )
1503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 }
Paul Bakker68884e32013-01-07 18:20:04 +01001508 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001510#if defined(MBEDTLS_GCM_C) || \
1511 defined(MBEDTLS_CCM_C) || \
1512 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001514 mode == MBEDTLS_MODE_CCM ||
1515 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001516 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001517 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001518 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001519 unsigned char *enc_msg;
1520 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001521 unsigned char iv[12];
1522 mbedtls_ssl_transform *transform = ssl->transform_out;
1523 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001525 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001526
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001527 /*
1528 * Prepare additional authenticated data
1529 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001530 memcpy( add_data, ssl->out_ctr, 8 );
1531 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001533 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001534 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1535 add_data[12] = ssl->out_msglen & 0xFF;
1536
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001537 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001538
Paul Bakker68884e32013-01-07 18:20:04 +01001539 /*
1540 * Generate IV
1541 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001542 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1543 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001544 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001545 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1546 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1547 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1548
1549 }
1550 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1551 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001552 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001553 unsigned char i;
1554
1555 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1556
1557 for( i = 0; i < 8; i++ )
1558 iv[i+4] ^= ssl->out_ctr[i];
1559 }
1560 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001561 {
1562 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001565 }
1566
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001567 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1568 iv, transform->ivlen );
1569 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1570 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001571
Paul Bakker68884e32013-01-07 18:20:04 +01001572 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001573 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001574 */
1575 enc_msg = ssl->out_msg;
1576 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001577 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 "including 0 bytes of padding",
1581 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001582
Paul Bakker68884e32013-01-07 18:20:04 +01001583 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001584 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001585 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1587 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001588 add_data, 13,
1589 enc_msg, enc_msglen,
1590 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001591 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001594 return( ret );
1595 }
1596
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001597 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 }
1602
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001603 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001604 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1610#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001611 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001614 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001615 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001616 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001617
Paul Bakker48916f92012-09-16 19:57:18 +00001618 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1619 ssl->transform_out->ivlen;
1620 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 padlen = 0;
1622
1623 for( i = 0; i <= padlen; i++ )
1624 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1625
1626 ssl->out_msglen += padlen + 1;
1627
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001628 enc_msglen = ssl->out_msglen;
1629 enc_msg = ssl->out_msg;
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001632 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001633 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1634 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001637 {
1638 /*
1639 * Generate IV
1640 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001641 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001642 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001643 if( ret != 0 )
1644 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645
Paul Bakker92be97b2013-01-02 17:30:03 +01001646 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001647 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648
1649 /*
1650 * Fix pointer positions and message length with added IV
1651 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001652 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001654 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001659 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001660 ssl->out_msglen, ssl->transform_out->ivlen,
1661 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001664 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001665 ssl->transform_out->ivlen,
1666 enc_msg, enc_msglen,
1667 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001670 return( ret );
1671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001672
Paul Bakkercca5b812013-08-31 17:40:26 +02001673 if( enc_msglen != olen )
1674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001677 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1680 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001681 {
1682 /*
1683 * Save IV in SSL3 and TLS1
1684 */
1685 memcpy( ssl->transform_out->iv_enc,
1686 ssl->transform_out->cipher_ctx_enc.iv,
1687 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001689#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001692 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001693 {
1694 /*
1695 * MAC(MAC_write_key, seq_num +
1696 * TLSCipherText.type +
1697 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001698 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001699 * IV + // except for TLS 1.0
1700 * ENC(content + padding + padding_length));
1701 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001702 unsigned char pseudo_hdr[13];
1703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001705
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001706 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1707 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001708 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1709 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1714 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001717 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719
1720 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001721 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001722 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001725 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001727 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001731 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001733 /* Make extra sure authentication was performed, exactly once */
1734 if( auth_done != 1 )
1735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001738 }
1739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
1742 return( 0 );
1743}
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001748 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001749#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001750 size_t padlen = 0, correct = 1;
1751#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001759 }
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762
Paul Bakker48916f92012-09-16 19:57:18 +00001763 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001766 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 }
1769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1771 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001772 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001773 int ret;
1774 size_t olen = 0;
1775
Paul Bakker68884e32013-01-07 18:20:04 +01001776 padlen = 0;
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001779 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001780 ssl->transform_in->ivlen,
1781 ssl->in_msg, ssl->in_msglen,
1782 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001785 return( ret );
1786 }
1787
1788 if( ssl->in_msglen != olen )
1789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 }
Paul Bakker68884e32013-01-07 18:20:04 +01001794 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001796#if defined(MBEDTLS_GCM_C) || \
1797 defined(MBEDTLS_CCM_C) || \
1798 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001800 mode == MBEDTLS_MODE_CCM ||
1801 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001802 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001803 int ret;
1804 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001805 unsigned char *dec_msg;
1806 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001807 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001808 unsigned char iv[12];
1809 mbedtls_ssl_transform *transform = ssl->transform_in;
1810 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001812 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001813
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001814 /*
1815 * Compute and update sizes
1816 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001817 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001820 "+ taglen (%d)", ssl->in_msglen,
1821 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001823 }
1824 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1825
Paul Bakker68884e32013-01-07 18:20:04 +01001826 dec_msg = ssl->in_msg;
1827 dec_msg_result = ssl->in_msg;
1828 ssl->in_msglen = dec_msglen;
1829
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001830 /*
1831 * Prepare additional authenticated data
1832 */
Paul Bakker68884e32013-01-07 18:20:04 +01001833 memcpy( add_data, ssl->in_ctr, 8 );
1834 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001836 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001837 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1838 add_data[12] = ssl->in_msglen & 0xFF;
1839
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001840 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001841
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001842 /*
1843 * Prepare IV
1844 */
1845 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1846 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001847 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001848 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1849 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001850
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 }
1852 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1853 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001854 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 unsigned char i;
1856
1857 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1858
1859 for( i = 0; i < 8; i++ )
1860 iv[i+4] ^= ssl->in_ctr[i];
1861 }
1862 else
1863 {
1864 /* Reminder if we ever add an AEAD mode with a different size */
1865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1867 }
1868
1869 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001871
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001872 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001873 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001876 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001877 add_data, 13,
1878 dec_msg, dec_msglen,
1879 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001880 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1885 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001887 return( ret );
1888 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001889 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001890
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001891 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001895 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001896 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1899#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001900 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 {
Paul Bakker45829992013-01-03 14:52:21 +01001903 /*
1904 * Decrypt and check the padding
1905 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001906 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001907 unsigned char *dec_msg;
1908 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001909 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001910 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001911 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001912
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 /*
Paul Bakker45829992013-01-03 14:52:21 +01001914 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1917 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001918 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001919#endif
Paul Bakker45829992013-01-03 14:52:21 +01001920
1921 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1922 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001925 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1926 ssl->transform_in->ivlen,
1927 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001929 }
1930
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001931 dec_msglen = ssl->in_msglen;
1932 dec_msg = ssl->in_msg;
1933 dec_msg_result = ssl->in_msg;
1934
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001935 /*
1936 * Authenticate before decrypt if enabled
1937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1939 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001940 {
Hanno Becker992b6872017-11-09 18:57:39 +00001941 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001942 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001945
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001946 dec_msglen -= ssl->transform_in->maclen;
1947 ssl->in_msglen -= ssl->transform_in->maclen;
1948
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001949 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1950 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1951 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1952 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1957 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001958 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001959 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001963 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001964 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001965 ssl->transform_in->maclen );
1966
Hanno Becker992b6872017-11-09 18:57:39 +00001967 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1968 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001974 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977
1978 /*
1979 * Check length sanity
1980 */
1981 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986 }
1987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001989 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001990 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001993 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001994 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001995 dec_msglen -= ssl->transform_in->ivlen;
1996 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997
Paul Bakker48916f92012-09-16 19:57:18 +00001998 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001999 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002004 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002005 ssl->transform_in->ivlen,
2006 dec_msg, dec_msglen,
2007 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002010 return( ret );
2011 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002012
Paul Bakkercca5b812013-08-31 17:40:26 +02002013 if( dec_msglen != olen )
2014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002017 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2020 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002021 {
2022 /*
2023 * Save IV in SSL3 and TLS1
2024 */
2025 memcpy( ssl->transform_in->iv_dec,
2026 ssl->transform_in->cipher_ctx_dec.iv,
2027 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002029#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
2031 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002032
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002033 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002034 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_SSL_DEBUG_ALL)
2037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002038 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002039#endif
Paul Bakker45829992013-01-03 14:52:21 +01002040 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002041 correct = 0;
2042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_PROTO_SSL3)
2045 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
Paul Bakker48916f92012-09-16 19:57:18 +00002047 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_SSL_DEBUG_ALL)
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002052 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002053#endif
Paul Bakker45829992013-01-03 14:52:21 +01002054 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056 }
2057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2059#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2060 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2061 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 {
2063 /*
Paul Bakker45829992013-01-03 14:52:21 +01002064 * TLSv1+: always check the padding up to the first failure
2065 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002067 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002068 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002069 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002070
Paul Bakker956c9e02013-12-19 14:42:28 +01002071 /*
2072 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002073 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002074 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002075 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002076 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002077 *
2078 * In both cases we reset padding_idx to a safe value (0) to
2079 * prevent out-of-buffer reads.
2080 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002081 correct &= ( padlen <= ssl->in_msglen );
2082 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002083 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002084
2085 padding_idx *= correct;
2086
Angus Grattonb512bc12018-06-19 15:57:50 +10002087 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002088 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002089 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002090 pad_count += real_count *
2091 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2092 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002093
2094 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002097 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002099#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002100 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2104 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002108 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002109
2110 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002112 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002114 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002120#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002123#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
2125 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002126 * Authenticate if not done yet.
2127 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002129#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002130 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002131 {
Hanno Becker992b6872017-11-09 18:57:39 +00002132 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002133
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002134 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002136 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2137 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139#if defined(MBEDTLS_SSL_PROTO_SSL3)
2140 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002141 {
2142 ssl_mac( &ssl->transform_in->md_ctx_dec,
2143 ssl->transform_in->mac_dec,
2144 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002145 ssl->in_ctr, ssl->in_msgtype,
2146 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002147 }
2148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2150#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2151 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2152 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002153 {
2154 /*
2155 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002156 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002157 *
2158 * Known timing attacks:
2159 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2160 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002161 * To compensate for different timings for the MAC calculation
2162 * depending on how much padding was removed (which is determined
2163 * by padlen), process extra_run more blocks through the hash
2164 * function.
2165 *
2166 * The formula in the paper is
2167 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2168 * where L1 is the size of the header plus the decrypted message
2169 * plus CBC padding and L2 is the size of the header plus the
2170 * decrypted message. This is for an underlying hash function
2171 * with 64-byte blocks.
2172 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2173 * correctly. We round down instead of up, so -56 is the correct
2174 * value for our calculations instead of -55.
2175 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002176 * Repeat the formula rather than defining a block_size variable.
2177 * This avoids requiring division by a variable at runtime
2178 * (which would be marginally less efficient and would require
2179 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002180 */
2181 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002182
2183 /*
2184 * The next two sizes are the minimum and maximum values of
2185 * in_msglen over all padlen values.
2186 *
2187 * They're independent of padlen, since we previously did
2188 * in_msglen -= padlen.
2189 *
2190 * Note that max_len + maclen is never more than the buffer
2191 * length, as we previously did in_msglen -= maclen too.
2192 */
2193 const size_t max_len = ssl->in_msglen + padlen;
2194 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2195
Gilles Peskine20b44082018-05-29 14:06:49 +02002196 switch( ssl->transform_in->ciphersuite_info->mac )
2197 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002198#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2199 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002200 case MBEDTLS_MD_MD5:
2201 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002202 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 /* 8 bytes of message size, 64-byte compression blocks */
2204 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2205 ( 13 + ssl->in_msglen + 8 ) / 64;
2206 break;
2207#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002208#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002209 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002210 /* 16 bytes of message size, 128-byte compression blocks */
2211 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2212 ( 13 + ssl->in_msglen + 16 ) / 128;
2213 break;
2214#endif
2215 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2218 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002219
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2223 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2224 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2225 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002226 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002227 /* Make sure we access everything even when padlen > 0. This
2228 * makes the synchronisation requirements for just-in-time
2229 * Prime+Probe attacks much tighter and hopefully impractical. */
2230 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002231 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002232
2233 /* Call mbedtls_md_process at least once due to cache attacks
2234 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002235 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002239
2240 /* Make sure we access all the memory that could contain the MAC,
2241 * before we check it in the next code block. This makes the
2242 * synchronisation requirements for just-in-time Prime+Probe
2243 * attacks much tighter and hopefully impractical. */
2244 ssl_read_memory( ssl->in_msg + min_len,
2245 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 }
2247 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2249 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002255#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002256 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2257 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2258 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Hanno Becker992b6872017-11-09 18:57:39 +00002261 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2262 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#if defined(MBEDTLS_SSL_DEBUG_ALL)
2265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002266#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002267 correct = 0;
2268 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002271 /*
2272 * Finally check the correct flag
2273 */
2274 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002276 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002277#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002278
2279 /* Make extra sure authentication was performed, exactly once */
2280 if( auth_done != 1 )
2281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
2286 if( ssl->in_msglen == 0 )
2287 {
Angus Gratton34817922018-06-19 15:58:22 +10002288#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2289 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2290 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2291 {
2292 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2294 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2295 }
2296#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2297
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 ssl->nb_zero++;
2299
2300 /*
2301 * Three or more empty messages may be a DoS attack
2302 * (excessive CPU consumption).
2303 */
2304 if( ssl->nb_zero > 3 )
2305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
2310 }
2311 else
2312 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002316 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002317 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002318 }
2319 else
2320#endif
2321 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002323 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2324 if( ++ssl->in_ctr[i - 1] != 0 )
2325 break;
2326
2327 /* The loop goes to its end iff the counter is wrapping */
2328 if( i == ssl_ep_len( ssl ) )
2329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2331 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002332 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002333 }
2334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
2337 return( 0 );
2338}
2339
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002340#undef MAC_NONE
2341#undef MAC_PLAINTEXT
2342#undef MAC_CIPHERTEXT
2343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002345/*
2346 * Compression/decompression functions
2347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002349{
2350 int ret;
2351 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002352 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002353 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002354 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002357
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002358 if( len_pre == 0 )
2359 return( 0 );
2360
Paul Bakker2770fbd2012-07-03 13:30:23 +00002361 memcpy( msg_pre, ssl->out_msg, len_pre );
2362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364 ssl->out_msglen ) );
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002367 ssl->out_msg, ssl->out_msglen );
2368
Paul Bakker48916f92012-09-16 19:57:18 +00002369 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2370 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2371 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002372 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002373
Paul Bakker48916f92012-09-16 19:57:18 +00002374 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002375 if( ret != Z_OK )
2376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2378 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002379 }
2380
Angus Grattond8213d02016-05-25 20:56:48 +10002381 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002382 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002385 ssl->out_msglen ) );
2386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 ssl->out_msg, ssl->out_msglen );
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391
2392 return( 0 );
2393}
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396{
2397 int ret;
2398 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002399 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002400 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002401 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002405 if( len_pre == 0 )
2406 return( 0 );
2407
Paul Bakker2770fbd2012-07-03 13:30:23 +00002408 memcpy( msg_pre, ssl->in_msg, len_pre );
2409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411 ssl->in_msglen ) );
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002414 ssl->in_msg, ssl->in_msglen );
2415
Paul Bakker48916f92012-09-16 19:57:18 +00002416 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2417 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2418 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002419 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002420 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421
Paul Bakker48916f92012-09-16 19:57:18 +00002422 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002423 if( ret != Z_OK )
2424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2426 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 }
2428
Angus Grattond8213d02016-05-25 20:56:48 +10002429 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002430 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002433 ssl->in_msglen ) );
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436 ssl->in_msg, ssl->in_msglen );
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002439
2440 return( 0 );
2441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2445static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_DTLS)
2448static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002449{
2450 /* If renegotiation is not enforced, retransmit until we would reach max
2451 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002452 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002453 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002454 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002455 unsigned char doublings = 1;
2456
2457 while( ratio != 0 )
2458 {
2459 ++doublings;
2460 ratio >>= 1;
2461 }
2462
2463 if( ++ssl->renego_records_seen > doublings )
2464 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002466 return( 0 );
2467 }
2468 }
2469
2470 return( ssl_write_hello_request( ssl ) );
2471}
2472#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002474
Paul Bakker5121ce52009-01-03 21:22:43 +00002475/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002476 * Fill the input message buffer by appending data to it.
2477 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002478 *
2479 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2480 * available (from this read and/or a previous one). Otherwise, an error code
2481 * is returned (possibly EOF or WANT_READ).
2482 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002483 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2484 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2485 * since we always read a whole datagram at once.
2486 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002487 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002488 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002491{
Paul Bakker23986e52011-04-24 08:57:21 +00002492 int ret;
2493 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002497 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002500 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002502 }
2503
Angus Grattond8213d02016-05-25 20:56:48 +10002504 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002508 }
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002513 uint32_t timeout;
2514
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002515 /* Just to be sure */
2516 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2517 {
2518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2519 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2521 }
2522
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002523 /*
2524 * The point is, we need to always read a full datagram at once, so we
2525 * sometimes read more then requested, and handle the additional data.
2526 * It could be the rest of the current record (while fetching the
2527 * header) and/or some other records in the same datagram.
2528 */
2529
2530 /*
2531 * Move to the next record in the already read datagram if applicable
2532 */
2533 if( ssl->next_record_offset != 0 )
2534 {
2535 if( ssl->in_left < ssl->next_record_offset )
2536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002539 }
2540
2541 ssl->in_left -= ssl->next_record_offset;
2542
2543 if( ssl->in_left != 0 )
2544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002546 ssl->next_record_offset ) );
2547 memmove( ssl->in_hdr,
2548 ssl->in_hdr + ssl->next_record_offset,
2549 ssl->in_left );
2550 }
2551
2552 ssl->next_record_offset = 0;
2553 }
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002557
2558 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002559 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002560 */
2561 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002564 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002565 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002566
2567 /*
2568 * A record can't be split accross datagrams. If we need to read but
2569 * are not at the beginning of a new record, the caller did something
2570 * wrong.
2571 */
2572 if( ssl->in_left != 0 )
2573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002576 }
2577
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002578 /*
2579 * Don't even try to read if time's out already.
2580 * This avoids by-passing the timer when repeatedly receiving messages
2581 * that will end up being dropped.
2582 */
2583 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002584 {
2585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002586 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002587 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002588 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002589 {
Angus Grattond8213d02016-05-25 20:56:48 +10002590 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002593 timeout = ssl->handshake->retransmit_timeout;
2594 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002595 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002598
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002599 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002600 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2601 timeout );
2602 else
2603 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002606
2607 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609 }
2610
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002611 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002614 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002617 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002618 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002621 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002622 }
2623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002627 return( ret );
2628 }
2629
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002630 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002631 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002633 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002635 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002636 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002639 return( ret );
2640 }
2641
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002642 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002645 }
2646
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 if( ret < 0 )
2648 return( ret );
2649
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002650 ssl->in_left = ret;
2651 }
2652 else
2653#endif
2654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002656 ssl->in_left, nb_want ) );
2657
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002658 while( ssl->in_left < nb_want )
2659 {
2660 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002661
2662 if( ssl_check_timer( ssl ) != 0 )
2663 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2664 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002665 {
2666 if( ssl->f_recv_timeout != NULL )
2667 {
2668 ret = ssl->f_recv_timeout( ssl->p_bio,
2669 ssl->in_hdr + ssl->in_left, len,
2670 ssl->conf->read_timeout );
2671 }
2672 else
2673 {
2674 ret = ssl->f_recv( ssl->p_bio,
2675 ssl->in_hdr + ssl->in_left, len );
2676 }
2677 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002680 ssl->in_left, nb_want ) );
2681 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002682
2683 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685
2686 if( ret < 0 )
2687 return( ret );
2688
mohammad160352aecb92018-03-28 23:41:40 -07002689 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002690 {
Darryl Green11999bb2018-03-13 15:22:58 +00002691 MBEDTLS_SSL_DEBUG_MSG( 1,
2692 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002693 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2695 }
2696
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002697 ssl->in_left += ret;
2698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
2703 return( 0 );
2704}
2705
2706/*
2707 * Flush any data not yet written
2708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002710{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002711 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002712 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002716 if( ssl->f_send == NULL )
2717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002719 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002721 }
2722
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002723 /* Avoid incrementing counter if data is flushed */
2724 if( ssl->out_left == 0 )
2725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002727 return( 0 );
2728 }
2729
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 while( ssl->out_left > 0 )
2731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2733 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Hanno Becker2b1e3542018-08-06 11:19:13 +01002735 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002736 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
2740 if( ret <= 0 )
2741 return( ret );
2742
mohammad160352aecb92018-03-28 23:41:40 -07002743 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002744 {
Darryl Green11999bb2018-03-13 15:22:58 +00002745 MBEDTLS_SSL_DEBUG_MSG( 1,
2746 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002747 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002748 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2749 }
2750
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 ssl->out_left -= ret;
2752 }
2753
Hanno Becker2b1e3542018-08-06 11:19:13 +01002754#if defined(MBEDTLS_SSL_PROTO_DTLS)
2755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2756 {
2757 ssl->out_hdr = ssl->out_buf;
2758 }
2759 else
2760#endif
2761 {
2762 ssl->out_hdr = ssl->out_buf + 8;
2763 }
2764 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
2768 return( 0 );
2769}
2770
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002771/*
2772 * Functions to handle the DTLS retransmission state machine
2773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002775/*
2776 * Append current handshake message to current outgoing flight
2777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2782 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2783 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002784
2785 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002786 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002787 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002790 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002791 }
2792
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002793 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002797 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002798 }
2799
2800 /* Copy current handshake message with headers */
2801 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2802 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002803 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002804 msg->next = NULL;
2805
2806 /* Append to the current flight */
2807 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002808 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002809 else
2810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812 while( cur->next != NULL )
2813 cur = cur->next;
2814 cur->next = msg;
2815 }
2816
Hanno Becker3b235902018-08-06 09:54:53 +01002817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002818 return( 0 );
2819}
2820
2821/*
2822 * Free the current flight of handshake messages
2823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 mbedtls_ssl_flight_item *cur = flight;
2827 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828
2829 while( cur != NULL )
2830 {
2831 next = cur->next;
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_free( cur->p );
2834 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002835
2836 cur = next;
2837 }
2838}
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2841static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002842#endif
2843
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002844/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002845 * Swap transform_out and out_ctr with the alternative ones
2846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002850 unsigned char tmp_out_ctr[8];
2851
2852 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002855 return;
2856 }
2857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002859
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002860 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002861 tmp_transform = ssl->transform_out;
2862 ssl->transform_out = ssl->handshake->alt_transform_out;
2863 ssl->handshake->alt_transform_out = tmp_transform;
2864
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002865 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002866 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2867 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002869
2870 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002871 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2874 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2879 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002880 }
2881 }
2882#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002883}
2884
2885/*
2886 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002887 */
2888int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2889{
2890 int ret = 0;
2891
2892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2893
2894 ret = mbedtls_ssl_flight_transmit( ssl );
2895
2896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2897
2898 return( ret );
2899}
2900
2901/*
2902 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002903 *
2904 * Need to remember the current message in case flush_output returns
2905 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002906 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002907 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002908int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002909{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002910 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002914 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002916
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002917 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002918 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002919 ssl_swap_epochs( ssl );
2920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002922 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002923
2924 while( ssl->handshake->cur_msg != NULL )
2925 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002926 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002927 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002928
Hanno Beckere1dcb032018-08-17 16:47:58 +01002929 int const is_finished =
2930 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2931 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2932
Hanno Becker04da1892018-08-14 13:22:10 +01002933 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2934 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2935
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002936 /* Swap epochs before sending Finished: we can't do it after
2937 * sending ChangeCipherSpec, in case write returns WANT_READ.
2938 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002939 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002940 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002942 ssl_swap_epochs( ssl );
2943 }
2944
Hanno Becker67bc7c32018-08-06 11:33:50 +01002945 ret = ssl_get_remaining_payload_in_datagram( ssl );
2946 if( ret < 0 )
2947 return( ret );
2948 max_frag_len = (size_t) ret;
2949
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002950 /* CCS is copied as is, while HS messages may need fragmentation */
2951 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2952 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002953 if( max_frag_len == 0 )
2954 {
2955 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2956 return( ret );
2957
2958 continue;
2959 }
2960
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002961 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002962 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002963 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002964
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002965 /* Update position inside current message */
2966 ssl->handshake->cur_msg_p += cur->len;
2967 }
2968 else
2969 {
2970 const unsigned char * const p = ssl->handshake->cur_msg_p;
2971 const size_t hs_len = cur->len - 12;
2972 const size_t frag_off = p - ( cur->p + 12 );
2973 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002974 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002975
Hanno Beckere1dcb032018-08-17 16:47:58 +01002976 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002977 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002978 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002979 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002980
2981 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2982 return( ret );
2983
2984 continue;
2985 }
2986 max_hs_frag_len = max_frag_len - 12;
2987
2988 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2989 max_hs_frag_len : rem_len;
2990
2991 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002992 {
2993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002994 (unsigned) cur_hs_frag_len,
2995 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002996 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002997
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002998 /* Messages are stored with handshake headers as if not fragmented,
2999 * copy beginning of headers then fill fragmentation fields.
3000 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3001 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003002
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003003 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3004 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3005 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3006
Hanno Becker67bc7c32018-08-06 11:33:50 +01003007 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3008 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3009 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003010
3011 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3012
Hanno Becker67bc7c32018-08-06 11:33:50 +01003013 /* Copy the handshame message content and set records fields */
3014 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3015 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003016 ssl->out_msgtype = cur->type;
3017
3018 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003019 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003020 }
3021
3022 /* If done with the current message move to the next one if any */
3023 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3024 {
3025 if( cur->next != NULL )
3026 {
3027 ssl->handshake->cur_msg = cur->next;
3028 ssl->handshake->cur_msg_p = cur->next->p + 12;
3029 }
3030 else
3031 {
3032 ssl->handshake->cur_msg = NULL;
3033 ssl->handshake->cur_msg_p = NULL;
3034 }
3035 }
3036
3037 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003038 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003041 return( ret );
3042 }
3043 }
3044
Hanno Becker67bc7c32018-08-06 11:33:50 +01003045 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3046 return( ret );
3047
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003048 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3050 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003051 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003054 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3055 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003056
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003058
3059 return( 0 );
3060}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003061
3062/*
3063 * To be called when the last message of an incoming flight is received.
3064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003066{
3067 /* We won't need to resend that one any more */
3068 ssl_flight_free( ssl->handshake->flight );
3069 ssl->handshake->flight = NULL;
3070 ssl->handshake->cur_msg = NULL;
3071
3072 /* The next incoming flight will start with this msg_seq */
3073 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3074
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003075 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003076 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003077
Hanno Becker0271f962018-08-16 13:23:47 +01003078 /* Clear future message buffering structure. */
3079 ssl_buffering_free( ssl );
3080
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003081 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003082 ssl_set_timer( ssl, 0 );
3083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3085 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003088 }
3089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003091}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003092
3093/*
3094 * To be called when the last message of an outgoing flight is send.
3095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003097{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003098 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003099 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3102 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003105 }
3106 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003110
Paul Bakker5121ce52009-01-03 21:22:43 +00003111/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003112 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003114
3115/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003116 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003117 *
3118 * - fill in handshake headers
3119 * - update handshake checksum
3120 * - DTLS: save message for resending
3121 * - then pass to the record layer
3122 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003123 * DTLS: except for HelloRequest, messages are only queued, and will only be
3124 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003125 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003126 * Inputs:
3127 * - ssl->out_msglen: 4 + actual handshake message len
3128 * (4 is the size of handshake headers for TLS)
3129 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3130 * - ssl->out_msg + 4: the handshake message body
3131 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003132 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003133 * - ssl->out_msglen: the length of the record contents
3134 * (including handshake headers but excluding record headers)
3135 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003137int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003138{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003139 int ret;
3140 const size_t hs_len = ssl->out_msglen - 4;
3141 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3144
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003145 /*
3146 * Sanity checks
3147 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003148 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3149 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3150 {
3151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003155 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3156 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3157 ssl->handshake == NULL )
3158 {
3159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3161 }
3162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003165 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003167 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003171#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003172
Hanno Beckerb50a2532018-08-06 11:52:54 +01003173 /* Double-check that we did not exceed the bounds
3174 * of the outgoing record buffer.
3175 * This should never fail as the various message
3176 * writing functions must obey the bounds of the
3177 * outgoing record buffer, but better be safe.
3178 *
3179 * Note: We deliberately do not check for the MTU or MFL here.
3180 */
3181 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3182 {
3183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3184 "size %u, maximum %u",
3185 (unsigned) ssl->out_msglen,
3186 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3188 }
3189
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003190 /*
3191 * Fill handshake headers
3192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003195 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3196 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3197 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003199 /*
3200 * DTLS has additional fields in the Handshake layer,
3201 * between the length field and the actual payload:
3202 * uint16 message_seq;
3203 * uint24 fragment_offset;
3204 * uint24 fragment_length;
3205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003208 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003209 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003210 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003211 {
3212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3213 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003214 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003215 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3217 }
3218
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003219 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003220 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003221
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003222 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003223 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003224 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003225 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3226 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3227 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003228 }
3229 else
3230 {
3231 ssl->out_msg[4] = 0;
3232 ssl->out_msg[5] = 0;
3233 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003234
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003235 /* Handshake hashes are computed without fragmentation,
3236 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003237 memset( ssl->out_msg + 6, 0x00, 3 );
3238 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003241
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003242 /* Update running hashes of hanshake messages seen */
3243 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3244 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 }
3246
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003247 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003249 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003250 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003251 {
3252 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003255 return( ret );
3256 }
3257 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003258 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003259#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003260 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003261 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003262 {
3263 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3264 return( ret );
3265 }
3266 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003267
3268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3269
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003270 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003271}
3272
3273/*
3274 * Record layer functions
3275 */
3276
3277/*
3278 * Write current record.
3279 *
3280 * Uses:
3281 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3282 * - ssl->out_msglen: length of the record content (excl headers)
3283 * - ssl->out_msg: record content
3284 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003285int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003286{
3287 int ret, done = 0;
3288 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003289 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003290
3291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003294 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003296 {
3297 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003300 return( ret );
3301 }
3302
3303 len = ssl->out_msglen;
3304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3308 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 ret = mbedtls_ssl_hw_record_write( ssl );
3313 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3316 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003317 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003318
3319 if( ret == 0 )
3320 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003323 if( !done )
3324 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003325 unsigned i;
3326 size_t protected_record_size;
3327
Paul Bakker05ef8352012-05-08 09:17:57 +00003328 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003330 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003331
Hanno Becker19859472018-08-06 09:40:20 +01003332 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003333 ssl->out_len[0] = (unsigned char)( len >> 8 );
3334 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003335
Paul Bakker48916f92012-09-16 19:57:18 +00003336 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003337 {
3338 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003341 return( ret );
3342 }
3343
3344 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003345 ssl->out_len[0] = (unsigned char)( len >> 8 );
3346 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003347 }
3348
Hanno Becker2b1e3542018-08-06 11:19:13 +01003349 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3350
3351#if defined(MBEDTLS_SSL_PROTO_DTLS)
3352 /* In case of DTLS, double-check that we don't exceed
3353 * the remaining space in the datagram. */
3354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3355 {
3356 ret = ssl_get_maximum_datagram_size( ssl );
3357 if( ret < 0 )
3358 return( ret );
3359
3360 if( protected_record_size > (size_t) ret )
3361 {
3362 /* Should never happen */
3363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3364 }
3365 }
3366#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003369 "version = [%d:%d], msglen = %d",
3370 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3371
Paul Bakker05ef8352012-05-08 09:17:57 +00003372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003374 ssl->out_hdr, protected_record_size );
3375
3376 ssl->out_left += protected_record_size;
3377 ssl->out_hdr += protected_record_size;
3378 ssl_update_out_pointers( ssl, ssl->transform_out );
3379
Hanno Becker04484622018-08-06 09:49:38 +01003380 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3381 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3382 break;
3383
3384 /* The loop goes to its end iff the counter is wrapping */
3385 if( i == ssl_ep_len( ssl ) )
3386 {
3387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3388 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 }
3391
Hanno Becker67bc7c32018-08-06 11:33:50 +01003392#if defined(MBEDTLS_SSL_PROTO_DTLS)
3393 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3394 {
3395 size_t remaining = ssl_get_remaining_payload_in_datagram( ssl );
3396 if( remaining == 0 )
3397 flush = SSL_FORCE_FLUSH;
3398 else
3399 {
3400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Stil %u bytes available in current datagram", (unsigned) remaining ) );
3401 }
3402 }
3403#endif /* MBEDTLS_SSL_PROTO_DTLS */
3404
3405 if( ( flush == SSL_FORCE_FLUSH ) &&
3406 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 return( ret );
3410 }
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003413
3414 return( 0 );
3415}
3416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003418
3419static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3420{
3421 if( ssl->in_msglen < ssl->in_hslen ||
3422 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3423 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3424 {
3425 return( 1 );
3426 }
3427 return( 0 );
3428}
Hanno Becker44650b72018-08-16 12:51:11 +01003429
3430static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3431{
3432 return( ( ssl->in_msg[9] << 16 ) |
3433 ( ssl->in_msg[10] << 8 ) |
3434 ssl->in_msg[11] );
3435}
3436
3437static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3438{
3439 return( ( ssl->in_msg[6] << 16 ) |
3440 ( ssl->in_msg[7] << 8 ) |
3441 ssl->in_msg[8] );
3442}
3443
3444static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3445{
3446 uint32_t msg_len, frag_off, frag_len;
3447
3448 msg_len = ssl_get_hs_total_len( ssl );
3449 frag_off = ssl_get_hs_frag_off( ssl );
3450 frag_len = ssl_get_hs_frag_len( ssl );
3451
3452 if( frag_off > msg_len )
3453 return( -1 );
3454
3455 if( frag_len > msg_len - frag_off )
3456 return( -1 );
3457
3458 if( frag_len + 12 > ssl->in_msglen )
3459 return( -1 );
3460
3461 return( 0 );
3462}
3463
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003464/*
3465 * Mark bits in bitmask (used for DTLS HS reassembly)
3466 */
3467static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3468{
3469 unsigned int start_bits, end_bits;
3470
3471 start_bits = 8 - ( offset % 8 );
3472 if( start_bits != 8 )
3473 {
3474 size_t first_byte_idx = offset / 8;
3475
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003476 /* Special case */
3477 if( len <= start_bits )
3478 {
3479 for( ; len != 0; len-- )
3480 mask[first_byte_idx] |= 1 << ( start_bits - len );
3481
3482 /* Avoid potential issues with offset or len becoming invalid */
3483 return;
3484 }
3485
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003486 offset += start_bits; /* Now offset % 8 == 0 */
3487 len -= start_bits;
3488
3489 for( ; start_bits != 0; start_bits-- )
3490 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3491 }
3492
3493 end_bits = len % 8;
3494 if( end_bits != 0 )
3495 {
3496 size_t last_byte_idx = ( offset + len ) / 8;
3497
3498 len -= end_bits; /* Now len % 8 == 0 */
3499
3500 for( ; end_bits != 0; end_bits-- )
3501 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3502 }
3503
3504 memset( mask + offset / 8, 0xFF, len / 8 );
3505}
3506
3507/*
3508 * Check that bitmask is full
3509 */
3510static int ssl_bitmask_check( unsigned char *mask, size_t len )
3511{
3512 size_t i;
3513
3514 for( i = 0; i < len / 8; i++ )
3515 if( mask[i] != 0xFF )
3516 return( -1 );
3517
3518 for( i = 0; i < len % 8; i++ )
3519 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3520 return( -1 );
3521
3522 return( 0 );
3523}
3524
Hanno Becker56e205e2018-08-16 09:06:12 +01003525/* msg_len does not include the handshake header */
3526static int ssl_prepare_reassembly_buffer( mbedtls_ssl_context *ssl, /* debug */
3527 unsigned msg_len,
Hanno Beckerd07df862018-08-16 09:14:58 +01003528 unsigned add_bitmap,
Hanno Becker56e205e2018-08-16 09:06:12 +01003529 unsigned char **target )
3530{
3531 size_t alloc_len;
3532 unsigned char *buf;
3533
3534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
3535 msg_len ) );
3536
3537 /* NOTE: That should be checked earlier */
3538 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
3539 {
3540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3541 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3542 }
3543
3544 alloc_len = 12; /* Handshake header */
3545 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003546
3547 if( add_bitmap )
3548 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003549
3550 buf = mbedtls_calloc( 1, alloc_len );
3551 if( buf == NULL )
3552 {
3553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
3554 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
3555 }
3556
3557 *target = buf;
3558 return( 0 );
3559}
3560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003562
Hanno Becker12555c62018-08-16 12:47:53 +01003563static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3564{
3565 return( ( ssl->in_msg[1] << 16 ) |
3566 ( ssl->in_msg[2] << 8 ) |
3567 ssl->in_msg[3] );
3568}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003569
Simon Butcher99000142016-10-13 17:21:01 +01003570int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003571{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003575 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003577 }
3578
Hanno Becker12555c62018-08-16 12:47:53 +01003579 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003582 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003583 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003587 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003588 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003589 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003590
Hanno Becker44650b72018-08-16 12:51:11 +01003591 if( ssl_check_hs_header( ssl ) != 0 )
3592 {
3593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3594 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3595 }
3596
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003597 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003598 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3599 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3600 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3601 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003602 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003603 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3604 {
3605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3606 recv_msg_seq,
3607 ssl->handshake->in_msg_seq ) );
3608 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3609 }
3610
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003611 /* Retransmit only on last message from previous flight, to avoid
3612 * too many retransmissions.
3613 * Besides, No sane server ever retransmits HelloVerifyRequest */
3614 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003618 "message_seq = %d, start_of_flight = %d",
3619 recv_msg_seq,
3620 ssl->handshake->in_flight_start_seq ) );
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003625 return( ret );
3626 }
3627 }
3628 else
3629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003631 "message_seq = %d, expected = %d",
3632 recv_msg_seq,
3633 ssl->handshake->in_msg_seq ) );
3634 }
3635
Hanno Becker90333da2017-10-10 11:27:13 +01003636 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003637 }
3638 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003639
Hanno Becker6d97ef52018-08-16 13:09:04 +01003640 /* Message reassembly is handled alongside buffering of future
3641 * messages; the commonality is that both handshake fragments and
3642 * future messages cannot be forwarded immediately to the handshake
3643 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003644 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003647 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003648 }
3649 }
3650 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003652 /* With TLS we don't handle fragmentation (for now) */
3653 if( ssl->in_msglen < ssl->in_hslen )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3656 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003657 }
3658
Simon Butcher99000142016-10-13 17:21:01 +01003659 return( 0 );
3660}
3661
3662void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3663{
Hanno Becker0271f962018-08-16 13:23:47 +01003664 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003665
Hanno Becker0271f962018-08-16 13:23:47 +01003666 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003667 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003668 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003669 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003671 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003674 ssl->handshake != NULL )
3675 {
Hanno Becker0271f962018-08-16 13:23:47 +01003676 unsigned offset;
3677 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003678
Hanno Becker0271f962018-08-16 13:23:47 +01003679 /* Increment handshake sequence number */
3680 hs->in_msg_seq++;
3681
3682 /*
3683 * Clear up handshake buffering and reassembly structure.
3684 */
3685
3686 /* Free first entry */
3687 hs_buf = &hs->buffering.hs[0];
3688 if( hs_buf->is_valid )
3689 mbedtls_free( hs_buf->data );
3690
3691 /* Shift all other entries */
3692 for( offset = 0; offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
3693 offset++, hs_buf++ )
3694 {
3695 *hs_buf = *(hs_buf + 1);
3696 }
3697
3698 /* Create a fresh last entry */
3699 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003700 }
3701#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003702}
3703
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003704/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003705 * DTLS anti-replay: RFC 6347 4.1.2.6
3706 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003707 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3708 * Bit n is set iff record number in_window_top - n has been seen.
3709 *
3710 * Usually, in_window_top is the last record number seen and the lsb of
3711 * in_window is set. The only exception is the initial state (record number 0
3712 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3715static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003716{
3717 ssl->in_window_top = 0;
3718 ssl->in_window = 0;
3719}
3720
3721static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3722{
3723 return( ( (uint64_t) buf[0] << 40 ) |
3724 ( (uint64_t) buf[1] << 32 ) |
3725 ( (uint64_t) buf[2] << 24 ) |
3726 ( (uint64_t) buf[3] << 16 ) |
3727 ( (uint64_t) buf[4] << 8 ) |
3728 ( (uint64_t) buf[5] ) );
3729}
3730
3731/*
3732 * Return 0 if sequence number is acceptable, -1 otherwise
3733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003735{
3736 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3737 uint64_t bit;
3738
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003739 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003740 return( 0 );
3741
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003742 if( rec_seqnum > ssl->in_window_top )
3743 return( 0 );
3744
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003745 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003746
3747 if( bit >= 64 )
3748 return( -1 );
3749
3750 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3751 return( -1 );
3752
3753 return( 0 );
3754}
3755
3756/*
3757 * Update replay window on new validated record
3758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003760{
3761 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3762
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003763 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003764 return;
3765
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003766 if( rec_seqnum > ssl->in_window_top )
3767 {
3768 /* Update window_top and the contents of the window */
3769 uint64_t shift = rec_seqnum - ssl->in_window_top;
3770
3771 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003772 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003773 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003774 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003775 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003776 ssl->in_window |= 1;
3777 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003778
3779 ssl->in_window_top = rec_seqnum;
3780 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003781 else
3782 {
3783 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003784 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003785
3786 if( bit < 64 ) /* Always true, but be extra sure */
3787 ssl->in_window |= (uint64_t) 1 << bit;
3788 }
3789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003791
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003792#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003793/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003794static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3795
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003796/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003797 * Without any SSL context, check if a datagram looks like a ClientHello with
3798 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003799 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003800 *
3801 * - if cookie is valid, return 0
3802 * - if ClientHello looks superficially valid but cookie is not,
3803 * fill obuf and set olen, then
3804 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3805 * - otherwise return a specific error code
3806 */
3807static int ssl_check_dtls_clihlo_cookie(
3808 mbedtls_ssl_cookie_write_t *f_cookie_write,
3809 mbedtls_ssl_cookie_check_t *f_cookie_check,
3810 void *p_cookie,
3811 const unsigned char *cli_id, size_t cli_id_len,
3812 const unsigned char *in, size_t in_len,
3813 unsigned char *obuf, size_t buf_len, size_t *olen )
3814{
3815 size_t sid_len, cookie_len;
3816 unsigned char *p;
3817
3818 if( f_cookie_write == NULL || f_cookie_check == NULL )
3819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3820
3821 /*
3822 * Structure of ClientHello with record and handshake headers,
3823 * and expected values. We don't need to check a lot, more checks will be
3824 * done when actually parsing the ClientHello - skipping those checks
3825 * avoids code duplication and does not make cookie forging any easier.
3826 *
3827 * 0-0 ContentType type; copied, must be handshake
3828 * 1-2 ProtocolVersion version; copied
3829 * 3-4 uint16 epoch; copied, must be 0
3830 * 5-10 uint48 sequence_number; copied
3831 * 11-12 uint16 length; (ignored)
3832 *
3833 * 13-13 HandshakeType msg_type; (ignored)
3834 * 14-16 uint24 length; (ignored)
3835 * 17-18 uint16 message_seq; copied
3836 * 19-21 uint24 fragment_offset; copied, must be 0
3837 * 22-24 uint24 fragment_length; (ignored)
3838 *
3839 * 25-26 ProtocolVersion client_version; (ignored)
3840 * 27-58 Random random; (ignored)
3841 * 59-xx SessionID session_id; 1 byte len + sid_len content
3842 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3843 * ...
3844 *
3845 * Minimum length is 61 bytes.
3846 */
3847 if( in_len < 61 ||
3848 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3849 in[3] != 0 || in[4] != 0 ||
3850 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3851 {
3852 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3853 }
3854
3855 sid_len = in[59];
3856 if( sid_len > in_len - 61 )
3857 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3858
3859 cookie_len = in[60 + sid_len];
3860 if( cookie_len > in_len - 60 )
3861 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3862
3863 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3864 cli_id, cli_id_len ) == 0 )
3865 {
3866 /* Valid cookie */
3867 return( 0 );
3868 }
3869
3870 /*
3871 * If we get here, we've got an invalid cookie, let's prepare HVR.
3872 *
3873 * 0-0 ContentType type; copied
3874 * 1-2 ProtocolVersion version; copied
3875 * 3-4 uint16 epoch; copied
3876 * 5-10 uint48 sequence_number; copied
3877 * 11-12 uint16 length; olen - 13
3878 *
3879 * 13-13 HandshakeType msg_type; hello_verify_request
3880 * 14-16 uint24 length; olen - 25
3881 * 17-18 uint16 message_seq; copied
3882 * 19-21 uint24 fragment_offset; copied
3883 * 22-24 uint24 fragment_length; olen - 25
3884 *
3885 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3886 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3887 *
3888 * Minimum length is 28.
3889 */
3890 if( buf_len < 28 )
3891 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3892
3893 /* Copy most fields and adapt others */
3894 memcpy( obuf, in, 25 );
3895 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3896 obuf[25] = 0xfe;
3897 obuf[26] = 0xff;
3898
3899 /* Generate and write actual cookie */
3900 p = obuf + 28;
3901 if( f_cookie_write( p_cookie,
3902 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3903 {
3904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3905 }
3906
3907 *olen = p - obuf;
3908
3909 /* Go back and fill length fields */
3910 obuf[27] = (unsigned char)( *olen - 28 );
3911
3912 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3913 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3914 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3915
3916 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3917 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3918
3919 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3920}
3921
3922/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003923 * Handle possible client reconnect with the same UDP quadruplet
3924 * (RFC 6347 Section 4.2.8).
3925 *
3926 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3927 * that looks like a ClientHello.
3928 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003929 * - if the input looks like a ClientHello without cookies,
3930 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003931 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003932 * - if the input looks like a ClientHello with a valid cookie,
3933 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003934 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003935 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003936 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003937 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003938 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3939 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003940 */
3941static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3942{
3943 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003944 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003945
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003946 ret = ssl_check_dtls_clihlo_cookie(
3947 ssl->conf->f_cookie_write,
3948 ssl->conf->f_cookie_check,
3949 ssl->conf->p_cookie,
3950 ssl->cli_id, ssl->cli_id_len,
3951 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003952 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003953
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003954 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3955
3956 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003957 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003958 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003959 * If the error is permanent we'll catch it later,
3960 * if it's not, then hopefully it'll work next time. */
3961 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3962
3963 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003964 }
3965
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003966 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003967 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003968 /* Got a valid cookie, partially reset context */
3969 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3970 {
3971 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3972 return( ret );
3973 }
3974
3975 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003976 }
3977
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003978 return( ret );
3979}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003980#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003981
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003982/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003983 * ContentType type;
3984 * ProtocolVersion version;
3985 * uint16 epoch; // DTLS only
3986 * uint48 sequence_number; // DTLS only
3987 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003988 *
3989 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003990 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003991 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3992 *
3993 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003994 * 1. proceed with the record if this function returns 0
3995 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3996 * 3. return CLIENT_RECONNECT if this function return that value
3997 * 4. drop the whole datagram if this function returns anything else.
3998 * Point 2 is needed when the peer is resending, and we have already received
3999 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004002{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004003 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004006
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004008 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004009 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004013 ssl->in_msgtype,
4014 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004016 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4018 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4019 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4020 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004023
4024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004025 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4026 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004027 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4028#endif /* MBEDTLS_SSL_PROTO_DTLS */
4029 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4030 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004033 }
4034
4035 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004036 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4039 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 }
4041
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004042 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4045 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 }
4047
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004048 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004049 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004050 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4053 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004054 }
4055
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004056 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004057 * DTLS-related tests.
4058 * Check epoch before checking length constraint because
4059 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4060 * message gets duplicated before the corresponding Finished message,
4061 * the second ChangeCipherSpec should be discarded because it belongs
4062 * to an old epoch, but not because its length is shorter than
4063 * the minimum record length for packets using the new record transform.
4064 * Note that these two kinds of failures are handled differently,
4065 * as an unexpected record is silently skipped but an invalid
4066 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004067 */
4068#if defined(MBEDTLS_SSL_PROTO_DTLS)
4069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4070 {
4071 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4072
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004073 /* Check epoch (and sequence number) with DTLS */
4074 if( rec_epoch != ssl->in_epoch )
4075 {
4076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4077 "expected %d, received %d",
4078 ssl->in_epoch, rec_epoch ) );
4079
4080#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4081 /*
4082 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4083 * access the first byte of record content (handshake type), as we
4084 * have an active transform (possibly iv_len != 0), so use the
4085 * fact that the record header len is 13 instead.
4086 */
4087 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4088 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4089 rec_epoch == 0 &&
4090 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4091 ssl->in_left > 13 &&
4092 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4093 {
4094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4095 "from the same port" ) );
4096 return( ssl_handle_possible_reconnect( ssl ) );
4097 }
4098 else
4099#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4100 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4101 }
4102
4103#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4104 /* Replay detection only works for the current epoch */
4105 if( rec_epoch == ssl->in_epoch &&
4106 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4107 {
4108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4109 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4110 }
4111#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004112
Hanno Becker52c6dc62017-05-26 16:07:36 +01004113 /* Drop unexpected ApplicationData records,
4114 * except at the beginning of renegotiations */
4115 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4116 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4117#if defined(MBEDTLS_SSL_RENEGOTIATION)
4118 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4119 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4120#endif
4121 )
4122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4124 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4125 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004126 }
4127#endif /* MBEDTLS_SSL_PROTO_DTLS */
4128
Hanno Becker52c6dc62017-05-26 16:07:36 +01004129
4130 /* Check length against bounds of the current transform and version */
4131 if( ssl->transform_in == NULL )
4132 {
4133 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004134 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4138 }
4139 }
4140 else
4141 {
4142 if( ssl->in_msglen < ssl->transform_in->minlen )
4143 {
4144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4146 }
4147
4148#if defined(MBEDTLS_SSL_PROTO_SSL3)
4149 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004150 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004151 {
4152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4154 }
4155#endif
4156#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4157 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4158 /*
4159 * TLS encrypted messages can have up to 256 bytes of padding
4160 */
4161 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4162 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004163 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004164 {
4165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4167 }
4168#endif
4169 }
4170
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004171 return( 0 );
4172}
Paul Bakker5121ce52009-01-03 21:22:43 +00004173
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004174/*
4175 * If applicable, decrypt (and decompress) record content
4176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004178{
4179 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4182 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4185 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 ret = mbedtls_ssl_hw_record_read( ssl );
4190 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4193 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004194 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004195
4196 if( ret == 0 )
4197 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004198 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004200 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 {
4202 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 return( ret );
4206 }
4207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004209 ssl->in_msg, ssl->in_msglen );
4210
Angus Grattond8213d02016-05-25 20:56:48 +10004211 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4214 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 }
4216 }
4217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004219 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004221 {
4222 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004225 return( ret );
4226 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004234 }
4235#endif
4236
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004237 return( 0 );
4238}
4239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004241
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004242/*
4243 * Read a record.
4244 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004245 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4246 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4247 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004248 */
Hanno Becker1097b342018-08-15 14:09:41 +01004249
4250/* Helper functions for mbedtls_ssl_read_record(). */
4251static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004252static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4253static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004254
Hanno Becker40f50842018-08-15 14:48:01 +01004255#if defined(MBEDTLS_SSL_PROTO_DTLS)
4256static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
4257static int ssl_buffer_message( mbedtls_ssl_context *ssl );
4258static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4259#endif /* MBEDTLS_SSL_PROTO_DTLS */
4260
Hanno Becker327c93b2018-08-15 13:56:18 +01004261int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
4262 unsigned update_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004263{
4264 int ret;
4265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004267
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004268 if( ssl->keep_current_message == 0 )
4269 {
4270 do {
Simon Butcher99000142016-10-13 17:21:01 +01004271
Hanno Becker26994592018-08-15 14:14:59 +01004272 ret = ssl_consume_current_message( ssl );
4273 if( ret != 0 )
4274 return( ret );
4275
Hanno Beckere74d5562018-08-15 14:26:08 +01004276 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004277 {
Hanno Becker40f50842018-08-15 14:48:01 +01004278#if defined(MBEDTLS_SSL_PROTO_DTLS)
4279 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004280
Hanno Becker40f50842018-08-15 14:48:01 +01004281 /* We only check for buffered messages if the
4282 * current datagram is fully consumed. */
4283 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4284 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004285 {
Hanno Becker40f50842018-08-15 14:48:01 +01004286 if( ssl_load_buffered_message( ssl ) == 0 )
4287 have_buffered = 1;
4288 }
4289
4290 if( have_buffered == 0 )
4291#endif /* MBEDTLS_SSL_PROTO_DTLS */
4292 {
4293 ret = ssl_get_next_record( ssl );
4294 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4295 continue;
4296
4297 if( ret != 0 )
4298 {
4299 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4300 return( ret );
4301 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004302 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004303 }
4304
4305 ret = mbedtls_ssl_handle_message_type( ssl );
4306
Hanno Becker40f50842018-08-15 14:48:01 +01004307#if defined(MBEDTLS_SSL_PROTO_DTLS)
4308 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4309 {
4310 /* Buffer future message */
4311 ret = ssl_buffer_message( ssl );
4312 if( ret != 0 )
4313 return( ret );
4314
4315 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4316 }
4317#endif /* MBEDTLS_SSL_PROTO_DTLS */
4318
Hanno Becker90333da2017-10-10 11:27:13 +01004319 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4320 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004321
4322 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004323 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004324 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004325 return( ret );
4326 }
4327
Hanno Becker327c93b2018-08-15 13:56:18 +01004328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4329 update_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004330 {
4331 mbedtls_ssl_update_handshake_status( ssl );
4332 }
Simon Butcher99000142016-10-13 17:21:01 +01004333 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004334 else
Simon Butcher99000142016-10-13 17:21:01 +01004335 {
Hanno Becker02f59072018-08-15 14:00:24 +01004336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004337 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004338 }
4339
4340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4341
4342 return( 0 );
4343}
4344
Hanno Becker40f50842018-08-15 14:48:01 +01004345#if defined(MBEDTLS_SSL_PROTO_DTLS)
4346static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4347{
4348 if( ssl->in_left > ssl->next_record_offset )
4349 return( 1 );
4350
4351 return( 0 );
4352}
4353
4354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4355{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004356 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004357 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004358 int ret = 0;
4359
4360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4361
4362 if( hs == NULL )
4363 return( -1 );
4364
4365 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4366 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4367 {
4368 /* Check if we have seen a ChangeCipherSpec before.
4369 * If yes, synthesize a CCS record. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004370 if( ! hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004371 {
4372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4373 ret = -1;
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004374 return( -1 );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004375 }
4376
4377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4378 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4379 ssl->in_msglen = 1;
4380 ssl->in_msg[0] = 1;
4381
4382 /* As long as they are equal, the exact value doesn't matter. */
4383 ssl->in_left = 0;
4384 ssl->next_record_offset = 0;
4385
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004386 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004387 goto exit;
4388 }
Hanno Becker37f95322018-08-16 13:55:32 +01004389
4390 /* Debug only */
4391 {
4392 unsigned offset;
4393 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4394 {
4395 hs_buf = &hs->buffering.hs[offset];
4396 if( hs_buf->is_valid == 1 )
4397 {
4398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4399 hs->in_msg_seq + offset,
4400 hs_buf->is_complete ? "fully" : "partitially" ) );
4401 }
4402 }
4403 }
4404
4405 /* Check if we have buffered and/or fully reassembled the
4406 * next handshake message. */
4407 hs_buf = &hs->buffering.hs[0];
4408 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4409 {
4410 /* Synthesize a record containing the buffered HS message. */
4411 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4412 ( hs_buf->data[2] << 8 ) |
4413 hs_buf->data[3];
4414
4415 /* Double-check that we haven't accidentally buffered
4416 * a message that doesn't fit into the input buffer. */
4417 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4418 {
4419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4421 }
4422
4423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4424 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4425 hs_buf->data, msg_len + 12 );
4426
4427 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4428 ssl->in_hslen = msg_len + 12;
4429 ssl->in_msglen = msg_len + 12;
4430 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4431
4432 ret = 0;
4433 goto exit;
4434 }
4435 else
4436 {
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4438 hs->in_msg_seq ) );
4439 }
4440
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004441 ret = -1;
4442
4443exit:
4444
4445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4446 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004447}
4448
4449static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4450{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004451 int ret = 0;
4452 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4453
4454 if( hs == NULL )
4455 return( 0 );
4456
4457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4458
4459 switch( ssl->in_msgtype )
4460 {
4461 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004463 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004464 break;
4465
4466 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004467 {
4468 unsigned recv_msg_seq_offset;
4469 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4470 mbedtls_ssl_hs_buffer *hs_buf;
4471 size_t msg_len = ssl->in_hslen - 12;
4472
4473 /* We should never receive an old handshake
4474 * message - double-check nonetheless. */
4475 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4476 {
4477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4478 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4479 }
4480
4481 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4482 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4483 {
4484 /* Silently ignore -- message too far in the future */
4485 MBEDTLS_SSL_DEBUG_MSG( 2,
4486 ( "Ignore future HS message with sequence number %u, "
4487 "buffering window %u - %u",
4488 recv_msg_seq, ssl->handshake->in_msg_seq,
4489 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4490
4491 goto exit;
4492 }
4493
4494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4495 recv_msg_seq, recv_msg_seq_offset ) );
4496
4497 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4498
4499 /* Check if the buffering for this seq nr has already commenced. */
4500 if( ! hs_buf->is_valid )
4501 {
4502 hs_buf->is_fragmented =
4503 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4504
4505 /* We copy the message back into the input buffer
4506 * after reassembly, so check that it's not too large.
4507 * This is an implementation-specific limitation
4508 * and not one from the standard, hence it is not
4509 * checked in ssl_check_hs_header(). */
4510 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
4511 {
4512 /* Ignore message */
4513 goto exit;
4514 }
4515
4516 ret = ssl_prepare_reassembly_buffer( ssl, msg_len,
4517 hs_buf->is_fragmented,
4518 &hs_buf->data );
4519 if( ret == MBEDTLS_ERR_SSL_ALLOC_FAILED &&
4520 recv_msg_seq_offset > 0 )
4521 {
4522 /* If we run out of RAM trying to buffer a *future*
4523 * message, simply ignore instead of failing. */
4524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Not enough RAM available to buffer future message - ignore" ) );
4525 goto exit;
4526 }
4527 else if( ret != 0 )
4528 return( ret );
4529
4530 /* Prepare final header: copy msg_type, length and message_seq,
4531 * then add standardised fragment_offset and fragment_length */
4532 memcpy( hs_buf->data, ssl->in_msg, 6 );
4533 memset( hs_buf->data + 6, 0, 3 );
4534 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4535
4536 hs_buf->is_valid = 1;
4537 }
4538 else
4539 {
4540 /* Make sure msg_type and length are consistent */
4541 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4542 {
4543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4544 /* Ignore */
4545 goto exit;
4546 }
4547 }
4548
4549 if( ! hs_buf->is_complete )
4550 {
4551 size_t frag_len, frag_off;
4552 unsigned char * const msg = hs_buf->data + 12;
4553
4554 /*
4555 * Check and copy current fragment
4556 */
4557
4558 /* Validation of header fields already done in
4559 * mbedtls_ssl_prepare_handshake_record(). */
4560 frag_off = ssl_get_hs_frag_off( ssl );
4561 frag_len = ssl_get_hs_frag_len( ssl );
4562
4563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4564 frag_off, frag_len ) );
4565 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4566
4567 if( hs_buf->is_fragmented )
4568 {
4569 unsigned char * const bitmask = msg + msg_len;
4570 ssl_bitmask_set( bitmask, frag_off, frag_len );
4571 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4572 msg_len ) == 0 );
4573 }
4574 else
4575 {
4576 hs_buf->is_complete = 1;
4577 }
4578
4579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4580 hs_buf->is_complete ? "" : "not yet " ) );
4581 }
4582
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004583 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004584 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004585
4586 default:
4587 break;
4588 }
4589
4590exit:
4591
4592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4593 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004594}
4595#endif /* MBEDTLS_SSL_PROTO_DTLS */
4596
Hanno Becker1097b342018-08-15 14:09:41 +01004597static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004598{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004599 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004600 * Consume last content-layer message and potentially
4601 * update in_msglen which keeps track of the contents'
4602 * consumption state.
4603 *
4604 * (1) Handshake messages:
4605 * Remove last handshake message, move content
4606 * and adapt in_msglen.
4607 *
4608 * (2) Alert messages:
4609 * Consume whole record content, in_msglen = 0.
4610 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004611 * (3) Change cipher spec:
4612 * Consume whole record content, in_msglen = 0.
4613 *
4614 * (4) Application data:
4615 * Don't do anything - the record layer provides
4616 * the application data as a stream transport
4617 * and consumes through mbedtls_ssl_read only.
4618 *
4619 */
4620
4621 /* Case (1): Handshake messages */
4622 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004623 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004624 /* Hard assertion to be sure that no application data
4625 * is in flight, as corrupting ssl->in_msglen during
4626 * ssl->in_offt != NULL is fatal. */
4627 if( ssl->in_offt != NULL )
4628 {
4629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4631 }
4632
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004633 /*
4634 * Get next Handshake message in the current record
4635 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004636
Hanno Becker4a810fb2017-05-24 16:27:30 +01004637 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004638 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004639 * current handshake content: If DTLS handshake
4640 * fragmentation is used, that's the fragment
4641 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004642 * size here is faulty and should be changed at
4643 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004644 * (2) While it doesn't seem to cause problems, one
4645 * has to be very careful not to assume that in_hslen
4646 * is always <= in_msglen in a sensible communication.
4647 * Again, it's wrong for DTLS handshake fragmentation.
4648 * The following check is therefore mandatory, and
4649 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004650 * Additionally, ssl->in_hslen might be arbitrarily out of
4651 * bounds after handling a DTLS message with an unexpected
4652 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004653 */
4654 if( ssl->in_hslen < ssl->in_msglen )
4655 {
4656 ssl->in_msglen -= ssl->in_hslen;
4657 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4658 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004659
Hanno Becker4a810fb2017-05-24 16:27:30 +01004660 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4661 ssl->in_msg, ssl->in_msglen );
4662 }
4663 else
4664 {
4665 ssl->in_msglen = 0;
4666 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004667
Hanno Becker4a810fb2017-05-24 16:27:30 +01004668 ssl->in_hslen = 0;
4669 }
4670 /* Case (4): Application data */
4671 else if( ssl->in_offt != NULL )
4672 {
4673 return( 0 );
4674 }
4675 /* Everything else (CCS & Alerts) */
4676 else
4677 {
4678 ssl->in_msglen = 0;
4679 }
4680
Hanno Becker1097b342018-08-15 14:09:41 +01004681 return( 0 );
4682}
4683
Hanno Beckere74d5562018-08-15 14:26:08 +01004684static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4685{
4686 if( ssl->in_msglen > 0 )
4687 return( 1 );
4688
4689 return( 0 );
4690}
4691
4692static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004693{
4694 int ret;
4695
4696 /*
Hanno Beckere74d5562018-08-15 14:26:08 +01004697 * Fetch and decode new record
Hanno Becker4a810fb2017-05-24 16:27:30 +01004698 */
4699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004703 return( ret );
4704 }
4705
4706 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004709 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4710 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004711 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4713 {
4714 /* Skip unexpected record (but not whole datagram) */
4715 ssl->next_record_offset = ssl->in_msglen
4716 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004717
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4719 "(header)" ) );
4720 }
4721 else
4722 {
4723 /* Skip invalid record and the rest of the datagram */
4724 ssl->next_record_offset = 0;
4725 ssl->in_left = 0;
4726
4727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4728 "(header)" ) );
4729 }
4730
4731 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004732 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004733 }
4734#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004735 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004736 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004737
4738 /*
4739 * Read and optionally decrypt the message contents
4740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4742 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004745 return( ret );
4746 }
4747
4748 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004753 if( ssl->next_record_offset < ssl->in_left )
4754 {
4755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4756 }
4757 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004758 else
4759#endif
4760 ssl->in_left = 0;
4761
4762 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004765 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004766 {
4767 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4769 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004770 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004771 /* Except when waiting for Finished as a bad mac here
4772 * probably means something went wrong in the handshake
4773 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4774 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4775 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4776 {
4777#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4778 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4779 {
4780 mbedtls_ssl_send_alert_message( ssl,
4781 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4782 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4783 }
4784#endif
4785 return( ret );
4786 }
4787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004789 if( ssl->conf->badmac_limit != 0 &&
4790 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4793 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004794 }
4795#endif
4796
Hanno Becker4a810fb2017-05-24 16:27:30 +01004797 /* As above, invalid records cause
4798 * dismissal of the whole datagram. */
4799
4800 ssl->next_record_offset = 0;
4801 ssl->in_left = 0;
4802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004804 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004805 }
4806
4807 return( ret );
4808 }
4809 else
4810#endif
4811 {
4812 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4814 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 mbedtls_ssl_send_alert_message( ssl,
4817 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4818 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004819 }
4820#endif
4821 return( ret );
4822 }
4823 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004824
Simon Butcher99000142016-10-13 17:21:01 +01004825 return( 0 );
4826}
4827
4828int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4829{
4830 int ret;
4831
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004832 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004833 * Handle particular types of records
4834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 {
Simon Butcher99000142016-10-13 17:21:01 +01004837 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4838 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004839 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004841 }
4842
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004843#if defined(MBEDTLS_SSL_PROTO_DTLS)
4844 /* Drop unexpected ChangeCipherSpec messages */
4845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4846 ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4847 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4848 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4849 {
4850 if( ssl->handshake == NULL )
4851 {
4852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4853 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4854 }
4855
4856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4857 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4858 }
4859#endif
4860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004863 if( ssl->in_msglen != 2 )
4864 {
4865 /* Note: Standard allows for more than one 2 byte alert
4866 to be packed in a single message, but Mbed TLS doesn't
4867 currently support this. */
4868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4869 ssl->in_msglen ) );
4870 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4871 }
4872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004874 ssl->in_msg[0], ssl->in_msg[1] ) );
4875
4876 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004877 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004882 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 }
4885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4887 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4890 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004891 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004892
4893#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4894 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4895 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4896 {
Hanno Becker90333da2017-10-10 11:27:13 +01004897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004898 /* Will be handled when trying to parse ServerHello */
4899 return( 0 );
4900 }
4901#endif
4902
4903#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4904 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4905 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4906 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4907 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4908 {
4909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4910 /* Will be handled in mbedtls_ssl_parse_certificate() */
4911 return( 0 );
4912 }
4913#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4914
4915 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004916 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004917 }
4918
Hanno Beckerc76c6192017-06-06 10:03:17 +01004919#if defined(MBEDTLS_SSL_PROTO_DTLS)
4920 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4921 ssl->handshake != NULL &&
4922 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4923 {
4924 ssl_handshake_wrapup_free_hs_transform( ssl );
4925 }
4926#endif
4927
Paul Bakker5121ce52009-01-03 21:22:43 +00004928 return( 0 );
4929}
4930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004932{
4933 int ret;
4934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4936 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4937 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004938 {
4939 return( ret );
4940 }
4941
4942 return( 0 );
4943}
4944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004946 unsigned char level,
4947 unsigned char message )
4948{
4949 int ret;
4950
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004951 if( ssl == NULL || ssl->conf == NULL )
4952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004958 ssl->out_msglen = 2;
4959 ssl->out_msg[0] = level;
4960 ssl->out_msg[1] = message;
4961
Hanno Becker67bc7c32018-08-06 11:33:50 +01004962 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004965 return( ret );
4966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004968
4969 return( 0 );
4970}
4971
Paul Bakker5121ce52009-01-03 21:22:43 +00004972/*
4973 * Handshake functions
4974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4976 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4977 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4978 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4979 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4980 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4981 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004982/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004984{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4990 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4992 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004995 ssl->state++;
4996 return( 0 );
4997 }
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5000 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005001}
5002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5010 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005011 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5012 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005015 ssl->state++;
5016 return( 0 );
5017 }
5018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5020 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005021}
Gilles Peskinef9828522017-05-03 12:28:43 +02005022
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005023#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005024/* Some certificate support -> implement write and parse */
5025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005029 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 const mbedtls_x509_crt *crt;
5031 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5036 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005037 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5038 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005041 ssl->state++;
5042 return( 0 );
5043 }
5044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005046 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005047 {
5048 if( ssl->client_auth == 0 )
5049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005051 ssl->state++;
5052 return( 0 );
5053 }
5054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 /*
5057 * If using SSLv3 and got no cert, send an Alert message
5058 * (otherwise an empty Certificate message will be sent).
5059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5061 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005062 {
5063 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5065 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5066 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 goto write_msg;
5070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073#endif /* MBEDTLS_SSL_CLI_C */
5074#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005075 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5080 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005081 }
5082 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005086
5087 /*
5088 * 0 . 0 handshake type
5089 * 1 . 3 handshake length
5090 * 4 . 6 length of all certs
5091 * 7 . 9 length of cert. 1
5092 * 10 . n-1 peer certificate
5093 * n . n+2 length of cert. 2
5094 * n+3 . ... upper level cert, etc.
5095 */
5096 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005098
Paul Bakker29087132010-03-21 21:03:34 +00005099 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005100 {
5101 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005102 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005105 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 }
5108
5109 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5110 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5111 ssl->out_msg[i + 2] = (unsigned char)( n );
5112
5113 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5114 i += n; crt = crt->next;
5115 }
5116
5117 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5118 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5119 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5120
5121 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5123 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005124
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005125#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005126write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005128
5129 ssl->state++;
5130
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005131 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005134 return( ret );
5135 }
5136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005138
Paul Bakkered27a042013-04-18 22:46:23 +02005139 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005140}
5141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005145 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005147 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005148 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5153 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005154 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5155 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005158 ssl->state++;
5159 return( 0 );
5160 }
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005163 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005164 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5165 {
5166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5167 ssl->state++;
5168 return( 0 );
5169 }
5170
5171#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5172 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5173 authmode = ssl->handshake->sni_authmode;
5174#endif
5175
5176 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5177 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005179 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005181 ssl->state++;
5182 return( 0 );
5183 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005185
Hanno Becker327c93b2018-08-15 13:56:18 +01005186 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005187 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005188 /* mbedtls_ssl_read_record may have sent an alert already. We
5189 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005191 return( ret );
5192 }
5193
5194 ssl->state++;
5195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196#if defined(MBEDTLS_SSL_SRV_C)
5197#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005198 /*
5199 * Check if the client sent an empty certificate
5200 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005201 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005203 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005204 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5206 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5207 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005210
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005211 /* The client was asked for a certificate but didn't send
5212 one. The client should know what's going on, so we
5213 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005214 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005215 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 return( 0 );
5217 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005219 }
5220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5224 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5229 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5230 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5231 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005234
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005235 /* The client was asked for a certificate but didn't send
5236 one. The client should know what's going on, so we
5237 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005238 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005239 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005240 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005241 else
5242 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 }
5244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5246 MBEDTLS_SSL_PROTO_TLS1_2 */
5247#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005252 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5253 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005255 }
5256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5258 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005261 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5262 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005264 }
5265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005267
Paul Bakker5121ce52009-01-03 21:22:43 +00005268 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005270 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005271 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005272
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005273 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005277 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005280 }
5281
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005282 /* In case we tried to reuse a session but it failed */
5283 if( ssl->session_negotiate->peer_cert != NULL )
5284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5286 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005287 }
5288
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005289 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005291 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005294 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5295 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005296 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005297 }
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005301 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005302
5303 while( i < ssl->in_hslen )
5304 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005305 if ( i + 3 > ssl->in_hslen ) {
5306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5308 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5309 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005311 if( ssl->in_msg[i] != 0 )
5312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5315 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005317 }
5318
5319 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5320 | (unsigned int) ssl->in_msg[i + 2];
5321 i += 3;
5322
5323 if( n < 128 || i + n > ssl->in_hslen )
5324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005326 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5327 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005329 }
5330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005332 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005333 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005334 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005335 case 0: /*ok*/
5336 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5337 /* Ignore certificate with an unknown algorithm: maybe a
5338 prior certificate was already trusted. */
5339 break;
5340
5341 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5342 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5343 goto crt_parse_der_failed;
5344
5345 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5346 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5347 goto crt_parse_der_failed;
5348
5349 default:
5350 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5351 crt_parse_der_failed:
5352 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005354 return( ret );
5355 }
5356
5357 i += n;
5358 }
5359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005361
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005362 /*
5363 * On client, make sure the server cert doesn't change during renego to
5364 * avoid "triple handshake" attack: https://secure-resumption.com/
5365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005369 {
5370 if( ssl->session->peer_cert == NULL )
5371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005373 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5374 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005376 }
5377
5378 if( ssl->session->peer_cert->raw.len !=
5379 ssl->session_negotiate->peer_cert->raw.len ||
5380 memcmp( ssl->session->peer_cert->raw.p,
5381 ssl->session_negotiate->peer_cert->raw.p,
5382 ssl->session->peer_cert->raw.len ) != 0 )
5383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5386 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005388 }
5389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005391
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005392 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005394 mbedtls_x509_crt *ca_chain;
5395 mbedtls_x509_crl *ca_crl;
5396
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005397#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005398 if( ssl->handshake->sni_ca_chain != NULL )
5399 {
5400 ca_chain = ssl->handshake->sni_ca_chain;
5401 ca_crl = ssl->handshake->sni_ca_crl;
5402 }
5403 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005404#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005405 {
5406 ca_chain = ssl->conf->ca_chain;
5407 ca_crl = ssl->conf->ca_crl;
5408 }
5409
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005410 /*
5411 * Main check: verify certificate
5412 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005413 ret = mbedtls_x509_crt_verify_with_profile(
5414 ssl->session_negotiate->peer_cert,
5415 ca_chain, ca_crl,
5416 ssl->conf->cert_profile,
5417 ssl->hostname,
5418 &ssl->session_negotiate->verify_result,
5419 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005420
5421 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005424 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005425
5426 /*
5427 * Secondary checks: always done, but change 'ret' only if it was 0
5428 */
5429
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005430#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005433
5434 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005436 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005437 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005438 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005441 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005443 }
5444 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005445#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005448 ciphersuite_info,
5449 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005450 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005453 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005455 }
5456
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005457 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5458 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5459 * with details encoded in the verification flags. All other kinds
5460 * of error codes, including those from the user provided f_vrfy
5461 * functions, are treated as fatal and lead to a failure of
5462 * ssl_parse_certificate even if verification was optional. */
5463 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5464 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5465 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5466 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005468 }
5469
5470 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5471 {
5472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5473 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5474 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005475
5476 if( ret != 0 )
5477 {
5478 /* The certificate may have been rejected for several reasons.
5479 Pick one and send the corresponding alert. Which alert to send
5480 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005481 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5482 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5483 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5484 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5485 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5486 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5487 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5488 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5489 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5490 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5491 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5492 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5493 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5494 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5495 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5496 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5497 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5498 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5499 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5500 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005501 else
5502 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5504 alert );
5505 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005506
Hanno Beckere6706e62017-05-15 16:05:15 +01005507#if defined(MBEDTLS_DEBUG_C)
5508 if( ssl->session_negotiate->verify_result != 0 )
5509 {
5510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5511 ssl->session_negotiate->verify_result ) );
5512 }
5513 else
5514 {
5515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5516 }
5517#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 }
5519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005521
5522 return( ret );
5523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5525 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5526 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5527 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5528 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5529 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5530 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005533{
5534 int ret;
5535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005539 ssl->out_msglen = 1;
5540 ssl->out_msg[0] = 1;
5541
Paul Bakker5121ce52009-01-03 21:22:43 +00005542 ssl->state++;
5543
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005544 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 return( ret );
5548 }
5549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005551
5552 return( 0 );
5553}
5554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005556{
5557 int ret;
5558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005560
Hanno Becker327c93b2018-08-15 13:56:18 +01005561 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 return( ret );
5565 }
5566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005570 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5571 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005573 }
5574
5575 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5579 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005583 /*
5584 * Switch to our negotiated transform and session parameters for inbound
5585 * data.
5586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005588 ssl->transform_in = ssl->transform_negotiate;
5589 ssl->session_in = ssl->session_negotiate;
5590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005595 ssl_dtls_replay_reset( ssl );
5596#endif
5597
5598 /* Increment epoch */
5599 if( ++ssl->in_epoch == 0 )
5600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005602 /* This is highly unlikely to happen for legitimate reasons, so
5603 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005605 }
5606 }
5607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005609 memset( ssl->in_ctr, 0, 8 );
5610
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005611 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5614 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005619 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5620 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005622 }
5623 }
5624#endif
5625
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 ssl->state++;
5627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005629
5630 return( 0 );
5631}
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5634 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005635{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005636 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5639 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5640 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005641 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005642 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5645#if defined(MBEDTLS_SHA512_C)
5646 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005647 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5648 else
5649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650#if defined(MBEDTLS_SHA256_C)
5651 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005652 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005653 else
5654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005658 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005659 }
Paul Bakker380da532012-04-18 16:10:25 +00005660}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5665 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005666 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5667 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5670#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005671 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005672#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005674 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005675#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005677}
5678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005680 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5683 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005684 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5685 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5688#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005689 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005692 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005695}
5696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5698 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5699static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005700 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005701{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005702 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5703 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005704}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005705#endif
Paul Bakker380da532012-04-18 16:10:25 +00005706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5708#if defined(MBEDTLS_SHA256_C)
5709static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005710 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005711{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005712 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005713}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005714#endif
Paul Bakker380da532012-04-18 16:10:25 +00005715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_SHA512_C)
5717static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005718 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005719{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005720 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005721}
Paul Bakker769075d2012-11-24 11:26:46 +01005722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005726static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005728{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005729 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005730 mbedtls_md5_context md5;
5731 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005732
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 unsigned char padbuf[48];
5734 unsigned char md5sum[16];
5735 unsigned char sha1sum[20];
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005738 if( !session )
5739 session = ssl->session;
5740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005742
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005743 mbedtls_md5_init( &md5 );
5744 mbedtls_sha1_init( &sha1 );
5745
5746 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5747 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748
5749 /*
5750 * SSLv3:
5751 * hash =
5752 * MD5( master + pad2 +
5753 * MD5( handshake + sender + master + pad1 ) )
5754 * + SHA1( master + pad2 +
5755 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 */
5757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005759 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5760 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005764 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5765 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005766#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005769 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
Paul Bakker1ef83d62012-04-11 12:09:53 +00005771 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005772
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005773 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5774 mbedtls_md5_update_ret( &md5, session->master, 48 );
5775 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5776 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005777
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005778 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5779 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5780 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5781 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782
Paul Bakker1ef83d62012-04-11 12:09:53 +00005783 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005784
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005785 mbedtls_md5_starts_ret( &md5 );
5786 mbedtls_md5_update_ret( &md5, session->master, 48 );
5787 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5788 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5789 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005790
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005791 mbedtls_sha1_starts_ret( &sha1 );
5792 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5793 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5794 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5795 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005798
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005799 mbedtls_md5_free( &md5 );
5800 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005801
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005802 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5803 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5804 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005811static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005813{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005814 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005815 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005816 mbedtls_md5_context md5;
5817 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005818 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005821 if( !session )
5822 session = ssl->session;
5823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005826 mbedtls_md5_init( &md5 );
5827 mbedtls_sha1_init( &sha1 );
5828
5829 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5830 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005831
Paul Bakker1ef83d62012-04-11 12:09:53 +00005832 /*
5833 * TLSv1:
5834 * hash = PRF( master, finished_label,
5835 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5836 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005839 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5840 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005841#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005844 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5845 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005846#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005849 ? "client finished"
5850 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005851
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005852 mbedtls_md5_finish_ret( &md5, padbuf );
5853 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005854
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005855 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005856 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005859
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005860 mbedtls_md5_free( &md5 );
5861 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005862
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005863 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005866}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5870#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005871static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005873{
5874 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005875 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005876 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005877 unsigned char padbuf[32];
5878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005880 if( !session )
5881 session = ssl->session;
5882
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005883 mbedtls_sha256_init( &sha256 );
5884
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005886
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005887 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005888
5889 /*
5890 * TLSv1.2:
5891 * hash = PRF( master, finished_label,
5892 * Hash( handshake ) )[0.11]
5893 */
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895#if !defined(MBEDTLS_SHA256_ALT)
5896 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005897 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005898#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005901 ? "client finished"
5902 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005903
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005904 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005905
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005906 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005907 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005910
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005911 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005912
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005913 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005920static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005922{
5923 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005924 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005925 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005926 unsigned char padbuf[48];
5927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005929 if( !session )
5930 session = ssl->session;
5931
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005932 mbedtls_sha512_init( &sha512 );
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005935
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005936 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005937
5938 /*
5939 * TLSv1.2:
5940 * hash = PRF( master, finished_label,
5941 * Hash( handshake ) )[0.11]
5942 */
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005945 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5946 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005947#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005950 ? "client finished"
5951 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005952
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005953 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005954
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005955 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005956 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005959
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005960 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005961
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005962 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966#endif /* MBEDTLS_SHA512_C */
5967#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005972
5973 /*
5974 * Free our handshake params
5975 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005976 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005978 ssl->handshake = NULL;
5979
5980 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005981 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005982 */
5983 if( ssl->transform )
5984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 mbedtls_ssl_transform_free( ssl->transform );
5986 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005987 }
5988 ssl->transform = ssl->transform_negotiate;
5989 ssl->transform_negotiate = NULL;
5990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005992}
5993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005995{
5996 int resume = ssl->handshake->resume;
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SSL_RENEGOTIATION)
6001 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006004 ssl->renego_records_seen = 0;
6005 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006006#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006007
6008 /*
6009 * Free the previous session and switch in the current one
6010 */
Paul Bakker0a597072012-09-25 21:55:46 +00006011 if( ssl->session )
6012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006014 /* RFC 7366 3.1: keep the EtM state */
6015 ssl->session_negotiate->encrypt_then_mac =
6016 ssl->session->encrypt_then_mac;
6017#endif
6018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 mbedtls_ssl_session_free( ssl->session );
6020 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006021 }
6022 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006023 ssl->session_negotiate = NULL;
6024
Paul Bakker0a597072012-09-25 21:55:46 +00006025 /*
6026 * Add cache entry
6027 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006028 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006029 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006030 resume == 0 )
6031 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006032 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006034 }
Paul Bakker0a597072012-09-25 21:55:46 +00006035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006038 ssl->handshake->flight != NULL )
6039 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006040 /* Cancel handshake timer */
6041 ssl_set_timer( ssl, 0 );
6042
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006043 /* Keep last flight around in case we need to resend it:
6044 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006046 }
6047 else
6048#endif
6049 ssl_handshake_wrapup_free_hs_transform( ssl );
6050
Paul Bakker48916f92012-09-16 19:57:18 +00006051 ssl->state++;
6052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006054}
6055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006057{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006058 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006061
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006062 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006063
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006064 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006065
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006066 /*
6067 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6068 * may define some other value. Currently (early 2016), no defined
6069 * ciphersuite does this (and this is unlikely to change as activity has
6070 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006075 ssl->verify_data_len = hash_len;
6076 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006077#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006078
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6081 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006082
6083 /*
6084 * In case of session resuming, invert the client and server
6085 * ChangeCipherSpec messages order.
6086 */
Paul Bakker0a597072012-09-25 21:55:46 +00006087 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006090 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006094 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006097 }
6098 else
6099 ssl->state++;
6100
Paul Bakker48916f92012-09-16 19:57:18 +00006101 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006102 * Switch to our negotiated transform and session parameters for outbound
6103 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006109 {
6110 unsigned char i;
6111
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006112 /* Remember current epoch settings for resending */
6113 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006114 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006115
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006116 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006117 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006118
6119 /* Increment epoch */
6120 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006121 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006122 break;
6123
6124 /* The loop goes to its end iff the counter is wrapping */
6125 if( i == 0 )
6126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6128 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006129 }
6130 }
6131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006133 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006134
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006135 ssl->transform_out = ssl->transform_negotiate;
6136 ssl->session_out = ssl->session_negotiate;
6137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6139 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6144 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006145 }
6146 }
6147#endif
6148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006152#endif
6153
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006154 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 return( ret );
6158 }
6159
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006160#if defined(MBEDTLS_SSL_PROTO_DTLS)
6161 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6162 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6163 {
6164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6165 return( ret );
6166 }
6167#endif
6168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170
6171 return( 0 );
6172}
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006175#define SSL_MAX_HASH_LEN 36
6176#else
6177#define SSL_MAX_HASH_LEN 12
6178#endif
6179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006181{
Paul Bakker23986e52011-04-24 08:57:21 +00006182 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006183 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006184 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006187
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006188 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006189
Hanno Becker327c93b2018-08-15 13:56:18 +01006190 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006193 return( ret );
6194 }
6195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006199 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6200 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006202 }
6203
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006204 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205#if defined(MBEDTLS_SSL_PROTO_SSL3)
6206 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006207 hash_len = 36;
6208 else
6209#endif
6210 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6213 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006216 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6217 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 }
6220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006222 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006225 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6226 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 }
6229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006231 ssl->verify_data_len = hash_len;
6232 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006233#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006234
Paul Bakker0a597072012-09-25 21:55:46 +00006235 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006238 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006242 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006244#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 }
6246 else
6247 ssl->state++;
6248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006252#endif
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006255
6256 return( 0 );
6257}
6258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6264 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6265 mbedtls_md5_init( &handshake->fin_md5 );
6266 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006267 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6268 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6271#if defined(MBEDTLS_SHA256_C)
6272 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006273 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275#if defined(MBEDTLS_SHA512_C)
6276 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006277 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006278#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006280
6281 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006282
6283#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6284 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6285 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6286#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288#if defined(MBEDTLS_DHM_C)
6289 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291#if defined(MBEDTLS_ECDH_C)
6292 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006293#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006294#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006295 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006296#if defined(MBEDTLS_SSL_CLI_C)
6297 handshake->ecjpake_cache = NULL;
6298 handshake->ecjpake_cache_len = 0;
6299#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006300#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006301
6302#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6303 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6304#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006305}
6306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6312 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 mbedtls_md_init( &transform->md_ctx_enc );
6315 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006316}
6317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006321}
6322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006324{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006325 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006326 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006328 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006330 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006331 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006332
6333 /*
6334 * Either the pointers are now NULL or cleared properly and can be freed.
6335 * Now allocate missing structures.
6336 */
6337 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006338 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006339 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006340 }
Paul Bakker48916f92012-09-16 19:57:18 +00006341
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006342 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006343 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006344 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006345 }
Paul Bakker48916f92012-09-16 19:57:18 +00006346
Paul Bakker82788fb2014-10-20 13:59:19 +02006347 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006348 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006349 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006350 }
Paul Bakker48916f92012-09-16 19:57:18 +00006351
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006352 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006353 if( ssl->handshake == NULL ||
6354 ssl->transform_negotiate == NULL ||
6355 ssl->session_negotiate == NULL )
6356 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 mbedtls_free( ssl->handshake );
6360 mbedtls_free( ssl->transform_negotiate );
6361 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006362
6363 ssl->handshake = NULL;
6364 ssl->transform_negotiate = NULL;
6365 ssl->session_negotiate = NULL;
6366
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006368 }
6369
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006370 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006372 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006373 ssl_handshake_params_init( ssl->handshake );
6374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6377 {
6378 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006379
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006380 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6381 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6382 else
6383 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006384
6385 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006386 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006387#endif
6388
Paul Bakker48916f92012-09-16 19:57:18 +00006389 return( 0 );
6390}
6391
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006392#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006393/* Dummy cookie callbacks for defaults */
6394static int ssl_cookie_write_dummy( void *ctx,
6395 unsigned char **p, unsigned char *end,
6396 const unsigned char *cli_id, size_t cli_id_len )
6397{
6398 ((void) ctx);
6399 ((void) p);
6400 ((void) end);
6401 ((void) cli_id);
6402 ((void) cli_id_len);
6403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006405}
6406
6407static int ssl_cookie_check_dummy( void *ctx,
6408 const unsigned char *cookie, size_t cookie_len,
6409 const unsigned char *cli_id, size_t cli_id_len )
6410{
6411 ((void) ctx);
6412 ((void) cookie);
6413 ((void) cookie_len);
6414 ((void) cli_id);
6415 ((void) cli_id_len);
6416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006418}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006419#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006420
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006421/* Once ssl->out_hdr as the address of the beginning of the
6422 * next outgoing record is set, deduce the other pointers.
6423 *
6424 * Note: For TLS, we save the implicit record sequence number
6425 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6426 * and the caller has to make sure there's space for this.
6427 */
6428
6429static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6430 mbedtls_ssl_transform *transform )
6431{
6432#if defined(MBEDTLS_SSL_PROTO_DTLS)
6433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6434 {
6435 ssl->out_ctr = ssl->out_hdr + 3;
6436 ssl->out_len = ssl->out_hdr + 11;
6437 ssl->out_iv = ssl->out_hdr + 13;
6438 }
6439 else
6440#endif
6441 {
6442 ssl->out_ctr = ssl->out_hdr - 8;
6443 ssl->out_len = ssl->out_hdr + 3;
6444 ssl->out_iv = ssl->out_hdr + 5;
6445 }
6446
6447 /* Adjust out_msg to make space for explicit IV, if used. */
6448 if( transform != NULL &&
6449 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6450 {
6451 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6452 }
6453 else
6454 ssl->out_msg = ssl->out_iv;
6455}
6456
6457/* Once ssl->in_hdr as the address of the beginning of the
6458 * next incoming record is set, deduce the other pointers.
6459 *
6460 * Note: For TLS, we save the implicit record sequence number
6461 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6462 * and the caller has to make sure there's space for this.
6463 */
6464
6465static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6466 mbedtls_ssl_transform *transform )
6467{
6468#if defined(MBEDTLS_SSL_PROTO_DTLS)
6469 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6470 {
6471 ssl->in_ctr = ssl->in_hdr + 3;
6472 ssl->in_len = ssl->in_hdr + 11;
6473 ssl->in_iv = ssl->in_hdr + 13;
6474 }
6475 else
6476#endif
6477 {
6478 ssl->in_ctr = ssl->in_hdr - 8;
6479 ssl->in_len = ssl->in_hdr + 3;
6480 ssl->in_iv = ssl->in_hdr + 5;
6481 }
6482
6483 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6484 if( transform != NULL &&
6485 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6486 {
6487 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6488 }
6489 else
6490 ssl->in_msg = ssl->in_iv;
6491}
6492
Paul Bakker5121ce52009-01-03 21:22:43 +00006493/*
6494 * Initialize an SSL context
6495 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006496void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6497{
6498 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6499}
6500
6501/*
6502 * Setup an SSL context
6503 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006504
6505static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6506{
6507 /* Set the incoming and outgoing record pointers. */
6508#if defined(MBEDTLS_SSL_PROTO_DTLS)
6509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6510 {
6511 ssl->out_hdr = ssl->out_buf;
6512 ssl->in_hdr = ssl->in_buf;
6513 }
6514 else
6515#endif /* MBEDTLS_SSL_PROTO_DTLS */
6516 {
6517 ssl->out_hdr = ssl->out_buf + 8;
6518 ssl->in_hdr = ssl->in_buf + 8;
6519 }
6520
6521 /* Derive other internal pointers. */
6522 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6523 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6524}
6525
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006526int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006527 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006528{
Paul Bakker48916f92012-09-16 19:57:18 +00006529 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006530
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006531 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006532
6533 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006534 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006535 */
Angus Grattond8213d02016-05-25 20:56:48 +10006536 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6537 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006538 {
Angus Grattond8213d02016-05-25 20:56:48 +10006539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6540 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6541 }
6542
6543 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6544 if( ssl->out_buf == NULL )
6545 {
6546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006548 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006549 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006550 }
6551
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006552 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006553
Paul Bakker48916f92012-09-16 19:57:18 +00006554 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6555 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006556
6557 return( 0 );
6558}
6559
6560/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006561 * Reset an initialized and used SSL context for re-use while retaining
6562 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006563 *
6564 * If partial is non-zero, keep data in the input buffer and client ID.
6565 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006566 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006567static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006568{
Paul Bakker48916f92012-09-16 19:57:18 +00006569 int ret;
6570
Hanno Becker7e772132018-08-10 12:38:21 +01006571#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6572 !defined(MBEDTLS_SSL_SRV_C)
6573 ((void) partial);
6574#endif
6575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006577
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006578 /* Cancel any possibly running timer */
6579 ssl_set_timer( ssl, 0 );
6580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581#if defined(MBEDTLS_SSL_RENEGOTIATION)
6582 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006583 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006584
6585 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6587 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006590
Paul Bakker7eb013f2011-10-06 12:37:39 +00006591 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006592 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006593
6594 ssl->in_msgtype = 0;
6595 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006597 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006598 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006599#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006601 ssl_dtls_replay_reset( ssl );
6602#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006603
6604 ssl->in_hslen = 0;
6605 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006606
6607 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006608
6609 ssl->out_msgtype = 0;
6610 ssl->out_msglen = 0;
6611 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6613 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006614 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006615#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006616
Hanno Becker19859472018-08-06 09:40:20 +01006617 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6618
Paul Bakker48916f92012-09-16 19:57:18 +00006619 ssl->transform_in = NULL;
6620 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006621
Hanno Becker78640902018-08-13 16:35:15 +01006622 ssl->session_in = NULL;
6623 ssl->session_out = NULL;
6624
Angus Grattond8213d02016-05-25 20:56:48 +10006625 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006626
6627#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006628 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006629#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6630 {
6631 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006632 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006633 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6636 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6639 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6642 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006643 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006644 }
6645#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006646
Paul Bakker48916f92012-09-16 19:57:18 +00006647 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 mbedtls_ssl_transform_free( ssl->transform );
6650 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006651 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006652 }
Paul Bakker48916f92012-09-16 19:57:18 +00006653
Paul Bakkerc0463502013-02-14 11:19:38 +01006654 if( ssl->session )
6655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 mbedtls_ssl_session_free( ssl->session );
6657 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006658 ssl->session = NULL;
6659 }
6660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006662 ssl->alpn_chosen = NULL;
6663#endif
6664
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006665#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006666#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006667 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006668#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006669 {
6670 mbedtls_free( ssl->cli_id );
6671 ssl->cli_id = NULL;
6672 ssl->cli_id_len = 0;
6673 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006674#endif
6675
Paul Bakker48916f92012-09-16 19:57:18 +00006676 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6677 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006678
6679 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006680}
6681
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006682/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006683 * Reset an initialized and used SSL context for re-use while retaining
6684 * all application-set variables, function pointers and data.
6685 */
6686int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6687{
6688 return( ssl_session_reset_int( ssl, 0 ) );
6689}
6690
6691/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 * SSL set accessors
6693 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006694void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006695{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006696 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006697}
6698
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006699void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006700{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006701 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006705void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006707 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006708}
6709#endif
6710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006712void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006714 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006715}
6716#endif
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006719
6720void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6721 unsigned allow_packing )
6722{
6723 ssl->disable_datagram_packing = !allow_packing;
6724}
6725
6726void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6727 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006729 conf->hs_timeout_min = min;
6730 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006731}
6732#endif
6733
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006734void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006735{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006736 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006737}
6738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006740void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006741 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006742 void *p_vrfy )
6743{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006744 conf->f_vrfy = f_vrfy;
6745 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006748
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006749void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006750 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 void *p_rng )
6752{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006753 conf->f_rng = f_rng;
6754 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006755}
6756
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006757void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006758 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 void *p_dbg )
6760{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006761 conf->f_dbg = f_dbg;
6762 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006763}
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006766 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006767 mbedtls_ssl_send_t *f_send,
6768 mbedtls_ssl_recv_t *f_recv,
6769 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006770{
6771 ssl->p_bio = p_bio;
6772 ssl->f_send = f_send;
6773 ssl->f_recv = f_recv;
6774 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006775}
6776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006777void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006778{
6779 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006780}
6781
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006782void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6783 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006784 mbedtls_ssl_set_timer_t *f_set_timer,
6785 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006786{
6787 ssl->p_timer = p_timer;
6788 ssl->f_set_timer = f_set_timer;
6789 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006790
6791 /* Make sure we start with no timer running */
6792 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006793}
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006796void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006797 void *p_cache,
6798 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6799 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006801 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006802 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006803 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807#if defined(MBEDTLS_SSL_CLI_C)
6808int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006809{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006810 int ret;
6811
6812 if( ssl == NULL ||
6813 session == NULL ||
6814 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006815 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006818 }
6819
6820 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6821 return( ret );
6822
Paul Bakker0a597072012-09-25 21:55:46 +00006823 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006824
6825 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006829void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006830 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006831{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006832 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6833 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6834 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6835 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006836}
6837
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006838void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006839 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006840 int major, int minor )
6841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006843 return;
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006846 return;
6847
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006848 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006849}
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006852void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006853 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006854{
6855 conf->cert_profile = profile;
6856}
6857
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006858/* Append a new keycert entry to a (possibly empty) list */
6859static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6860 mbedtls_x509_crt *cert,
6861 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006862{
niisato8ee24222018-06-25 19:05:48 +09006863 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006864
niisato8ee24222018-06-25 19:05:48 +09006865 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6866 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006867 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006868
niisato8ee24222018-06-25 19:05:48 +09006869 new_cert->cert = cert;
6870 new_cert->key = key;
6871 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006872
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006873 /* Update head is the list was null, else add to the end */
6874 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006875 {
niisato8ee24222018-06-25 19:05:48 +09006876 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006877 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006878 else
6879 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006880 mbedtls_ssl_key_cert *cur = *head;
6881 while( cur->next != NULL )
6882 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006883 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006884 }
6885
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006886 return( 0 );
6887}
6888
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006889int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006890 mbedtls_x509_crt *own_cert,
6891 mbedtls_pk_context *pk_key )
6892{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006893 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006894}
6895
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006896void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006897 mbedtls_x509_crt *ca_chain,
6898 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006899{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006900 conf->ca_chain = ca_chain;
6901 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006904
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006905#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6906int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6907 mbedtls_x509_crt *own_cert,
6908 mbedtls_pk_context *pk_key )
6909{
6910 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6911 own_cert, pk_key ) );
6912}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006913
6914void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6915 mbedtls_x509_crt *ca_chain,
6916 mbedtls_x509_crl *ca_crl )
6917{
6918 ssl->handshake->sni_ca_chain = ca_chain;
6919 ssl->handshake->sni_ca_crl = ca_crl;
6920}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006921
6922void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6923 int authmode )
6924{
6925 ssl->handshake->sni_authmode = authmode;
6926}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006927#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6928
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006929#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006930/*
6931 * Set EC J-PAKE password for current handshake
6932 */
6933int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6934 const unsigned char *pw,
6935 size_t pw_len )
6936{
6937 mbedtls_ecjpake_role role;
6938
Janos Follath8eb64132016-06-03 15:40:57 +01006939 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6941
6942 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6943 role = MBEDTLS_ECJPAKE_SERVER;
6944 else
6945 role = MBEDTLS_ECJPAKE_CLIENT;
6946
6947 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6948 role,
6949 MBEDTLS_MD_SHA256,
6950 MBEDTLS_ECP_DP_SECP256R1,
6951 pw, pw_len ) );
6952}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006953#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006956int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006957 const unsigned char *psk, size_t psk_len,
6958 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006959{
Paul Bakker6db455e2013-09-18 17:29:31 +02006960 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006965
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006966 /* Identity len will be encoded on two bytes */
6967 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006968 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006969 {
6970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6971 }
6972
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006973 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006974 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006975 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006976
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006977 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006978 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006979 conf->psk_len = 0;
6980 }
6981 if( conf->psk_identity != NULL )
6982 {
6983 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006984 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006985 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006986 }
6987
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006988 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6989 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006990 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006991 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006992 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006993 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006994 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006995 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006996 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006997
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006998 conf->psk_len = psk_len;
6999 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007000
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007001 memcpy( conf->psk, psk, conf->psk_len );
7002 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007003
7004 return( 0 );
7005}
7006
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007007int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7008 const unsigned char *psk, size_t psk_len )
7009{
7010 if( psk == NULL || ssl->handshake == NULL )
7011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7012
7013 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7015
7016 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007017 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007018 mbedtls_platform_zeroize( ssl->handshake->psk,
7019 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007020 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007021 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007022 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007023
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007024 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007025 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007026
7027 ssl->handshake->psk_len = psk_len;
7028 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7029
7030 return( 0 );
7031}
7032
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007033void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007035 size_t),
7036 void *p_psk )
7037{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007038 conf->f_psk = f_psk;
7039 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007042
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007043#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007044
7045#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007046int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007047{
7048 int ret;
7049
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007050 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7051 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7052 {
7053 mbedtls_mpi_free( &conf->dhm_P );
7054 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007055 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
7058 return( 0 );
7059}
Hanno Becker470a8c42017-10-04 15:28:46 +01007060#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007061
Hanno Beckera90658f2017-10-04 15:29:08 +01007062int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7063 const unsigned char *dhm_P, size_t P_len,
7064 const unsigned char *dhm_G, size_t G_len )
7065{
7066 int ret;
7067
7068 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7069 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7070 {
7071 mbedtls_mpi_free( &conf->dhm_P );
7072 mbedtls_mpi_free( &conf->dhm_G );
7073 return( ret );
7074 }
7075
7076 return( 0 );
7077}
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007079int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007080{
7081 int ret;
7082
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007083 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7084 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7085 {
7086 mbedtls_mpi_free( &conf->dhm_P );
7087 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007088 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007089 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007090
7091 return( 0 );
7092}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007093#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007094
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007095#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7096/*
7097 * Set the minimum length for Diffie-Hellman parameters
7098 */
7099void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7100 unsigned int bitlen )
7101{
7102 conf->dhm_min_bitlen = bitlen;
7103}
7104#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7105
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007106#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007107/*
7108 * Set allowed/preferred hashes for handshake signatures
7109 */
7110void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7111 const int *hashes )
7112{
7113 conf->sig_hashes = hashes;
7114}
Hanno Becker947194e2017-04-07 13:25:49 +01007115#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007116
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007117#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007118/*
7119 * Set the allowed elliptic curves
7120 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007121void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007122 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007124 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007125}
Hanno Becker947194e2017-04-07 13:25:49 +01007126#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007127
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007128#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007130{
Hanno Becker947194e2017-04-07 13:25:49 +01007131 /* Initialize to suppress unnecessary compiler warning */
7132 size_t hostname_len = 0;
7133
7134 /* Check if new hostname is valid before
7135 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007136 if( hostname != NULL )
7137 {
7138 hostname_len = strlen( hostname );
7139
7140 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7142 }
7143
7144 /* Now it's clear that we will overwrite the old hostname,
7145 * so we can free it safely */
7146
7147 if( ssl->hostname != NULL )
7148 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007149 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007150 mbedtls_free( ssl->hostname );
7151 }
7152
7153 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007154
Paul Bakker5121ce52009-01-03 21:22:43 +00007155 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007156 {
7157 ssl->hostname = NULL;
7158 }
7159 else
7160 {
7161 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007162 if( ssl->hostname == NULL )
7163 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007164
Hanno Becker947194e2017-04-07 13:25:49 +01007165 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007166
Hanno Becker947194e2017-04-07 13:25:49 +01007167 ssl->hostname[hostname_len] = '\0';
7168 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007169
7170 return( 0 );
7171}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007173
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007174#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007175void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007177 const unsigned char *, size_t),
7178 void *p_sni )
7179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007180 conf->f_sni = f_sni;
7181 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007186int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007187{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007188 size_t cur_len, tot_len;
7189 const char **p;
7190
7191 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007192 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7193 * MUST NOT be truncated."
7194 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007195 */
7196 tot_len = 0;
7197 for( p = protos; *p != NULL; p++ )
7198 {
7199 cur_len = strlen( *p );
7200 tot_len += cur_len;
7201
7202 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007204 }
7205
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007206 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007207
7208 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007209}
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007212{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007213 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007216
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007217void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007218{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007219 conf->max_major_ver = major;
7220 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007221}
7222
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007223void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007224{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007225 conf->min_major_ver = major;
7226 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007227}
7228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007230void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007231{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007232 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007233}
7234#endif
7235
Janos Follath088ce432017-04-10 12:42:31 +01007236#if defined(MBEDTLS_SSL_SRV_C)
7237void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7238 char cert_req_ca_list )
7239{
7240 conf->cert_req_ca_list = cert_req_ca_list;
7241}
7242#endif
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007245void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007246{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007247 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007248}
7249#endif
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007252void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007254 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007255}
7256#endif
7257
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007258#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007259void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007261 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007262}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007263#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007264
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007265#if defined(MBEDTLS_SSL_PROTO_DTLS)
7266void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7267{
7268 conf->mtu = mtu;
7269}
7270#endif
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007273int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007276 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007279 }
7280
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007281 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007282
7283 return( 0 );
7284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007288void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007290 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007295void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007296{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007297 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007298}
7299#endif
7300
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007301void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007303 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007304}
7305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007307void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007309 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007310}
7311
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007312void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007314 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007315}
7316
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007317void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007318 const unsigned char period[8] )
7319{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007320 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007321}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007325#if defined(MBEDTLS_SSL_CLI_C)
7326void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007327{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007328 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007329}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007330#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007331
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007332#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007333void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7334 mbedtls_ssl_ticket_write_t *f_ticket_write,
7335 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7336 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007337{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007338 conf->f_ticket_write = f_ticket_write;
7339 conf->f_ticket_parse = f_ticket_parse;
7340 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007341}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007344
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007345#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7346void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7347 mbedtls_ssl_export_keys_t *f_export_keys,
7348 void *p_export_keys )
7349{
7350 conf->f_export_keys = f_export_keys;
7351 conf->p_export_keys = p_export_keys;
7352}
7353#endif
7354
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007355#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007356void mbedtls_ssl_conf_async_private_cb(
7357 mbedtls_ssl_config *conf,
7358 mbedtls_ssl_async_sign_t *f_async_sign,
7359 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7360 mbedtls_ssl_async_resume_t *f_async_resume,
7361 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007362 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007363{
7364 conf->f_async_sign_start = f_async_sign;
7365 conf->f_async_decrypt_start = f_async_decrypt;
7366 conf->f_async_resume = f_async_resume;
7367 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007368 conf->p_async_config_data = async_config_data;
7369}
7370
Gilles Peskine8f97af72018-04-26 11:46:10 +02007371void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7372{
7373 return( conf->p_async_config_data );
7374}
7375
Gilles Peskine1febfef2018-04-30 11:54:39 +02007376void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007377{
7378 if( ssl->handshake == NULL )
7379 return( NULL );
7380 else
7381 return( ssl->handshake->user_async_ctx );
7382}
7383
Gilles Peskine1febfef2018-04-30 11:54:39 +02007384void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007385 void *ctx )
7386{
7387 if( ssl->handshake != NULL )
7388 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007389}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007390#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007391
Paul Bakker5121ce52009-01-03 21:22:43 +00007392/*
7393 * SSL get accessors
7394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007396{
7397 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7398}
7399
Hanno Becker8b170a02017-10-10 11:51:19 +01007400int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7401{
7402 /*
7403 * Case A: We're currently holding back
7404 * a message for further processing.
7405 */
7406
7407 if( ssl->keep_current_message == 1 )
7408 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007410 return( 1 );
7411 }
7412
7413 /*
7414 * Case B: Further records are pending in the current datagram.
7415 */
7416
7417#if defined(MBEDTLS_SSL_PROTO_DTLS)
7418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7419 ssl->in_left > ssl->next_record_offset )
7420 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007422 return( 1 );
7423 }
7424#endif /* MBEDTLS_SSL_PROTO_DTLS */
7425
7426 /*
7427 * Case C: A handshake message is being processed.
7428 */
7429
Hanno Becker8b170a02017-10-10 11:51:19 +01007430 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7431 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007433 return( 1 );
7434 }
7435
7436 /*
7437 * Case D: An application data message is being processed
7438 */
7439 if( ssl->in_offt != NULL )
7440 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007442 return( 1 );
7443 }
7444
7445 /*
7446 * In all other cases, the rest of the message can be dropped.
7447 * As in ssl_read_record_layer, this needs to be adapted if
7448 * we implement support for multiple alerts in single records.
7449 */
7450
7451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7452 return( 0 );
7453}
7454
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007455uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007456{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007457 if( ssl->session != NULL )
7458 return( ssl->session->verify_result );
7459
7460 if( ssl->session_negotiate != NULL )
7461 return( ssl->session_negotiate->verify_result );
7462
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007463 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007464}
7465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007467{
Paul Bakker926c8e42013-03-06 10:23:34 +01007468 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007469 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007472}
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007475{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007477 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007478 {
7479 switch( ssl->minor_ver )
7480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007482 return( "DTLSv1.0" );
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007485 return( "DTLSv1.2" );
7486
7487 default:
7488 return( "unknown (DTLS)" );
7489 }
7490 }
7491#endif
7492
Paul Bakker43ca69c2011-01-15 17:35:19 +00007493 switch( ssl->minor_ver )
7494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007496 return( "SSLv3.0" );
7497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007499 return( "TLSv1.0" );
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007502 return( "TLSv1.1" );
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007505 return( "TLSv1.2" );
7506
Paul Bakker43ca69c2011-01-15 17:35:19 +00007507 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007508 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007509 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007510}
7511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007513{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007514 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007516 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007517
Hanno Becker78640902018-08-13 16:35:15 +01007518 if( transform == NULL )
7519 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_ZLIB_SUPPORT)
7522 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7523 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007524 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007525#endif
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 case MBEDTLS_MODE_GCM:
7530 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007531 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007533 transform_expansion = transform->minlen;
7534 break;
7535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007537
7538 block_size = mbedtls_cipher_get_block_size(
7539 &transform->cipher_ctx_enc );
7540
7541#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7542 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7543 {
7544 /* Expansion due to addition of
7545 * - MAC
7546 * - CBC padding (theoretically up to 256 bytes, but
7547 * we never use more than block_size)
7548 * - explicit IV
7549 */
7550 transform_expansion = transform->maclen + 2 * block_size;
7551 }
7552 else
7553#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7554 {
7555 /* No explicit IV prior to TLS 1.1. */
7556 transform_expansion = transform->maclen + block_size;
7557 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007558 break;
7559
7560 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007563 }
7564
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007565 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007566}
7567
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007568#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7569size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7570{
7571 size_t max_len;
7572
7573 /*
7574 * Assume mfl_code is correct since it was checked when set
7575 */
Angus Grattond8213d02016-05-25 20:56:48 +10007576 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007577
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007578 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007579 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007580 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007581 {
Angus Grattond8213d02016-05-25 20:56:48 +10007582 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007583 }
7584
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007585 /* During a handshake, use the value being negotiated */
7586 if( ssl->session_negotiate != NULL &&
7587 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7588 {
7589 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7590 }
7591
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007592 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007593}
7594#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7595
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007596int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7597{
7598 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7599
7600#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7601 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7602
7603 if( max_len > mfl )
7604 max_len = mfl;
7605#endif
7606
7607#if defined(MBEDTLS_SSL_PROTO_DTLS)
7608 if( ssl->conf->mtu != 0 )
7609 {
7610 const size_t mtu = ssl->conf->mtu;
7611 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7612 const size_t overhead = (size_t) ret;
7613
7614 if( ret < 0 )
7615 return( ret );
7616
7617 if( mtu <= overhead )
7618 {
7619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7620 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7621 }
7622
7623 if( max_len > mtu - overhead )
7624 max_len = mtu - overhead;
7625 }
7626#endif
7627
Hanno Becker0defedb2018-08-10 12:35:02 +01007628#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7629 !defined(MBEDTLS_SSL_PROTO_DTLS)
7630 ((void) ssl);
7631#endif
7632
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007633 return( (int) max_len );
7634}
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#if defined(MBEDTLS_X509_CRT_PARSE_C)
7637const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007638{
7639 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007640 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007641
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007642 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_SSL_CLI_C)
7647int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007648{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007649 if( ssl == NULL ||
7650 dst == NULL ||
7651 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007652 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007655 }
7656
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007657 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007660
Paul Bakker5121ce52009-01-03 21:22:43 +00007661/*
Paul Bakker1961b702013-01-25 14:49:24 +01007662 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007667
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007668 if( ssl == NULL || ssl->conf == NULL )
7669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007672 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007676 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007678#endif
7679
Paul Bakker1961b702013-01-25 14:49:24 +01007680 return( ret );
7681}
7682
7683/*
7684 * Perform the SSL handshake
7685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007687{
7688 int ret = 0;
7689
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007690 if( ssl == NULL || ssl->conf == NULL )
7691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007698
7699 if( ret != 0 )
7700 break;
7701 }
7702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007704
7705 return( ret );
7706}
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#if defined(MBEDTLS_SSL_RENEGOTIATION)
7709#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007710/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007711 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007714{
7715 int ret;
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007718
7719 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7721 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007722
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007723 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007724 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007726 return( ret );
7727 }
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007730
7731 return( 0 );
7732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007734
7735/*
7736 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 * - any side: calling mbedtls_ssl_renegotiate(),
7738 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7739 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007740 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007741 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007745{
7746 int ret;
7747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007749
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007750 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7751 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007752
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007753 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7754 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007758 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007759 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007760 ssl->handshake->out_msg_seq = 1;
7761 else
7762 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007763 }
7764#endif
7765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7767 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007772 return( ret );
7773 }
7774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007776
7777 return( 0 );
7778}
7779
7780/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007781 * Renegotiate current connection on client,
7782 * or request renegotiation on server
7783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007787
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007788 if( ssl == NULL || ssl->conf == NULL )
7789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007792 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007793 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007799
7800 /* Did we already try/start sending HelloRequest? */
7801 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007803
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007804 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007805 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007809 /*
7810 * On client, either start the renegotiation process or,
7811 * if already in progress, continue the handshake
7812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7816 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007817
7818 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007821 return( ret );
7822 }
7823 }
7824 else
7825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007829 return( ret );
7830 }
7831 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007833
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007834 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007835}
7836
7837/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007838 * Check record counters and renegotiate if they're above the limit.
7839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007841{
Andres AG2196c7f2016-12-15 17:01:16 +00007842 size_t ep_len = ssl_ep_len( ssl );
7843 int in_ctr_cmp;
7844 int out_ctr_cmp;
7845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7847 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007848 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007849 {
7850 return( 0 );
7851 }
7852
Andres AG2196c7f2016-12-15 17:01:16 +00007853 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7854 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007855 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007856 ssl->conf->renego_period + ep_len, 8 - ep_len );
7857
7858 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007859 {
7860 return( 0 );
7861 }
7862
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007867
7868/*
7869 * Receive application data decrypted from the SSL layer
7870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007872{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007873 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007874 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007875
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007876 if( ssl == NULL || ssl->conf == NULL )
7877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007885 return( ret );
7886
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007887 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007889 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007890 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007891 return( ret );
7892 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007893 }
7894#endif
7895
Hanno Becker4a810fb2017-05-24 16:27:30 +01007896 /*
7897 * Check if renegotiation is necessary and/or handshake is
7898 * in process. If yes, perform/continue, and fall through
7899 * if an unexpected packet is received while the client
7900 * is waiting for the ServerHello.
7901 *
7902 * (There is no equivalent to the last condition on
7903 * the server-side as it is not treated as within
7904 * a handshake while waiting for the ClientHello
7905 * after a renegotiation request.)
7906 */
7907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007909 ret = ssl_check_ctr_renegotiate( ssl );
7910 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7911 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007914 return( ret );
7915 }
7916#endif
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007921 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7922 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007925 return( ret );
7926 }
7927 }
7928
Hanno Beckere41158b2017-10-23 13:30:32 +01007929 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007930 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007931 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007932 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007933 if( ssl->f_get_timer != NULL &&
7934 ssl->f_get_timer( ssl->p_timer ) == -1 )
7935 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007936 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007937 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007938
Hanno Becker327c93b2018-08-15 13:56:18 +01007939 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007940 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007941 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7942 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007943
Hanno Becker4a810fb2017-05-24 16:27:30 +01007944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7945 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007946 }
7947
7948 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007950 {
7951 /*
7952 * OpenSSL sends empty messages to randomize the IV
7953 */
Hanno Becker327c93b2018-08-15 13:56:18 +01007954 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007957 return( 0 );
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007960 return( ret );
7961 }
7962 }
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007967
Hanno Becker4a810fb2017-05-24 16:27:30 +01007968 /*
7969 * - For client-side, expect SERVER_HELLO_REQUEST.
7970 * - For server-side, expect CLIENT_HELLO.
7971 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7972 */
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007977 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007980
7981 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007984 {
7985 continue;
7986 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007989 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007990#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007991
Hanno Becker4a810fb2017-05-24 16:27:30 +01007992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007993 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007997
7998 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008001 {
8002 continue;
8003 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008006 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008007#endif /* MBEDTLS_SSL_SRV_C */
8008
Hanno Becker21df7f92017-10-17 11:03:26 +01008009#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008010 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008011 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8012 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8013 ssl->conf->allow_legacy_renegotiation ==
8014 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8015 {
8016 /*
8017 * Accept renegotiation request
8018 */
Paul Bakker48916f92012-09-16 19:57:18 +00008019
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008020 /* DTLS clients need to know renego is server-initiated */
8021#if defined(MBEDTLS_SSL_PROTO_DTLS)
8022 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8023 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8024 {
8025 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8026 }
8027#endif
8028 ret = ssl_start_renegotiation( ssl );
8029 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8030 ret != 0 )
8031 {
8032 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8033 return( ret );
8034 }
8035 }
8036 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008037#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008038 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008039 /*
8040 * Refuse renegotiation
8041 */
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#if defined(MBEDTLS_SSL_PROTO_SSL3)
8046 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008047 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008048 /* SSLv3 does not have a "no_renegotiation" warning, so
8049 we send a fatal alert and abort the connection. */
8050 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8051 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8052 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008053 }
8054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8058 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8061 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8062 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008063 {
8064 return( ret );
8065 }
Paul Bakker48916f92012-09-16 19:57:18 +00008066 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008067 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8069 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008073 }
Paul Bakker48916f92012-09-16 19:57:18 +00008074 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008075
Hanno Becker90333da2017-10-10 11:27:13 +01008076 /* At this point, we don't know whether the renegotiation has been
8077 * completed or not. The cases to consider are the following:
8078 * 1) The renegotiation is complete. In this case, no new record
8079 * has been read yet.
8080 * 2) The renegotiation is incomplete because the client received
8081 * an application data record while awaiting the ServerHello.
8082 * 3) The renegotiation is incomplete because the client received
8083 * a non-handshake, non-application data message while awaiting
8084 * the ServerHello.
8085 * In each of these case, looping will be the proper action:
8086 * - For 1), the next iteration will read a new record and check
8087 * if it's application data.
8088 * - For 2), the loop condition isn't satisfied as application data
8089 * is present, hence continue is the same as break
8090 * - For 3), the loop condition is satisfied and read_record
8091 * will re-deliver the message that was held back by the client
8092 * when expecting the ServerHello.
8093 */
8094 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008095 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008096#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008098 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008099 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008100 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008101 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008104 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008106 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008107 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8112 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008115 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008116 }
8117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8121 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 }
8123
8124 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008125
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008126 /* We're going to return something now, cancel timer,
8127 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008129 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008130
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008131#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008132 /* If we requested renego but received AppData, resend HelloRequest.
8133 * Do it now, after setting in_offt, to avoid taking this branch
8134 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008136 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008138 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008139 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008142 return( ret );
8143 }
8144 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008146#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008147 }
8148
8149 n = ( len < ssl->in_msglen )
8150 ? len : ssl->in_msglen;
8151
8152 memcpy( buf, ssl->in_offt, n );
8153 ssl->in_msglen -= n;
8154
8155 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008156 {
8157 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008158 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008159 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008161 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008162 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008163 /* more data available */
8164 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008168
Paul Bakker23986e52011-04-24 08:57:21 +00008169 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008170}
8171
8172/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008173 * Send application data to be encrypted by the SSL layer, taking care of max
8174 * fragment length and buffer size.
8175 *
8176 * According to RFC 5246 Section 6.2.1:
8177 *
8178 * Zero-length fragments of Application data MAY be sent as they are
8179 * potentially useful as a traffic analysis countermeasure.
8180 *
8181 * Therefore, it is possible that the input message length is 0 and the
8182 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008183 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008184static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008185 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008186{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008187 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8188 const size_t max_len = (size_t) ret;
8189
8190 if( ret < 0 )
8191 {
8192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8193 return( ret );
8194 }
8195
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008196 if( len > max_len )
8197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008202 "maximum fragment length: %d > %d",
8203 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008205 }
8206 else
8207#endif
8208 len = max_len;
8209 }
Paul Bakker887bd502011-06-08 13:10:54 +00008210
Paul Bakker5121ce52009-01-03 21:22:43 +00008211 if( ssl->out_left != 0 )
8212 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008213 /*
8214 * The user has previously tried to send the data and
8215 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8216 * written. In this case, we expect the high-level write function
8217 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008222 return( ret );
8223 }
8224 }
Paul Bakker887bd502011-06-08 13:10:54 +00008225 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008226 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008227 /*
8228 * The user is trying to send a message the first time, so we need to
8229 * copy the data into the internal buffers and setup the data structure
8230 * to keep track of partial writes
8231 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008232 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008234 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008235
Hanno Becker67bc7c32018-08-06 11:33:50 +01008236 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008239 return( ret );
8240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008241 }
8242
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008243 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008244}
8245
8246/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008247 * Write application data, doing 1/n-1 splitting if necessary.
8248 *
8249 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008250 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008251 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008254static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008255 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008256{
8257 int ret;
8258
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008259 if( ssl->conf->cbc_record_splitting ==
8260 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008261 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8263 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8264 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008265 {
8266 return( ssl_write_real( ssl, buf, len ) );
8267 }
8268
8269 if( ssl->split_done == 0 )
8270 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008271 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008272 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008273 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008274 }
8275
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008276 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8277 return( ret );
8278 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008279
8280 return( ret + 1 );
8281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008283
8284/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008285 * Write application data (public-facing wrapper)
8286 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008287int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008288{
8289 int ret;
8290
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008292
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008293 if( ssl == NULL || ssl->conf == NULL )
8294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8295
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008296#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008297 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8298 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008299 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008300 return( ret );
8301 }
8302#endif
8303
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008304 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008305 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008306 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008307 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008309 return( ret );
8310 }
8311 }
8312
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008313#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008314 ret = ssl_write_split( ssl, buf, len );
8315#else
8316 ret = ssl_write_real( ssl, buf, len );
8317#endif
8318
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008320
8321 return( ret );
8322}
8323
8324/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008325 * Notify the peer that the connection is being closed
8326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008328{
8329 int ret;
8330
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008331 if( ssl == NULL || ssl->conf == NULL )
8332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008335
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008336 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8342 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8343 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008346 return( ret );
8347 }
8348 }
8349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008351
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008352 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008353}
8354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008356{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008357 if( transform == NULL )
8358 return;
8359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008361 deflateEnd( &transform->ctx_deflate );
8362 inflateEnd( &transform->ctx_inflate );
8363#endif
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8366 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 mbedtls_md_free( &transform->md_ctx_enc );
8369 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008370
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008371 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008372}
8373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_X509_CRT_PARSE_C)
8375static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008378
8379 while( cur != NULL )
8380 {
8381 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008383 cur = next;
8384 }
8385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008387
Hanno Becker0271f962018-08-16 13:23:47 +01008388#if defined(MBEDTLS_SSL_PROTO_DTLS)
8389
8390static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8391{
8392 unsigned offset;
8393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8394
8395 if( hs == NULL )
8396 return;
8397
8398 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
8399 {
8400 mbedtls_ssl_hs_buffer *hs_buf = &hs->buffering.hs[offset];
8401 if( hs_buf->is_valid == 1 )
8402 {
8403 mbedtls_free( hs_buf->data );
8404 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
8405 }
8406 }
8407}
8408
8409#endif /* MBEDTLS_SSL_PROTO_DTLS */
8410
Gilles Peskine9b562d52018-04-25 20:32:43 +02008411void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008412{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008413 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8414
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008415 if( handshake == NULL )
8416 return;
8417
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008418#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8419 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8420 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008421 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008422 handshake->async_in_progress = 0;
8423 }
8424#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8425
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008426#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8427 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8428 mbedtls_md5_free( &handshake->fin_md5 );
8429 mbedtls_sha1_free( &handshake->fin_sha1 );
8430#endif
8431#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8432#if defined(MBEDTLS_SHA256_C)
8433 mbedtls_sha256_free( &handshake->fin_sha256 );
8434#endif
8435#if defined(MBEDTLS_SHA512_C)
8436 mbedtls_sha512_free( &handshake->fin_sha512 );
8437#endif
8438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440#if defined(MBEDTLS_DHM_C)
8441 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008442#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#if defined(MBEDTLS_ECDH_C)
8444 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008445#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008446#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008447 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008448#if defined(MBEDTLS_SSL_CLI_C)
8449 mbedtls_free( handshake->ecjpake_cache );
8450 handshake->ecjpake_cache = NULL;
8451 handshake->ecjpake_cache_len = 0;
8452#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008453#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008454
Janos Follath4ae5c292016-02-10 11:27:43 +00008455#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8456 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008457 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008459#endif
8460
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008461#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8462 if( handshake->psk != NULL )
8463 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008464 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008465 mbedtls_free( handshake->psk );
8466 }
8467#endif
8468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8470 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008471 /*
8472 * Free only the linked list wrapper, not the keys themselves
8473 * since the belong to the SNI callback
8474 */
8475 if( handshake->sni_key_cert != NULL )
8476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008478
8479 while( cur != NULL )
8480 {
8481 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008483 cur = next;
8484 }
8485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488#if defined(MBEDTLS_SSL_PROTO_DTLS)
8489 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008490 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008491 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008492#endif
8493
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008494 mbedtls_platform_zeroize( handshake,
8495 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008496}
8497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008499{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008500 if( session == NULL )
8501 return;
8502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008504 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 mbedtls_x509_crt_free( session->peer_cert );
8507 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008508 }
Paul Bakkered27a042013-04-18 22:46:23 +02008509#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008510
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008511#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008513#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008514
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008515 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008516}
8517
Paul Bakker5121ce52009-01-03 21:22:43 +00008518/*
8519 * Free an SSL context
8520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008522{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008523 if( ssl == NULL )
8524 return;
8525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008527
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008528 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008529 {
Angus Grattond8213d02016-05-25 20:56:48 +10008530 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008532 }
8533
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008534 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008535 {
Angus Grattond8213d02016-05-25 20:56:48 +10008536 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008538 }
8539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008541 if( ssl->compress_buf != NULL )
8542 {
Angus Grattond8213d02016-05-25 20:56:48 +10008543 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008545 }
8546#endif
8547
Paul Bakker48916f92012-09-16 19:57:18 +00008548 if( ssl->transform )
8549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550 mbedtls_ssl_transform_free( ssl->transform );
8551 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008552 }
8553
8554 if( ssl->handshake )
8555 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008556 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8558 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 mbedtls_free( ssl->handshake );
8561 mbedtls_free( ssl->transform_negotiate );
8562 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008563 }
8564
Paul Bakkerc0463502013-02-14 11:19:38 +01008565 if( ssl->session )
8566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 mbedtls_ssl_session_free( ssl->session );
8568 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008569 }
8570
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008571#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008572 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008573 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008574 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008576 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8580 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8583 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008584 }
8585#endif
8586
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008587#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008589#endif
8590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008592
Paul Bakker86f04f42013-02-14 11:20:09 +01008593 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008594 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008595}
8596
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008597/*
8598 * Initialze mbedtls_ssl_config
8599 */
8600void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8601{
8602 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8603}
8604
Simon Butcherc97b6972015-12-27 23:48:17 +00008605#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008606static int ssl_preset_default_hashes[] = {
8607#if defined(MBEDTLS_SHA512_C)
8608 MBEDTLS_MD_SHA512,
8609 MBEDTLS_MD_SHA384,
8610#endif
8611#if defined(MBEDTLS_SHA256_C)
8612 MBEDTLS_MD_SHA256,
8613 MBEDTLS_MD_SHA224,
8614#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008615#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008616 MBEDTLS_MD_SHA1,
8617#endif
8618 MBEDTLS_MD_NONE
8619};
Simon Butcherc97b6972015-12-27 23:48:17 +00008620#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008621
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008622static int ssl_preset_suiteb_ciphersuites[] = {
8623 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8624 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8625 0
8626};
8627
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008628#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008629static int ssl_preset_suiteb_hashes[] = {
8630 MBEDTLS_MD_SHA256,
8631 MBEDTLS_MD_SHA384,
8632 MBEDTLS_MD_NONE
8633};
8634#endif
8635
8636#if defined(MBEDTLS_ECP_C)
8637static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8638 MBEDTLS_ECP_DP_SECP256R1,
8639 MBEDTLS_ECP_DP_SECP384R1,
8640 MBEDTLS_ECP_DP_NONE
8641};
8642#endif
8643
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008644/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008645 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008646 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008647int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008648 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008649{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008650#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008651 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008652#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008653
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008654 /* Use the functions here so that they are covered in tests,
8655 * but otherwise access member directly for efficiency */
8656 mbedtls_ssl_conf_endpoint( conf, endpoint );
8657 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008658
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008659 /*
8660 * Things that are common to all presets
8661 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008662#if defined(MBEDTLS_SSL_CLI_C)
8663 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8664 {
8665 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8666#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8667 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8668#endif
8669 }
8670#endif
8671
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008672#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008673 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008674#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008675
8676#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8677 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8678#endif
8679
8680#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8681 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8682#endif
8683
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008684#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8685 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8686#endif
8687
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008688#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008689 conf->f_cookie_write = ssl_cookie_write_dummy;
8690 conf->f_cookie_check = ssl_cookie_check_dummy;
8691#endif
8692
8693#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8694 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8695#endif
8696
Janos Follath088ce432017-04-10 12:42:31 +01008697#if defined(MBEDTLS_SSL_SRV_C)
8698 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8699#endif
8700
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008701#if defined(MBEDTLS_SSL_PROTO_DTLS)
8702 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8703 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8704#endif
8705
8706#if defined(MBEDTLS_SSL_RENEGOTIATION)
8707 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008708 memset( conf->renego_period, 0x00, 2 );
8709 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008710#endif
8711
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008712#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8713 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8714 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008715 const unsigned char dhm_p[] =
8716 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8717 const unsigned char dhm_g[] =
8718 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8719
Hanno Beckera90658f2017-10-04 15:29:08 +01008720 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8721 dhm_p, sizeof( dhm_p ),
8722 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008723 {
8724 return( ret );
8725 }
8726 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008727#endif
8728
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008729 /*
8730 * Preset-specific defaults
8731 */
8732 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008733 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008734 /*
8735 * NSA Suite B
8736 */
8737 case MBEDTLS_SSL_PRESET_SUITEB:
8738 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8739 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8740 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8741 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8742
8743 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8744 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8745 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8746 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8747 ssl_preset_suiteb_ciphersuites;
8748
8749#if defined(MBEDTLS_X509_CRT_PARSE_C)
8750 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008751#endif
8752
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008753#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008754 conf->sig_hashes = ssl_preset_suiteb_hashes;
8755#endif
8756
8757#if defined(MBEDTLS_ECP_C)
8758 conf->curve_list = ssl_preset_suiteb_curves;
8759#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008760 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008761
8762 /*
8763 * Default
8764 */
8765 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008766 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8767 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8768 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8769 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8770 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8771 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8772 MBEDTLS_SSL_MIN_MINOR_VERSION :
8773 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008774 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8775 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8776
8777#if defined(MBEDTLS_SSL_PROTO_DTLS)
8778 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8779 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8780#endif
8781
8782 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8783 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8784 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8785 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8786 mbedtls_ssl_list_ciphersuites();
8787
8788#if defined(MBEDTLS_X509_CRT_PARSE_C)
8789 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8790#endif
8791
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008792#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008793 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008794#endif
8795
8796#if defined(MBEDTLS_ECP_C)
8797 conf->curve_list = mbedtls_ecp_grp_id_list();
8798#endif
8799
8800#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8801 conf->dhm_min_bitlen = 1024;
8802#endif
8803 }
8804
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008805 return( 0 );
8806}
8807
8808/*
8809 * Free mbedtls_ssl_config
8810 */
8811void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8812{
8813#if defined(MBEDTLS_DHM_C)
8814 mbedtls_mpi_free( &conf->dhm_P );
8815 mbedtls_mpi_free( &conf->dhm_G );
8816#endif
8817
8818#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8819 if( conf->psk != NULL )
8820 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008821 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008822 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008823 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008824 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008825 }
8826
8827 if( conf->psk_identity != NULL )
8828 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008829 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008830 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008831 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008832 conf->psk_identity_len = 0;
8833 }
8834#endif
8835
8836#if defined(MBEDTLS_X509_CRT_PARSE_C)
8837 ssl_key_cert_free( conf->key_cert );
8838#endif
8839
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008840 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008841}
8842
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008843#if defined(MBEDTLS_PK_C) && \
8844 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008845/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008849{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850#if defined(MBEDTLS_RSA_C)
8851 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8852 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#if defined(MBEDTLS_ECDSA_C)
8855 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8856 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008859}
8860
Hanno Becker7e5437a2017-04-28 17:15:26 +01008861unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8862{
8863 switch( type ) {
8864 case MBEDTLS_PK_RSA:
8865 return( MBEDTLS_SSL_SIG_RSA );
8866 case MBEDTLS_PK_ECDSA:
8867 case MBEDTLS_PK_ECKEY:
8868 return( MBEDTLS_SSL_SIG_ECDSA );
8869 default:
8870 return( MBEDTLS_SSL_SIG_ANON );
8871 }
8872}
8873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008875{
8876 switch( sig )
8877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878#if defined(MBEDTLS_RSA_C)
8879 case MBEDTLS_SSL_SIG_RSA:
8880 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882#if defined(MBEDTLS_ECDSA_C)
8883 case MBEDTLS_SSL_SIG_ECDSA:
8884 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008885#endif
8886 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008888 }
8889}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008890#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008891
Hanno Becker7e5437a2017-04-28 17:15:26 +01008892#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8893 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8894
8895/* Find an entry in a signature-hash set matching a given hash algorithm. */
8896mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8897 mbedtls_pk_type_t sig_alg )
8898{
8899 switch( sig_alg )
8900 {
8901 case MBEDTLS_PK_RSA:
8902 return( set->rsa );
8903 case MBEDTLS_PK_ECDSA:
8904 return( set->ecdsa );
8905 default:
8906 return( MBEDTLS_MD_NONE );
8907 }
8908}
8909
8910/* Add a signature-hash-pair to a signature-hash set */
8911void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8912 mbedtls_pk_type_t sig_alg,
8913 mbedtls_md_type_t md_alg )
8914{
8915 switch( sig_alg )
8916 {
8917 case MBEDTLS_PK_RSA:
8918 if( set->rsa == MBEDTLS_MD_NONE )
8919 set->rsa = md_alg;
8920 break;
8921
8922 case MBEDTLS_PK_ECDSA:
8923 if( set->ecdsa == MBEDTLS_MD_NONE )
8924 set->ecdsa = md_alg;
8925 break;
8926
8927 default:
8928 break;
8929 }
8930}
8931
8932/* Allow exactly one hash algorithm for each signature. */
8933void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8934 mbedtls_md_type_t md_alg )
8935{
8936 set->rsa = md_alg;
8937 set->ecdsa = md_alg;
8938}
8939
8940#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8941 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8942
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008943/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008944 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008947{
8948 switch( hash )
8949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950#if defined(MBEDTLS_MD5_C)
8951 case MBEDTLS_SSL_HASH_MD5:
8952 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_SHA1_C)
8955 case MBEDTLS_SSL_HASH_SHA1:
8956 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008957#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958#if defined(MBEDTLS_SHA256_C)
8959 case MBEDTLS_SSL_HASH_SHA224:
8960 return( MBEDTLS_MD_SHA224 );
8961 case MBEDTLS_SSL_HASH_SHA256:
8962 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008963#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#if defined(MBEDTLS_SHA512_C)
8965 case MBEDTLS_SSL_HASH_SHA384:
8966 return( MBEDTLS_MD_SHA384 );
8967 case MBEDTLS_SSL_HASH_SHA512:
8968 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008969#endif
8970 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008972 }
8973}
8974
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008975/*
8976 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8977 */
8978unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8979{
8980 switch( md )
8981 {
8982#if defined(MBEDTLS_MD5_C)
8983 case MBEDTLS_MD_MD5:
8984 return( MBEDTLS_SSL_HASH_MD5 );
8985#endif
8986#if defined(MBEDTLS_SHA1_C)
8987 case MBEDTLS_MD_SHA1:
8988 return( MBEDTLS_SSL_HASH_SHA1 );
8989#endif
8990#if defined(MBEDTLS_SHA256_C)
8991 case MBEDTLS_MD_SHA224:
8992 return( MBEDTLS_SSL_HASH_SHA224 );
8993 case MBEDTLS_MD_SHA256:
8994 return( MBEDTLS_SSL_HASH_SHA256 );
8995#endif
8996#if defined(MBEDTLS_SHA512_C)
8997 case MBEDTLS_MD_SHA384:
8998 return( MBEDTLS_SSL_HASH_SHA384 );
8999 case MBEDTLS_MD_SHA512:
9000 return( MBEDTLS_SSL_HASH_SHA512 );
9001#endif
9002 default:
9003 return( MBEDTLS_SSL_HASH_NONE );
9004 }
9005}
9006
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009007#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009008/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009009 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009010 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009011 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009012int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009015
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009016 if( ssl->conf->curve_list == NULL )
9017 return( -1 );
9018
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009019 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009020 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009021 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009022
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009023 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009024}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009025#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009026
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009027#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009028/*
9029 * Check if a hash proposed by the peer is in our list.
9030 * Return 0 if we're willing to use it, -1 otherwise.
9031 */
9032int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9033 mbedtls_md_type_t md )
9034{
9035 const int *cur;
9036
9037 if( ssl->conf->sig_hashes == NULL )
9038 return( -1 );
9039
9040 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9041 if( *cur == (int) md )
9042 return( 0 );
9043
9044 return( -1 );
9045}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009046#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048#if defined(MBEDTLS_X509_CRT_PARSE_C)
9049int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9050 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009051 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009052 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009053{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009054 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009056 int usage = 0;
9057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009059 const char *ext_oid;
9060 size_t ext_len;
9061#endif
9062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9064 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009065 ((void) cert);
9066 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009067 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009068#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9071 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009072 {
9073 /* Server part of the key exchange */
9074 switch( ciphersuite->key_exchange )
9075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 case MBEDTLS_KEY_EXCHANGE_RSA:
9077 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009078 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009079 break;
9080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9082 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9083 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9084 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009085 break;
9086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9088 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009089 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009090 break;
9091
9092 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093 case MBEDTLS_KEY_EXCHANGE_NONE:
9094 case MBEDTLS_KEY_EXCHANGE_PSK:
9095 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9096 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009097 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009098 usage = 0;
9099 }
9100 }
9101 else
9102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9104 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009105 }
9106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009108 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009109 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009110 ret = -1;
9111 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009112#else
9113 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9117 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9120 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009121 }
9122 else
9123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9125 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009126 }
9127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009129 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009130 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009131 ret = -1;
9132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009134
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009135 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009137#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009138
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009139/*
9140 * Convert version numbers to/from wire format
9141 * and, for DTLS, to/from TLS equivalent.
9142 *
9143 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009144 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009145 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9146 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009149 unsigned char ver[2] )
9150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151#if defined(MBEDTLS_SSL_PROTO_DTLS)
9152 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009155 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9156
9157 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9158 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9159 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009160 else
9161#else
9162 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009163#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009164 {
9165 ver[0] = (unsigned char) major;
9166 ver[1] = (unsigned char) minor;
9167 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009168}
9169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009171 const unsigned char ver[2] )
9172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173#if defined(MBEDTLS_SSL_PROTO_DTLS)
9174 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009175 {
9176 *major = 255 - ver[0] + 2;
9177 *minor = 255 - ver[1] + 1;
9178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009180 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9181 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009182 else
9183#else
9184 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009185#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009186 {
9187 *major = ver[0];
9188 *minor = ver[1];
9189 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009190}
9191
Simon Butcher99000142016-10-13 17:21:01 +01009192int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9193{
9194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9195 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9196 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9197
9198 switch( md )
9199 {
9200#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9201#if defined(MBEDTLS_MD5_C)
9202 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009203 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009204#endif
9205#if defined(MBEDTLS_SHA1_C)
9206 case MBEDTLS_SSL_HASH_SHA1:
9207 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9208 break;
9209#endif
9210#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9211#if defined(MBEDTLS_SHA512_C)
9212 case MBEDTLS_SSL_HASH_SHA384:
9213 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9214 break;
9215#endif
9216#if defined(MBEDTLS_SHA256_C)
9217 case MBEDTLS_SSL_HASH_SHA256:
9218 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9219 break;
9220#endif
9221 default:
9222 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9223 }
9224
9225 return 0;
9226#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9227 (void) ssl;
9228 (void) md;
9229
9230 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9232}
9233
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009234#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9235 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9236int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9237 unsigned char *output,
9238 unsigned char *data, size_t data_len )
9239{
9240 int ret = 0;
9241 mbedtls_md5_context mbedtls_md5;
9242 mbedtls_sha1_context mbedtls_sha1;
9243
9244 mbedtls_md5_init( &mbedtls_md5 );
9245 mbedtls_sha1_init( &mbedtls_sha1 );
9246
9247 /*
9248 * digitally-signed struct {
9249 * opaque md5_hash[16];
9250 * opaque sha_hash[20];
9251 * };
9252 *
9253 * md5_hash
9254 * MD5(ClientHello.random + ServerHello.random
9255 * + ServerParams);
9256 * sha_hash
9257 * SHA(ClientHello.random + ServerHello.random
9258 * + ServerParams);
9259 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009260 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009261 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009263 goto exit;
9264 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009265 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009266 ssl->handshake->randbytes, 64 ) ) != 0 )
9267 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009269 goto exit;
9270 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009271 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009272 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009274 goto exit;
9275 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009276 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009277 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009279 goto exit;
9280 }
9281
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009282 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009283 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009285 goto exit;
9286 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009287 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009288 ssl->handshake->randbytes, 64 ) ) != 0 )
9289 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009291 goto exit;
9292 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009293 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009294 data_len ) ) != 0 )
9295 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009297 goto exit;
9298 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009299 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009300 output + 16 ) ) != 0 )
9301 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009303 goto exit;
9304 }
9305
9306exit:
9307 mbedtls_md5_free( &mbedtls_md5 );
9308 mbedtls_sha1_free( &mbedtls_sha1 );
9309
9310 if( ret != 0 )
9311 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9312 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9313
9314 return( ret );
9315
9316}
9317#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9318 MBEDTLS_SSL_PROTO_TLS1_1 */
9319
9320#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9321 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9322int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009323 unsigned char *hash, size_t *hashlen,
9324 unsigned char *data, size_t data_len,
9325 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009326{
9327 int ret = 0;
9328 mbedtls_md_context_t ctx;
9329 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009330 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009331
9332 mbedtls_md_init( &ctx );
9333
9334 /*
9335 * digitally-signed struct {
9336 * opaque client_random[32];
9337 * opaque server_random[32];
9338 * ServerDHParams params;
9339 * };
9340 */
9341 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9342 {
9343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9344 goto exit;
9345 }
9346 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9347 {
9348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9349 goto exit;
9350 }
9351 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9352 {
9353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9354 goto exit;
9355 }
9356 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9357 {
9358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9359 goto exit;
9360 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009361 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009362 {
9363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9364 goto exit;
9365 }
9366
9367exit:
9368 mbedtls_md_free( &ctx );
9369
9370 if( ret != 0 )
9371 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9372 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9373
9374 return( ret );
9375}
9376#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9377 MBEDTLS_SSL_PROTO_TLS1_2 */
9378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379#endif /* MBEDTLS_SSL_TLS_C */