blob: 4b64fe6239f43fe71d588cac5bf2d4fdb3f4075c [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
Hanno Becker56d5eaa2018-08-17 09:06:31 +01003534#if !defined(MBEDTLS_DEBUG_C)
3535 /* The SSL context is used for debugging only. */
3536 ((void) ssl);
3537#endif
3538
Hanno Becker56e205e2018-08-16 09:06:12 +01003539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
3540 msg_len ) );
3541
3542 /* NOTE: That should be checked earlier */
3543 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
3544 {
3545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3546 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3547 }
3548
3549 alloc_len = 12; /* Handshake header */
3550 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003551
3552 if( add_bitmap )
3553 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003554
3555 buf = mbedtls_calloc( 1, alloc_len );
3556 if( buf == NULL )
3557 {
3558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
3559 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
3560 }
3561
3562 *target = buf;
3563 return( 0 );
3564}
3565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003567
Hanno Becker12555c62018-08-16 12:47:53 +01003568static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3569{
3570 return( ( ssl->in_msg[1] << 16 ) |
3571 ( ssl->in_msg[2] << 8 ) |
3572 ssl->in_msg[3] );
3573}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003574
Simon Butcher99000142016-10-13 17:21:01 +01003575int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003580 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003582 }
3583
Hanno Becker12555c62018-08-16 12:47:53 +01003584 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003587 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003588 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003591 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003592 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003593 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003594 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003595
Hanno Becker44650b72018-08-16 12:51:11 +01003596 if( ssl_check_hs_header( ssl ) != 0 )
3597 {
3598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3599 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3600 }
3601
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003602 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003603 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3604 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3605 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3606 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003607 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003608 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3609 {
3610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3611 recv_msg_seq,
3612 ssl->handshake->in_msg_seq ) );
3613 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3614 }
3615
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003616 /* Retransmit only on last message from previous flight, to avoid
3617 * too many retransmissions.
3618 * Besides, No sane server ever retransmits HelloVerifyRequest */
3619 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003623 "message_seq = %d, start_of_flight = %d",
3624 recv_msg_seq,
3625 ssl->handshake->in_flight_start_seq ) );
3626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003630 return( ret );
3631 }
3632 }
3633 else
3634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003636 "message_seq = %d, expected = %d",
3637 recv_msg_seq,
3638 ssl->handshake->in_msg_seq ) );
3639 }
3640
Hanno Becker90333da2017-10-10 11:27:13 +01003641 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003642 }
3643 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003644
Hanno Becker6d97ef52018-08-16 13:09:04 +01003645 /* Message reassembly is handled alongside buffering of future
3646 * messages; the commonality is that both handshake fragments and
3647 * future messages cannot be forwarded immediately to the handshake
3648 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003649 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003652 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003653 }
3654 }
3655 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003657 /* With TLS we don't handle fragmentation (for now) */
3658 if( ssl->in_msglen < ssl->in_hslen )
3659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3661 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003662 }
3663
Simon Butcher99000142016-10-13 17:21:01 +01003664 return( 0 );
3665}
3666
3667void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3668{
Hanno Becker0271f962018-08-16 13:23:47 +01003669 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003670
Hanno Becker0271f962018-08-16 13:23:47 +01003671 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003672 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003673 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003674 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003675
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003676 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003678 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003679 ssl->handshake != NULL )
3680 {
Hanno Becker0271f962018-08-16 13:23:47 +01003681 unsigned offset;
3682 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003683
Hanno Becker0271f962018-08-16 13:23:47 +01003684 /* Increment handshake sequence number */
3685 hs->in_msg_seq++;
3686
3687 /*
3688 * Clear up handshake buffering and reassembly structure.
3689 */
3690
3691 /* Free first entry */
3692 hs_buf = &hs->buffering.hs[0];
3693 if( hs_buf->is_valid )
3694 mbedtls_free( hs_buf->data );
3695
3696 /* Shift all other entries */
3697 for( offset = 0; offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
3698 offset++, hs_buf++ )
3699 {
3700 *hs_buf = *(hs_buf + 1);
3701 }
3702
3703 /* Create a fresh last entry */
3704 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003705 }
3706#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003707}
3708
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003709/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003710 * DTLS anti-replay: RFC 6347 4.1.2.6
3711 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003712 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3713 * Bit n is set iff record number in_window_top - n has been seen.
3714 *
3715 * Usually, in_window_top is the last record number seen and the lsb of
3716 * in_window is set. The only exception is the initial state (record number 0
3717 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3720static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003721{
3722 ssl->in_window_top = 0;
3723 ssl->in_window = 0;
3724}
3725
3726static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3727{
3728 return( ( (uint64_t) buf[0] << 40 ) |
3729 ( (uint64_t) buf[1] << 32 ) |
3730 ( (uint64_t) buf[2] << 24 ) |
3731 ( (uint64_t) buf[3] << 16 ) |
3732 ( (uint64_t) buf[4] << 8 ) |
3733 ( (uint64_t) buf[5] ) );
3734}
3735
3736/*
3737 * Return 0 if sequence number is acceptable, -1 otherwise
3738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003740{
3741 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3742 uint64_t bit;
3743
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003744 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003745 return( 0 );
3746
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003747 if( rec_seqnum > ssl->in_window_top )
3748 return( 0 );
3749
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003750 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003751
3752 if( bit >= 64 )
3753 return( -1 );
3754
3755 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3756 return( -1 );
3757
3758 return( 0 );
3759}
3760
3761/*
3762 * Update replay window on new validated record
3763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003765{
3766 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3767
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003768 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003769 return;
3770
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003771 if( rec_seqnum > ssl->in_window_top )
3772 {
3773 /* Update window_top and the contents of the window */
3774 uint64_t shift = rec_seqnum - ssl->in_window_top;
3775
3776 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003777 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003778 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003779 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003780 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003781 ssl->in_window |= 1;
3782 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003783
3784 ssl->in_window_top = rec_seqnum;
3785 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003786 else
3787 {
3788 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003789 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003790
3791 if( bit < 64 ) /* Always true, but be extra sure */
3792 ssl->in_window |= (uint64_t) 1 << bit;
3793 }
3794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003796
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003797#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003798/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003799static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3800
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003801/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003802 * Without any SSL context, check if a datagram looks like a ClientHello with
3803 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003804 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003805 *
3806 * - if cookie is valid, return 0
3807 * - if ClientHello looks superficially valid but cookie is not,
3808 * fill obuf and set olen, then
3809 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3810 * - otherwise return a specific error code
3811 */
3812static int ssl_check_dtls_clihlo_cookie(
3813 mbedtls_ssl_cookie_write_t *f_cookie_write,
3814 mbedtls_ssl_cookie_check_t *f_cookie_check,
3815 void *p_cookie,
3816 const unsigned char *cli_id, size_t cli_id_len,
3817 const unsigned char *in, size_t in_len,
3818 unsigned char *obuf, size_t buf_len, size_t *olen )
3819{
3820 size_t sid_len, cookie_len;
3821 unsigned char *p;
3822
3823 if( f_cookie_write == NULL || f_cookie_check == NULL )
3824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3825
3826 /*
3827 * Structure of ClientHello with record and handshake headers,
3828 * and expected values. We don't need to check a lot, more checks will be
3829 * done when actually parsing the ClientHello - skipping those checks
3830 * avoids code duplication and does not make cookie forging any easier.
3831 *
3832 * 0-0 ContentType type; copied, must be handshake
3833 * 1-2 ProtocolVersion version; copied
3834 * 3-4 uint16 epoch; copied, must be 0
3835 * 5-10 uint48 sequence_number; copied
3836 * 11-12 uint16 length; (ignored)
3837 *
3838 * 13-13 HandshakeType msg_type; (ignored)
3839 * 14-16 uint24 length; (ignored)
3840 * 17-18 uint16 message_seq; copied
3841 * 19-21 uint24 fragment_offset; copied, must be 0
3842 * 22-24 uint24 fragment_length; (ignored)
3843 *
3844 * 25-26 ProtocolVersion client_version; (ignored)
3845 * 27-58 Random random; (ignored)
3846 * 59-xx SessionID session_id; 1 byte len + sid_len content
3847 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3848 * ...
3849 *
3850 * Minimum length is 61 bytes.
3851 */
3852 if( in_len < 61 ||
3853 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3854 in[3] != 0 || in[4] != 0 ||
3855 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3856 {
3857 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3858 }
3859
3860 sid_len = in[59];
3861 if( sid_len > in_len - 61 )
3862 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3863
3864 cookie_len = in[60 + sid_len];
3865 if( cookie_len > in_len - 60 )
3866 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3867
3868 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3869 cli_id, cli_id_len ) == 0 )
3870 {
3871 /* Valid cookie */
3872 return( 0 );
3873 }
3874
3875 /*
3876 * If we get here, we've got an invalid cookie, let's prepare HVR.
3877 *
3878 * 0-0 ContentType type; copied
3879 * 1-2 ProtocolVersion version; copied
3880 * 3-4 uint16 epoch; copied
3881 * 5-10 uint48 sequence_number; copied
3882 * 11-12 uint16 length; olen - 13
3883 *
3884 * 13-13 HandshakeType msg_type; hello_verify_request
3885 * 14-16 uint24 length; olen - 25
3886 * 17-18 uint16 message_seq; copied
3887 * 19-21 uint24 fragment_offset; copied
3888 * 22-24 uint24 fragment_length; olen - 25
3889 *
3890 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3891 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3892 *
3893 * Minimum length is 28.
3894 */
3895 if( buf_len < 28 )
3896 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3897
3898 /* Copy most fields and adapt others */
3899 memcpy( obuf, in, 25 );
3900 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3901 obuf[25] = 0xfe;
3902 obuf[26] = 0xff;
3903
3904 /* Generate and write actual cookie */
3905 p = obuf + 28;
3906 if( f_cookie_write( p_cookie,
3907 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3908 {
3909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3910 }
3911
3912 *olen = p - obuf;
3913
3914 /* Go back and fill length fields */
3915 obuf[27] = (unsigned char)( *olen - 28 );
3916
3917 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3918 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3919 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3920
3921 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3922 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3923
3924 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3925}
3926
3927/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003928 * Handle possible client reconnect with the same UDP quadruplet
3929 * (RFC 6347 Section 4.2.8).
3930 *
3931 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3932 * that looks like a ClientHello.
3933 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003934 * - if the input looks like a ClientHello without cookies,
3935 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003936 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003937 * - if the input looks like a ClientHello with a valid cookie,
3938 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003939 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003940 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003941 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003942 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003943 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3944 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003945 */
3946static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3947{
3948 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003949 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003950
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003951 ret = ssl_check_dtls_clihlo_cookie(
3952 ssl->conf->f_cookie_write,
3953 ssl->conf->f_cookie_check,
3954 ssl->conf->p_cookie,
3955 ssl->cli_id, ssl->cli_id_len,
3956 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003957 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003958
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003959 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3960
3961 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003962 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003963 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003964 * If the error is permanent we'll catch it later,
3965 * if it's not, then hopefully it'll work next time. */
3966 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3967
3968 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003969 }
3970
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003971 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003972 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003973 /* Got a valid cookie, partially reset context */
3974 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3975 {
3976 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3977 return( ret );
3978 }
3979
3980 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003981 }
3982
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003983 return( ret );
3984}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003985#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003986
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003987/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003988 * ContentType type;
3989 * ProtocolVersion version;
3990 * uint16 epoch; // DTLS only
3991 * uint48 sequence_number; // DTLS only
3992 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003993 *
3994 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003995 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003996 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3997 *
3998 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003999 * 1. proceed with the record if this function returns 0
4000 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4001 * 3. return CLIENT_RECONNECT if this function return that value
4002 * 4. drop the whole datagram if this function returns anything else.
4003 * Point 2 is needed when the peer is resending, and we have already received
4004 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004007{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004008 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 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 +02004011
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004013 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004014 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004018 ssl->in_msgtype,
4019 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004021 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4023 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4024 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4025 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004028
4029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004030 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4031 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004032 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4033#endif /* MBEDTLS_SSL_PROTO_DTLS */
4034 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4035 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004038 }
4039
4040 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004041 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4044 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 }
4046
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004047 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4050 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004051 }
4052
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004053 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004054 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004055 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4058 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004059 }
4060
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004061 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004062 * DTLS-related tests.
4063 * Check epoch before checking length constraint because
4064 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4065 * message gets duplicated before the corresponding Finished message,
4066 * the second ChangeCipherSpec should be discarded because it belongs
4067 * to an old epoch, but not because its length is shorter than
4068 * the minimum record length for packets using the new record transform.
4069 * Note that these two kinds of failures are handled differently,
4070 * as an unexpected record is silently skipped but an invalid
4071 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004072 */
4073#if defined(MBEDTLS_SSL_PROTO_DTLS)
4074 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4075 {
4076 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4077
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004078 /* Check epoch (and sequence number) with DTLS */
4079 if( rec_epoch != ssl->in_epoch )
4080 {
4081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4082 "expected %d, received %d",
4083 ssl->in_epoch, rec_epoch ) );
4084
4085#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4086 /*
4087 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4088 * access the first byte of record content (handshake type), as we
4089 * have an active transform (possibly iv_len != 0), so use the
4090 * fact that the record header len is 13 instead.
4091 */
4092 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4093 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4094 rec_epoch == 0 &&
4095 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4096 ssl->in_left > 13 &&
4097 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4098 {
4099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4100 "from the same port" ) );
4101 return( ssl_handle_possible_reconnect( ssl ) );
4102 }
4103 else
4104#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004105 {
4106 /* Consider buffering the record. */
4107 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4108 {
4109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4110 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4111 }
4112
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004113 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004114 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004115 }
4116
4117#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4118 /* Replay detection only works for the current epoch */
4119 if( rec_epoch == ssl->in_epoch &&
4120 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4121 {
4122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4123 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4124 }
4125#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004126
Hanno Becker52c6dc62017-05-26 16:07:36 +01004127 /* Drop unexpected ApplicationData records,
4128 * except at the beginning of renegotiations */
4129 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4130 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4131#if defined(MBEDTLS_SSL_RENEGOTIATION)
4132 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4133 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4134#endif
4135 )
4136 {
4137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4138 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4139 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004140 }
4141#endif /* MBEDTLS_SSL_PROTO_DTLS */
4142
Hanno Becker52c6dc62017-05-26 16:07:36 +01004143
4144 /* Check length against bounds of the current transform and version */
4145 if( ssl->transform_in == NULL )
4146 {
4147 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004148 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004149 {
4150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4151 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4152 }
4153 }
4154 else
4155 {
4156 if( ssl->in_msglen < ssl->transform_in->minlen )
4157 {
4158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4160 }
4161
4162#if defined(MBEDTLS_SSL_PROTO_SSL3)
4163 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004164 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004165 {
4166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4167 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4168 }
4169#endif
4170#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4171 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4172 /*
4173 * TLS encrypted messages can have up to 256 bytes of padding
4174 */
4175 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4176 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004177 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004178 {
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4180 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4181 }
4182#endif
4183 }
4184
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004185 return( 0 );
4186}
Paul Bakker5121ce52009-01-03 21:22:43 +00004187
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004188/*
4189 * If applicable, decrypt (and decompress) record content
4190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004192{
4193 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4196 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4199 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 ret = mbedtls_ssl_hw_record_read( ssl );
4204 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4207 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004208 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004209
4210 if( ret == 0 )
4211 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004212 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004214 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 {
4216 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004219 return( ret );
4220 }
4221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 ssl->in_msg, ssl->in_msglen );
4224
Angus Grattond8213d02016-05-25 20:56:48 +10004225 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4228 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 }
4230 }
4231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004233 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004235 {
4236 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004239 return( ret );
4240 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004241 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004248 }
4249#endif
4250
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004251 return( 0 );
4252}
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004255
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004256/*
4257 * Read a record.
4258 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004259 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4260 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4261 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004262 */
Hanno Becker1097b342018-08-15 14:09:41 +01004263
4264/* Helper functions for mbedtls_ssl_read_record(). */
4265static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004266static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4267static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004268
Hanno Becker40f50842018-08-15 14:48:01 +01004269#if defined(MBEDTLS_SSL_PROTO_DTLS)
4270static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004271static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004272static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004273static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004274static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4275#endif /* MBEDTLS_SSL_PROTO_DTLS */
4276
Hanno Becker327c93b2018-08-15 13:56:18 +01004277int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004278 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004279{
4280 int ret;
4281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004284 if( ssl->keep_current_message == 0 )
4285 {
4286 do {
Simon Butcher99000142016-10-13 17:21:01 +01004287
Hanno Becker26994592018-08-15 14:14:59 +01004288 ret = ssl_consume_current_message( ssl );
4289 if( ret != 0 )
4290 return( ret );
4291
Hanno Beckere74d5562018-08-15 14:26:08 +01004292 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004293 {
Hanno Becker40f50842018-08-15 14:48:01 +01004294#if defined(MBEDTLS_SSL_PROTO_DTLS)
4295 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004296
Hanno Becker40f50842018-08-15 14:48:01 +01004297 /* We only check for buffered messages if the
4298 * current datagram is fully consumed. */
4299 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4300 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004301 {
Hanno Becker40f50842018-08-15 14:48:01 +01004302 if( ssl_load_buffered_message( ssl ) == 0 )
4303 have_buffered = 1;
4304 }
4305
4306 if( have_buffered == 0 )
4307#endif /* MBEDTLS_SSL_PROTO_DTLS */
4308 {
4309 ret = ssl_get_next_record( ssl );
4310 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4311 continue;
4312
4313 if( ret != 0 )
4314 {
4315 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4316 return( ret );
4317 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004318 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004319 }
4320
4321 ret = mbedtls_ssl_handle_message_type( ssl );
4322
Hanno Becker40f50842018-08-15 14:48:01 +01004323#if defined(MBEDTLS_SSL_PROTO_DTLS)
4324 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4325 {
4326 /* Buffer future message */
4327 ret = ssl_buffer_message( ssl );
4328 if( ret != 0 )
4329 return( ret );
4330
4331 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4332 }
4333#endif /* MBEDTLS_SSL_PROTO_DTLS */
4334
Hanno Becker90333da2017-10-10 11:27:13 +01004335 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4336 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004337
4338 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004339 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004340 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004341 return( ret );
4342 }
4343
Hanno Becker327c93b2018-08-15 13:56:18 +01004344 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004345 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004346 {
4347 mbedtls_ssl_update_handshake_status( ssl );
4348 }
Simon Butcher99000142016-10-13 17:21:01 +01004349 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004350 else
Simon Butcher99000142016-10-13 17:21:01 +01004351 {
Hanno Becker02f59072018-08-15 14:00:24 +01004352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004353 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004354 }
4355
4356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4357
4358 return( 0 );
4359}
4360
Hanno Becker40f50842018-08-15 14:48:01 +01004361#if defined(MBEDTLS_SSL_PROTO_DTLS)
4362static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4363{
4364 if( ssl->in_left > ssl->next_record_offset )
4365 return( 1 );
4366
4367 return( 0 );
4368}
4369
4370static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4371{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004372 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004373 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004374 int ret = 0;
4375
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004376 if( hs == NULL )
4377 return( -1 );
4378
Hanno Beckere00ae372018-08-20 09:39:42 +01004379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4380
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004381 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4382 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4383 {
4384 /* Check if we have seen a ChangeCipherSpec before.
4385 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004386 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004387 {
4388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4389 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004390 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004391 }
4392
4393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4394 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4395 ssl->in_msglen = 1;
4396 ssl->in_msg[0] = 1;
4397
4398 /* As long as they are equal, the exact value doesn't matter. */
4399 ssl->in_left = 0;
4400 ssl->next_record_offset = 0;
4401
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004402 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004403 goto exit;
4404 }
Hanno Becker37f95322018-08-16 13:55:32 +01004405
4406 /* Debug only */
4407 {
4408 unsigned offset;
4409 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4410 {
4411 hs_buf = &hs->buffering.hs[offset];
4412 if( hs_buf->is_valid == 1 )
4413 {
4414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4415 hs->in_msg_seq + offset,
4416 hs_buf->is_complete ? "fully" : "partitially" ) );
4417 }
4418 }
4419 }
4420
4421 /* Check if we have buffered and/or fully reassembled the
4422 * next handshake message. */
4423 hs_buf = &hs->buffering.hs[0];
4424 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4425 {
4426 /* Synthesize a record containing the buffered HS message. */
4427 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4428 ( hs_buf->data[2] << 8 ) |
4429 hs_buf->data[3];
4430
4431 /* Double-check that we haven't accidentally buffered
4432 * a message that doesn't fit into the input buffer. */
4433 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4434 {
4435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4437 }
4438
4439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4440 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4441 hs_buf->data, msg_len + 12 );
4442
4443 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4444 ssl->in_hslen = msg_len + 12;
4445 ssl->in_msglen = msg_len + 12;
4446 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4447
4448 ret = 0;
4449 goto exit;
4450 }
4451 else
4452 {
4453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4454 hs->in_msg_seq ) );
4455 }
4456
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004457 ret = -1;
4458
4459exit:
4460
4461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4462 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004463}
4464
4465static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4466{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004467 int ret = 0;
4468 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4469
4470 if( hs == NULL )
4471 return( 0 );
4472
4473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4474
4475 switch( ssl->in_msgtype )
4476 {
4477 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004479
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004480 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004481 break;
4482
4483 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004484 {
4485 unsigned recv_msg_seq_offset;
4486 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4487 mbedtls_ssl_hs_buffer *hs_buf;
4488 size_t msg_len = ssl->in_hslen - 12;
4489
4490 /* We should never receive an old handshake
4491 * message - double-check nonetheless. */
4492 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4493 {
4494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4496 }
4497
4498 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4499 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4500 {
4501 /* Silently ignore -- message too far in the future */
4502 MBEDTLS_SSL_DEBUG_MSG( 2,
4503 ( "Ignore future HS message with sequence number %u, "
4504 "buffering window %u - %u",
4505 recv_msg_seq, ssl->handshake->in_msg_seq,
4506 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4507
4508 goto exit;
4509 }
4510
4511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4512 recv_msg_seq, recv_msg_seq_offset ) );
4513
4514 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4515
4516 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004517 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004518 {
4519 hs_buf->is_fragmented =
4520 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4521
4522 /* We copy the message back into the input buffer
4523 * after reassembly, so check that it's not too large.
4524 * This is an implementation-specific limitation
4525 * and not one from the standard, hence it is not
4526 * checked in ssl_check_hs_header(). */
4527 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
4528 {
4529 /* Ignore message */
4530 goto exit;
4531 }
4532
4533 ret = ssl_prepare_reassembly_buffer( ssl, msg_len,
4534 hs_buf->is_fragmented,
4535 &hs_buf->data );
4536 if( ret == MBEDTLS_ERR_SSL_ALLOC_FAILED &&
4537 recv_msg_seq_offset > 0 )
4538 {
4539 /* If we run out of RAM trying to buffer a *future*
4540 * message, simply ignore instead of failing. */
4541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Not enough RAM available to buffer future message - ignore" ) );
4542 goto exit;
4543 }
4544 else if( ret != 0 )
4545 return( ret );
4546
4547 /* Prepare final header: copy msg_type, length and message_seq,
4548 * then add standardised fragment_offset and fragment_length */
4549 memcpy( hs_buf->data, ssl->in_msg, 6 );
4550 memset( hs_buf->data + 6, 0, 3 );
4551 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4552
4553 hs_buf->is_valid = 1;
4554 }
4555 else
4556 {
4557 /* Make sure msg_type and length are consistent */
4558 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4559 {
4560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4561 /* Ignore */
4562 goto exit;
4563 }
4564 }
4565
Hanno Becker4422bbb2018-08-20 09:40:19 +01004566 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004567 {
4568 size_t frag_len, frag_off;
4569 unsigned char * const msg = hs_buf->data + 12;
4570
4571 /*
4572 * Check and copy current fragment
4573 */
4574
4575 /* Validation of header fields already done in
4576 * mbedtls_ssl_prepare_handshake_record(). */
4577 frag_off = ssl_get_hs_frag_off( ssl );
4578 frag_len = ssl_get_hs_frag_len( ssl );
4579
4580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4581 frag_off, frag_len ) );
4582 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4583
4584 if( hs_buf->is_fragmented )
4585 {
4586 unsigned char * const bitmask = msg + msg_len;
4587 ssl_bitmask_set( bitmask, frag_off, frag_len );
4588 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4589 msg_len ) == 0 );
4590 }
4591 else
4592 {
4593 hs_buf->is_complete = 1;
4594 }
4595
4596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4597 hs_buf->is_complete ? "" : "not yet " ) );
4598 }
4599
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004600 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004601 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004602
4603 default:
4604 break;
4605 }
4606
4607exit:
4608
4609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4610 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004611}
4612#endif /* MBEDTLS_SSL_PROTO_DTLS */
4613
Hanno Becker1097b342018-08-15 14:09:41 +01004614static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004615{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004616 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004617 * Consume last content-layer message and potentially
4618 * update in_msglen which keeps track of the contents'
4619 * consumption state.
4620 *
4621 * (1) Handshake messages:
4622 * Remove last handshake message, move content
4623 * and adapt in_msglen.
4624 *
4625 * (2) Alert messages:
4626 * Consume whole record content, in_msglen = 0.
4627 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004628 * (3) Change cipher spec:
4629 * Consume whole record content, in_msglen = 0.
4630 *
4631 * (4) Application data:
4632 * Don't do anything - the record layer provides
4633 * the application data as a stream transport
4634 * and consumes through mbedtls_ssl_read only.
4635 *
4636 */
4637
4638 /* Case (1): Handshake messages */
4639 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004640 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004641 /* Hard assertion to be sure that no application data
4642 * is in flight, as corrupting ssl->in_msglen during
4643 * ssl->in_offt != NULL is fatal. */
4644 if( ssl->in_offt != NULL )
4645 {
4646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4648 }
4649
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004650 /*
4651 * Get next Handshake message in the current record
4652 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004653
Hanno Becker4a810fb2017-05-24 16:27:30 +01004654 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004655 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004656 * current handshake content: If DTLS handshake
4657 * fragmentation is used, that's the fragment
4658 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004659 * size here is faulty and should be changed at
4660 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004661 * (2) While it doesn't seem to cause problems, one
4662 * has to be very careful not to assume that in_hslen
4663 * is always <= in_msglen in a sensible communication.
4664 * Again, it's wrong for DTLS handshake fragmentation.
4665 * The following check is therefore mandatory, and
4666 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004667 * Additionally, ssl->in_hslen might be arbitrarily out of
4668 * bounds after handling a DTLS message with an unexpected
4669 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004670 */
4671 if( ssl->in_hslen < ssl->in_msglen )
4672 {
4673 ssl->in_msglen -= ssl->in_hslen;
4674 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4675 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004676
Hanno Becker4a810fb2017-05-24 16:27:30 +01004677 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4678 ssl->in_msg, ssl->in_msglen );
4679 }
4680 else
4681 {
4682 ssl->in_msglen = 0;
4683 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004684
Hanno Becker4a810fb2017-05-24 16:27:30 +01004685 ssl->in_hslen = 0;
4686 }
4687 /* Case (4): Application data */
4688 else if( ssl->in_offt != NULL )
4689 {
4690 return( 0 );
4691 }
4692 /* Everything else (CCS & Alerts) */
4693 else
4694 {
4695 ssl->in_msglen = 0;
4696 }
4697
Hanno Becker1097b342018-08-15 14:09:41 +01004698 return( 0 );
4699}
4700
Hanno Beckere74d5562018-08-15 14:26:08 +01004701static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4702{
4703 if( ssl->in_msglen > 0 )
4704 return( 1 );
4705
4706 return( 0 );
4707}
4708
Hanno Becker5f066e72018-08-16 14:56:31 +01004709#if defined(MBEDTLS_SSL_PROTO_DTLS)
4710
4711static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4712{
4713 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4714 if( hs == NULL )
4715 return;
4716
4717 mbedtls_free( hs->buffering.future_record.data );
4718 hs->buffering.future_record.data = NULL;
4719}
4720
4721static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4722{
4723 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4724 unsigned char * rec;
4725 size_t rec_len;
4726 unsigned rec_epoch;
4727
4728 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4729 return( 0 );
4730
4731 if( hs == NULL )
4732 return( 0 );
4733
Hanno Becker5f066e72018-08-16 14:56:31 +01004734 rec = hs->buffering.future_record.data;
4735 rec_len = hs->buffering.future_record.len;
4736 rec_epoch = hs->buffering.future_record.epoch;
4737
4738 if( rec == NULL )
4739 return( 0 );
4740
Hanno Becker4cb782d2018-08-20 11:19:05 +01004741 /* Only consider loading future records if the
4742 * input buffer is empty. */
4743 if( ssl_another_record_in_datagram( ssl ) == 1 )
4744 return( 0 );
4745
Hanno Becker5f066e72018-08-16 14:56:31 +01004746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4747
4748 if( rec_epoch != ssl->in_epoch )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4751 goto exit;
4752 }
4753
4754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4755
4756 /* Double-check that the record is not too large */
4757 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4758 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4759 {
4760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4762 }
4763
4764 memcpy( ssl->in_hdr, rec, rec_len );
4765 ssl->in_left = rec_len;
4766 ssl->next_record_offset = 0;
4767
4768 ssl_free_buffered_record( ssl );
4769
4770exit:
4771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4772 return( 0 );
4773}
4774
4775static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4776{
4777 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4778 size_t const rec_hdr_len = 13;
4779
4780 /* Don't buffer future records outside handshakes. */
4781 if( hs == NULL )
4782 return( 0 );
4783
4784 /* Only buffer handshake records (we are only interested
4785 * in Finished messages). */
4786 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4787 return( 0 );
4788
4789 /* Don't buffer more than one future epoch record. */
4790 if( hs->buffering.future_record.data != NULL )
4791 return( 0 );
4792
4793 /* Buffer record */
4794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4795 ssl->in_epoch + 1 ) );
4796 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4797 rec_hdr_len + ssl->in_msglen );
4798
4799 /* ssl_parse_record_header() only considers records
4800 * of the next epoch as candidates for buffering. */
4801 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
4802 hs->buffering.future_record.len = rec_hdr_len + ssl->in_msglen;
4803
4804 hs->buffering.future_record.data =
4805 mbedtls_calloc( 1, hs->buffering.future_record.len );
4806 if( hs->buffering.future_record.data == NULL )
4807 {
4808 /* If we run out of RAM trying to buffer a
4809 * record from the next epoch, just ignore. */
4810 return( 0 );
4811 }
4812
4813 memcpy( hs->buffering.future_record.data,
4814 ssl->in_hdr, rec_hdr_len + ssl->in_msglen );
4815
4816 return( 0 );
4817}
4818
4819#endif /* MBEDTLS_SSL_PROTO_DTLS */
4820
Hanno Beckere74d5562018-08-15 14:26:08 +01004821static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004822{
4823 int ret;
4824
Hanno Becker5f066e72018-08-16 14:56:31 +01004825#if defined(MBEDTLS_SSL_PROTO_DTLS)
4826 /* We might have buffered a future record; if so,
4827 * and if the epoch matches now, load it.
4828 * On success, this call will set ssl->in_left to
4829 * the length of the buffered record, so that
4830 * the calls to ssl_fetch_input() below will
4831 * essentially be no-ops. */
4832 ret = ssl_load_buffered_record( ssl );
4833 if( ret != 0 )
4834 return( ret );
4835#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004840 return( ret );
4841 }
4842
4843 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004846 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4847 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004848 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004849 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4850 {
4851 ret = ssl_buffer_future_record( ssl );
4852 if( ret != 0 )
4853 return( ret );
4854
4855 /* Fall through to handling of unexpected records */
4856 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4857 }
4858
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004859 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4860 {
4861 /* Skip unexpected record (but not whole datagram) */
4862 ssl->next_record_offset = ssl->in_msglen
4863 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004864
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4866 "(header)" ) );
4867 }
4868 else
4869 {
4870 /* Skip invalid record and the rest of the datagram */
4871 ssl->next_record_offset = 0;
4872 ssl->in_left = 0;
4873
4874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4875 "(header)" ) );
4876 }
4877
4878 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004879 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004880 }
4881#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004882 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004883 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004884
4885 /*
4886 * Read and optionally decrypt the message contents
4887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4889 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004892 return( ret );
4893 }
4894
4895 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004900 if( ssl->next_record_offset < ssl->in_left )
4901 {
4902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4903 }
4904 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004905 else
4906#endif
4907 ssl->in_left = 0;
4908
4909 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004912 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004913 {
4914 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4916 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004917 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004918 /* Except when waiting for Finished as a bad mac here
4919 * probably means something went wrong in the handshake
4920 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4921 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4922 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4923 {
4924#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4925 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4926 {
4927 mbedtls_ssl_send_alert_message( ssl,
4928 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4929 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4930 }
4931#endif
4932 return( ret );
4933 }
4934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004936 if( ssl->conf->badmac_limit != 0 &&
4937 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4940 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004941 }
4942#endif
4943
Hanno Becker4a810fb2017-05-24 16:27:30 +01004944 /* As above, invalid records cause
4945 * dismissal of the whole datagram. */
4946
4947 ssl->next_record_offset = 0;
4948 ssl->in_left = 0;
4949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004951 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004952 }
4953
4954 return( ret );
4955 }
4956 else
4957#endif
4958 {
4959 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4961 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 mbedtls_ssl_send_alert_message( ssl,
4964 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4965 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004966 }
4967#endif
4968 return( ret );
4969 }
4970 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004971
Simon Butcher99000142016-10-13 17:21:01 +01004972 return( 0 );
4973}
4974
4975int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4976{
4977 int ret;
4978
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004979 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004980 * Handle particular types of records
4981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004983 {
Simon Butcher99000142016-10-13 17:21:01 +01004984 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4985 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004986 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004988 }
4989
Hanno Beckere678eaa2018-08-21 14:57:46 +01004990 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004991 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004992 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004993 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4995 ssl->in_msglen ) );
4996 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004997 }
4998
Hanno Beckere678eaa2018-08-21 14:57:46 +01004999 if( ssl->in_msg[0] != 1 )
5000 {
5001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5002 ssl->in_msg[0] ) );
5003 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5004 }
5005
5006#if defined(MBEDTLS_SSL_PROTO_DTLS)
5007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5008 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5009 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5010 {
5011 if( ssl->handshake == NULL )
5012 {
5013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5014 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5015 }
5016
5017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5018 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5019 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005020#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005021 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005024 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005025 if( ssl->in_msglen != 2 )
5026 {
5027 /* Note: Standard allows for more than one 2 byte alert
5028 to be packed in a single message, but Mbed TLS doesn't
5029 currently support this. */
5030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5031 ssl->in_msglen ) );
5032 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5033 }
5034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005036 ssl->in_msg[0], ssl->in_msg[1] ) );
5037
5038 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005039 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005044 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005046 }
5047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5049 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5052 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005053 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005054
5055#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5056 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5057 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5058 {
Hanno Becker90333da2017-10-10 11:27:13 +01005059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005060 /* Will be handled when trying to parse ServerHello */
5061 return( 0 );
5062 }
5063#endif
5064
5065#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5066 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5067 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5068 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5069 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5070 {
5071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5072 /* Will be handled in mbedtls_ssl_parse_certificate() */
5073 return( 0 );
5074 }
5075#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5076
5077 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005078 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005079 }
5080
Hanno Beckerc76c6192017-06-06 10:03:17 +01005081#if defined(MBEDTLS_SSL_PROTO_DTLS)
5082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5083 ssl->handshake != NULL &&
5084 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5085 {
5086 ssl_handshake_wrapup_free_hs_transform( ssl );
5087 }
5088#endif
5089
Paul Bakker5121ce52009-01-03 21:22:43 +00005090 return( 0 );
5091}
5092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005094{
5095 int ret;
5096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5098 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5099 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005100 {
5101 return( ret );
5102 }
5103
5104 return( 0 );
5105}
5106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005108 unsigned char level,
5109 unsigned char message )
5110{
5111 int ret;
5112
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005113 if( ssl == NULL || ssl->conf == NULL )
5114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005120 ssl->out_msglen = 2;
5121 ssl->out_msg[0] = level;
5122 ssl->out_msg[1] = message;
5123
Hanno Becker67bc7c32018-08-06 11:33:50 +01005124 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005127 return( ret );
5128 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005130
5131 return( 0 );
5132}
5133
Paul Bakker5121ce52009-01-03 21:22:43 +00005134/*
5135 * Handshake functions
5136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5138 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5139 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5140 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5141 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5142 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5143 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005144/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5152 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005153 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5154 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005157 ssl->state++;
5158 return( 0 );
5159 }
5160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005163}
5164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5172 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005173 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5174 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005177 ssl->state++;
5178 return( 0 );
5179 }
5180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005183}
Gilles Peskinef9828522017-05-03 12:28:43 +02005184
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005185#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005186/* Some certificate support -> implement write and parse */
5187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005191 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 const mbedtls_x509_crt *crt;
5193 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5198 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005199 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5200 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005203 ssl->state++;
5204 return( 0 );
5205 }
5206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005208 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005209 {
5210 if( ssl->client_auth == 0 )
5211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 ssl->state++;
5214 return( 0 );
5215 }
5216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005218 /*
5219 * If using SSLv3 and got no cert, send an Alert message
5220 * (otherwise an empty Certificate message will be sent).
5221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5223 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005224 {
5225 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5227 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5228 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 goto write_msg;
5232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005234 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235#endif /* MBEDTLS_SSL_CLI_C */
5236#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005237 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5242 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 }
5244 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005248
5249 /*
5250 * 0 . 0 handshake type
5251 * 1 . 3 handshake length
5252 * 4 . 6 length of all certs
5253 * 7 . 9 length of cert. 1
5254 * 10 . n-1 peer certificate
5255 * n . n+2 length of cert. 2
5256 * n+3 . ... upper level cert, etc.
5257 */
5258 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005260
Paul Bakker29087132010-03-21 21:03:34 +00005261 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005262 {
5263 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005264 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005267 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 }
5270
5271 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5272 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5273 ssl->out_msg[i + 2] = (unsigned char)( n );
5274
5275 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5276 i += n; crt = crt->next;
5277 }
5278
5279 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5280 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5281 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5282
5283 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5285 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005286
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005287#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005288write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
5291 ssl->state++;
5292
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005293 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005294 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005296 return( ret );
5297 }
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Paul Bakkered27a042013-04-18 22:46:23 +02005301 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005302}
5303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005307 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005309 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005310 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5315 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005316 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5317 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005320 ssl->state++;
5321 return( 0 );
5322 }
5323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005325 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005326 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5327 {
5328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5329 ssl->state++;
5330 return( 0 );
5331 }
5332
5333#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5334 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5335 authmode = ssl->handshake->sni_authmode;
5336#endif
5337
5338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5339 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005340 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005341 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 ssl->state++;
5344 return( 0 );
5345 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005347
Hanno Becker327c93b2018-08-15 13:56:18 +01005348 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005349 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005350 /* mbedtls_ssl_read_record may have sent an alert already. We
5351 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005353 return( ret );
5354 }
5355
5356 ssl->state++;
5357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358#if defined(MBEDTLS_SSL_SRV_C)
5359#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005360 /*
5361 * Check if the client sent an empty certificate
5362 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005365 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005366 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5368 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5369 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005372
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005373 /* The client was asked for a certificate but didn't send
5374 one. The client should know what's going on, so we
5375 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005376 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005377 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005378 return( 0 );
5379 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 }
5382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5386 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005387 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5391 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5392 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5393 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005397 /* The client was asked for a certificate but didn't send
5398 one. The client should know what's going on, so we
5399 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005400 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005401 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005402 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005403 else
5404 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405 }
5406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5408 MBEDTLS_SSL_PROTO_TLS1_2 */
5409#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005414 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5415 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005417 }
5418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5420 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005423 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5424 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005429
Paul Bakker5121ce52009-01-03 21:22:43 +00005430 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005432 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005433 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005434
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005435 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5440 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 }
5443
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005444 /* In case we tried to reuse a session but it failed */
5445 if( ssl->session_negotiate->peer_cert != NULL )
5446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5448 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005449 }
5450
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005451 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005456 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5457 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005458 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005459 }
5460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005462
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005463 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005464
5465 while( i < ssl->in_hslen )
5466 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005467 if ( i + 3 > ssl->in_hslen ) {
5468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5469 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5470 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5471 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 if( ssl->in_msg[i] != 0 )
5474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005476 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5477 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 }
5480
5481 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5482 | (unsigned int) ssl->in_msg[i + 2];
5483 i += 3;
5484
5485 if( n < 128 || i + n > ssl->in_hslen )
5486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005488 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5489 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 }
5492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005494 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005495 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005497 case 0: /*ok*/
5498 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5499 /* Ignore certificate with an unknown algorithm: maybe a
5500 prior certificate was already trusted. */
5501 break;
5502
5503 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5504 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5505 goto crt_parse_der_failed;
5506
5507 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5508 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5509 goto crt_parse_der_failed;
5510
5511 default:
5512 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5513 crt_parse_der_failed:
5514 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 return( ret );
5517 }
5518
5519 i += n;
5520 }
5521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005523
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005524 /*
5525 * On client, make sure the server cert doesn't change during renego to
5526 * avoid "triple handshake" attack: https://secure-resumption.com/
5527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005529 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005531 {
5532 if( ssl->session->peer_cert == NULL )
5533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005535 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5536 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005538 }
5539
5540 if( ssl->session->peer_cert->raw.len !=
5541 ssl->session_negotiate->peer_cert->raw.len ||
5542 memcmp( ssl->session->peer_cert->raw.p,
5543 ssl->session_negotiate->peer_cert->raw.p,
5544 ssl->session->peer_cert->raw.len ) != 0 )
5545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005547 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5548 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005550 }
5551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005553
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005554 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005556 mbedtls_x509_crt *ca_chain;
5557 mbedtls_x509_crl *ca_crl;
5558
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005559#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005560 if( ssl->handshake->sni_ca_chain != NULL )
5561 {
5562 ca_chain = ssl->handshake->sni_ca_chain;
5563 ca_crl = ssl->handshake->sni_ca_crl;
5564 }
5565 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005566#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005567 {
5568 ca_chain = ssl->conf->ca_chain;
5569 ca_crl = ssl->conf->ca_crl;
5570 }
5571
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005572 /*
5573 * Main check: verify certificate
5574 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005575 ret = mbedtls_x509_crt_verify_with_profile(
5576 ssl->session_negotiate->peer_cert,
5577 ca_chain, ca_crl,
5578 ssl->conf->cert_profile,
5579 ssl->hostname,
5580 &ssl->session_negotiate->verify_result,
5581 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
5583 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005586 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005587
5588 /*
5589 * Secondary checks: always done, but change 'ret' only if it was 0
5590 */
5591
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005592#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005595
5596 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005598 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005599 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005600 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005603 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005605 }
5606 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005607#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005610 ciphersuite_info,
5611 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005612 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005615 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005617 }
5618
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005619 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5620 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5621 * with details encoded in the verification flags. All other kinds
5622 * of error codes, including those from the user provided f_vrfy
5623 * functions, are treated as fatal and lead to a failure of
5624 * ssl_parse_certificate even if verification was optional. */
5625 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5626 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5627 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5628 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005629 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005630 }
5631
5632 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5633 {
5634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5635 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5636 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005637
5638 if( ret != 0 )
5639 {
5640 /* The certificate may have been rejected for several reasons.
5641 Pick one and send the corresponding alert. Which alert to send
5642 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005643 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5644 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5645 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5646 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5647 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5648 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5649 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5650 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5651 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5652 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5653 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5654 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5655 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5656 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5657 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5658 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5659 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5660 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5661 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5662 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005663 else
5664 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005665 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5666 alert );
5667 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005668
Hanno Beckere6706e62017-05-15 16:05:15 +01005669#if defined(MBEDTLS_DEBUG_C)
5670 if( ssl->session_negotiate->verify_result != 0 )
5671 {
5672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5673 ssl->session_negotiate->verify_result ) );
5674 }
5675 else
5676 {
5677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5678 }
5679#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005680 }
5681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683
5684 return( ret );
5685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5687 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5688 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5689 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5690 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5691 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5692 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005695{
5696 int ret;
5697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 ssl->out_msglen = 1;
5702 ssl->out_msg[0] = 1;
5703
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 ssl->state++;
5705
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005706 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 return( ret );
5710 }
5711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005713
5714 return( 0 );
5715}
5716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005718{
5719 int ret;
5720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005722
Hanno Becker327c93b2018-08-15 13:56:18 +01005723 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005726 return( ret );
5727 }
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5733 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 }
5736
Hanno Beckere678eaa2018-08-21 14:57:46 +01005737 /* CCS records are only accepted if they have length 1 and content '1',
5738 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005739
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005740 /*
5741 * Switch to our negotiated transform and session parameters for inbound
5742 * data.
5743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005745 ssl->transform_in = ssl->transform_negotiate;
5746 ssl->session_in = ssl->session_negotiate;
5747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005752 ssl_dtls_replay_reset( ssl );
5753#endif
5754
5755 /* Increment epoch */
5756 if( ++ssl->in_epoch == 0 )
5757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005759 /* This is highly unlikely to happen for legitimate reasons, so
5760 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005762 }
5763 }
5764 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005766 memset( ssl->in_ctr, 0, 8 );
5767
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005768 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5771 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005776 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5777 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005779 }
5780 }
5781#endif
5782
Paul Bakker5121ce52009-01-03 21:22:43 +00005783 ssl->state++;
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005786
5787 return( 0 );
5788}
5789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5791 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005792{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005793 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5796 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5797 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005798 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005799 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5802#if defined(MBEDTLS_SHA512_C)
5803 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005804 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5805 else
5806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807#if defined(MBEDTLS_SHA256_C)
5808 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005809 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005810 else
5811#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005815 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005816 }
Paul Bakker380da532012-04-18 16:10:25 +00005817}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5822 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005823 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5824 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005825#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5827#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005828 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005831 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005832#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005834}
5835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005837 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5840 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005841 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5842 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005843#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5845#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005846 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005849 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005852}
5853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5855 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5856static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005857 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005858{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005859 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5860 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005861}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005862#endif
Paul Bakker380da532012-04-18 16:10:25 +00005863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5865#if defined(MBEDTLS_SHA256_C)
5866static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005867 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005868{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005869 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005870}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005871#endif
Paul Bakker380da532012-04-18 16:10:25 +00005872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873#if defined(MBEDTLS_SHA512_C)
5874static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005875 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005876{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005877 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005878}
Paul Bakker769075d2012-11-24 11:26:46 +01005879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005883static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005885{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005886 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005887 mbedtls_md5_context md5;
5888 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005889
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 unsigned char padbuf[48];
5891 unsigned char md5sum[16];
5892 unsigned char sha1sum[20];
5893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005895 if( !session )
5896 session = ssl->session;
5897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005899
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005900 mbedtls_md5_init( &md5 );
5901 mbedtls_sha1_init( &sha1 );
5902
5903 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5904 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005905
5906 /*
5907 * SSLv3:
5908 * hash =
5909 * MD5( master + pad2 +
5910 * MD5( handshake + sender + master + pad1 ) )
5911 * + SHA1( master + pad2 +
5912 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 */
5914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005916 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5917 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005918#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005921 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5922 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005923#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005926 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005927
Paul Bakker1ef83d62012-04-11 12:09:53 +00005928 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005929
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005930 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5931 mbedtls_md5_update_ret( &md5, session->master, 48 );
5932 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5933 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005934
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005935 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5936 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5937 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5938 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939
Paul Bakker1ef83d62012-04-11 12:09:53 +00005940 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005941
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005942 mbedtls_md5_starts_ret( &md5 );
5943 mbedtls_md5_update_ret( &md5, session->master, 48 );
5944 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5945 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5946 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005947
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005948 mbedtls_sha1_starts_ret( &sha1 );
5949 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5950 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5951 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5952 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005955
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005956 mbedtls_md5_free( &md5 );
5957 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005958
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005959 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5960 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5961 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005968static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005970{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005971 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005972 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005973 mbedtls_md5_context md5;
5974 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005975 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005978 if( !session )
5979 session = ssl->session;
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005982
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005983 mbedtls_md5_init( &md5 );
5984 mbedtls_sha1_init( &sha1 );
5985
5986 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5987 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
Paul Bakker1ef83d62012-04-11 12:09:53 +00005989 /*
5990 * TLSv1:
5991 * hash = PRF( master, finished_label,
5992 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5993 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005996 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5997 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005998#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006001 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6002 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006003#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006006 ? "client finished"
6007 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006008
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006009 mbedtls_md5_finish_ret( &md5, padbuf );
6010 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006011
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006012 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006013 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006016
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006017 mbedtls_md5_free( &md5 );
6018 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006019
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006020 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006023}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6027#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006028static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006030{
6031 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006032 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006033 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006034 unsigned char padbuf[32];
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006037 if( !session )
6038 session = ssl->session;
6039
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006040 mbedtls_sha256_init( &sha256 );
6041
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006043
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006044 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006045
6046 /*
6047 * TLSv1.2:
6048 * hash = PRF( master, finished_label,
6049 * Hash( handshake ) )[0.11]
6050 */
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if !defined(MBEDTLS_SHA256_ALT)
6053 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006054 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006055#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006058 ? "client finished"
6059 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006060
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006061 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006062
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006063 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006064 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006067
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006068 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006069
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006070 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006077static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006079{
6080 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006081 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006082 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006083 unsigned char padbuf[48];
6084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006086 if( !session )
6087 session = ssl->session;
6088
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006089 mbedtls_sha512_init( &sha512 );
6090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006092
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006093 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006094
6095 /*
6096 * TLSv1.2:
6097 * hash = PRF( master, finished_label,
6098 * Hash( handshake ) )[0.11]
6099 */
6100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006102 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6103 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006104#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006107 ? "client finished"
6108 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006109
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006110 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006111
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006112 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006113 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006116
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006117 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006118
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006119 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123#endif /* MBEDTLS_SHA512_C */
6124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006129
6130 /*
6131 * Free our handshake params
6132 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006133 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006135 ssl->handshake = NULL;
6136
6137 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006138 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006139 */
6140 if( ssl->transform )
6141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 mbedtls_ssl_transform_free( ssl->transform );
6143 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006144 }
6145 ssl->transform = ssl->transform_negotiate;
6146 ssl->transform_negotiate = NULL;
6147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006149}
6150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006152{
6153 int resume = ssl->handshake->resume;
6154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#if defined(MBEDTLS_SSL_RENEGOTIATION)
6158 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006161 ssl->renego_records_seen = 0;
6162 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006163#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006164
6165 /*
6166 * Free the previous session and switch in the current one
6167 */
Paul Bakker0a597072012-09-25 21:55:46 +00006168 if( ssl->session )
6169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006171 /* RFC 7366 3.1: keep the EtM state */
6172 ssl->session_negotiate->encrypt_then_mac =
6173 ssl->session->encrypt_then_mac;
6174#endif
6175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 mbedtls_ssl_session_free( ssl->session );
6177 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006178 }
6179 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006180 ssl->session_negotiate = NULL;
6181
Paul Bakker0a597072012-09-25 21:55:46 +00006182 /*
6183 * Add cache entry
6184 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006185 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006186 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006187 resume == 0 )
6188 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006189 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006191 }
Paul Bakker0a597072012-09-25 21:55:46 +00006192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006194 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006195 ssl->handshake->flight != NULL )
6196 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006197 /* Cancel handshake timer */
6198 ssl_set_timer( ssl, 0 );
6199
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006200 /* Keep last flight around in case we need to resend it:
6201 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006203 }
6204 else
6205#endif
6206 ssl_handshake_wrapup_free_hs_transform( ssl );
6207
Paul Bakker48916f92012-09-16 19:57:18 +00006208 ssl->state++;
6209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006211}
6212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006214{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006215 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006218
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006219 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006220
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006221 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006222
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006223 /*
6224 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6225 * may define some other value. Currently (early 2016), no defined
6226 * ciphersuite does this (and this is unlikely to change as activity has
6227 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006232 ssl->verify_data_len = hash_len;
6233 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006234#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006235
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6238 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006239
6240 /*
6241 * In case of session resuming, invert the client and server
6242 * ChangeCipherSpec messages order.
6243 */
Paul Bakker0a597072012-09-25 21:55:46 +00006244 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006247 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006251 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006253#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 }
6255 else
6256 ssl->state++;
6257
Paul Bakker48916f92012-09-16 19:57:18 +00006258 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006259 * Switch to our negotiated transform and session parameters for outbound
6260 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006266 {
6267 unsigned char i;
6268
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006269 /* Remember current epoch settings for resending */
6270 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006271 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006272
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006273 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006274 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006275
6276 /* Increment epoch */
6277 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006278 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006279 break;
6280
6281 /* The loop goes to its end iff the counter is wrapping */
6282 if( i == 0 )
6283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6285 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006286 }
6287 }
6288 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006290 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006292 ssl->transform_out = ssl->transform_negotiate;
6293 ssl->session_out = ssl->session_negotiate;
6294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6296 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6301 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006302 }
6303 }
6304#endif
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006307 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006309#endif
6310
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006311 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006312 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006314 return( ret );
6315 }
6316
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006317#if defined(MBEDTLS_SSL_PROTO_DTLS)
6318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6319 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6320 {
6321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6322 return( ret );
6323 }
6324#endif
6325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006327
6328 return( 0 );
6329}
6330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006332#define SSL_MAX_HASH_LEN 36
6333#else
6334#define SSL_MAX_HASH_LEN 12
6335#endif
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006338{
Paul Bakker23986e52011-04-24 08:57:21 +00006339 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006340 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006341 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006344
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006345 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346
Hanno Becker327c93b2018-08-15 13:56:18 +01006347 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 return( ret );
6351 }
6352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006356 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6357 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006359 }
6360
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006361 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#if defined(MBEDTLS_SSL_PROTO_SSL3)
6363 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006364 hash_len = 36;
6365 else
6366#endif
6367 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6370 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006373 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6374 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006376 }
6377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006379 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006382 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6383 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 }
6386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006388 ssl->verify_data_len = hash_len;
6389 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006390#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006391
Paul Bakker0a597072012-09-25 21:55:46 +00006392 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006395 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006399 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006401#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006402 }
6403 else
6404 ssl->state++;
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006409#endif
6410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
6413 return( 0 );
6414}
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6421 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6422 mbedtls_md5_init( &handshake->fin_md5 );
6423 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006424 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6425 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6428#if defined(MBEDTLS_SHA256_C)
6429 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006430 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432#if defined(MBEDTLS_SHA512_C)
6433 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006434 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006437
6438 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006439
6440#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6441 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6442 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6443#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_DHM_C)
6446 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006447#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448#if defined(MBEDTLS_ECDH_C)
6449 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006450#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006451#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006452 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006453#if defined(MBEDTLS_SSL_CLI_C)
6454 handshake->ecjpake_cache = NULL;
6455 handshake->ecjpake_cache_len = 0;
6456#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006457#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006458
6459#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6460 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6461#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006462}
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6469 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 mbedtls_md_init( &transform->md_ctx_enc );
6472 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006473}
6474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006478}
6479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006481{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006482 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006483 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006485 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006487 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006488 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006489
6490 /*
6491 * Either the pointers are now NULL or cleared properly and can be freed.
6492 * Now allocate missing structures.
6493 */
6494 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006495 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006496 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006497 }
Paul Bakker48916f92012-09-16 19:57:18 +00006498
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006499 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006500 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006501 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006502 }
Paul Bakker48916f92012-09-16 19:57:18 +00006503
Paul Bakker82788fb2014-10-20 13:59:19 +02006504 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006505 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006506 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006507 }
Paul Bakker48916f92012-09-16 19:57:18 +00006508
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006509 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006510 if( ssl->handshake == NULL ||
6511 ssl->transform_negotiate == NULL ||
6512 ssl->session_negotiate == NULL )
6513 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 mbedtls_free( ssl->handshake );
6517 mbedtls_free( ssl->transform_negotiate );
6518 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006519
6520 ssl->handshake = NULL;
6521 ssl->transform_negotiate = NULL;
6522 ssl->session_negotiate = NULL;
6523
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006524 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006525 }
6526
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006527 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006529 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006530 ssl_handshake_params_init( ssl->handshake );
6531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006533 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6534 {
6535 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006536
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6538 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6539 else
6540 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006541
6542 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006543 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006544#endif
6545
Paul Bakker48916f92012-09-16 19:57:18 +00006546 return( 0 );
6547}
6548
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006549#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006550/* Dummy cookie callbacks for defaults */
6551static int ssl_cookie_write_dummy( void *ctx,
6552 unsigned char **p, unsigned char *end,
6553 const unsigned char *cli_id, size_t cli_id_len )
6554{
6555 ((void) ctx);
6556 ((void) p);
6557 ((void) end);
6558 ((void) cli_id);
6559 ((void) cli_id_len);
6560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006562}
6563
6564static int ssl_cookie_check_dummy( void *ctx,
6565 const unsigned char *cookie, size_t cookie_len,
6566 const unsigned char *cli_id, size_t cli_id_len )
6567{
6568 ((void) ctx);
6569 ((void) cookie);
6570 ((void) cookie_len);
6571 ((void) cli_id);
6572 ((void) cli_id_len);
6573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006575}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006576#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006577
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006578/* Once ssl->out_hdr as the address of the beginning of the
6579 * next outgoing record is set, deduce the other pointers.
6580 *
6581 * Note: For TLS, we save the implicit record sequence number
6582 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6583 * and the caller has to make sure there's space for this.
6584 */
6585
6586static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6587 mbedtls_ssl_transform *transform )
6588{
6589#if defined(MBEDTLS_SSL_PROTO_DTLS)
6590 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6591 {
6592 ssl->out_ctr = ssl->out_hdr + 3;
6593 ssl->out_len = ssl->out_hdr + 11;
6594 ssl->out_iv = ssl->out_hdr + 13;
6595 }
6596 else
6597#endif
6598 {
6599 ssl->out_ctr = ssl->out_hdr - 8;
6600 ssl->out_len = ssl->out_hdr + 3;
6601 ssl->out_iv = ssl->out_hdr + 5;
6602 }
6603
6604 /* Adjust out_msg to make space for explicit IV, if used. */
6605 if( transform != NULL &&
6606 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6607 {
6608 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6609 }
6610 else
6611 ssl->out_msg = ssl->out_iv;
6612}
6613
6614/* Once ssl->in_hdr as the address of the beginning of the
6615 * next incoming record is set, deduce the other pointers.
6616 *
6617 * Note: For TLS, we save the implicit record sequence number
6618 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6619 * and the caller has to make sure there's space for this.
6620 */
6621
6622static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6623 mbedtls_ssl_transform *transform )
6624{
6625#if defined(MBEDTLS_SSL_PROTO_DTLS)
6626 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6627 {
6628 ssl->in_ctr = ssl->in_hdr + 3;
6629 ssl->in_len = ssl->in_hdr + 11;
6630 ssl->in_iv = ssl->in_hdr + 13;
6631 }
6632 else
6633#endif
6634 {
6635 ssl->in_ctr = ssl->in_hdr - 8;
6636 ssl->in_len = ssl->in_hdr + 3;
6637 ssl->in_iv = ssl->in_hdr + 5;
6638 }
6639
6640 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6641 if( transform != NULL &&
6642 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6643 {
6644 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6645 }
6646 else
6647 ssl->in_msg = ssl->in_iv;
6648}
6649
Paul Bakker5121ce52009-01-03 21:22:43 +00006650/*
6651 * Initialize an SSL context
6652 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006653void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6654{
6655 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6656}
6657
6658/*
6659 * Setup an SSL context
6660 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006661
6662static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6663{
6664 /* Set the incoming and outgoing record pointers. */
6665#if defined(MBEDTLS_SSL_PROTO_DTLS)
6666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6667 {
6668 ssl->out_hdr = ssl->out_buf;
6669 ssl->in_hdr = ssl->in_buf;
6670 }
6671 else
6672#endif /* MBEDTLS_SSL_PROTO_DTLS */
6673 {
6674 ssl->out_hdr = ssl->out_buf + 8;
6675 ssl->in_hdr = ssl->in_buf + 8;
6676 }
6677
6678 /* Derive other internal pointers. */
6679 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6680 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6681}
6682
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006683int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006684 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006685{
Paul Bakker48916f92012-09-16 19:57:18 +00006686 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006687
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006688 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006689
6690 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006691 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006692 */
Angus Grattond8213d02016-05-25 20:56:48 +10006693 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6694 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006695 {
Angus Grattond8213d02016-05-25 20:56:48 +10006696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6697 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6698 }
6699
6700 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6701 if( ssl->out_buf == NULL )
6702 {
6703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006705 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006706 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006707 }
6708
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006709 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006710
Paul Bakker48916f92012-09-16 19:57:18 +00006711 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006713
6714 return( 0 );
6715}
6716
6717/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006718 * Reset an initialized and used SSL context for re-use while retaining
6719 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006720 *
6721 * If partial is non-zero, keep data in the input buffer and client ID.
6722 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006723 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006724static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006725{
Paul Bakker48916f92012-09-16 19:57:18 +00006726 int ret;
6727
Hanno Becker7e772132018-08-10 12:38:21 +01006728#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6729 !defined(MBEDTLS_SSL_SRV_C)
6730 ((void) partial);
6731#endif
6732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006734
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006735 /* Cancel any possibly running timer */
6736 ssl_set_timer( ssl, 0 );
6737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#if defined(MBEDTLS_SSL_RENEGOTIATION)
6739 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006740 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006741
6742 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6744 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006747
Paul Bakker7eb013f2011-10-06 12:37:39 +00006748 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006749 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006750
6751 ssl->in_msgtype = 0;
6752 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006754 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006755 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006758 ssl_dtls_replay_reset( ssl );
6759#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006760
6761 ssl->in_hslen = 0;
6762 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006763
6764 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006765
6766 ssl->out_msgtype = 0;
6767 ssl->out_msglen = 0;
6768 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6770 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006771 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006772#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006773
Hanno Becker19859472018-08-06 09:40:20 +01006774 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6775
Paul Bakker48916f92012-09-16 19:57:18 +00006776 ssl->transform_in = NULL;
6777 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006778
Hanno Becker78640902018-08-13 16:35:15 +01006779 ssl->session_in = NULL;
6780 ssl->session_out = NULL;
6781
Angus Grattond8213d02016-05-25 20:56:48 +10006782 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006783
6784#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006785 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006786#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6787 {
6788 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006789 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006790 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6793 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6796 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6799 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006800 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006801 }
6802#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006803
Paul Bakker48916f92012-09-16 19:57:18 +00006804 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 mbedtls_ssl_transform_free( ssl->transform );
6807 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006808 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006809 }
Paul Bakker48916f92012-09-16 19:57:18 +00006810
Paul Bakkerc0463502013-02-14 11:19:38 +01006811 if( ssl->session )
6812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 mbedtls_ssl_session_free( ssl->session );
6814 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006815 ssl->session = NULL;
6816 }
6817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006819 ssl->alpn_chosen = NULL;
6820#endif
6821
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006822#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006823#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006824 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006825#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006826 {
6827 mbedtls_free( ssl->cli_id );
6828 ssl->cli_id = NULL;
6829 ssl->cli_id_len = 0;
6830 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006831#endif
6832
Paul Bakker48916f92012-09-16 19:57:18 +00006833 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6834 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006835
6836 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006837}
6838
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006839/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006840 * Reset an initialized and used SSL context for re-use while retaining
6841 * all application-set variables, function pointers and data.
6842 */
6843int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6844{
6845 return( ssl_session_reset_int( ssl, 0 ) );
6846}
6847
6848/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006849 * SSL set accessors
6850 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006851void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006852{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006853 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006854}
6855
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006856void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006857{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006858 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006859}
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006862void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006863{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006864 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006865}
6866#endif
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006869void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006870{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006871 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006872}
6873#endif
6874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006876
6877void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6878 unsigned allow_packing )
6879{
6880 ssl->disable_datagram_packing = !allow_packing;
6881}
6882
6883void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6884 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006885{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006886 conf->hs_timeout_min = min;
6887 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006888}
6889#endif
6890
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006891void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006892{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006893 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006894}
6895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006897void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006898 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006899 void *p_vrfy )
6900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006901 conf->f_vrfy = f_vrfy;
6902 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006905
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006906void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006907 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 void *p_rng )
6909{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006910 conf->f_rng = f_rng;
6911 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006912}
6913
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006914void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006915 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006916 void *p_dbg )
6917{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006918 conf->f_dbg = f_dbg;
6919 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006920}
6921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006923 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006924 mbedtls_ssl_send_t *f_send,
6925 mbedtls_ssl_recv_t *f_recv,
6926 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006927{
6928 ssl->p_bio = p_bio;
6929 ssl->f_send = f_send;
6930 ssl->f_recv = f_recv;
6931 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006932}
6933
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006934void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006935{
6936 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006937}
6938
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006939void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6940 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006941 mbedtls_ssl_set_timer_t *f_set_timer,
6942 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006943{
6944 ssl->p_timer = p_timer;
6945 ssl->f_set_timer = f_set_timer;
6946 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006947
6948 /* Make sure we start with no timer running */
6949 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006950}
6951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006953void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006954 void *p_cache,
6955 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6956 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006957{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006958 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006959 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006960 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_CLI_C)
6965int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006966{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006967 int ret;
6968
6969 if( ssl == NULL ||
6970 session == NULL ||
6971 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006972 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006975 }
6976
6977 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6978 return( ret );
6979
Paul Bakker0a597072012-09-25 21:55:46 +00006980 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006981
6982 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006985
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006986void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006987 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006989 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6990 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6991 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6992 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006993}
6994
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006995void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006996 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006997 int major, int minor )
6998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007000 return;
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007003 return;
7004
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007005 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007006}
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007009void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007010 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007011{
7012 conf->cert_profile = profile;
7013}
7014
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007015/* Append a new keycert entry to a (possibly empty) list */
7016static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7017 mbedtls_x509_crt *cert,
7018 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007019{
niisato8ee24222018-06-25 19:05:48 +09007020 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007021
niisato8ee24222018-06-25 19:05:48 +09007022 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7023 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007024 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007025
niisato8ee24222018-06-25 19:05:48 +09007026 new_cert->cert = cert;
7027 new_cert->key = key;
7028 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007029
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007030 /* Update head is the list was null, else add to the end */
7031 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007032 {
niisato8ee24222018-06-25 19:05:48 +09007033 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007034 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007035 else
7036 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007037 mbedtls_ssl_key_cert *cur = *head;
7038 while( cur->next != NULL )
7039 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007040 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007041 }
7042
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007043 return( 0 );
7044}
7045
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007046int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007047 mbedtls_x509_crt *own_cert,
7048 mbedtls_pk_context *pk_key )
7049{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007050 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007051}
7052
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007053void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007054 mbedtls_x509_crt *ca_chain,
7055 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007056{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007057 conf->ca_chain = ca_chain;
7058 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007061
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007062#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7063int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7064 mbedtls_x509_crt *own_cert,
7065 mbedtls_pk_context *pk_key )
7066{
7067 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7068 own_cert, pk_key ) );
7069}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007070
7071void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7072 mbedtls_x509_crt *ca_chain,
7073 mbedtls_x509_crl *ca_crl )
7074{
7075 ssl->handshake->sni_ca_chain = ca_chain;
7076 ssl->handshake->sni_ca_crl = ca_crl;
7077}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007078
7079void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7080 int authmode )
7081{
7082 ssl->handshake->sni_authmode = authmode;
7083}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007084#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7085
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007086#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007087/*
7088 * Set EC J-PAKE password for current handshake
7089 */
7090int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7091 const unsigned char *pw,
7092 size_t pw_len )
7093{
7094 mbedtls_ecjpake_role role;
7095
Janos Follath8eb64132016-06-03 15:40:57 +01007096 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7098
7099 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7100 role = MBEDTLS_ECJPAKE_SERVER;
7101 else
7102 role = MBEDTLS_ECJPAKE_CLIENT;
7103
7104 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7105 role,
7106 MBEDTLS_MD_SHA256,
7107 MBEDTLS_ECP_DP_SECP256R1,
7108 pw, pw_len ) );
7109}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007110#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007113int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007114 const unsigned char *psk, size_t psk_len,
7115 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007116{
Paul Bakker6db455e2013-09-18 17:29:31 +02007117 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007122
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007123 /* Identity len will be encoded on two bytes */
7124 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007125 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007126 {
7127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7128 }
7129
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007130 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007131 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007132 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007133
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007134 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007135 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007136 conf->psk_len = 0;
7137 }
7138 if( conf->psk_identity != NULL )
7139 {
7140 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007141 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007142 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007143 }
7144
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007145 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7146 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007147 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007148 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007149 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007150 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007151 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007152 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007153 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007154
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007155 conf->psk_len = psk_len;
7156 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007157
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007158 memcpy( conf->psk, psk, conf->psk_len );
7159 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007160
7161 return( 0 );
7162}
7163
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007164int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7165 const unsigned char *psk, size_t psk_len )
7166{
7167 if( psk == NULL || ssl->handshake == NULL )
7168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7169
7170 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7172
7173 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007174 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007175 mbedtls_platform_zeroize( ssl->handshake->psk,
7176 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007177 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007178 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007179 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007180
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007181 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007182 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007183
7184 ssl->handshake->psk_len = psk_len;
7185 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7186
7187 return( 0 );
7188}
7189
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007190void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007192 size_t),
7193 void *p_psk )
7194{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007195 conf->f_psk = f_psk;
7196 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007199
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007200#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007201
7202#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007203int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007204{
7205 int ret;
7206
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007207 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7208 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7209 {
7210 mbedtls_mpi_free( &conf->dhm_P );
7211 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007212 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007214
7215 return( 0 );
7216}
Hanno Becker470a8c42017-10-04 15:28:46 +01007217#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007218
Hanno Beckera90658f2017-10-04 15:29:08 +01007219int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7220 const unsigned char *dhm_P, size_t P_len,
7221 const unsigned char *dhm_G, size_t G_len )
7222{
7223 int ret;
7224
7225 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7226 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7227 {
7228 mbedtls_mpi_free( &conf->dhm_P );
7229 mbedtls_mpi_free( &conf->dhm_G );
7230 return( ret );
7231 }
7232
7233 return( 0 );
7234}
Paul Bakker5121ce52009-01-03 21:22:43 +00007235
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007236int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007237{
7238 int ret;
7239
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007240 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7241 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7242 {
7243 mbedtls_mpi_free( &conf->dhm_P );
7244 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007245 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007246 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007247
7248 return( 0 );
7249}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007250#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007251
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007252#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7253/*
7254 * Set the minimum length for Diffie-Hellman parameters
7255 */
7256void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7257 unsigned int bitlen )
7258{
7259 conf->dhm_min_bitlen = bitlen;
7260}
7261#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7262
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007263#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007264/*
7265 * Set allowed/preferred hashes for handshake signatures
7266 */
7267void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7268 const int *hashes )
7269{
7270 conf->sig_hashes = hashes;
7271}
Hanno Becker947194e2017-04-07 13:25:49 +01007272#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007273
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007274#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007275/*
7276 * Set the allowed elliptic curves
7277 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007278void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007279 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007280{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007281 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007282}
Hanno Becker947194e2017-04-07 13:25:49 +01007283#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007284
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007285#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007287{
Hanno Becker947194e2017-04-07 13:25:49 +01007288 /* Initialize to suppress unnecessary compiler warning */
7289 size_t hostname_len = 0;
7290
7291 /* Check if new hostname is valid before
7292 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007293 if( hostname != NULL )
7294 {
7295 hostname_len = strlen( hostname );
7296
7297 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7299 }
7300
7301 /* Now it's clear that we will overwrite the old hostname,
7302 * so we can free it safely */
7303
7304 if( ssl->hostname != NULL )
7305 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007306 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007307 mbedtls_free( ssl->hostname );
7308 }
7309
7310 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007311
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007313 {
7314 ssl->hostname = NULL;
7315 }
7316 else
7317 {
7318 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007319 if( ssl->hostname == NULL )
7320 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007321
Hanno Becker947194e2017-04-07 13:25:49 +01007322 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007323
Hanno Becker947194e2017-04-07 13:25:49 +01007324 ssl->hostname[hostname_len] = '\0';
7325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
7327 return( 0 );
7328}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007329#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007330
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007331#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007332void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007334 const unsigned char *, size_t),
7335 void *p_sni )
7336{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007337 conf->f_sni = f_sni;
7338 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007343int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007344{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007345 size_t cur_len, tot_len;
7346 const char **p;
7347
7348 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007349 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7350 * MUST NOT be truncated."
7351 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007352 */
7353 tot_len = 0;
7354 for( p = protos; *p != NULL; p++ )
7355 {
7356 cur_len = strlen( *p );
7357 tot_len += cur_len;
7358
7359 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007361 }
7362
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007363 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007364
7365 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007366}
7367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007369{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007370 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007373
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007374void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007376 conf->max_major_ver = major;
7377 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007378}
7379
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007380void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007381{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007382 conf->min_major_ver = major;
7383 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007384}
7385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007387void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007388{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007389 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007390}
7391#endif
7392
Janos Follath088ce432017-04-10 12:42:31 +01007393#if defined(MBEDTLS_SSL_SRV_C)
7394void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7395 char cert_req_ca_list )
7396{
7397 conf->cert_req_ca_list = cert_req_ca_list;
7398}
7399#endif
7400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007402void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007403{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007404 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007405}
7406#endif
7407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007409void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007411 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007412}
7413#endif
7414
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007415#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007416void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007417{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007418 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007419}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007420#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007421
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007422#if defined(MBEDTLS_SSL_PROTO_DTLS)
7423void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7424{
7425 conf->mtu = mtu;
7426}
7427#endif
7428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007430int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007433 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007436 }
7437
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007438 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007439
7440 return( 0 );
7441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007445void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007447 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007452void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007453{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007454 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007455}
7456#endif
7457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007458void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007460 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007461}
7462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007464void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007466 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007467}
7468
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007469void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007470{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007471 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007472}
7473
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007474void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007475 const unsigned char period[8] )
7476{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007477 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007482#if defined(MBEDTLS_SSL_CLI_C)
7483void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007484{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007485 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007486}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007487#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007488
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007489#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007490void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7491 mbedtls_ssl_ticket_write_t *f_ticket_write,
7492 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7493 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007494{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007495 conf->f_ticket_write = f_ticket_write;
7496 conf->f_ticket_parse = f_ticket_parse;
7497 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007498}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007501
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007502#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7503void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7504 mbedtls_ssl_export_keys_t *f_export_keys,
7505 void *p_export_keys )
7506{
7507 conf->f_export_keys = f_export_keys;
7508 conf->p_export_keys = p_export_keys;
7509}
7510#endif
7511
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007512#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007513void mbedtls_ssl_conf_async_private_cb(
7514 mbedtls_ssl_config *conf,
7515 mbedtls_ssl_async_sign_t *f_async_sign,
7516 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7517 mbedtls_ssl_async_resume_t *f_async_resume,
7518 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007519 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007520{
7521 conf->f_async_sign_start = f_async_sign;
7522 conf->f_async_decrypt_start = f_async_decrypt;
7523 conf->f_async_resume = f_async_resume;
7524 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007525 conf->p_async_config_data = async_config_data;
7526}
7527
Gilles Peskine8f97af72018-04-26 11:46:10 +02007528void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7529{
7530 return( conf->p_async_config_data );
7531}
7532
Gilles Peskine1febfef2018-04-30 11:54:39 +02007533void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007534{
7535 if( ssl->handshake == NULL )
7536 return( NULL );
7537 else
7538 return( ssl->handshake->user_async_ctx );
7539}
7540
Gilles Peskine1febfef2018-04-30 11:54:39 +02007541void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007542 void *ctx )
7543{
7544 if( ssl->handshake != NULL )
7545 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007546}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007547#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007548
Paul Bakker5121ce52009-01-03 21:22:43 +00007549/*
7550 * SSL get accessors
7551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007553{
7554 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7555}
7556
Hanno Becker8b170a02017-10-10 11:51:19 +01007557int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7558{
7559 /*
7560 * Case A: We're currently holding back
7561 * a message for further processing.
7562 */
7563
7564 if( ssl->keep_current_message == 1 )
7565 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007567 return( 1 );
7568 }
7569
7570 /*
7571 * Case B: Further records are pending in the current datagram.
7572 */
7573
7574#if defined(MBEDTLS_SSL_PROTO_DTLS)
7575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7576 ssl->in_left > ssl->next_record_offset )
7577 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007579 return( 1 );
7580 }
7581#endif /* MBEDTLS_SSL_PROTO_DTLS */
7582
7583 /*
7584 * Case C: A handshake message is being processed.
7585 */
7586
Hanno Becker8b170a02017-10-10 11:51:19 +01007587 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7588 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007590 return( 1 );
7591 }
7592
7593 /*
7594 * Case D: An application data message is being processed
7595 */
7596 if( ssl->in_offt != NULL )
7597 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007599 return( 1 );
7600 }
7601
7602 /*
7603 * In all other cases, the rest of the message can be dropped.
7604 * As in ssl_read_record_layer, this needs to be adapted if
7605 * we implement support for multiple alerts in single records.
7606 */
7607
7608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7609 return( 0 );
7610}
7611
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007612uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007613{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007614 if( ssl->session != NULL )
7615 return( ssl->session->verify_result );
7616
7617 if( ssl->session_negotiate != NULL )
7618 return( ssl->session_negotiate->verify_result );
7619
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007620 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007621}
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007624{
Paul Bakker926c8e42013-03-06 10:23:34 +01007625 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007626 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007629}
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007635 {
7636 switch( ssl->minor_ver )
7637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007639 return( "DTLSv1.0" );
7640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007642 return( "DTLSv1.2" );
7643
7644 default:
7645 return( "unknown (DTLS)" );
7646 }
7647 }
7648#endif
7649
Paul Bakker43ca69c2011-01-15 17:35:19 +00007650 switch( ssl->minor_ver )
7651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007653 return( "SSLv3.0" );
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007656 return( "TLSv1.0" );
7657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007659 return( "TLSv1.1" );
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007662 return( "TLSv1.2" );
7663
Paul Bakker43ca69c2011-01-15 17:35:19 +00007664 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007665 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007666 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007667}
7668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007670{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007671 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007673 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007674
Hanno Becker78640902018-08-13 16:35:15 +01007675 if( transform == NULL )
7676 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_ZLIB_SUPPORT)
7679 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7680 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007681 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007682#endif
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686 case MBEDTLS_MODE_GCM:
7687 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007688 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007690 transform_expansion = transform->minlen;
7691 break;
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007694
7695 block_size = mbedtls_cipher_get_block_size(
7696 &transform->cipher_ctx_enc );
7697
7698#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7699 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7700 {
7701 /* Expansion due to addition of
7702 * - MAC
7703 * - CBC padding (theoretically up to 256 bytes, but
7704 * we never use more than block_size)
7705 * - explicit IV
7706 */
7707 transform_expansion = transform->maclen + 2 * block_size;
7708 }
7709 else
7710#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7711 {
7712 /* No explicit IV prior to TLS 1.1. */
7713 transform_expansion = transform->maclen + block_size;
7714 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007715 break;
7716
7717 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007720 }
7721
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007722 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007723}
7724
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007725#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7726size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7727{
7728 size_t max_len;
7729
7730 /*
7731 * Assume mfl_code is correct since it was checked when set
7732 */
Angus Grattond8213d02016-05-25 20:56:48 +10007733 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007734
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007735 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007736 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007737 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007738 {
Angus Grattond8213d02016-05-25 20:56:48 +10007739 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007740 }
7741
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007742 /* During a handshake, use the value being negotiated */
7743 if( ssl->session_negotiate != NULL &&
7744 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7745 {
7746 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7747 }
7748
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007749 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007750}
7751#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7752
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007753int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7754{
7755 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7756
7757#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7758 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7759
7760 if( max_len > mfl )
7761 max_len = mfl;
7762#endif
7763
7764#if defined(MBEDTLS_SSL_PROTO_DTLS)
7765 if( ssl->conf->mtu != 0 )
7766 {
7767 const size_t mtu = ssl->conf->mtu;
7768 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7769 const size_t overhead = (size_t) ret;
7770
7771 if( ret < 0 )
7772 return( ret );
7773
7774 if( mtu <= overhead )
7775 {
7776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7777 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7778 }
7779
7780 if( max_len > mtu - overhead )
7781 max_len = mtu - overhead;
7782 }
7783#endif
7784
Hanno Becker0defedb2018-08-10 12:35:02 +01007785#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7786 !defined(MBEDTLS_SSL_PROTO_DTLS)
7787 ((void) ssl);
7788#endif
7789
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007790 return( (int) max_len );
7791}
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#if defined(MBEDTLS_X509_CRT_PARSE_C)
7794const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007795{
7796 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007797 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007798
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007799 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if defined(MBEDTLS_SSL_CLI_C)
7804int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007805{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007806 if( ssl == NULL ||
7807 dst == NULL ||
7808 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007809 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007812 }
7813
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007814 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007817
Paul Bakker5121ce52009-01-03 21:22:43 +00007818/*
Paul Bakker1961b702013-01-25 14:49:24 +01007819 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007824
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007825 if( ssl == NULL || ssl->conf == NULL )
7826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007829 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007831#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007833 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007835#endif
7836
Paul Bakker1961b702013-01-25 14:49:24 +01007837 return( ret );
7838}
7839
7840/*
7841 * Perform the SSL handshake
7842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007844{
7845 int ret = 0;
7846
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007847 if( ssl == NULL || ssl->conf == NULL )
7848 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007855
7856 if( ret != 0 )
7857 break;
7858 }
7859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007861
7862 return( ret );
7863}
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_RENEGOTIATION)
7866#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007867/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007868 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007871{
7872 int ret;
7873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007875
7876 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7878 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007879
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007880 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007881 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007883 return( ret );
7884 }
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007887
7888 return( 0 );
7889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007891
7892/*
7893 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 * - any side: calling mbedtls_ssl_renegotiate(),
7895 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7896 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007897 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007898 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007902{
7903 int ret;
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007906
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007907 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7908 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007909
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007910 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7911 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007913 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007915 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007916 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007917 ssl->handshake->out_msg_seq = 1;
7918 else
7919 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007920 }
7921#endif
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7924 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007929 return( ret );
7930 }
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007933
7934 return( 0 );
7935}
7936
7937/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007938 * Renegotiate current connection on client,
7939 * or request renegotiation on server
7940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007944
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007945 if( ssl == NULL || ssl->conf == NULL )
7946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007949 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007950 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007956
7957 /* Did we already try/start sending HelloRequest? */
7958 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007960
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007961 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007962 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007966 /*
7967 * On client, either start the renegotiation process or,
7968 * if already in progress, continue the handshake
7969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007974
7975 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007978 return( ret );
7979 }
7980 }
7981 else
7982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007986 return( ret );
7987 }
7988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007990
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007991 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007992}
7993
7994/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007995 * Check record counters and renegotiate if they're above the limit.
7996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007998{
Andres AG2196c7f2016-12-15 17:01:16 +00007999 size_t ep_len = ssl_ep_len( ssl );
8000 int in_ctr_cmp;
8001 int out_ctr_cmp;
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8004 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008005 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008006 {
8007 return( 0 );
8008 }
8009
Andres AG2196c7f2016-12-15 17:01:16 +00008010 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8011 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008012 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008013 ssl->conf->renego_period + ep_len, 8 - ep_len );
8014
8015 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008016 {
8017 return( 0 );
8018 }
8019
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008024
8025/*
8026 * Receive application data decrypted from the SSL layer
8027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008029{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008030 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008031 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008032
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008033 if( ssl == NULL || ssl->conf == NULL )
8034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008039 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008042 return( ret );
8043
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008044 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008046 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008047 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008048 return( ret );
8049 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008050 }
8051#endif
8052
Hanno Becker4a810fb2017-05-24 16:27:30 +01008053 /*
8054 * Check if renegotiation is necessary and/or handshake is
8055 * in process. If yes, perform/continue, and fall through
8056 * if an unexpected packet is received while the client
8057 * is waiting for the ServerHello.
8058 *
8059 * (There is no equivalent to the last condition on
8060 * the server-side as it is not treated as within
8061 * a handshake while waiting for the ClientHello
8062 * after a renegotiation request.)
8063 */
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008066 ret = ssl_check_ctr_renegotiate( ssl );
8067 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8068 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008071 return( ret );
8072 }
8073#endif
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008078 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8079 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008082 return( ret );
8083 }
8084 }
8085
Hanno Beckere41158b2017-10-23 13:30:32 +01008086 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008087 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008089 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008090 if( ssl->f_get_timer != NULL &&
8091 ssl->f_get_timer( ssl->p_timer ) == -1 )
8092 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008093 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008094 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008095
Hanno Becker327c93b2018-08-15 13:56:18 +01008096 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008097 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008098 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8099 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008100
Hanno Becker4a810fb2017-05-24 16:27:30 +01008101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8102 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008103 }
8104
8105 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008107 {
8108 /*
8109 * OpenSSL sends empty messages to randomize the IV
8110 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008111 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008114 return( 0 );
8115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008117 return( ret );
8118 }
8119 }
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008124
Hanno Becker4a810fb2017-05-24 16:27:30 +01008125 /*
8126 * - For client-side, expect SERVER_HELLO_REQUEST.
8127 * - For server-side, expect CLIENT_HELLO.
8128 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8129 */
8130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008132 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008134 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008137
8138 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008141 {
8142 continue;
8143 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008146 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008147#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008148
Hanno Becker4a810fb2017-05-24 16:27:30 +01008149#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008150 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008154
8155 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008158 {
8159 continue;
8160 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008163 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008164#endif /* MBEDTLS_SSL_SRV_C */
8165
Hanno Becker21df7f92017-10-17 11:03:26 +01008166#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008167 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008168 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8169 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8170 ssl->conf->allow_legacy_renegotiation ==
8171 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8172 {
8173 /*
8174 * Accept renegotiation request
8175 */
Paul Bakker48916f92012-09-16 19:57:18 +00008176
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008177 /* DTLS clients need to know renego is server-initiated */
8178#if defined(MBEDTLS_SSL_PROTO_DTLS)
8179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8180 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8181 {
8182 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8183 }
8184#endif
8185 ret = ssl_start_renegotiation( ssl );
8186 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8187 ret != 0 )
8188 {
8189 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8190 return( ret );
8191 }
8192 }
8193 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008194#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008195 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008196 /*
8197 * Refuse renegotiation
8198 */
8199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202#if defined(MBEDTLS_SSL_PROTO_SSL3)
8203 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008204 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008205 /* SSLv3 does not have a "no_renegotiation" warning, so
8206 we send a fatal alert and abort the connection. */
8207 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8208 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8209 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008210 }
8211 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8213#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8214 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8215 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8218 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8219 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008220 {
8221 return( ret );
8222 }
Paul Bakker48916f92012-09-16 19:57:18 +00008223 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008224 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8226 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008230 }
Paul Bakker48916f92012-09-16 19:57:18 +00008231 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008232
Hanno Becker90333da2017-10-10 11:27:13 +01008233 /* At this point, we don't know whether the renegotiation has been
8234 * completed or not. The cases to consider are the following:
8235 * 1) The renegotiation is complete. In this case, no new record
8236 * has been read yet.
8237 * 2) The renegotiation is incomplete because the client received
8238 * an application data record while awaiting the ServerHello.
8239 * 3) The renegotiation is incomplete because the client received
8240 * a non-handshake, non-application data message while awaiting
8241 * the ServerHello.
8242 * In each of these case, looping will be the proper action:
8243 * - For 1), the next iteration will read a new record and check
8244 * if it's application data.
8245 * - For 2), the loop condition isn't satisfied as application data
8246 * is present, hence continue is the same as break
8247 * - For 3), the loop condition is satisfied and read_record
8248 * will re-deliver the message that was held back by the client
8249 * when expecting the ServerHello.
8250 */
8251 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008252 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008253#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008255 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008256 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008257 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008258 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008261 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008263 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008264 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8269 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008272 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008273 }
8274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8278 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008279 }
8280
8281 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008282
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008283 /* We're going to return something now, cancel timer,
8284 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008286 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008287
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008289 /* If we requested renego but received AppData, resend HelloRequest.
8290 * Do it now, after setting in_offt, to avoid taking this branch
8291 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008295 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008296 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008299 return( ret );
8300 }
8301 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008304 }
8305
8306 n = ( len < ssl->in_msglen )
8307 ? len : ssl->in_msglen;
8308
8309 memcpy( buf, ssl->in_offt, n );
8310 ssl->in_msglen -= n;
8311
8312 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008313 {
8314 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008316 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008318 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008319 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008320 /* more data available */
8321 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008325
Paul Bakker23986e52011-04-24 08:57:21 +00008326 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008327}
8328
8329/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008330 * Send application data to be encrypted by the SSL layer, taking care of max
8331 * fragment length and buffer size.
8332 *
8333 * According to RFC 5246 Section 6.2.1:
8334 *
8335 * Zero-length fragments of Application data MAY be sent as they are
8336 * potentially useful as a traffic analysis countermeasure.
8337 *
8338 * Therefore, it is possible that the input message length is 0 and the
8339 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008340 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008341static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008342 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008343{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008344 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8345 const size_t max_len = (size_t) ret;
8346
8347 if( ret < 0 )
8348 {
8349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8350 return( ret );
8351 }
8352
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008353 if( len > max_len )
8354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008359 "maximum fragment length: %d > %d",
8360 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008362 }
8363 else
8364#endif
8365 len = max_len;
8366 }
Paul Bakker887bd502011-06-08 13:10:54 +00008367
Paul Bakker5121ce52009-01-03 21:22:43 +00008368 if( ssl->out_left != 0 )
8369 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008370 /*
8371 * The user has previously tried to send the data and
8372 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8373 * written. In this case, we expect the high-level write function
8374 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008379 return( ret );
8380 }
8381 }
Paul Bakker887bd502011-06-08 13:10:54 +00008382 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008383 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008384 /*
8385 * The user is trying to send a message the first time, so we need to
8386 * copy the data into the internal buffers and setup the data structure
8387 * to keep track of partial writes
8388 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008389 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008391 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008392
Hanno Becker67bc7c32018-08-06 11:33:50 +01008393 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008396 return( ret );
8397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008398 }
8399
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008400 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008401}
8402
8403/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008404 * Write application data, doing 1/n-1 splitting if necessary.
8405 *
8406 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008407 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008408 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008411static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008412 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008413{
8414 int ret;
8415
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008416 if( ssl->conf->cbc_record_splitting ==
8417 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008418 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8420 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8421 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008422 {
8423 return( ssl_write_real( ssl, buf, len ) );
8424 }
8425
8426 if( ssl->split_done == 0 )
8427 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008428 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008429 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008430 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008431 }
8432
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008433 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8434 return( ret );
8435 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008436
8437 return( ret + 1 );
8438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008440
8441/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008442 * Write application data (public-facing wrapper)
8443 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008444int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008445{
8446 int ret;
8447
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008449
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008450 if( ssl == NULL || ssl->conf == NULL )
8451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8452
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008453#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008454 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8455 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008456 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008457 return( ret );
8458 }
8459#endif
8460
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008461 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008462 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008463 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008464 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008466 return( ret );
8467 }
8468 }
8469
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008470#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008471 ret = ssl_write_split( ssl, buf, len );
8472#else
8473 ret = ssl_write_real( ssl, buf, len );
8474#endif
8475
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008477
8478 return( ret );
8479}
8480
8481/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008482 * Notify the peer that the connection is being closed
8483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008485{
8486 int ret;
8487
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008488 if( ssl == NULL || ssl->conf == NULL )
8489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
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 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8499 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8500 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008503 return( ret );
8504 }
8505 }
8506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008508
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008509 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008510}
8511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008513{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008514 if( transform == NULL )
8515 return;
8516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008518 deflateEnd( &transform->ctx_deflate );
8519 inflateEnd( &transform->ctx_inflate );
8520#endif
8521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8523 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525 mbedtls_md_free( &transform->md_ctx_enc );
8526 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008527
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008528 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008529}
8530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531#if defined(MBEDTLS_X509_CRT_PARSE_C)
8532static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008535
8536 while( cur != NULL )
8537 {
8538 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008540 cur = next;
8541 }
8542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008544
Hanno Becker0271f962018-08-16 13:23:47 +01008545#if defined(MBEDTLS_SSL_PROTO_DTLS)
8546
8547static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8548{
8549 unsigned offset;
8550 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8551
8552 if( hs == NULL )
8553 return;
8554
8555 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
8556 {
8557 mbedtls_ssl_hs_buffer *hs_buf = &hs->buffering.hs[offset];
8558 if( hs_buf->is_valid == 1 )
8559 {
8560 mbedtls_free( hs_buf->data );
8561 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
8562 }
8563 }
8564}
8565
8566#endif /* MBEDTLS_SSL_PROTO_DTLS */
8567
Gilles Peskine9b562d52018-04-25 20:32:43 +02008568void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008569{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008570 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8571
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008572 if( handshake == NULL )
8573 return;
8574
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008575#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8576 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8577 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008578 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008579 handshake->async_in_progress = 0;
8580 }
8581#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8582
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008583#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8584 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8585 mbedtls_md5_free( &handshake->fin_md5 );
8586 mbedtls_sha1_free( &handshake->fin_sha1 );
8587#endif
8588#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8589#if defined(MBEDTLS_SHA256_C)
8590 mbedtls_sha256_free( &handshake->fin_sha256 );
8591#endif
8592#if defined(MBEDTLS_SHA512_C)
8593 mbedtls_sha512_free( &handshake->fin_sha512 );
8594#endif
8595#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597#if defined(MBEDTLS_DHM_C)
8598 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008599#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_ECDH_C)
8601 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008602#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008603#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008604 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008605#if defined(MBEDTLS_SSL_CLI_C)
8606 mbedtls_free( handshake->ecjpake_cache );
8607 handshake->ecjpake_cache = NULL;
8608 handshake->ecjpake_cache_len = 0;
8609#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008610#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008611
Janos Follath4ae5c292016-02-10 11:27:43 +00008612#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8613 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008614 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008616#endif
8617
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008618#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8619 if( handshake->psk != NULL )
8620 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008621 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008622 mbedtls_free( handshake->psk );
8623 }
8624#endif
8625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8627 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008628 /*
8629 * Free only the linked list wrapper, not the keys themselves
8630 * since the belong to the SNI callback
8631 */
8632 if( handshake->sni_key_cert != NULL )
8633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008635
8636 while( cur != NULL )
8637 {
8638 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008640 cur = next;
8641 }
8642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_SSL_PROTO_DTLS)
8646 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008647 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008648 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008649 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008650#endif
8651
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008652 mbedtls_platform_zeroize( handshake,
8653 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008654}
8655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008657{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008658 if( session == NULL )
8659 return;
8660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008662 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664 mbedtls_x509_crt_free( session->peer_cert );
8665 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008666 }
Paul Bakkered27a042013-04-18 22:46:23 +02008667#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008668
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008669#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008671#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008672
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008673 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008674}
8675
Paul Bakker5121ce52009-01-03 21:22:43 +00008676/*
8677 * Free an SSL context
8678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008680{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008681 if( ssl == NULL )
8682 return;
8683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008685
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008686 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008687 {
Angus Grattond8213d02016-05-25 20:56:48 +10008688 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008690 }
8691
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008692 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008693 {
Angus Grattond8213d02016-05-25 20:56:48 +10008694 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008696 }
8697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008699 if( ssl->compress_buf != NULL )
8700 {
Angus Grattond8213d02016-05-25 20:56:48 +10008701 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008703 }
8704#endif
8705
Paul Bakker48916f92012-09-16 19:57:18 +00008706 if( ssl->transform )
8707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 mbedtls_ssl_transform_free( ssl->transform );
8709 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008710 }
8711
8712 if( ssl->handshake )
8713 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008714 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8716 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718 mbedtls_free( ssl->handshake );
8719 mbedtls_free( ssl->transform_negotiate );
8720 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008721 }
8722
Paul Bakkerc0463502013-02-14 11:19:38 +01008723 if( ssl->session )
8724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725 mbedtls_ssl_session_free( ssl->session );
8726 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008727 }
8728
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008729#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008730 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008731 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008732 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008734 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008735#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8738 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8741 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008742 }
8743#endif
8744
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008745#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008747#endif
8748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008750
Paul Bakker86f04f42013-02-14 11:20:09 +01008751 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008752 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008753}
8754
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008755/*
8756 * Initialze mbedtls_ssl_config
8757 */
8758void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8759{
8760 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8761}
8762
Simon Butcherc97b6972015-12-27 23:48:17 +00008763#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008764static int ssl_preset_default_hashes[] = {
8765#if defined(MBEDTLS_SHA512_C)
8766 MBEDTLS_MD_SHA512,
8767 MBEDTLS_MD_SHA384,
8768#endif
8769#if defined(MBEDTLS_SHA256_C)
8770 MBEDTLS_MD_SHA256,
8771 MBEDTLS_MD_SHA224,
8772#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008773#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008774 MBEDTLS_MD_SHA1,
8775#endif
8776 MBEDTLS_MD_NONE
8777};
Simon Butcherc97b6972015-12-27 23:48:17 +00008778#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008779
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008780static int ssl_preset_suiteb_ciphersuites[] = {
8781 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8782 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8783 0
8784};
8785
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008786#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008787static int ssl_preset_suiteb_hashes[] = {
8788 MBEDTLS_MD_SHA256,
8789 MBEDTLS_MD_SHA384,
8790 MBEDTLS_MD_NONE
8791};
8792#endif
8793
8794#if defined(MBEDTLS_ECP_C)
8795static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8796 MBEDTLS_ECP_DP_SECP256R1,
8797 MBEDTLS_ECP_DP_SECP384R1,
8798 MBEDTLS_ECP_DP_NONE
8799};
8800#endif
8801
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008802/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008803 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008804 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008805int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008806 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008807{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008808#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008809 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008810#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008811
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008812 /* Use the functions here so that they are covered in tests,
8813 * but otherwise access member directly for efficiency */
8814 mbedtls_ssl_conf_endpoint( conf, endpoint );
8815 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008816
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008817 /*
8818 * Things that are common to all presets
8819 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008820#if defined(MBEDTLS_SSL_CLI_C)
8821 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8822 {
8823 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8825 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8826#endif
8827 }
8828#endif
8829
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008830#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008831 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008832#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008833
8834#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8835 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8836#endif
8837
8838#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8839 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8840#endif
8841
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008842#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8843 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8844#endif
8845
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008846#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008847 conf->f_cookie_write = ssl_cookie_write_dummy;
8848 conf->f_cookie_check = ssl_cookie_check_dummy;
8849#endif
8850
8851#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8852 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8853#endif
8854
Janos Follath088ce432017-04-10 12:42:31 +01008855#if defined(MBEDTLS_SSL_SRV_C)
8856 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8857#endif
8858
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008859#if defined(MBEDTLS_SSL_PROTO_DTLS)
8860 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8861 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8862#endif
8863
8864#if defined(MBEDTLS_SSL_RENEGOTIATION)
8865 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008866 memset( conf->renego_period, 0x00, 2 );
8867 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008868#endif
8869
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8871 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8872 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008873 const unsigned char dhm_p[] =
8874 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8875 const unsigned char dhm_g[] =
8876 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8877
Hanno Beckera90658f2017-10-04 15:29:08 +01008878 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8879 dhm_p, sizeof( dhm_p ),
8880 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008881 {
8882 return( ret );
8883 }
8884 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008885#endif
8886
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008887 /*
8888 * Preset-specific defaults
8889 */
8890 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008891 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008892 /*
8893 * NSA Suite B
8894 */
8895 case MBEDTLS_SSL_PRESET_SUITEB:
8896 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8897 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8898 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8899 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8900
8901 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8902 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8903 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8904 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8905 ssl_preset_suiteb_ciphersuites;
8906
8907#if defined(MBEDTLS_X509_CRT_PARSE_C)
8908 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008909#endif
8910
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008911#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008912 conf->sig_hashes = ssl_preset_suiteb_hashes;
8913#endif
8914
8915#if defined(MBEDTLS_ECP_C)
8916 conf->curve_list = ssl_preset_suiteb_curves;
8917#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008918 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008919
8920 /*
8921 * Default
8922 */
8923 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008924 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8925 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8926 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8927 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8928 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8929 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8930 MBEDTLS_SSL_MIN_MINOR_VERSION :
8931 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008932 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8933 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8934
8935#if defined(MBEDTLS_SSL_PROTO_DTLS)
8936 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8937 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8938#endif
8939
8940 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8941 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8942 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8943 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8944 mbedtls_ssl_list_ciphersuites();
8945
8946#if defined(MBEDTLS_X509_CRT_PARSE_C)
8947 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8948#endif
8949
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008950#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008951 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008952#endif
8953
8954#if defined(MBEDTLS_ECP_C)
8955 conf->curve_list = mbedtls_ecp_grp_id_list();
8956#endif
8957
8958#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8959 conf->dhm_min_bitlen = 1024;
8960#endif
8961 }
8962
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008963 return( 0 );
8964}
8965
8966/*
8967 * Free mbedtls_ssl_config
8968 */
8969void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8970{
8971#if defined(MBEDTLS_DHM_C)
8972 mbedtls_mpi_free( &conf->dhm_P );
8973 mbedtls_mpi_free( &conf->dhm_G );
8974#endif
8975
8976#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8977 if( conf->psk != NULL )
8978 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008979 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008980 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008981 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008982 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008983 }
8984
8985 if( conf->psk_identity != NULL )
8986 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008987 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008988 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008989 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008990 conf->psk_identity_len = 0;
8991 }
8992#endif
8993
8994#if defined(MBEDTLS_X509_CRT_PARSE_C)
8995 ssl_key_cert_free( conf->key_cert );
8996#endif
8997
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008998 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008999}
9000
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009001#if defined(MBEDTLS_PK_C) && \
9002 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009003/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008#if defined(MBEDTLS_RSA_C)
9009 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9010 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012#if defined(MBEDTLS_ECDSA_C)
9013 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9014 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009017}
9018
Hanno Becker7e5437a2017-04-28 17:15:26 +01009019unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9020{
9021 switch( type ) {
9022 case MBEDTLS_PK_RSA:
9023 return( MBEDTLS_SSL_SIG_RSA );
9024 case MBEDTLS_PK_ECDSA:
9025 case MBEDTLS_PK_ECKEY:
9026 return( MBEDTLS_SSL_SIG_ECDSA );
9027 default:
9028 return( MBEDTLS_SSL_SIG_ANON );
9029 }
9030}
9031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009033{
9034 switch( sig )
9035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036#if defined(MBEDTLS_RSA_C)
9037 case MBEDTLS_SSL_SIG_RSA:
9038 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040#if defined(MBEDTLS_ECDSA_C)
9041 case MBEDTLS_SSL_SIG_ECDSA:
9042 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009043#endif
9044 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009046 }
9047}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009048#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009049
Hanno Becker7e5437a2017-04-28 17:15:26 +01009050#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9051 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9052
9053/* Find an entry in a signature-hash set matching a given hash algorithm. */
9054mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9055 mbedtls_pk_type_t sig_alg )
9056{
9057 switch( sig_alg )
9058 {
9059 case MBEDTLS_PK_RSA:
9060 return( set->rsa );
9061 case MBEDTLS_PK_ECDSA:
9062 return( set->ecdsa );
9063 default:
9064 return( MBEDTLS_MD_NONE );
9065 }
9066}
9067
9068/* Add a signature-hash-pair to a signature-hash set */
9069void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9070 mbedtls_pk_type_t sig_alg,
9071 mbedtls_md_type_t md_alg )
9072{
9073 switch( sig_alg )
9074 {
9075 case MBEDTLS_PK_RSA:
9076 if( set->rsa == MBEDTLS_MD_NONE )
9077 set->rsa = md_alg;
9078 break;
9079
9080 case MBEDTLS_PK_ECDSA:
9081 if( set->ecdsa == MBEDTLS_MD_NONE )
9082 set->ecdsa = md_alg;
9083 break;
9084
9085 default:
9086 break;
9087 }
9088}
9089
9090/* Allow exactly one hash algorithm for each signature. */
9091void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9092 mbedtls_md_type_t md_alg )
9093{
9094 set->rsa = md_alg;
9095 set->ecdsa = md_alg;
9096}
9097
9098#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9099 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9100
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009101/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009102 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009105{
9106 switch( hash )
9107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#if defined(MBEDTLS_MD5_C)
9109 case MBEDTLS_SSL_HASH_MD5:
9110 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112#if defined(MBEDTLS_SHA1_C)
9113 case MBEDTLS_SSL_HASH_SHA1:
9114 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116#if defined(MBEDTLS_SHA256_C)
9117 case MBEDTLS_SSL_HASH_SHA224:
9118 return( MBEDTLS_MD_SHA224 );
9119 case MBEDTLS_SSL_HASH_SHA256:
9120 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122#if defined(MBEDTLS_SHA512_C)
9123 case MBEDTLS_SSL_HASH_SHA384:
9124 return( MBEDTLS_MD_SHA384 );
9125 case MBEDTLS_SSL_HASH_SHA512:
9126 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009127#endif
9128 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009130 }
9131}
9132
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009133/*
9134 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9135 */
9136unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9137{
9138 switch( md )
9139 {
9140#if defined(MBEDTLS_MD5_C)
9141 case MBEDTLS_MD_MD5:
9142 return( MBEDTLS_SSL_HASH_MD5 );
9143#endif
9144#if defined(MBEDTLS_SHA1_C)
9145 case MBEDTLS_MD_SHA1:
9146 return( MBEDTLS_SSL_HASH_SHA1 );
9147#endif
9148#if defined(MBEDTLS_SHA256_C)
9149 case MBEDTLS_MD_SHA224:
9150 return( MBEDTLS_SSL_HASH_SHA224 );
9151 case MBEDTLS_MD_SHA256:
9152 return( MBEDTLS_SSL_HASH_SHA256 );
9153#endif
9154#if defined(MBEDTLS_SHA512_C)
9155 case MBEDTLS_MD_SHA384:
9156 return( MBEDTLS_SSL_HASH_SHA384 );
9157 case MBEDTLS_MD_SHA512:
9158 return( MBEDTLS_SSL_HASH_SHA512 );
9159#endif
9160 default:
9161 return( MBEDTLS_SSL_HASH_NONE );
9162 }
9163}
9164
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009165#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009166/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009167 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009168 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009169 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009170int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009173
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009174 if( ssl->conf->curve_list == NULL )
9175 return( -1 );
9176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009177 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009178 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009179 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009180
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009181 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009182}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009183#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009184
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009185#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009186/*
9187 * Check if a hash proposed by the peer is in our list.
9188 * Return 0 if we're willing to use it, -1 otherwise.
9189 */
9190int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9191 mbedtls_md_type_t md )
9192{
9193 const int *cur;
9194
9195 if( ssl->conf->sig_hashes == NULL )
9196 return( -1 );
9197
9198 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9199 if( *cur == (int) md )
9200 return( 0 );
9201
9202 return( -1 );
9203}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009204#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206#if defined(MBEDTLS_X509_CRT_PARSE_C)
9207int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9208 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009209 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009210 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009211{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009212 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009214 int usage = 0;
9215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009217 const char *ext_oid;
9218 size_t ext_len;
9219#endif
9220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9222 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009223 ((void) cert);
9224 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009225 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009226#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9229 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009230 {
9231 /* Server part of the key exchange */
9232 switch( ciphersuite->key_exchange )
9233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 case MBEDTLS_KEY_EXCHANGE_RSA:
9235 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009236 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009237 break;
9238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9240 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9241 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9242 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009243 break;
9244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9246 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009247 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009248 break;
9249
9250 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 case MBEDTLS_KEY_EXCHANGE_NONE:
9252 case MBEDTLS_KEY_EXCHANGE_PSK:
9253 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9254 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009255 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009256 usage = 0;
9257 }
9258 }
9259 else
9260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9262 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009263 }
9264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009266 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009267 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009268 ret = -1;
9269 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009270#else
9271 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9275 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9278 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009279 }
9280 else
9281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9283 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009284 }
9285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009287 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009288 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009289 ret = -1;
9290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009292
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009293 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009296
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009297/*
9298 * Convert version numbers to/from wire format
9299 * and, for DTLS, to/from TLS equivalent.
9300 *
9301 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009302 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009303 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9304 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009307 unsigned char ver[2] )
9308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309#if defined(MBEDTLS_SSL_PROTO_DTLS)
9310 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009313 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9314
9315 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9316 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9317 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009318 else
9319#else
9320 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009321#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009322 {
9323 ver[0] = (unsigned char) major;
9324 ver[1] = (unsigned char) minor;
9325 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009326}
9327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009329 const unsigned char ver[2] )
9330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331#if defined(MBEDTLS_SSL_PROTO_DTLS)
9332 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009333 {
9334 *major = 255 - ver[0] + 2;
9335 *minor = 255 - ver[1] + 1;
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009338 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9339 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009340 else
9341#else
9342 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009343#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009344 {
9345 *major = ver[0];
9346 *minor = ver[1];
9347 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009348}
9349
Simon Butcher99000142016-10-13 17:21:01 +01009350int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9351{
9352#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9353 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9354 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9355
9356 switch( md )
9357 {
9358#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9359#if defined(MBEDTLS_MD5_C)
9360 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009361 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009362#endif
9363#if defined(MBEDTLS_SHA1_C)
9364 case MBEDTLS_SSL_HASH_SHA1:
9365 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9366 break;
9367#endif
9368#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9369#if defined(MBEDTLS_SHA512_C)
9370 case MBEDTLS_SSL_HASH_SHA384:
9371 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9372 break;
9373#endif
9374#if defined(MBEDTLS_SHA256_C)
9375 case MBEDTLS_SSL_HASH_SHA256:
9376 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9377 break;
9378#endif
9379 default:
9380 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9381 }
9382
9383 return 0;
9384#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9385 (void) ssl;
9386 (void) md;
9387
9388 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9389#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9390}
9391
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009392#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9393 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9394int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9395 unsigned char *output,
9396 unsigned char *data, size_t data_len )
9397{
9398 int ret = 0;
9399 mbedtls_md5_context mbedtls_md5;
9400 mbedtls_sha1_context mbedtls_sha1;
9401
9402 mbedtls_md5_init( &mbedtls_md5 );
9403 mbedtls_sha1_init( &mbedtls_sha1 );
9404
9405 /*
9406 * digitally-signed struct {
9407 * opaque md5_hash[16];
9408 * opaque sha_hash[20];
9409 * };
9410 *
9411 * md5_hash
9412 * MD5(ClientHello.random + ServerHello.random
9413 * + ServerParams);
9414 * sha_hash
9415 * SHA(ClientHello.random + ServerHello.random
9416 * + ServerParams);
9417 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009418 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 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_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009421 goto exit;
9422 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009423 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009424 ssl->handshake->randbytes, 64 ) ) != 0 )
9425 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_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_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009430 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009432 goto exit;
9433 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009434 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009435 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009437 goto exit;
9438 }
9439
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009440 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009441 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009443 goto exit;
9444 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009445 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009446 ssl->handshake->randbytes, 64 ) ) != 0 )
9447 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009449 goto exit;
9450 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009451 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009452 data_len ) ) != 0 )
9453 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009455 goto exit;
9456 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009457 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009458 output + 16 ) ) != 0 )
9459 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009461 goto exit;
9462 }
9463
9464exit:
9465 mbedtls_md5_free( &mbedtls_md5 );
9466 mbedtls_sha1_free( &mbedtls_sha1 );
9467
9468 if( ret != 0 )
9469 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9470 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9471
9472 return( ret );
9473
9474}
9475#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9476 MBEDTLS_SSL_PROTO_TLS1_1 */
9477
9478#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9479 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9480int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009481 unsigned char *hash, size_t *hashlen,
9482 unsigned char *data, size_t data_len,
9483 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009484{
9485 int ret = 0;
9486 mbedtls_md_context_t ctx;
9487 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009488 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009489
9490 mbedtls_md_init( &ctx );
9491
9492 /*
9493 * digitally-signed struct {
9494 * opaque client_random[32];
9495 * opaque server_random[32];
9496 * ServerDHParams params;
9497 * };
9498 */
9499 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9500 {
9501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9502 goto exit;
9503 }
9504 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9505 {
9506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9507 goto exit;
9508 }
9509 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9510 {
9511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9512 goto exit;
9513 }
9514 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9515 {
9516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9517 goto exit;
9518 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009519 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009520 {
9521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9522 goto exit;
9523 }
9524
9525exit:
9526 mbedtls_md_free( &ctx );
9527
9528 if( ret != 0 )
9529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9530 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9531
9532 return( ret );
9533}
9534#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9535 MBEDTLS_SSL_PROTO_TLS1_2 */
9536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#endif /* MBEDTLS_SSL_TLS_C */