blob: 85ed1e51c7d329fbdd533ea94f2f0222d4760cfe [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 */
Hanno Becker5f066e72018-08-16 14:56:31 +01004100 {
4101 /* Consider buffering the record. */
4102 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4103 {
4104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4105 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4106 }
4107
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004108 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004109 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004110 }
4111
4112#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4113 /* Replay detection only works for the current epoch */
4114 if( rec_epoch == ssl->in_epoch &&
4115 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4116 {
4117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4118 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4119 }
4120#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004121
Hanno Becker52c6dc62017-05-26 16:07:36 +01004122 /* Drop unexpected ApplicationData records,
4123 * except at the beginning of renegotiations */
4124 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4125 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4126#if defined(MBEDTLS_SSL_RENEGOTIATION)
4127 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4128 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4129#endif
4130 )
4131 {
4132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4133 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4134 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004135 }
4136#endif /* MBEDTLS_SSL_PROTO_DTLS */
4137
Hanno Becker52c6dc62017-05-26 16:07:36 +01004138
4139 /* Check length against bounds of the current transform and version */
4140 if( ssl->transform_in == NULL )
4141 {
4142 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004143 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004144 {
4145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4146 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4147 }
4148 }
4149 else
4150 {
4151 if( ssl->in_msglen < ssl->transform_in->minlen )
4152 {
4153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4154 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4155 }
4156
4157#if defined(MBEDTLS_SSL_PROTO_SSL3)
4158 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004159 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004160 {
4161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4162 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4163 }
4164#endif
4165#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4166 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4167 /*
4168 * TLS encrypted messages can have up to 256 bytes of padding
4169 */
4170 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4171 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004172 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004173 {
4174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4175 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4176 }
4177#endif
4178 }
4179
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004180 return( 0 );
4181}
Paul Bakker5121ce52009-01-03 21:22:43 +00004182
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004183/*
4184 * If applicable, decrypt (and decompress) record content
4185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004187{
4188 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4191 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4194 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 ret = mbedtls_ssl_hw_record_read( ssl );
4199 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4202 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004203 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004204
4205 if( ret == 0 )
4206 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004209 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004210 {
4211 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004214 return( ret );
4215 }
4216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 ssl->in_msg, ssl->in_msglen );
4219
Angus Grattond8213d02016-05-25 20:56:48 +10004220 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4223 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004224 }
4225 }
4226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004228 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004230 {
4231 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004234 return( ret );
4235 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004236 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004243 }
4244#endif
4245
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004246 return( 0 );
4247}
4248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004250
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004251/*
4252 * Read a record.
4253 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004254 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4255 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4256 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004257 */
Hanno Becker1097b342018-08-15 14:09:41 +01004258
4259/* Helper functions for mbedtls_ssl_read_record(). */
4260static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004261static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4262static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004263
Hanno Becker40f50842018-08-15 14:48:01 +01004264#if defined(MBEDTLS_SSL_PROTO_DTLS)
4265static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004266static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004267static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004268static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004269static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4270#endif /* MBEDTLS_SSL_PROTO_DTLS */
4271
Hanno Becker327c93b2018-08-15 13:56:18 +01004272int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
4273 unsigned update_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004274{
4275 int ret;
4276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004278
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004279 if( ssl->keep_current_message == 0 )
4280 {
4281 do {
Simon Butcher99000142016-10-13 17:21:01 +01004282
Hanno Becker26994592018-08-15 14:14:59 +01004283 ret = ssl_consume_current_message( ssl );
4284 if( ret != 0 )
4285 return( ret );
4286
Hanno Beckere74d5562018-08-15 14:26:08 +01004287 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004288 {
Hanno Becker40f50842018-08-15 14:48:01 +01004289#if defined(MBEDTLS_SSL_PROTO_DTLS)
4290 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004291
Hanno Becker40f50842018-08-15 14:48:01 +01004292 /* We only check for buffered messages if the
4293 * current datagram is fully consumed. */
4294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4295 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004296 {
Hanno Becker40f50842018-08-15 14:48:01 +01004297 if( ssl_load_buffered_message( ssl ) == 0 )
4298 have_buffered = 1;
4299 }
4300
4301 if( have_buffered == 0 )
4302#endif /* MBEDTLS_SSL_PROTO_DTLS */
4303 {
4304 ret = ssl_get_next_record( ssl );
4305 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4306 continue;
4307
4308 if( ret != 0 )
4309 {
4310 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4311 return( ret );
4312 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004313 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004314 }
4315
4316 ret = mbedtls_ssl_handle_message_type( ssl );
4317
Hanno Becker40f50842018-08-15 14:48:01 +01004318#if defined(MBEDTLS_SSL_PROTO_DTLS)
4319 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4320 {
4321 /* Buffer future message */
4322 ret = ssl_buffer_message( ssl );
4323 if( ret != 0 )
4324 return( ret );
4325
4326 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4327 }
4328#endif /* MBEDTLS_SSL_PROTO_DTLS */
4329
Hanno Becker90333da2017-10-10 11:27:13 +01004330 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4331 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004332
4333 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004334 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004335 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004336 return( ret );
4337 }
4338
Hanno Becker327c93b2018-08-15 13:56:18 +01004339 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4340 update_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004341 {
4342 mbedtls_ssl_update_handshake_status( ssl );
4343 }
Simon Butcher99000142016-10-13 17:21:01 +01004344 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004345 else
Simon Butcher99000142016-10-13 17:21:01 +01004346 {
Hanno Becker02f59072018-08-15 14:00:24 +01004347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004348 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004349 }
4350
4351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4352
4353 return( 0 );
4354}
4355
Hanno Becker40f50842018-08-15 14:48:01 +01004356#if defined(MBEDTLS_SSL_PROTO_DTLS)
4357static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4358{
4359 if( ssl->in_left > ssl->next_record_offset )
4360 return( 1 );
4361
4362 return( 0 );
4363}
4364
4365static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4366{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004367 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004368 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004369 int ret = 0;
4370
4371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4372
4373 if( hs == NULL )
4374 return( -1 );
4375
4376 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4377 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4378 {
4379 /* Check if we have seen a ChangeCipherSpec before.
4380 * If yes, synthesize a CCS record. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004381 if( ! hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004382 {
4383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4384 ret = -1;
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004385 return( -1 );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004386 }
4387
4388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4389 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4390 ssl->in_msglen = 1;
4391 ssl->in_msg[0] = 1;
4392
4393 /* As long as they are equal, the exact value doesn't matter. */
4394 ssl->in_left = 0;
4395 ssl->next_record_offset = 0;
4396
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004397 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004398 goto exit;
4399 }
Hanno Becker37f95322018-08-16 13:55:32 +01004400
4401 /* Debug only */
4402 {
4403 unsigned offset;
4404 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4405 {
4406 hs_buf = &hs->buffering.hs[offset];
4407 if( hs_buf->is_valid == 1 )
4408 {
4409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4410 hs->in_msg_seq + offset,
4411 hs_buf->is_complete ? "fully" : "partitially" ) );
4412 }
4413 }
4414 }
4415
4416 /* Check if we have buffered and/or fully reassembled the
4417 * next handshake message. */
4418 hs_buf = &hs->buffering.hs[0];
4419 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4420 {
4421 /* Synthesize a record containing the buffered HS message. */
4422 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4423 ( hs_buf->data[2] << 8 ) |
4424 hs_buf->data[3];
4425
4426 /* Double-check that we haven't accidentally buffered
4427 * a message that doesn't fit into the input buffer. */
4428 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4429 {
4430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4432 }
4433
4434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4435 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4436 hs_buf->data, msg_len + 12 );
4437
4438 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4439 ssl->in_hslen = msg_len + 12;
4440 ssl->in_msglen = msg_len + 12;
4441 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4442
4443 ret = 0;
4444 goto exit;
4445 }
4446 else
4447 {
4448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4449 hs->in_msg_seq ) );
4450 }
4451
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004452 ret = -1;
4453
4454exit:
4455
4456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4457 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004458}
4459
4460static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4461{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004462 int ret = 0;
4463 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4464
4465 if( hs == NULL )
4466 return( 0 );
4467
4468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4469
4470 switch( ssl->in_msgtype )
4471 {
4472 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004474 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004475 break;
4476
4477 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004478 {
4479 unsigned recv_msg_seq_offset;
4480 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4481 mbedtls_ssl_hs_buffer *hs_buf;
4482 size_t msg_len = ssl->in_hslen - 12;
4483
4484 /* We should never receive an old handshake
4485 * message - double-check nonetheless. */
4486 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4487 {
4488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4490 }
4491
4492 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4493 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4494 {
4495 /* Silently ignore -- message too far in the future */
4496 MBEDTLS_SSL_DEBUG_MSG( 2,
4497 ( "Ignore future HS message with sequence number %u, "
4498 "buffering window %u - %u",
4499 recv_msg_seq, ssl->handshake->in_msg_seq,
4500 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4501
4502 goto exit;
4503 }
4504
4505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4506 recv_msg_seq, recv_msg_seq_offset ) );
4507
4508 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4509
4510 /* Check if the buffering for this seq nr has already commenced. */
4511 if( ! hs_buf->is_valid )
4512 {
4513 hs_buf->is_fragmented =
4514 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4515
4516 /* We copy the message back into the input buffer
4517 * after reassembly, so check that it's not too large.
4518 * This is an implementation-specific limitation
4519 * and not one from the standard, hence it is not
4520 * checked in ssl_check_hs_header(). */
4521 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
4522 {
4523 /* Ignore message */
4524 goto exit;
4525 }
4526
4527 ret = ssl_prepare_reassembly_buffer( ssl, msg_len,
4528 hs_buf->is_fragmented,
4529 &hs_buf->data );
4530 if( ret == MBEDTLS_ERR_SSL_ALLOC_FAILED &&
4531 recv_msg_seq_offset > 0 )
4532 {
4533 /* If we run out of RAM trying to buffer a *future*
4534 * message, simply ignore instead of failing. */
4535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Not enough RAM available to buffer future message - ignore" ) );
4536 goto exit;
4537 }
4538 else if( ret != 0 )
4539 return( ret );
4540
4541 /* Prepare final header: copy msg_type, length and message_seq,
4542 * then add standardised fragment_offset and fragment_length */
4543 memcpy( hs_buf->data, ssl->in_msg, 6 );
4544 memset( hs_buf->data + 6, 0, 3 );
4545 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4546
4547 hs_buf->is_valid = 1;
4548 }
4549 else
4550 {
4551 /* Make sure msg_type and length are consistent */
4552 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4553 {
4554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4555 /* Ignore */
4556 goto exit;
4557 }
4558 }
4559
4560 if( ! hs_buf->is_complete )
4561 {
4562 size_t frag_len, frag_off;
4563 unsigned char * const msg = hs_buf->data + 12;
4564
4565 /*
4566 * Check and copy current fragment
4567 */
4568
4569 /* Validation of header fields already done in
4570 * mbedtls_ssl_prepare_handshake_record(). */
4571 frag_off = ssl_get_hs_frag_off( ssl );
4572 frag_len = ssl_get_hs_frag_len( ssl );
4573
4574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4575 frag_off, frag_len ) );
4576 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4577
4578 if( hs_buf->is_fragmented )
4579 {
4580 unsigned char * const bitmask = msg + msg_len;
4581 ssl_bitmask_set( bitmask, frag_off, frag_len );
4582 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4583 msg_len ) == 0 );
4584 }
4585 else
4586 {
4587 hs_buf->is_complete = 1;
4588 }
4589
4590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4591 hs_buf->is_complete ? "" : "not yet " ) );
4592 }
4593
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004594 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004595 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004596
4597 default:
4598 break;
4599 }
4600
4601exit:
4602
4603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4604 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004605}
4606#endif /* MBEDTLS_SSL_PROTO_DTLS */
4607
Hanno Becker1097b342018-08-15 14:09:41 +01004608static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004609{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004610 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004611 * Consume last content-layer message and potentially
4612 * update in_msglen which keeps track of the contents'
4613 * consumption state.
4614 *
4615 * (1) Handshake messages:
4616 * Remove last handshake message, move content
4617 * and adapt in_msglen.
4618 *
4619 * (2) Alert messages:
4620 * Consume whole record content, in_msglen = 0.
4621 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004622 * (3) Change cipher spec:
4623 * Consume whole record content, in_msglen = 0.
4624 *
4625 * (4) Application data:
4626 * Don't do anything - the record layer provides
4627 * the application data as a stream transport
4628 * and consumes through mbedtls_ssl_read only.
4629 *
4630 */
4631
4632 /* Case (1): Handshake messages */
4633 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004634 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004635 /* Hard assertion to be sure that no application data
4636 * is in flight, as corrupting ssl->in_msglen during
4637 * ssl->in_offt != NULL is fatal. */
4638 if( ssl->in_offt != NULL )
4639 {
4640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4642 }
4643
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004644 /*
4645 * Get next Handshake message in the current record
4646 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004647
Hanno Becker4a810fb2017-05-24 16:27:30 +01004648 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004649 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004650 * current handshake content: If DTLS handshake
4651 * fragmentation is used, that's the fragment
4652 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004653 * size here is faulty and should be changed at
4654 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004655 * (2) While it doesn't seem to cause problems, one
4656 * has to be very careful not to assume that in_hslen
4657 * is always <= in_msglen in a sensible communication.
4658 * Again, it's wrong for DTLS handshake fragmentation.
4659 * The following check is therefore mandatory, and
4660 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004661 * Additionally, ssl->in_hslen might be arbitrarily out of
4662 * bounds after handling a DTLS message with an unexpected
4663 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004664 */
4665 if( ssl->in_hslen < ssl->in_msglen )
4666 {
4667 ssl->in_msglen -= ssl->in_hslen;
4668 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4669 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004670
Hanno Becker4a810fb2017-05-24 16:27:30 +01004671 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4672 ssl->in_msg, ssl->in_msglen );
4673 }
4674 else
4675 {
4676 ssl->in_msglen = 0;
4677 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004678
Hanno Becker4a810fb2017-05-24 16:27:30 +01004679 ssl->in_hslen = 0;
4680 }
4681 /* Case (4): Application data */
4682 else if( ssl->in_offt != NULL )
4683 {
4684 return( 0 );
4685 }
4686 /* Everything else (CCS & Alerts) */
4687 else
4688 {
4689 ssl->in_msglen = 0;
4690 }
4691
Hanno Becker1097b342018-08-15 14:09:41 +01004692 return( 0 );
4693}
4694
Hanno Beckere74d5562018-08-15 14:26:08 +01004695static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4696{
4697 if( ssl->in_msglen > 0 )
4698 return( 1 );
4699
4700 return( 0 );
4701}
4702
Hanno Becker5f066e72018-08-16 14:56:31 +01004703#if defined(MBEDTLS_SSL_PROTO_DTLS)
4704
4705static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4706{
4707 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4708 if( hs == NULL )
4709 return;
4710
4711 mbedtls_free( hs->buffering.future_record.data );
4712 hs->buffering.future_record.data = NULL;
4713}
4714
4715static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4716{
4717 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4718 unsigned char * rec;
4719 size_t rec_len;
4720 unsigned rec_epoch;
4721
4722 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4723 return( 0 );
4724
4725 if( hs == NULL )
4726 return( 0 );
4727
4728 /* Only consider loading future records if the
4729 * input buffer is empty. */
4730 if( ssl_another_record_in_datagram( ssl ) == 1 )
4731 return( 0 );
4732
4733 rec = hs->buffering.future_record.data;
4734 rec_len = hs->buffering.future_record.len;
4735 rec_epoch = hs->buffering.future_record.epoch;
4736
4737 if( rec == NULL )
4738 return( 0 );
4739
4740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4741
4742 if( rec_epoch != ssl->in_epoch )
4743 {
4744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4745 goto exit;
4746 }
4747
4748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4749
4750 /* Double-check that the record is not too large */
4751 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4752 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4753 {
4754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4756 }
4757
4758 memcpy( ssl->in_hdr, rec, rec_len );
4759 ssl->in_left = rec_len;
4760 ssl->next_record_offset = 0;
4761
4762 ssl_free_buffered_record( ssl );
4763
4764exit:
4765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4766 return( 0 );
4767}
4768
4769static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4770{
4771 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4772 size_t const rec_hdr_len = 13;
4773
4774 /* Don't buffer future records outside handshakes. */
4775 if( hs == NULL )
4776 return( 0 );
4777
4778 /* Only buffer handshake records (we are only interested
4779 * in Finished messages). */
4780 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4781 return( 0 );
4782
4783 /* Don't buffer more than one future epoch record. */
4784 if( hs->buffering.future_record.data != NULL )
4785 return( 0 );
4786
4787 /* Buffer record */
4788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4789 ssl->in_epoch + 1 ) );
4790 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4791 rec_hdr_len + ssl->in_msglen );
4792
4793 /* ssl_parse_record_header() only considers records
4794 * of the next epoch as candidates for buffering. */
4795 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
4796 hs->buffering.future_record.len = rec_hdr_len + ssl->in_msglen;
4797
4798 hs->buffering.future_record.data =
4799 mbedtls_calloc( 1, hs->buffering.future_record.len );
4800 if( hs->buffering.future_record.data == NULL )
4801 {
4802 /* If we run out of RAM trying to buffer a
4803 * record from the next epoch, just ignore. */
4804 return( 0 );
4805 }
4806
4807 memcpy( hs->buffering.future_record.data,
4808 ssl->in_hdr, rec_hdr_len + ssl->in_msglen );
4809
4810 return( 0 );
4811}
4812
4813#endif /* MBEDTLS_SSL_PROTO_DTLS */
4814
Hanno Beckere74d5562018-08-15 14:26:08 +01004815static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004816{
4817 int ret;
4818
Hanno Becker5f066e72018-08-16 14:56:31 +01004819#if defined(MBEDTLS_SSL_PROTO_DTLS)
4820 /* We might have buffered a future record; if so,
4821 * and if the epoch matches now, load it.
4822 * On success, this call will set ssl->in_left to
4823 * the length of the buffered record, so that
4824 * the calls to ssl_fetch_input() below will
4825 * essentially be no-ops. */
4826 ret = ssl_load_buffered_record( ssl );
4827 if( ret != 0 )
4828 return( ret );
4829#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004834 return( ret );
4835 }
4836
4837 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004840 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4841 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004842 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004843 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4844 {
4845 ret = ssl_buffer_future_record( ssl );
4846 if( ret != 0 )
4847 return( ret );
4848
4849 /* Fall through to handling of unexpected records */
4850 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4851 }
4852
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004853 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4854 {
4855 /* Skip unexpected record (but not whole datagram) */
4856 ssl->next_record_offset = ssl->in_msglen
4857 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004858
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4860 "(header)" ) );
4861 }
4862 else
4863 {
4864 /* Skip invalid record and the rest of the datagram */
4865 ssl->next_record_offset = 0;
4866 ssl->in_left = 0;
4867
4868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4869 "(header)" ) );
4870 }
4871
4872 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004873 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004874 }
4875#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004876 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004877 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004878
4879 /*
4880 * Read and optionally decrypt the message contents
4881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4883 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004886 return( ret );
4887 }
4888
4889 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004891 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004894 if( ssl->next_record_offset < ssl->in_left )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4897 }
4898 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004899 else
4900#endif
4901 ssl->in_left = 0;
4902
4903 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004907 {
4908 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4910 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004911 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004912 /* Except when waiting for Finished as a bad mac here
4913 * probably means something went wrong in the handshake
4914 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4915 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4916 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4917 {
4918#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4919 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4920 {
4921 mbedtls_ssl_send_alert_message( ssl,
4922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4923 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4924 }
4925#endif
4926 return( ret );
4927 }
4928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004930 if( ssl->conf->badmac_limit != 0 &&
4931 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4934 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004935 }
4936#endif
4937
Hanno Becker4a810fb2017-05-24 16:27:30 +01004938 /* As above, invalid records cause
4939 * dismissal of the whole datagram. */
4940
4941 ssl->next_record_offset = 0;
4942 ssl->in_left = 0;
4943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004945 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004946 }
4947
4948 return( ret );
4949 }
4950 else
4951#endif
4952 {
4953 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4955 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 mbedtls_ssl_send_alert_message( ssl,
4958 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4959 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004960 }
4961#endif
4962 return( ret );
4963 }
4964 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004965
Simon Butcher99000142016-10-13 17:21:01 +01004966 return( 0 );
4967}
4968
4969int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4970{
4971 int ret;
4972
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004973 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004974 * Handle particular types of records
4975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004977 {
Simon Butcher99000142016-10-13 17:21:01 +01004978 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4979 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004980 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004982 }
4983
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004984#if defined(MBEDTLS_SSL_PROTO_DTLS)
4985 /* Drop unexpected ChangeCipherSpec messages */
4986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4987 ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4988 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4989 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4990 {
4991 if( ssl->handshake == NULL )
4992 {
4993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4994 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4995 }
4996
4997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4998 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4999 }
5000#endif
5001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005003 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005004 if( ssl->in_msglen != 2 )
5005 {
5006 /* Note: Standard allows for more than one 2 byte alert
5007 to be packed in a single message, but Mbed TLS doesn't
5008 currently support this. */
5009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5010 ssl->in_msglen ) );
5011 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5012 }
5013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 ssl->in_msg[0], ssl->in_msg[1] ) );
5016
5017 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005018 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005023 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005025 }
5026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5028 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5031 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005032 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005033
5034#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5035 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5036 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5037 {
Hanno Becker90333da2017-10-10 11:27:13 +01005038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005039 /* Will be handled when trying to parse ServerHello */
5040 return( 0 );
5041 }
5042#endif
5043
5044#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5045 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5046 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5047 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5048 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5049 {
5050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5051 /* Will be handled in mbedtls_ssl_parse_certificate() */
5052 return( 0 );
5053 }
5054#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5055
5056 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005057 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005058 }
5059
Hanno Beckerc76c6192017-06-06 10:03:17 +01005060#if defined(MBEDTLS_SSL_PROTO_DTLS)
5061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5062 ssl->handshake != NULL &&
5063 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5064 {
5065 ssl_handshake_wrapup_free_hs_transform( ssl );
5066 }
5067#endif
5068
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 return( 0 );
5070}
5071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005073{
5074 int ret;
5075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5077 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5078 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005079 {
5080 return( ret );
5081 }
5082
5083 return( 0 );
5084}
5085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005087 unsigned char level,
5088 unsigned char message )
5089{
5090 int ret;
5091
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005092 if( ssl == NULL || ssl->conf == NULL )
5093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005099 ssl->out_msglen = 2;
5100 ssl->out_msg[0] = level;
5101 ssl->out_msg[1] = message;
5102
Hanno Becker67bc7c32018-08-06 11:33:50 +01005103 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005106 return( ret );
5107 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005109
5110 return( 0 );
5111}
5112
Paul Bakker5121ce52009-01-03 21:22:43 +00005113/*
5114 * Handshake functions
5115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5117 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5118 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5119 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5120 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5121 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5122 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005123/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5131 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005132 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5133 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005136 ssl->state++;
5137 return( 0 );
5138 }
5139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005142}
5143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5151 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005152 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5153 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005156 ssl->state++;
5157 return( 0 );
5158 }
5159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005162}
Gilles Peskinef9828522017-05-03 12:28:43 +02005163
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005164#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005165/* Some certificate support -> implement write and parse */
5166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005170 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 const mbedtls_x509_crt *crt;
5172 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5177 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005178 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5179 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005182 ssl->state++;
5183 return( 0 );
5184 }
5185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005187 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005188 {
5189 if( ssl->client_auth == 0 )
5190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005192 ssl->state++;
5193 return( 0 );
5194 }
5195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005197 /*
5198 * If using SSLv3 and got no cert, send an Alert message
5199 * (otherwise an empty Certificate message will be sent).
5200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5202 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005203 {
5204 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5206 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5207 ssl->out_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( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005210 goto write_msg;
5211 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214#endif /* MBEDTLS_SSL_CLI_C */
5215#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5221 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005222 }
5223 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005224#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005227
5228 /*
5229 * 0 . 0 handshake type
5230 * 1 . 3 handshake length
5231 * 4 . 6 length of all certs
5232 * 7 . 9 length of cert. 1
5233 * 10 . n-1 peer certificate
5234 * n . n+2 length of cert. 2
5235 * n+3 . ... upper level cert, etc.
5236 */
5237 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005239
Paul Bakker29087132010-03-21 21:03:34 +00005240 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005241 {
5242 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005243 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005246 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005248 }
5249
5250 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5251 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5252 ssl->out_msg[i + 2] = (unsigned char)( n );
5253
5254 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5255 i += n; crt = crt->next;
5256 }
5257
5258 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5259 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5260 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5261
5262 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5264 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005265
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005266#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005267write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005269
5270 ssl->state++;
5271
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005272 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005275 return( ret );
5276 }
5277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005279
Paul Bakkered27a042013-04-18 22:46:23 +02005280 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005281}
5282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005286 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005288 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005289 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5294 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005295 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5296 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005299 ssl->state++;
5300 return( 0 );
5301 }
5302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005304 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005305 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5306 {
5307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5308 ssl->state++;
5309 return( 0 );
5310 }
5311
5312#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5313 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5314 authmode = ssl->handshake->sni_authmode;
5315#endif
5316
5317 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5318 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005320 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 ssl->state++;
5323 return( 0 );
5324 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005326
Hanno Becker327c93b2018-08-15 13:56:18 +01005327 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005329 /* mbedtls_ssl_read_record may have sent an alert already. We
5330 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005332 return( ret );
5333 }
5334
5335 ssl->state++;
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if defined(MBEDTLS_SSL_SRV_C)
5338#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 /*
5340 * Check if the client sent an empty certificate
5341 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005342 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005345 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5347 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5348 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005351
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005352 /* The client was asked for a certificate but didn't send
5353 one. The client should know what's going on, so we
5354 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005355 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005356 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005357 return( 0 );
5358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005360 }
5361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5365 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5370 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5371 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5372 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005376 /* The client was asked for a certificate but didn't send
5377 one. The client should know what's going on, so we
5378 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005379 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005380 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005382 else
5383 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005384 }
5385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5387 MBEDTLS_SSL_PROTO_TLS1_2 */
5388#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005393 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5394 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 }
5397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5399 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005402 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5403 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405 }
5406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005408
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005411 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005412 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005413
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005414 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005418 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5419 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 }
5422
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005423 /* In case we tried to reuse a session but it failed */
5424 if( ssl->session_negotiate->peer_cert != NULL )
5425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5427 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005428 }
5429
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005430 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005432 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005435 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5436 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005437 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 }
5439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005441
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005442 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005443
5444 while( i < ssl->in_hslen )
5445 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005446 if ( i + 3 > ssl->in_hslen ) {
5447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5448 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5449 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5450 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005452 if( ssl->in_msg[i] != 0 )
5453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005455 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005458 }
5459
5460 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5461 | (unsigned int) ssl->in_msg[i + 2];
5462 i += 3;
5463
5464 if( n < 128 || i + n > ssl->in_hslen )
5465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005467 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5468 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 }
5471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005473 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005474 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005476 case 0: /*ok*/
5477 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5478 /* Ignore certificate with an unknown algorithm: maybe a
5479 prior certificate was already trusted. */
5480 break;
5481
5482 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5483 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5484 goto crt_parse_der_failed;
5485
5486 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5487 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5488 goto crt_parse_der_failed;
5489
5490 default:
5491 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5492 crt_parse_der_failed:
5493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005495 return( ret );
5496 }
5497
5498 i += n;
5499 }
5500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005502
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005503 /*
5504 * On client, make sure the server cert doesn't change during renego to
5505 * avoid "triple handshake" attack: https://secure-resumption.com/
5506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005508 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005510 {
5511 if( ssl->session->peer_cert == NULL )
5512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005514 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5515 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005517 }
5518
5519 if( ssl->session->peer_cert->raw.len !=
5520 ssl->session_negotiate->peer_cert->raw.len ||
5521 memcmp( ssl->session->peer_cert->raw.p,
5522 ssl->session_negotiate->peer_cert->raw.p,
5523 ssl->session->peer_cert->raw.len ) != 0 )
5524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005526 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5527 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005529 }
5530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005532
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005533 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005534 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005535 mbedtls_x509_crt *ca_chain;
5536 mbedtls_x509_crl *ca_crl;
5537
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005538#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005539 if( ssl->handshake->sni_ca_chain != NULL )
5540 {
5541 ca_chain = ssl->handshake->sni_ca_chain;
5542 ca_crl = ssl->handshake->sni_ca_crl;
5543 }
5544 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005545#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005546 {
5547 ca_chain = ssl->conf->ca_chain;
5548 ca_crl = ssl->conf->ca_crl;
5549 }
5550
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005551 /*
5552 * Main check: verify certificate
5553 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005554 ret = mbedtls_x509_crt_verify_with_profile(
5555 ssl->session_negotiate->peer_cert,
5556 ca_chain, ca_crl,
5557 ssl->conf->cert_profile,
5558 ssl->hostname,
5559 &ssl->session_negotiate->verify_result,
5560 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005561
5562 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005565 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005566
5567 /*
5568 * Secondary checks: always done, but change 'ret' only if it was 0
5569 */
5570
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005571#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005574
5575 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005577 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005578 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005579 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005582 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005584 }
5585 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005586#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005589 ciphersuite_info,
5590 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005591 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005594 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005596 }
5597
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005598 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5599 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5600 * with details encoded in the verification flags. All other kinds
5601 * of error codes, including those from the user provided f_vrfy
5602 * functions, are treated as fatal and lead to a failure of
5603 * ssl_parse_certificate even if verification was optional. */
5604 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5605 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5606 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5607 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005609 }
5610
5611 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5612 {
5613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5614 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5615 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005616
5617 if( ret != 0 )
5618 {
5619 /* The certificate may have been rejected for several reasons.
5620 Pick one and send the corresponding alert. Which alert to send
5621 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005622 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5623 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5624 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5625 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5626 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5627 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5628 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5629 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5630 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5631 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5632 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5633 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5634 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5635 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5636 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5637 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5638 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5639 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5640 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5641 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005642 else
5643 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005644 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5645 alert );
5646 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005647
Hanno Beckere6706e62017-05-15 16:05:15 +01005648#if defined(MBEDTLS_DEBUG_C)
5649 if( ssl->session_negotiate->verify_result != 0 )
5650 {
5651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5652 ssl->session_negotiate->verify_result ) );
5653 }
5654 else
5655 {
5656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5657 }
5658#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 }
5660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
5663 return( ret );
5664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5666 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5667 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5668 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5669 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5670 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5671 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005674{
5675 int ret;
5676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005680 ssl->out_msglen = 1;
5681 ssl->out_msg[0] = 1;
5682
Paul Bakker5121ce52009-01-03 21:22:43 +00005683 ssl->state++;
5684
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005685 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005686 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 return( ret );
5689 }
5690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005692
5693 return( 0 );
5694}
5695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005697{
5698 int ret;
5699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005701
Hanno Becker327c93b2018-08-15 13:56:18 +01005702 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005705 return( ret );
5706 }
5707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005711 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5712 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005714 }
5715
5716 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005719 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5720 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005722 }
5723
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005724 /*
5725 * Switch to our negotiated transform and session parameters for inbound
5726 * data.
5727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005729 ssl->transform_in = ssl->transform_negotiate;
5730 ssl->session_in = ssl->session_negotiate;
5731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005733 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005736 ssl_dtls_replay_reset( ssl );
5737#endif
5738
5739 /* Increment epoch */
5740 if( ++ssl->in_epoch == 0 )
5741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005743 /* This is highly unlikely to happen for legitimate reasons, so
5744 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005746 }
5747 }
5748 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005750 memset( ssl->in_ctr, 0, 8 );
5751
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005752 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5755 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5761 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005763 }
5764 }
5765#endif
5766
Paul Bakker5121ce52009-01-03 21:22:43 +00005767 ssl->state++;
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
5771 return( 0 );
5772}
5773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5775 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005776{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005777 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5780 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5781 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005782 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005783 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5786#if defined(MBEDTLS_SHA512_C)
5787 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005788 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5789 else
5790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791#if defined(MBEDTLS_SHA256_C)
5792 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005793 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005794 else
5795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005799 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005800 }
Paul Bakker380da532012-04-18 16:10:25 +00005801}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005804{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5806 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005807 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5808 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5811#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005812 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005815 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005816#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005818}
5819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005821 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5824 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005825 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5826 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5829#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005830 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005831#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005833 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005834#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005836}
5837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5839 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5840static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005841 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005842{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005843 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5844 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005845}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005846#endif
Paul Bakker380da532012-04-18 16:10:25 +00005847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5849#if defined(MBEDTLS_SHA256_C)
5850static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005851 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005852{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005853 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005854}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005855#endif
Paul Bakker380da532012-04-18 16:10:25 +00005856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857#if defined(MBEDTLS_SHA512_C)
5858static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005859 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005860{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005861 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005862}
Paul Bakker769075d2012-11-24 11:26:46 +01005863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005867static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005869{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005870 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005871 mbedtls_md5_context md5;
5872 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005873
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 unsigned char padbuf[48];
5875 unsigned char md5sum[16];
5876 unsigned char sha1sum[20];
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005879 if( !session )
5880 session = ssl->session;
5881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005883
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005884 mbedtls_md5_init( &md5 );
5885 mbedtls_sha1_init( &sha1 );
5886
5887 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5888 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889
5890 /*
5891 * SSLv3:
5892 * hash =
5893 * MD5( master + pad2 +
5894 * MD5( handshake + sender + master + pad1 ) )
5895 * + SHA1( master + pad2 +
5896 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 */
5898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005900 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5901 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005902#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005905 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5906 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005907#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005910 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005911
Paul Bakker1ef83d62012-04-11 12:09:53 +00005912 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005914 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5915 mbedtls_md5_update_ret( &md5, session->master, 48 );
5916 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5917 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005918
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005919 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5920 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5921 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5922 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005923
Paul Bakker1ef83d62012-04-11 12:09:53 +00005924 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005925
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005926 mbedtls_md5_starts_ret( &md5 );
5927 mbedtls_md5_update_ret( &md5, session->master, 48 );
5928 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5929 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5930 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005931
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005932 mbedtls_sha1_starts_ret( &sha1 );
5933 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5934 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5935 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5936 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005940 mbedtls_md5_free( &md5 );
5941 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005943 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5944 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5945 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005952static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005954{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005955 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005956 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005957 mbedtls_md5_context md5;
5958 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005959 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005962 if( !session )
5963 session = ssl->session;
5964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005967 mbedtls_md5_init( &md5 );
5968 mbedtls_sha1_init( &sha1 );
5969
5970 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5971 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005972
Paul Bakker1ef83d62012-04-11 12:09:53 +00005973 /*
5974 * TLSv1:
5975 * hash = PRF( master, finished_label,
5976 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5977 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005980 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5981 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005982#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005985 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5986 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005987#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005990 ? "client finished"
5991 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005992
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005993 mbedtls_md5_finish_ret( &md5, padbuf );
5994 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005995
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005996 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005997 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006000
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006001 mbedtls_md5_free( &md5 );
6002 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006003
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006004 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6011#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006012static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006014{
6015 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006016 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006017 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006018 unsigned char padbuf[32];
6019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006021 if( !session )
6022 session = ssl->session;
6023
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006024 mbedtls_sha256_init( &sha256 );
6025
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006027
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006028 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006029
6030 /*
6031 * TLSv1.2:
6032 * hash = PRF( master, finished_label,
6033 * Hash( handshake ) )[0.11]
6034 */
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if !defined(MBEDTLS_SHA256_ALT)
6037 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006038 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006039#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006042 ? "client finished"
6043 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006044
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006045 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006046
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006047 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006048 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006051
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006052 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006053
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006054 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006061static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006063{
6064 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006065 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006066 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006067 unsigned char padbuf[48];
6068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006070 if( !session )
6071 session = ssl->session;
6072
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006073 mbedtls_sha512_init( &sha512 );
6074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006076
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006077 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006078
6079 /*
6080 * TLSv1.2:
6081 * hash = PRF( master, finished_label,
6082 * Hash( handshake ) )[0.11]
6083 */
6084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006086 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6087 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006088#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006091 ? "client finished"
6092 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006093
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006094 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006095
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006096 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006097 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006100
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006101 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006102
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006103 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107#endif /* MBEDTLS_SHA512_C */
6108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006113
6114 /*
6115 * Free our handshake params
6116 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006117 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006119 ssl->handshake = NULL;
6120
6121 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006122 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006123 */
6124 if( ssl->transform )
6125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126 mbedtls_ssl_transform_free( ssl->transform );
6127 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006128 }
6129 ssl->transform = ssl->transform_negotiate;
6130 ssl->transform_negotiate = NULL;
6131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006133}
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006136{
6137 int resume = ssl->handshake->resume;
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141#if defined(MBEDTLS_SSL_RENEGOTIATION)
6142 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006145 ssl->renego_records_seen = 0;
6146 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006147#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006148
6149 /*
6150 * Free the previous session and switch in the current one
6151 */
Paul Bakker0a597072012-09-25 21:55:46 +00006152 if( ssl->session )
6153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006155 /* RFC 7366 3.1: keep the EtM state */
6156 ssl->session_negotiate->encrypt_then_mac =
6157 ssl->session->encrypt_then_mac;
6158#endif
6159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 mbedtls_ssl_session_free( ssl->session );
6161 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006162 }
6163 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006164 ssl->session_negotiate = NULL;
6165
Paul Bakker0a597072012-09-25 21:55:46 +00006166 /*
6167 * Add cache entry
6168 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006169 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006170 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006171 resume == 0 )
6172 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006173 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006175 }
Paul Bakker0a597072012-09-25 21:55:46 +00006176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006179 ssl->handshake->flight != NULL )
6180 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006181 /* Cancel handshake timer */
6182 ssl_set_timer( ssl, 0 );
6183
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006184 /* Keep last flight around in case we need to resend it:
6185 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006187 }
6188 else
6189#endif
6190 ssl_handshake_wrapup_free_hs_transform( ssl );
6191
Paul Bakker48916f92012-09-16 19:57:18 +00006192 ssl->state++;
6193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006195}
6196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006198{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006199 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006202
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006203 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006204
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006205 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006206
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006207 /*
6208 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6209 * may define some other value. Currently (early 2016), no defined
6210 * ciphersuite does this (and this is unlikely to change as activity has
6211 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006216 ssl->verify_data_len = hash_len;
6217 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006218#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006219
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6222 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006223
6224 /*
6225 * In case of session resuming, invert the client and server
6226 * ChangeCipherSpec messages order.
6227 */
Paul Bakker0a597072012-09-25 21:55:46 +00006228 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006231 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006235 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006237#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006238 }
6239 else
6240 ssl->state++;
6241
Paul Bakker48916f92012-09-16 19:57:18 +00006242 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006243 * Switch to our negotiated transform and session parameters for outbound
6244 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006249 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006250 {
6251 unsigned char i;
6252
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006253 /* Remember current epoch settings for resending */
6254 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006255 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006256
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006257 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006258 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006259
6260 /* Increment epoch */
6261 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006262 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006263 break;
6264
6265 /* The loop goes to its end iff the counter is wrapping */
6266 if( i == 0 )
6267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6269 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006270 }
6271 }
6272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006274 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006275
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006276 ssl->transform_out = ssl->transform_negotiate;
6277 ssl->session_out = ssl->session_negotiate;
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6280 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6285 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006286 }
6287 }
6288#endif
6289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006293#endif
6294
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006295 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 return( ret );
6299 }
6300
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006301#if defined(MBEDTLS_SSL_PROTO_DTLS)
6302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6303 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6304 {
6305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6306 return( ret );
6307 }
6308#endif
6309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006311
6312 return( 0 );
6313}
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006316#define SSL_MAX_HASH_LEN 36
6317#else
6318#define SSL_MAX_HASH_LEN 12
6319#endif
6320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006322{
Paul Bakker23986e52011-04-24 08:57:21 +00006323 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006324 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006325 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006328
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006329 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006330
Hanno Becker327c93b2018-08-15 13:56:18 +01006331 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 return( ret );
6335 }
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6341 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006343 }
6344
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006345 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346#if defined(MBEDTLS_SSL_PROTO_SSL3)
6347 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006348 hash_len = 36;
6349 else
6350#endif
6351 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6354 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006357 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6358 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 }
6361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006363 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006366 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6367 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 }
6370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006372 ssl->verify_data_len = hash_len;
6373 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006374#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006375
Paul Bakker0a597072012-09-25 21:55:46 +00006376 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006379 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006385#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006386 }
6387 else
6388 ssl->state++;
6389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006393#endif
6394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006396
6397 return( 0 );
6398}
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6405 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6406 mbedtls_md5_init( &handshake->fin_md5 );
6407 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006408 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6409 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6412#if defined(MBEDTLS_SHA256_C)
6413 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006414 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#if defined(MBEDTLS_SHA512_C)
6417 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006418 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006421
6422 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006423
6424#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6425 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6426 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6427#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429#if defined(MBEDTLS_DHM_C)
6430 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432#if defined(MBEDTLS_ECDH_C)
6433 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006434#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006435#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006436 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006437#if defined(MBEDTLS_SSL_CLI_C)
6438 handshake->ecjpake_cache = NULL;
6439 handshake->ecjpake_cache_len = 0;
6440#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006441#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006442
6443#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6444 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6445#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006446}
6447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6453 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 mbedtls_md_init( &transform->md_ctx_enc );
6456 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006457}
6458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006462}
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006465{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006466 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006467 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006469 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006471 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006472 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006473
6474 /*
6475 * Either the pointers are now NULL or cleared properly and can be freed.
6476 * Now allocate missing structures.
6477 */
6478 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006479 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006480 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006481 }
Paul Bakker48916f92012-09-16 19:57:18 +00006482
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006483 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006484 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006485 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006486 }
Paul Bakker48916f92012-09-16 19:57:18 +00006487
Paul Bakker82788fb2014-10-20 13:59:19 +02006488 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006489 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006490 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006491 }
Paul Bakker48916f92012-09-16 19:57:18 +00006492
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006493 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006494 if( ssl->handshake == NULL ||
6495 ssl->transform_negotiate == NULL ||
6496 ssl->session_negotiate == NULL )
6497 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 mbedtls_free( ssl->handshake );
6501 mbedtls_free( ssl->transform_negotiate );
6502 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006503
6504 ssl->handshake = NULL;
6505 ssl->transform_negotiate = NULL;
6506 ssl->session_negotiate = NULL;
6507
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006508 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006509 }
6510
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006511 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006513 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006514 ssl_handshake_params_init( ssl->handshake );
6515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6518 {
6519 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006520
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006521 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6522 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6523 else
6524 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006525
6526 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006527 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006528#endif
6529
Paul Bakker48916f92012-09-16 19:57:18 +00006530 return( 0 );
6531}
6532
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006533#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006534/* Dummy cookie callbacks for defaults */
6535static int ssl_cookie_write_dummy( void *ctx,
6536 unsigned char **p, unsigned char *end,
6537 const unsigned char *cli_id, size_t cli_id_len )
6538{
6539 ((void) ctx);
6540 ((void) p);
6541 ((void) end);
6542 ((void) cli_id);
6543 ((void) cli_id_len);
6544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006546}
6547
6548static int ssl_cookie_check_dummy( void *ctx,
6549 const unsigned char *cookie, size_t cookie_len,
6550 const unsigned char *cli_id, size_t cli_id_len )
6551{
6552 ((void) ctx);
6553 ((void) cookie);
6554 ((void) cookie_len);
6555 ((void) cli_id);
6556 ((void) cli_id_len);
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006559}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006560#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006561
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006562/* Once ssl->out_hdr as the address of the beginning of the
6563 * next outgoing record is set, deduce the other pointers.
6564 *
6565 * Note: For TLS, we save the implicit record sequence number
6566 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6567 * and the caller has to make sure there's space for this.
6568 */
6569
6570static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6571 mbedtls_ssl_transform *transform )
6572{
6573#if defined(MBEDTLS_SSL_PROTO_DTLS)
6574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6575 {
6576 ssl->out_ctr = ssl->out_hdr + 3;
6577 ssl->out_len = ssl->out_hdr + 11;
6578 ssl->out_iv = ssl->out_hdr + 13;
6579 }
6580 else
6581#endif
6582 {
6583 ssl->out_ctr = ssl->out_hdr - 8;
6584 ssl->out_len = ssl->out_hdr + 3;
6585 ssl->out_iv = ssl->out_hdr + 5;
6586 }
6587
6588 /* Adjust out_msg to make space for explicit IV, if used. */
6589 if( transform != NULL &&
6590 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6591 {
6592 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6593 }
6594 else
6595 ssl->out_msg = ssl->out_iv;
6596}
6597
6598/* Once ssl->in_hdr as the address of the beginning of the
6599 * next incoming record is set, deduce the other pointers.
6600 *
6601 * Note: For TLS, we save the implicit record sequence number
6602 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6603 * and the caller has to make sure there's space for this.
6604 */
6605
6606static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6607 mbedtls_ssl_transform *transform )
6608{
6609#if defined(MBEDTLS_SSL_PROTO_DTLS)
6610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6611 {
6612 ssl->in_ctr = ssl->in_hdr + 3;
6613 ssl->in_len = ssl->in_hdr + 11;
6614 ssl->in_iv = ssl->in_hdr + 13;
6615 }
6616 else
6617#endif
6618 {
6619 ssl->in_ctr = ssl->in_hdr - 8;
6620 ssl->in_len = ssl->in_hdr + 3;
6621 ssl->in_iv = ssl->in_hdr + 5;
6622 }
6623
6624 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6625 if( transform != NULL &&
6626 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6627 {
6628 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6629 }
6630 else
6631 ssl->in_msg = ssl->in_iv;
6632}
6633
Paul Bakker5121ce52009-01-03 21:22:43 +00006634/*
6635 * Initialize an SSL context
6636 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006637void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6638{
6639 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6640}
6641
6642/*
6643 * Setup an SSL context
6644 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006645
6646static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6647{
6648 /* Set the incoming and outgoing record pointers. */
6649#if defined(MBEDTLS_SSL_PROTO_DTLS)
6650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6651 {
6652 ssl->out_hdr = ssl->out_buf;
6653 ssl->in_hdr = ssl->in_buf;
6654 }
6655 else
6656#endif /* MBEDTLS_SSL_PROTO_DTLS */
6657 {
6658 ssl->out_hdr = ssl->out_buf + 8;
6659 ssl->in_hdr = ssl->in_buf + 8;
6660 }
6661
6662 /* Derive other internal pointers. */
6663 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6664 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6665}
6666
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006667int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006668 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006669{
Paul Bakker48916f92012-09-16 19:57:18 +00006670 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006672 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006673
6674 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006675 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006676 */
Angus Grattond8213d02016-05-25 20:56:48 +10006677 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6678 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006679 {
Angus Grattond8213d02016-05-25 20:56:48 +10006680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6681 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6682 }
6683
6684 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6685 if( ssl->out_buf == NULL )
6686 {
6687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006689 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006690 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006691 }
6692
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006693 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006694
Paul Bakker48916f92012-09-16 19:57:18 +00006695 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6696 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006697
6698 return( 0 );
6699}
6700
6701/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006702 * Reset an initialized and used SSL context for re-use while retaining
6703 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006704 *
6705 * If partial is non-zero, keep data in the input buffer and client ID.
6706 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006707 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006708static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006709{
Paul Bakker48916f92012-09-16 19:57:18 +00006710 int ret;
6711
Hanno Becker7e772132018-08-10 12:38:21 +01006712#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6713 !defined(MBEDTLS_SSL_SRV_C)
6714 ((void) partial);
6715#endif
6716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006718
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006719 /* Cancel any possibly running timer */
6720 ssl_set_timer( ssl, 0 );
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if defined(MBEDTLS_SSL_RENEGOTIATION)
6723 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006724 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006725
6726 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6728 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006731
Paul Bakker7eb013f2011-10-06 12:37:39 +00006732 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006733 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006734
6735 ssl->in_msgtype = 0;
6736 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006738 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006739 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006742 ssl_dtls_replay_reset( ssl );
6743#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006744
6745 ssl->in_hslen = 0;
6746 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006747
6748 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006749
6750 ssl->out_msgtype = 0;
6751 ssl->out_msglen = 0;
6752 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6754 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006755 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006756#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006757
Hanno Becker19859472018-08-06 09:40:20 +01006758 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6759
Paul Bakker48916f92012-09-16 19:57:18 +00006760 ssl->transform_in = NULL;
6761 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006762
Hanno Becker78640902018-08-13 16:35:15 +01006763 ssl->session_in = NULL;
6764 ssl->session_out = NULL;
6765
Angus Grattond8213d02016-05-25 20:56:48 +10006766 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006767
6768#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006769 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006770#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6771 {
6772 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006773 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006774 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6777 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6780 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6783 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006784 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006785 }
6786#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006787
Paul Bakker48916f92012-09-16 19:57:18 +00006788 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 mbedtls_ssl_transform_free( ssl->transform );
6791 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006792 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006793 }
Paul Bakker48916f92012-09-16 19:57:18 +00006794
Paul Bakkerc0463502013-02-14 11:19:38 +01006795 if( ssl->session )
6796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 mbedtls_ssl_session_free( ssl->session );
6798 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006799 ssl->session = NULL;
6800 }
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006803 ssl->alpn_chosen = NULL;
6804#endif
6805
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006806#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006807#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006808 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006809#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006810 {
6811 mbedtls_free( ssl->cli_id );
6812 ssl->cli_id = NULL;
6813 ssl->cli_id_len = 0;
6814 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006815#endif
6816
Paul Bakker48916f92012-09-16 19:57:18 +00006817 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6818 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006819
6820 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006821}
6822
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006823/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006824 * Reset an initialized and used SSL context for re-use while retaining
6825 * all application-set variables, function pointers and data.
6826 */
6827int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6828{
6829 return( ssl_session_reset_int( ssl, 0 ) );
6830}
6831
6832/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 * SSL set accessors
6834 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006835void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006836{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006837 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006838}
6839
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006840void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006841{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006842 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006843}
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006846void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006847{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006848 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006849}
6850#endif
6851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006853void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006855 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006856}
6857#endif
6858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006860
6861void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6862 unsigned allow_packing )
6863{
6864 ssl->disable_datagram_packing = !allow_packing;
6865}
6866
6867void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6868 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006869{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006870 conf->hs_timeout_min = min;
6871 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006872}
6873#endif
6874
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006875void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006877 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006878}
6879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006881void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006882 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006883 void *p_vrfy )
6884{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006885 conf->f_vrfy = f_vrfy;
6886 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006889
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006890void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006891 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006892 void *p_rng )
6893{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006894 conf->f_rng = f_rng;
6895 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006896}
6897
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006898void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006899 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006900 void *p_dbg )
6901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006902 conf->f_dbg = f_dbg;
6903 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006904}
6905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006907 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006908 mbedtls_ssl_send_t *f_send,
6909 mbedtls_ssl_recv_t *f_recv,
6910 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006911{
6912 ssl->p_bio = p_bio;
6913 ssl->f_send = f_send;
6914 ssl->f_recv = f_recv;
6915 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006916}
6917
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006918void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006919{
6920 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006921}
6922
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006923void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6924 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006925 mbedtls_ssl_set_timer_t *f_set_timer,
6926 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006927{
6928 ssl->p_timer = p_timer;
6929 ssl->f_set_timer = f_set_timer;
6930 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006931
6932 /* Make sure we start with no timer running */
6933 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006934}
6935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006937void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006938 void *p_cache,
6939 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6940 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006941{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006942 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006943 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006944 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_CLI_C)
6949int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006950{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006951 int ret;
6952
6953 if( ssl == NULL ||
6954 session == NULL ||
6955 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006956 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006959 }
6960
6961 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6962 return( ret );
6963
Paul Bakker0a597072012-09-25 21:55:46 +00006964 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006965
6966 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006969
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006970void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006971 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006973 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6974 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6975 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006977}
6978
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006979void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006980 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006981 int major, int minor )
6982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006984 return;
6985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006987 return;
6988
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006989 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006990}
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006993void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006994 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006995{
6996 conf->cert_profile = profile;
6997}
6998
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006999/* Append a new keycert entry to a (possibly empty) list */
7000static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7001 mbedtls_x509_crt *cert,
7002 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007003{
niisato8ee24222018-06-25 19:05:48 +09007004 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007005
niisato8ee24222018-06-25 19:05:48 +09007006 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7007 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007008 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007009
niisato8ee24222018-06-25 19:05:48 +09007010 new_cert->cert = cert;
7011 new_cert->key = key;
7012 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007013
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007014 /* Update head is the list was null, else add to the end */
7015 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007016 {
niisato8ee24222018-06-25 19:05:48 +09007017 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007018 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007019 else
7020 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007021 mbedtls_ssl_key_cert *cur = *head;
7022 while( cur->next != NULL )
7023 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007024 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007025 }
7026
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007027 return( 0 );
7028}
7029
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007030int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007031 mbedtls_x509_crt *own_cert,
7032 mbedtls_pk_context *pk_key )
7033{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007034 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007035}
7036
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007037void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007038 mbedtls_x509_crt *ca_chain,
7039 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007040{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007041 conf->ca_chain = ca_chain;
7042 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007045
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007046#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7047int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7048 mbedtls_x509_crt *own_cert,
7049 mbedtls_pk_context *pk_key )
7050{
7051 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7052 own_cert, pk_key ) );
7053}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007054
7055void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7056 mbedtls_x509_crt *ca_chain,
7057 mbedtls_x509_crl *ca_crl )
7058{
7059 ssl->handshake->sni_ca_chain = ca_chain;
7060 ssl->handshake->sni_ca_crl = ca_crl;
7061}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007062
7063void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7064 int authmode )
7065{
7066 ssl->handshake->sni_authmode = authmode;
7067}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007068#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7069
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007070#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007071/*
7072 * Set EC J-PAKE password for current handshake
7073 */
7074int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7075 const unsigned char *pw,
7076 size_t pw_len )
7077{
7078 mbedtls_ecjpake_role role;
7079
Janos Follath8eb64132016-06-03 15:40:57 +01007080 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7082
7083 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7084 role = MBEDTLS_ECJPAKE_SERVER;
7085 else
7086 role = MBEDTLS_ECJPAKE_CLIENT;
7087
7088 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7089 role,
7090 MBEDTLS_MD_SHA256,
7091 MBEDTLS_ECP_DP_SECP256R1,
7092 pw, pw_len ) );
7093}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007094#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007097int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007098 const unsigned char *psk, size_t psk_len,
7099 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007100{
Paul Bakker6db455e2013-09-18 17:29:31 +02007101 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007106
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007107 /* Identity len will be encoded on two bytes */
7108 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007109 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007110 {
7111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7112 }
7113
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007114 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007115 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007116 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007117
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007118 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007119 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007120 conf->psk_len = 0;
7121 }
7122 if( conf->psk_identity != NULL )
7123 {
7124 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007125 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007126 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007127 }
7128
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007129 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7130 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007131 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007132 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007133 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007134 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007135 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007136 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007137 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007138
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007139 conf->psk_len = psk_len;
7140 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007141
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007142 memcpy( conf->psk, psk, conf->psk_len );
7143 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007144
7145 return( 0 );
7146}
7147
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007148int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7149 const unsigned char *psk, size_t psk_len )
7150{
7151 if( psk == NULL || ssl->handshake == NULL )
7152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7153
7154 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7156
7157 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007158 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007159 mbedtls_platform_zeroize( ssl->handshake->psk,
7160 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007161 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007162 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007163 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007164
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007165 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007166 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007167
7168 ssl->handshake->psk_len = psk_len;
7169 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7170
7171 return( 0 );
7172}
7173
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007174void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007176 size_t),
7177 void *p_psk )
7178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007179 conf->f_psk = f_psk;
7180 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007183
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007184#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007185
7186#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007187int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007188{
7189 int ret;
7190
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007191 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7192 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7193 {
7194 mbedtls_mpi_free( &conf->dhm_P );
7195 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007196 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007198
7199 return( 0 );
7200}
Hanno Becker470a8c42017-10-04 15:28:46 +01007201#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007202
Hanno Beckera90658f2017-10-04 15:29:08 +01007203int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7204 const unsigned char *dhm_P, size_t P_len,
7205 const unsigned char *dhm_G, size_t G_len )
7206{
7207 int ret;
7208
7209 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7210 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7211 {
7212 mbedtls_mpi_free( &conf->dhm_P );
7213 mbedtls_mpi_free( &conf->dhm_G );
7214 return( ret );
7215 }
7216
7217 return( 0 );
7218}
Paul Bakker5121ce52009-01-03 21:22:43 +00007219
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007220int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007221{
7222 int ret;
7223
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007224 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7225 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7226 {
7227 mbedtls_mpi_free( &conf->dhm_P );
7228 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007229 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007230 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007231
7232 return( 0 );
7233}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007234#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007235
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007236#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7237/*
7238 * Set the minimum length for Diffie-Hellman parameters
7239 */
7240void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7241 unsigned int bitlen )
7242{
7243 conf->dhm_min_bitlen = bitlen;
7244}
7245#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7246
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007247#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007248/*
7249 * Set allowed/preferred hashes for handshake signatures
7250 */
7251void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7252 const int *hashes )
7253{
7254 conf->sig_hashes = hashes;
7255}
Hanno Becker947194e2017-04-07 13:25:49 +01007256#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007257
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007258#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007259/*
7260 * Set the allowed elliptic curves
7261 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007262void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007263 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007265 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007266}
Hanno Becker947194e2017-04-07 13:25:49 +01007267#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007268
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007269#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007271{
Hanno Becker947194e2017-04-07 13:25:49 +01007272 /* Initialize to suppress unnecessary compiler warning */
7273 size_t hostname_len = 0;
7274
7275 /* Check if new hostname is valid before
7276 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007277 if( hostname != NULL )
7278 {
7279 hostname_len = strlen( hostname );
7280
7281 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7283 }
7284
7285 /* Now it's clear that we will overwrite the old hostname,
7286 * so we can free it safely */
7287
7288 if( ssl->hostname != NULL )
7289 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007290 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007291 mbedtls_free( ssl->hostname );
7292 }
7293
7294 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007295
Paul Bakker5121ce52009-01-03 21:22:43 +00007296 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007297 {
7298 ssl->hostname = NULL;
7299 }
7300 else
7301 {
7302 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007303 if( ssl->hostname == NULL )
7304 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007305
Hanno Becker947194e2017-04-07 13:25:49 +01007306 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007307
Hanno Becker947194e2017-04-07 13:25:49 +01007308 ssl->hostname[hostname_len] = '\0';
7309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007310
7311 return( 0 );
7312}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007313#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007314
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007315#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007316void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007318 const unsigned char *, size_t),
7319 void *p_sni )
7320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007321 conf->f_sni = f_sni;
7322 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007327int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007328{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007329 size_t cur_len, tot_len;
7330 const char **p;
7331
7332 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007333 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7334 * MUST NOT be truncated."
7335 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007336 */
7337 tot_len = 0;
7338 for( p = protos; *p != NULL; p++ )
7339 {
7340 cur_len = strlen( *p );
7341 tot_len += cur_len;
7342
7343 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007345 }
7346
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007347 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007348
7349 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007350}
7351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007353{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007354 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007357
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007358void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007359{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007360 conf->max_major_ver = major;
7361 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007362}
7363
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007364void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007365{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007366 conf->min_major_ver = major;
7367 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007368}
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007371void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007372{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007373 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007374}
7375#endif
7376
Janos Follath088ce432017-04-10 12:42:31 +01007377#if defined(MBEDTLS_SSL_SRV_C)
7378void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7379 char cert_req_ca_list )
7380{
7381 conf->cert_req_ca_list = cert_req_ca_list;
7382}
7383#endif
7384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007386void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007387{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007388 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007389}
7390#endif
7391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007393void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007395 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007396}
7397#endif
7398
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007399#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007400void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007402 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007403}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007404#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007405
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007406#if defined(MBEDTLS_SSL_PROTO_DTLS)
7407void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7408{
7409 conf->mtu = mtu;
7410}
7411#endif
7412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007414int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007417 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007420 }
7421
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007422 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007423
7424 return( 0 );
7425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007429void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007431 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007436void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007437{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007438 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007439}
7440#endif
7441
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007442void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007444 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007445}
7446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007448void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007450 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007451}
7452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007453void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007454{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007455 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007456}
7457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007458void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007459 const unsigned char period[8] )
7460{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007461 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007466#if defined(MBEDTLS_SSL_CLI_C)
7467void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007468{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007469 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007470}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007471#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007472
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007473#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007474void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7475 mbedtls_ssl_ticket_write_t *f_ticket_write,
7476 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7477 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007478{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007479 conf->f_ticket_write = f_ticket_write;
7480 conf->f_ticket_parse = f_ticket_parse;
7481 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007482}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007485
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007486#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7487void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7488 mbedtls_ssl_export_keys_t *f_export_keys,
7489 void *p_export_keys )
7490{
7491 conf->f_export_keys = f_export_keys;
7492 conf->p_export_keys = p_export_keys;
7493}
7494#endif
7495
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007496#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007497void mbedtls_ssl_conf_async_private_cb(
7498 mbedtls_ssl_config *conf,
7499 mbedtls_ssl_async_sign_t *f_async_sign,
7500 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7501 mbedtls_ssl_async_resume_t *f_async_resume,
7502 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007503 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007504{
7505 conf->f_async_sign_start = f_async_sign;
7506 conf->f_async_decrypt_start = f_async_decrypt;
7507 conf->f_async_resume = f_async_resume;
7508 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007509 conf->p_async_config_data = async_config_data;
7510}
7511
Gilles Peskine8f97af72018-04-26 11:46:10 +02007512void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7513{
7514 return( conf->p_async_config_data );
7515}
7516
Gilles Peskine1febfef2018-04-30 11:54:39 +02007517void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007518{
7519 if( ssl->handshake == NULL )
7520 return( NULL );
7521 else
7522 return( ssl->handshake->user_async_ctx );
7523}
7524
Gilles Peskine1febfef2018-04-30 11:54:39 +02007525void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007526 void *ctx )
7527{
7528 if( ssl->handshake != NULL )
7529 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007530}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007531#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007532
Paul Bakker5121ce52009-01-03 21:22:43 +00007533/*
7534 * SSL get accessors
7535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007537{
7538 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7539}
7540
Hanno Becker8b170a02017-10-10 11:51:19 +01007541int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7542{
7543 /*
7544 * Case A: We're currently holding back
7545 * a message for further processing.
7546 */
7547
7548 if( ssl->keep_current_message == 1 )
7549 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007551 return( 1 );
7552 }
7553
7554 /*
7555 * Case B: Further records are pending in the current datagram.
7556 */
7557
7558#if defined(MBEDTLS_SSL_PROTO_DTLS)
7559 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7560 ssl->in_left > ssl->next_record_offset )
7561 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007563 return( 1 );
7564 }
7565#endif /* MBEDTLS_SSL_PROTO_DTLS */
7566
7567 /*
7568 * Case C: A handshake message is being processed.
7569 */
7570
Hanno Becker8b170a02017-10-10 11:51:19 +01007571 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7572 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007574 return( 1 );
7575 }
7576
7577 /*
7578 * Case D: An application data message is being processed
7579 */
7580 if( ssl->in_offt != NULL )
7581 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007583 return( 1 );
7584 }
7585
7586 /*
7587 * In all other cases, the rest of the message can be dropped.
7588 * As in ssl_read_record_layer, this needs to be adapted if
7589 * we implement support for multiple alerts in single records.
7590 */
7591
7592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7593 return( 0 );
7594}
7595
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007596uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007597{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007598 if( ssl->session != NULL )
7599 return( ssl->session->verify_result );
7600
7601 if( ssl->session_negotiate != NULL )
7602 return( ssl->session_negotiate->verify_result );
7603
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007604 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007605}
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007608{
Paul Bakker926c8e42013-03-06 10:23:34 +01007609 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007610 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007613}
7614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007618 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007619 {
7620 switch( ssl->minor_ver )
7621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007623 return( "DTLSv1.0" );
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007626 return( "DTLSv1.2" );
7627
7628 default:
7629 return( "unknown (DTLS)" );
7630 }
7631 }
7632#endif
7633
Paul Bakker43ca69c2011-01-15 17:35:19 +00007634 switch( ssl->minor_ver )
7635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007637 return( "SSLv3.0" );
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007640 return( "TLSv1.0" );
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007643 return( "TLSv1.1" );
7644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007646 return( "TLSv1.2" );
7647
Paul Bakker43ca69c2011-01-15 17:35:19 +00007648 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007649 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007650 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007651}
7652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007654{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007655 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007657 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007658
Hanno Becker78640902018-08-13 16:35:15 +01007659 if( transform == NULL )
7660 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_ZLIB_SUPPORT)
7663 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7664 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007665 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007666#endif
7667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 case MBEDTLS_MODE_GCM:
7671 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007672 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007674 transform_expansion = transform->minlen;
7675 break;
7676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007678
7679 block_size = mbedtls_cipher_get_block_size(
7680 &transform->cipher_ctx_enc );
7681
7682#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7683 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7684 {
7685 /* Expansion due to addition of
7686 * - MAC
7687 * - CBC padding (theoretically up to 256 bytes, but
7688 * we never use more than block_size)
7689 * - explicit IV
7690 */
7691 transform_expansion = transform->maclen + 2 * block_size;
7692 }
7693 else
7694#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7695 {
7696 /* No explicit IV prior to TLS 1.1. */
7697 transform_expansion = transform->maclen + block_size;
7698 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007699 break;
7700
7701 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007704 }
7705
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007706 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007707}
7708
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007709#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7710size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7711{
7712 size_t max_len;
7713
7714 /*
7715 * Assume mfl_code is correct since it was checked when set
7716 */
Angus Grattond8213d02016-05-25 20:56:48 +10007717 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007718
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007719 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007720 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007721 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007722 {
Angus Grattond8213d02016-05-25 20:56:48 +10007723 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007724 }
7725
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007726 /* During a handshake, use the value being negotiated */
7727 if( ssl->session_negotiate != NULL &&
7728 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7729 {
7730 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7731 }
7732
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007733 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007734}
7735#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7736
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007737int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7738{
7739 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7740
7741#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7742 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7743
7744 if( max_len > mfl )
7745 max_len = mfl;
7746#endif
7747
7748#if defined(MBEDTLS_SSL_PROTO_DTLS)
7749 if( ssl->conf->mtu != 0 )
7750 {
7751 const size_t mtu = ssl->conf->mtu;
7752 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7753 const size_t overhead = (size_t) ret;
7754
7755 if( ret < 0 )
7756 return( ret );
7757
7758 if( mtu <= overhead )
7759 {
7760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7761 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7762 }
7763
7764 if( max_len > mtu - overhead )
7765 max_len = mtu - overhead;
7766 }
7767#endif
7768
Hanno Becker0defedb2018-08-10 12:35:02 +01007769#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7770 !defined(MBEDTLS_SSL_PROTO_DTLS)
7771 ((void) ssl);
7772#endif
7773
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007774 return( (int) max_len );
7775}
7776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_X509_CRT_PARSE_C)
7778const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007779{
7780 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007781 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007782
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007783 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SSL_CLI_C)
7788int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007789{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007790 if( ssl == NULL ||
7791 dst == NULL ||
7792 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007793 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007796 }
7797
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007798 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007801
Paul Bakker5121ce52009-01-03 21:22:43 +00007802/*
Paul Bakker1961b702013-01-25 14:49:24 +01007803 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007808
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007809 if( ssl == NULL || ssl->conf == NULL )
7810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007813 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007817 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819#endif
7820
Paul Bakker1961b702013-01-25 14:49:24 +01007821 return( ret );
7822}
7823
7824/*
7825 * Perform the SSL handshake
7826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007828{
7829 int ret = 0;
7830
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007831 if( ssl == NULL || ssl->conf == NULL )
7832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007839
7840 if( ret != 0 )
7841 break;
7842 }
7843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845
7846 return( ret );
7847}
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_SSL_RENEGOTIATION)
7850#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007851/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007852 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007855{
7856 int ret;
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007859
7860 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7862 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007863
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007864 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007865 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007867 return( ret );
7868 }
7869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007871
7872 return( 0 );
7873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007875
7876/*
7877 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 * - any side: calling mbedtls_ssl_renegotiate(),
7879 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7880 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007881 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007882 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007886{
7887 int ret;
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007890
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007891 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7892 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007893
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007894 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7895 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007899 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007900 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007901 ssl->handshake->out_msg_seq = 1;
7902 else
7903 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007904 }
7905#endif
7906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7908 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007913 return( ret );
7914 }
7915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007917
7918 return( 0 );
7919}
7920
7921/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007922 * Renegotiate current connection on client,
7923 * or request renegotiation on server
7924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007928
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007929 if( ssl == NULL || ssl->conf == NULL )
7930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007933 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007934 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007940
7941 /* Did we already try/start sending HelloRequest? */
7942 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007944
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007945 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007950 /*
7951 * On client, either start the renegotiation process or,
7952 * if already in progress, continue the handshake
7953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007958
7959 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007962 return( ret );
7963 }
7964 }
7965 else
7966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007970 return( ret );
7971 }
7972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007974
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007975 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007976}
7977
7978/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007979 * Check record counters and renegotiate if they're above the limit.
7980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007982{
Andres AG2196c7f2016-12-15 17:01:16 +00007983 size_t ep_len = ssl_ep_len( ssl );
7984 int in_ctr_cmp;
7985 int out_ctr_cmp;
7986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7988 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007989 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007990 {
7991 return( 0 );
7992 }
7993
Andres AG2196c7f2016-12-15 17:01:16 +00007994 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7995 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007996 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007997 ssl->conf->renego_period + ep_len, 8 - ep_len );
7998
7999 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008000 {
8001 return( 0 );
8002 }
8003
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008008
8009/*
8010 * Receive application data decrypted from the SSL layer
8011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008013{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008014 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008015 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008016
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008017 if( ssl == NULL || ssl->conf == NULL )
8018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008026 return( ret );
8027
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008028 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008030 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008031 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008032 return( ret );
8033 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008034 }
8035#endif
8036
Hanno Becker4a810fb2017-05-24 16:27:30 +01008037 /*
8038 * Check if renegotiation is necessary and/or handshake is
8039 * in process. If yes, perform/continue, and fall through
8040 * if an unexpected packet is received while the client
8041 * is waiting for the ServerHello.
8042 *
8043 * (There is no equivalent to the last condition on
8044 * the server-side as it is not treated as within
8045 * a handshake while waiting for the ClientHello
8046 * after a renegotiation request.)
8047 */
8048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008050 ret = ssl_check_ctr_renegotiate( ssl );
8051 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8052 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008055 return( ret );
8056 }
8057#endif
8058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008062 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8063 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008066 return( ret );
8067 }
8068 }
8069
Hanno Beckere41158b2017-10-23 13:30:32 +01008070 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008071 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008072 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008073 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008074 if( ssl->f_get_timer != NULL &&
8075 ssl->f_get_timer( ssl->p_timer ) == -1 )
8076 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008077 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008078 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008079
Hanno Becker327c93b2018-08-15 13:56:18 +01008080 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008081 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008082 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8083 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008084
Hanno Becker4a810fb2017-05-24 16:27:30 +01008085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8086 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008087 }
8088
8089 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008091 {
8092 /*
8093 * OpenSSL sends empty messages to randomize the IV
8094 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008095 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008098 return( 0 );
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008101 return( ret );
8102 }
8103 }
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008108
Hanno Becker4a810fb2017-05-24 16:27:30 +01008109 /*
8110 * - For client-side, expect SERVER_HELLO_REQUEST.
8111 * - For server-side, expect CLIENT_HELLO.
8112 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8113 */
8114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008116 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008118 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008121
8122 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008124 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008125 {
8126 continue;
8127 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008130 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008131#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008132
Hanno Becker4a810fb2017-05-24 16:27:30 +01008133#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008134 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008138
8139 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008142 {
8143 continue;
8144 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008147 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008148#endif /* MBEDTLS_SSL_SRV_C */
8149
Hanno Becker21df7f92017-10-17 11:03:26 +01008150#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008151 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008152 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8153 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8154 ssl->conf->allow_legacy_renegotiation ==
8155 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8156 {
8157 /*
8158 * Accept renegotiation request
8159 */
Paul Bakker48916f92012-09-16 19:57:18 +00008160
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008161 /* DTLS clients need to know renego is server-initiated */
8162#if defined(MBEDTLS_SSL_PROTO_DTLS)
8163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8164 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8165 {
8166 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8167 }
8168#endif
8169 ret = ssl_start_renegotiation( ssl );
8170 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8171 ret != 0 )
8172 {
8173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8174 return( ret );
8175 }
8176 }
8177 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008178#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008179 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008180 /*
8181 * Refuse renegotiation
8182 */
8183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186#if defined(MBEDTLS_SSL_PROTO_SSL3)
8187 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008188 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008189 /* SSLv3 does not have a "no_renegotiation" warning, so
8190 we send a fatal alert and abort the connection. */
8191 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8192 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8193 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008194 }
8195 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8197#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8198 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8199 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8202 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8203 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008204 {
8205 return( ret );
8206 }
Paul Bakker48916f92012-09-16 19:57:18 +00008207 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8210 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8213 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008214 }
Paul Bakker48916f92012-09-16 19:57:18 +00008215 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008216
Hanno Becker90333da2017-10-10 11:27:13 +01008217 /* At this point, we don't know whether the renegotiation has been
8218 * completed or not. The cases to consider are the following:
8219 * 1) The renegotiation is complete. In this case, no new record
8220 * has been read yet.
8221 * 2) The renegotiation is incomplete because the client received
8222 * an application data record while awaiting the ServerHello.
8223 * 3) The renegotiation is incomplete because the client received
8224 * a non-handshake, non-application data message while awaiting
8225 * the ServerHello.
8226 * In each of these case, looping will be the proper action:
8227 * - For 1), the next iteration will read a new record and check
8228 * if it's application data.
8229 * - For 2), the loop condition isn't satisfied as application data
8230 * is present, hence continue is the same as break
8231 * - For 3), the loop condition is satisfied and read_record
8232 * will re-deliver the message that was held back by the client
8233 * when expecting the ServerHello.
8234 */
8235 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008236 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008237#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008239 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008240 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008241 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008242 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008245 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008247 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008248 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8253 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008256 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008257 }
8258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8262 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008263 }
8264
8265 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008266
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008267 /* We're going to return something now, cancel timer,
8268 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008270 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008271
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008272#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008273 /* If we requested renego but received AppData, resend HelloRequest.
8274 * Do it now, after setting in_offt, to avoid taking this branch
8275 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008277 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008279 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008280 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008283 return( ret );
8284 }
8285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008287#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008288 }
8289
8290 n = ( len < ssl->in_msglen )
8291 ? len : ssl->in_msglen;
8292
8293 memcpy( buf, ssl->in_offt, n );
8294 ssl->in_msglen -= n;
8295
8296 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008297 {
8298 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008299 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008300 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008302 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008303 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008304 /* more data available */
8305 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008309
Paul Bakker23986e52011-04-24 08:57:21 +00008310 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008311}
8312
8313/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008314 * Send application data to be encrypted by the SSL layer, taking care of max
8315 * fragment length and buffer size.
8316 *
8317 * According to RFC 5246 Section 6.2.1:
8318 *
8319 * Zero-length fragments of Application data MAY be sent as they are
8320 * potentially useful as a traffic analysis countermeasure.
8321 *
8322 * Therefore, it is possible that the input message length is 0 and the
8323 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008324 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008325static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008326 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008327{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008328 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8329 const size_t max_len = (size_t) ret;
8330
8331 if( ret < 0 )
8332 {
8333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8334 return( ret );
8335 }
8336
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008337 if( len > max_len )
8338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008343 "maximum fragment length: %d > %d",
8344 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008346 }
8347 else
8348#endif
8349 len = max_len;
8350 }
Paul Bakker887bd502011-06-08 13:10:54 +00008351
Paul Bakker5121ce52009-01-03 21:22:43 +00008352 if( ssl->out_left != 0 )
8353 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008354 /*
8355 * The user has previously tried to send the data and
8356 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8357 * written. In this case, we expect the high-level write function
8358 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008363 return( ret );
8364 }
8365 }
Paul Bakker887bd502011-06-08 13:10:54 +00008366 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008367 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008368 /*
8369 * The user is trying to send a message the first time, so we need to
8370 * copy the data into the internal buffers and setup the data structure
8371 * to keep track of partial writes
8372 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008373 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008375 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008376
Hanno Becker67bc7c32018-08-06 11:33:50 +01008377 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008380 return( ret );
8381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008382 }
8383
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008384 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008385}
8386
8387/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008388 * Write application data, doing 1/n-1 splitting if necessary.
8389 *
8390 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008391 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008392 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008395static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008396 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008397{
8398 int ret;
8399
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008400 if( ssl->conf->cbc_record_splitting ==
8401 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008402 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8404 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8405 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008406 {
8407 return( ssl_write_real( ssl, buf, len ) );
8408 }
8409
8410 if( ssl->split_done == 0 )
8411 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008412 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008413 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008414 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008415 }
8416
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008417 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8418 return( ret );
8419 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008420
8421 return( ret + 1 );
8422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008424
8425/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008426 * Write application data (public-facing wrapper)
8427 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008428int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008429{
8430 int ret;
8431
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008433
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008434 if( ssl == NULL || ssl->conf == NULL )
8435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8436
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008437#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008438 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8439 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008441 return( ret );
8442 }
8443#endif
8444
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008445 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008446 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008447 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008448 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008450 return( ret );
8451 }
8452 }
8453
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008454#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008455 ret = ssl_write_split( ssl, buf, len );
8456#else
8457 ret = ssl_write_real( ssl, buf, len );
8458#endif
8459
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008461
8462 return( ret );
8463}
8464
8465/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008466 * Notify the peer that the connection is being closed
8467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008469{
8470 int ret;
8471
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008472 if( ssl == NULL || ssl->conf == NULL )
8473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008476
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008477 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8483 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8484 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008487 return( ret );
8488 }
8489 }
8490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008492
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008493 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008494}
8495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008497{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008498 if( transform == NULL )
8499 return;
8500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008502 deflateEnd( &transform->ctx_deflate );
8503 inflateEnd( &transform->ctx_inflate );
8504#endif
8505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8507 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 mbedtls_md_free( &transform->md_ctx_enc );
8510 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008511
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008512 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008513}
8514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515#if defined(MBEDTLS_X509_CRT_PARSE_C)
8516static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008519
8520 while( cur != NULL )
8521 {
8522 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008524 cur = next;
8525 }
8526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008528
Hanno Becker0271f962018-08-16 13:23:47 +01008529#if defined(MBEDTLS_SSL_PROTO_DTLS)
8530
8531static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8532{
8533 unsigned offset;
8534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8535
8536 if( hs == NULL )
8537 return;
8538
8539 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
8540 {
8541 mbedtls_ssl_hs_buffer *hs_buf = &hs->buffering.hs[offset];
8542 if( hs_buf->is_valid == 1 )
8543 {
8544 mbedtls_free( hs_buf->data );
8545 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
8546 }
8547 }
8548}
8549
8550#endif /* MBEDTLS_SSL_PROTO_DTLS */
8551
Gilles Peskine9b562d52018-04-25 20:32:43 +02008552void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008553{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008554 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8555
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008556 if( handshake == NULL )
8557 return;
8558
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008559#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8560 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8561 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008562 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008563 handshake->async_in_progress = 0;
8564 }
8565#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8566
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8568 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8569 mbedtls_md5_free( &handshake->fin_md5 );
8570 mbedtls_sha1_free( &handshake->fin_sha1 );
8571#endif
8572#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8573#if defined(MBEDTLS_SHA256_C)
8574 mbedtls_sha256_free( &handshake->fin_sha256 );
8575#endif
8576#if defined(MBEDTLS_SHA512_C)
8577 mbedtls_sha512_free( &handshake->fin_sha512 );
8578#endif
8579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581#if defined(MBEDTLS_DHM_C)
8582 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008583#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#if defined(MBEDTLS_ECDH_C)
8585 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008586#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008587#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008588 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008589#if defined(MBEDTLS_SSL_CLI_C)
8590 mbedtls_free( handshake->ecjpake_cache );
8591 handshake->ecjpake_cache = NULL;
8592 handshake->ecjpake_cache_len = 0;
8593#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008594#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008595
Janos Follath4ae5c292016-02-10 11:27:43 +00008596#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8597 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008598 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008600#endif
8601
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008602#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8603 if( handshake->psk != NULL )
8604 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008605 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008606 mbedtls_free( handshake->psk );
8607 }
8608#endif
8609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8611 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008612 /*
8613 * Free only the linked list wrapper, not the keys themselves
8614 * since the belong to the SNI callback
8615 */
8616 if( handshake->sni_key_cert != NULL )
8617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008619
8620 while( cur != NULL )
8621 {
8622 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008624 cur = next;
8625 }
8626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629#if defined(MBEDTLS_SSL_PROTO_DTLS)
8630 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008631 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008632 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008633 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008634#endif
8635
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008636 mbedtls_platform_zeroize( handshake,
8637 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008638}
8639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008641{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008642 if( session == NULL )
8643 return;
8644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008646 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648 mbedtls_x509_crt_free( session->peer_cert );
8649 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008650 }
Paul Bakkered27a042013-04-18 22:46:23 +02008651#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008652
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008653#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008655#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008656
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008657 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008658}
8659
Paul Bakker5121ce52009-01-03 21:22:43 +00008660/*
8661 * Free an SSL context
8662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008664{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008665 if( ssl == NULL )
8666 return;
8667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008669
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008670 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008671 {
Angus Grattond8213d02016-05-25 20:56:48 +10008672 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008674 }
8675
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008676 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008677 {
Angus Grattond8213d02016-05-25 20:56:48 +10008678 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008680 }
8681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008683 if( ssl->compress_buf != NULL )
8684 {
Angus Grattond8213d02016-05-25 20:56:48 +10008685 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008687 }
8688#endif
8689
Paul Bakker48916f92012-09-16 19:57:18 +00008690 if( ssl->transform )
8691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 mbedtls_ssl_transform_free( ssl->transform );
8693 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008694 }
8695
8696 if( ssl->handshake )
8697 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008698 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8700 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702 mbedtls_free( ssl->handshake );
8703 mbedtls_free( ssl->transform_negotiate );
8704 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008705 }
8706
Paul Bakkerc0463502013-02-14 11:19:38 +01008707 if( ssl->session )
8708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 mbedtls_ssl_session_free( ssl->session );
8710 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008711 }
8712
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008713#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008714 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008715 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008716 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008718 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8722 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8725 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008726 }
8727#endif
8728
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008729#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008731#endif
8732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008734
Paul Bakker86f04f42013-02-14 11:20:09 +01008735 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008736 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008737}
8738
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008739/*
8740 * Initialze mbedtls_ssl_config
8741 */
8742void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8743{
8744 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8745}
8746
Simon Butcherc97b6972015-12-27 23:48:17 +00008747#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008748static int ssl_preset_default_hashes[] = {
8749#if defined(MBEDTLS_SHA512_C)
8750 MBEDTLS_MD_SHA512,
8751 MBEDTLS_MD_SHA384,
8752#endif
8753#if defined(MBEDTLS_SHA256_C)
8754 MBEDTLS_MD_SHA256,
8755 MBEDTLS_MD_SHA224,
8756#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008757#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008758 MBEDTLS_MD_SHA1,
8759#endif
8760 MBEDTLS_MD_NONE
8761};
Simon Butcherc97b6972015-12-27 23:48:17 +00008762#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008763
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008764static int ssl_preset_suiteb_ciphersuites[] = {
8765 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8766 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8767 0
8768};
8769
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008770#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008771static int ssl_preset_suiteb_hashes[] = {
8772 MBEDTLS_MD_SHA256,
8773 MBEDTLS_MD_SHA384,
8774 MBEDTLS_MD_NONE
8775};
8776#endif
8777
8778#if defined(MBEDTLS_ECP_C)
8779static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8780 MBEDTLS_ECP_DP_SECP256R1,
8781 MBEDTLS_ECP_DP_SECP384R1,
8782 MBEDTLS_ECP_DP_NONE
8783};
8784#endif
8785
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008786/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008787 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008788 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008789int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008790 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008791{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008792#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008793 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008794#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008795
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008796 /* Use the functions here so that they are covered in tests,
8797 * but otherwise access member directly for efficiency */
8798 mbedtls_ssl_conf_endpoint( conf, endpoint );
8799 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008800
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008801 /*
8802 * Things that are common to all presets
8803 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008804#if defined(MBEDTLS_SSL_CLI_C)
8805 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8806 {
8807 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8808#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8809 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8810#endif
8811 }
8812#endif
8813
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008814#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008815 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008816#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008817
8818#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8819 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8820#endif
8821
8822#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8823 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8824#endif
8825
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008826#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8827 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8828#endif
8829
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008830#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008831 conf->f_cookie_write = ssl_cookie_write_dummy;
8832 conf->f_cookie_check = ssl_cookie_check_dummy;
8833#endif
8834
8835#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8836 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8837#endif
8838
Janos Follath088ce432017-04-10 12:42:31 +01008839#if defined(MBEDTLS_SSL_SRV_C)
8840 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8841#endif
8842
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008843#if defined(MBEDTLS_SSL_PROTO_DTLS)
8844 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8845 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8846#endif
8847
8848#if defined(MBEDTLS_SSL_RENEGOTIATION)
8849 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008850 memset( conf->renego_period, 0x00, 2 );
8851 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008852#endif
8853
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008854#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8855 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8856 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008857 const unsigned char dhm_p[] =
8858 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8859 const unsigned char dhm_g[] =
8860 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8861
Hanno Beckera90658f2017-10-04 15:29:08 +01008862 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8863 dhm_p, sizeof( dhm_p ),
8864 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008865 {
8866 return( ret );
8867 }
8868 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008869#endif
8870
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008871 /*
8872 * Preset-specific defaults
8873 */
8874 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008875 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008876 /*
8877 * NSA Suite B
8878 */
8879 case MBEDTLS_SSL_PRESET_SUITEB:
8880 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8881 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8882 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8883 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8884
8885 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8886 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8887 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8888 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8889 ssl_preset_suiteb_ciphersuites;
8890
8891#if defined(MBEDTLS_X509_CRT_PARSE_C)
8892 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008893#endif
8894
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008895#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008896 conf->sig_hashes = ssl_preset_suiteb_hashes;
8897#endif
8898
8899#if defined(MBEDTLS_ECP_C)
8900 conf->curve_list = ssl_preset_suiteb_curves;
8901#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008902 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008903
8904 /*
8905 * Default
8906 */
8907 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008908 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8909 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8910 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8911 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8912 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8913 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8914 MBEDTLS_SSL_MIN_MINOR_VERSION :
8915 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008916 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8917 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8918
8919#if defined(MBEDTLS_SSL_PROTO_DTLS)
8920 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8921 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8922#endif
8923
8924 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8927 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8928 mbedtls_ssl_list_ciphersuites();
8929
8930#if defined(MBEDTLS_X509_CRT_PARSE_C)
8931 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8932#endif
8933
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008934#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008935 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008936#endif
8937
8938#if defined(MBEDTLS_ECP_C)
8939 conf->curve_list = mbedtls_ecp_grp_id_list();
8940#endif
8941
8942#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8943 conf->dhm_min_bitlen = 1024;
8944#endif
8945 }
8946
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008947 return( 0 );
8948}
8949
8950/*
8951 * Free mbedtls_ssl_config
8952 */
8953void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8954{
8955#if defined(MBEDTLS_DHM_C)
8956 mbedtls_mpi_free( &conf->dhm_P );
8957 mbedtls_mpi_free( &conf->dhm_G );
8958#endif
8959
8960#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8961 if( conf->psk != NULL )
8962 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008963 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008964 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008965 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008966 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008967 }
8968
8969 if( conf->psk_identity != NULL )
8970 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008971 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008972 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008973 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008974 conf->psk_identity_len = 0;
8975 }
8976#endif
8977
8978#if defined(MBEDTLS_X509_CRT_PARSE_C)
8979 ssl_key_cert_free( conf->key_cert );
8980#endif
8981
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008982 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008983}
8984
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008985#if defined(MBEDTLS_PK_C) && \
8986 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008987/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992#if defined(MBEDTLS_RSA_C)
8993 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8994 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996#if defined(MBEDTLS_ECDSA_C)
8997 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8998 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008999#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009001}
9002
Hanno Becker7e5437a2017-04-28 17:15:26 +01009003unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9004{
9005 switch( type ) {
9006 case MBEDTLS_PK_RSA:
9007 return( MBEDTLS_SSL_SIG_RSA );
9008 case MBEDTLS_PK_ECDSA:
9009 case MBEDTLS_PK_ECKEY:
9010 return( MBEDTLS_SSL_SIG_ECDSA );
9011 default:
9012 return( MBEDTLS_SSL_SIG_ANON );
9013 }
9014}
9015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009017{
9018 switch( sig )
9019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020#if defined(MBEDTLS_RSA_C)
9021 case MBEDTLS_SSL_SIG_RSA:
9022 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024#if defined(MBEDTLS_ECDSA_C)
9025 case MBEDTLS_SSL_SIG_ECDSA:
9026 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009027#endif
9028 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009030 }
9031}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009032#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009033
Hanno Becker7e5437a2017-04-28 17:15:26 +01009034#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9035 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9036
9037/* Find an entry in a signature-hash set matching a given hash algorithm. */
9038mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9039 mbedtls_pk_type_t sig_alg )
9040{
9041 switch( sig_alg )
9042 {
9043 case MBEDTLS_PK_RSA:
9044 return( set->rsa );
9045 case MBEDTLS_PK_ECDSA:
9046 return( set->ecdsa );
9047 default:
9048 return( MBEDTLS_MD_NONE );
9049 }
9050}
9051
9052/* Add a signature-hash-pair to a signature-hash set */
9053void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9054 mbedtls_pk_type_t sig_alg,
9055 mbedtls_md_type_t md_alg )
9056{
9057 switch( sig_alg )
9058 {
9059 case MBEDTLS_PK_RSA:
9060 if( set->rsa == MBEDTLS_MD_NONE )
9061 set->rsa = md_alg;
9062 break;
9063
9064 case MBEDTLS_PK_ECDSA:
9065 if( set->ecdsa == MBEDTLS_MD_NONE )
9066 set->ecdsa = md_alg;
9067 break;
9068
9069 default:
9070 break;
9071 }
9072}
9073
9074/* Allow exactly one hash algorithm for each signature. */
9075void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9076 mbedtls_md_type_t md_alg )
9077{
9078 set->rsa = md_alg;
9079 set->ecdsa = md_alg;
9080}
9081
9082#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9083 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9084
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009085/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009086 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009089{
9090 switch( hash )
9091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#if defined(MBEDTLS_MD5_C)
9093 case MBEDTLS_SSL_HASH_MD5:
9094 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_SHA1_C)
9097 case MBEDTLS_SSL_HASH_SHA1:
9098 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100#if defined(MBEDTLS_SHA256_C)
9101 case MBEDTLS_SSL_HASH_SHA224:
9102 return( MBEDTLS_MD_SHA224 );
9103 case MBEDTLS_SSL_HASH_SHA256:
9104 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106#if defined(MBEDTLS_SHA512_C)
9107 case MBEDTLS_SSL_HASH_SHA384:
9108 return( MBEDTLS_MD_SHA384 );
9109 case MBEDTLS_SSL_HASH_SHA512:
9110 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009111#endif
9112 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009114 }
9115}
9116
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009117/*
9118 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9119 */
9120unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9121{
9122 switch( md )
9123 {
9124#if defined(MBEDTLS_MD5_C)
9125 case MBEDTLS_MD_MD5:
9126 return( MBEDTLS_SSL_HASH_MD5 );
9127#endif
9128#if defined(MBEDTLS_SHA1_C)
9129 case MBEDTLS_MD_SHA1:
9130 return( MBEDTLS_SSL_HASH_SHA1 );
9131#endif
9132#if defined(MBEDTLS_SHA256_C)
9133 case MBEDTLS_MD_SHA224:
9134 return( MBEDTLS_SSL_HASH_SHA224 );
9135 case MBEDTLS_MD_SHA256:
9136 return( MBEDTLS_SSL_HASH_SHA256 );
9137#endif
9138#if defined(MBEDTLS_SHA512_C)
9139 case MBEDTLS_MD_SHA384:
9140 return( MBEDTLS_SSL_HASH_SHA384 );
9141 case MBEDTLS_MD_SHA512:
9142 return( MBEDTLS_SSL_HASH_SHA512 );
9143#endif
9144 default:
9145 return( MBEDTLS_SSL_HASH_NONE );
9146 }
9147}
9148
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009149#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009150/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009151 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009152 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009153 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009154int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009157
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009158 if( ssl->conf->curve_list == NULL )
9159 return( -1 );
9160
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009161 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009162 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009163 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009164
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009165 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009166}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009167#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009168
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009169#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009170/*
9171 * Check if a hash proposed by the peer is in our list.
9172 * Return 0 if we're willing to use it, -1 otherwise.
9173 */
9174int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9175 mbedtls_md_type_t md )
9176{
9177 const int *cur;
9178
9179 if( ssl->conf->sig_hashes == NULL )
9180 return( -1 );
9181
9182 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9183 if( *cur == (int) md )
9184 return( 0 );
9185
9186 return( -1 );
9187}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009188#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190#if defined(MBEDTLS_X509_CRT_PARSE_C)
9191int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9192 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009193 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009194 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009195{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009196 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009198 int usage = 0;
9199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009201 const char *ext_oid;
9202 size_t ext_len;
9203#endif
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9206 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009207 ((void) cert);
9208 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009209 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009210#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9213 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009214 {
9215 /* Server part of the key exchange */
9216 switch( ciphersuite->key_exchange )
9217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 case MBEDTLS_KEY_EXCHANGE_RSA:
9219 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009220 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009221 break;
9222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9224 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9225 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9226 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009227 break;
9228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9230 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009231 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009232 break;
9233
9234 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235 case MBEDTLS_KEY_EXCHANGE_NONE:
9236 case MBEDTLS_KEY_EXCHANGE_PSK:
9237 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9238 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009239 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009240 usage = 0;
9241 }
9242 }
9243 else
9244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9246 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009247 }
9248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009250 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009251 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009252 ret = -1;
9253 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009254#else
9255 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9259 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9262 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009263 }
9264 else
9265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9267 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009268 }
9269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009271 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009272 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009273 ret = -1;
9274 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009276
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009277 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009280
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009281/*
9282 * Convert version numbers to/from wire format
9283 * and, for DTLS, to/from TLS equivalent.
9284 *
9285 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009286 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009287 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9288 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009291 unsigned char ver[2] )
9292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293#if defined(MBEDTLS_SSL_PROTO_DTLS)
9294 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009297 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9298
9299 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9300 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9301 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009302 else
9303#else
9304 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009305#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009306 {
9307 ver[0] = (unsigned char) major;
9308 ver[1] = (unsigned char) minor;
9309 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009310}
9311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009313 const unsigned char ver[2] )
9314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_SSL_PROTO_DTLS)
9316 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009317 {
9318 *major = 255 - ver[0] + 2;
9319 *minor = 255 - ver[1] + 1;
9320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009322 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9323 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009324 else
9325#else
9326 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009327#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009328 {
9329 *major = ver[0];
9330 *minor = ver[1];
9331 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009332}
9333
Simon Butcher99000142016-10-13 17:21:01 +01009334int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9335{
9336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9337 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9338 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9339
9340 switch( md )
9341 {
9342#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9343#if defined(MBEDTLS_MD5_C)
9344 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009345 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009346#endif
9347#if defined(MBEDTLS_SHA1_C)
9348 case MBEDTLS_SSL_HASH_SHA1:
9349 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9350 break;
9351#endif
9352#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9353#if defined(MBEDTLS_SHA512_C)
9354 case MBEDTLS_SSL_HASH_SHA384:
9355 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9356 break;
9357#endif
9358#if defined(MBEDTLS_SHA256_C)
9359 case MBEDTLS_SSL_HASH_SHA256:
9360 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9361 break;
9362#endif
9363 default:
9364 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9365 }
9366
9367 return 0;
9368#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9369 (void) ssl;
9370 (void) md;
9371
9372 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9373#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9374}
9375
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009376#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9377 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9378int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9379 unsigned char *output,
9380 unsigned char *data, size_t data_len )
9381{
9382 int ret = 0;
9383 mbedtls_md5_context mbedtls_md5;
9384 mbedtls_sha1_context mbedtls_sha1;
9385
9386 mbedtls_md5_init( &mbedtls_md5 );
9387 mbedtls_sha1_init( &mbedtls_sha1 );
9388
9389 /*
9390 * digitally-signed struct {
9391 * opaque md5_hash[16];
9392 * opaque sha_hash[20];
9393 * };
9394 *
9395 * md5_hash
9396 * MD5(ClientHello.random + ServerHello.random
9397 * + ServerParams);
9398 * sha_hash
9399 * SHA(ClientHello.random + ServerHello.random
9400 * + ServerParams);
9401 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009402 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009403 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009405 goto exit;
9406 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009407 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009408 ssl->handshake->randbytes, 64 ) ) != 0 )
9409 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009411 goto exit;
9412 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009413 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009414 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009416 goto exit;
9417 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009418 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009419 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009421 goto exit;
9422 }
9423
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009424 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009425 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009427 goto exit;
9428 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009429 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009430 ssl->handshake->randbytes, 64 ) ) != 0 )
9431 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009433 goto exit;
9434 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009435 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009436 data_len ) ) != 0 )
9437 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009439 goto exit;
9440 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009441 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009442 output + 16 ) ) != 0 )
9443 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009445 goto exit;
9446 }
9447
9448exit:
9449 mbedtls_md5_free( &mbedtls_md5 );
9450 mbedtls_sha1_free( &mbedtls_sha1 );
9451
9452 if( ret != 0 )
9453 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9454 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9455
9456 return( ret );
9457
9458}
9459#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9460 MBEDTLS_SSL_PROTO_TLS1_1 */
9461
9462#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9463 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9464int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009465 unsigned char *hash, size_t *hashlen,
9466 unsigned char *data, size_t data_len,
9467 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009468{
9469 int ret = 0;
9470 mbedtls_md_context_t ctx;
9471 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009472 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009473
9474 mbedtls_md_init( &ctx );
9475
9476 /*
9477 * digitally-signed struct {
9478 * opaque client_random[32];
9479 * opaque server_random[32];
9480 * ServerDHParams params;
9481 * };
9482 */
9483 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9484 {
9485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9486 goto exit;
9487 }
9488 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9489 {
9490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9491 goto exit;
9492 }
9493 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9494 {
9495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9496 goto exit;
9497 }
9498 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9499 {
9500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9501 goto exit;
9502 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009503 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009504 {
9505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9506 goto exit;
9507 }
9508
9509exit:
9510 mbedtls_md_free( &ctx );
9511
9512 if( ret != 0 )
9513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9514 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9515
9516 return( ret );
9517}
9518#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9519 MBEDTLS_SSL_PROTO_TLS1_2 */
9520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#endif /* MBEDTLS_SSL_TLS_C */