blob: d0d5d72c574c2f7725a054e56e5a72b64e18ce43 [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
Hanno Beckere605b192018-08-21 15:59:07 +0100173static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
174 uint8_t slot );
175
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200176/*
177 * Double the retransmit timeout value, within the allowed range,
178 * returning -1 if the maximum value has already been reached.
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200181{
182 uint32_t new_timeout;
183
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200184 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200185 return( -1 );
186
187 new_timeout = 2 * ssl->handshake->retransmit_timeout;
188
189 /* Avoid arithmetic overflow and range overflow */
190 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200191 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200192 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200193 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200194 }
195
196 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200198 ssl->handshake->retransmit_timeout ) );
199
200 return( 0 );
201}
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200204{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200205 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200207 ssl->handshake->retransmit_timeout ) );
208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200212/*
213 * Convert max_fragment_length codes to length.
214 * RFC 6066 says:
215 * enum{
216 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
217 * } MaxFragmentLength;
218 * and we add 0 -> extension unused
219 */
Angus Grattond8213d02016-05-25 20:56:48 +1000220static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200221{
Angus Grattond8213d02016-05-25 20:56:48 +1000222 switch( mfl )
223 {
224 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
225 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
226 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
227 return 512;
228 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
229 return 1024;
230 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
231 return 2048;
232 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
233 return 4096;
234 default:
235 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
236 }
237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200239
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200240#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ssl_session_free( dst );
244 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200247 if( src->peer_cert != NULL )
248 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200249 int ret;
250
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200251 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200252 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200258 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 dst->peer_cert = NULL;
262 return( ret );
263 }
264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200267#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 if( src->ticket != NULL )
269 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200270 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200271 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200272 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273
274 memcpy( dst->ticket, src->ticket, src->ticket_len );
275 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200276#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277
278 return( 0 );
279}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200280#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
283int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200284 const unsigned char *key_enc, const unsigned char *key_dec,
285 size_t keylen,
286 const unsigned char *iv_enc, const unsigned char *iv_dec,
287 size_t ivlen,
288 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200289 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
291int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
292int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
293int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
294int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
295#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000296
Paul Bakker5121ce52009-01-03 21:22:43 +0000297/*
298 * Key material generation
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int ssl3_prf( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000304 unsigned char *dstbuf, size_t dlen )
305{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100306 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000307 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200308 mbedtls_md5_context md5;
309 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000310 unsigned char padding[16];
311 unsigned char sha1sum[20];
312 ((void)label);
313
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200314 mbedtls_md5_init( &md5 );
315 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200316
Paul Bakker5f70b252012-09-13 14:23:06 +0000317 /*
318 * SSLv3:
319 * block =
320 * MD5( secret + SHA1( 'A' + secret + random ) ) +
321 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
322 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
323 * ...
324 */
325 for( i = 0; i < dlen / 16; i++ )
326 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200327 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000328
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100330 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100331 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100332 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100333 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100334 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100335 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100336 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100337 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100338 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000339
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100340 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100341 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100342 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100343 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100344 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100345 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100346 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100347 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000348 }
349
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100350exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200351 mbedtls_md5_free( &md5 );
352 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000353
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500354 mbedtls_platform_zeroize( padding, sizeof( padding ) );
355 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000356
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100357 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200362static int tls1_prf( const unsigned char *secret, size_t slen,
363 const char *label,
364 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000365 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000366{
Paul Bakker23986e52011-04-24 08:57:21 +0000367 size_t nb, hs;
368 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200369 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 unsigned char tmp[128];
371 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 const mbedtls_md_info_t *md_info;
373 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100374 int ret;
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381 hs = ( slen + 1 ) / 2;
382 S1 = secret;
383 S2 = secret + slen - hs;
384
385 nb = strlen( label );
386 memcpy( tmp + 20, label, nb );
387 memcpy( tmp + 20 + nb, random, rlen );
388 nb += rlen;
389
390 /*
391 * First compute P_md5(secret,label+random)[0..dlen]
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100397 return( ret );
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
400 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
401 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403 for( i = 0; i < dlen; i += 16 )
404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_md_hmac_reset ( &md_ctx );
406 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
407 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_reset ( &md_ctx );
410 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
411 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
414
415 for( j = 0; j < k; j++ )
416 dstbuf[i + j] = h_i[j];
417 }
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 /*
422 * XOR out with P_sha1(secret,label+random)[0..dlen]
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100428 return( ret );
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
431 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
432 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 for( i = 0; i < dlen; i += 20 )
435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_hmac_reset ( &md_ctx );
437 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
438 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_reset ( &md_ctx );
441 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
442 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
445
446 for( j = 0; j < k; j++ )
447 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
448 }
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100451
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500452 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
453 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455 return( 0 );
456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
460static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100461 const unsigned char *secret, size_t slen,
462 const char *label,
463 const unsigned char *random, size_t rlen,
464 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000465{
466 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100467 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000468 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
470 const mbedtls_md_info_t *md_info;
471 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100472 int ret;
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100480
481 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000483
484 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100485 memcpy( tmp + md_len, label, nb );
486 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 nb += rlen;
488
489 /*
490 * Compute P_<hash>(secret, label + random)[0..dlen]
491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100493 return( ret );
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
496 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
497 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100498
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100499 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_md_hmac_reset ( &md_ctx );
502 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
503 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_md_hmac_reset ( &md_ctx );
506 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
507 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000508
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100509 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000510
511 for( j = 0; j < k; j++ )
512 dstbuf[i + j] = h_i[j];
513 }
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100516
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500517 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
518 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000519
520 return( 0 );
521}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100524static int tls_prf_sha256( const unsigned char *secret, size_t slen,
525 const char *label,
526 const unsigned char *random, size_t rlen,
527 unsigned char *dstbuf, size_t dlen )
528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100530 label, random, rlen, dstbuf, dlen ) );
531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200535static int tls_prf_sha384( const unsigned char *secret, size_t slen,
536 const char *label,
537 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000538 unsigned char *dstbuf, size_t dlen )
539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100541 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_SHA512_C */
544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
549 defined(MBEDTLS_SSL_PROTO_TLS1_1)
550static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200551#endif
Paul Bakker380da532012-04-18 16:10:25 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_SSL_PROTO_SSL3)
554static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
555static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200556#endif
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
559static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
560static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200561#endif
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
564#if defined(MBEDTLS_SHA256_C)
565static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
566static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
567static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200568#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_SHA512_C)
571static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
572static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
573static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200579 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 unsigned char keyblk[256];
582 unsigned char *key1;
583 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100584 unsigned char *mac_enc;
585 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000586 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200587 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 const mbedtls_cipher_info_t *cipher_info;
589 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_ssl_session *session = ssl->session_negotiate;
592 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
593 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100598 if( cipher_info == NULL )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100601 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100603 }
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100606 if( md_info == NULL )
607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100609 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100611 }
612
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SSL_PROTO_SSL3)
617 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000618 {
Paul Bakker48916f92012-09-16 19:57:18 +0000619 handshake->tls_prf = ssl3_prf;
620 handshake->calc_verify = ssl_calc_verify_ssl;
621 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200623 else
624#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
626 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000627 {
Paul Bakker48916f92012-09-16 19:57:18 +0000628 handshake->tls_prf = tls1_prf;
629 handshake->calc_verify = ssl_calc_verify_tls;
630 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200632 else
633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
635#if defined(MBEDTLS_SHA512_C)
636 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
637 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000638 {
Paul Bakker48916f92012-09-16 19:57:18 +0000639 handshake->tls_prf = tls_prf_sha384;
640 handshake->calc_verify = ssl_calc_verify_tls_sha384;
641 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000642 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000643 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SHA256_C)
646 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000647 {
Paul Bakker48916f92012-09-16 19:57:18 +0000648 handshake->tls_prf = tls_prf_sha256;
649 handshake->calc_verify = ssl_calc_verify_tls_sha256;
650 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200652 else
653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200658 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000659
660 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * SSLv3:
662 * master =
663 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
664 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
665 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200666 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200667 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 * master = PRF( premaster, "master secret", randbytes )[0..47]
669 */
Paul Bakker0a597072012-09-25 21:55:46 +0000670 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000673 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
676 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200677 {
678 unsigned char session_hash[48];
679 size_t hash_len;
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200682
683 ssl->handshake->calc_verify( ssl, session_hash );
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
686 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200689 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200691 {
692 hash_len = 48;
693 }
694 else
695#endif
696 hash_len = 32;
697 }
698 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200700 hash_len = 36;
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200703
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100704 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
705 "extended master secret",
706 session_hash, hash_len,
707 session->master, 48 );
708 if( ret != 0 )
709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100711 return( ret );
712 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200713
714 }
715 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200716#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100717 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
718 "master secret",
719 handshake->randbytes, 64,
720 session->master, 48 );
721 if( ret != 0 )
722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100724 return( ret );
725 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200726
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500727 mbedtls_platform_zeroize( handshake->premaster,
728 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 }
730 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 /*
734 * Swap the client and server random values.
735 */
Paul Bakker48916f92012-09-16 19:57:18 +0000736 memcpy( tmp, handshake->randbytes, 64 );
737 memcpy( handshake->randbytes, tmp + 32, 32 );
738 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500739 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741 /*
742 * SSLv3:
743 * key block =
744 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
745 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
746 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
747 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
748 * ...
749 *
750 * TLSv1:
751 * key block = PRF( master, "key expansion", randbytes )
752 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100753 ret = handshake->tls_prf( session->master, 48, "key expansion",
754 handshake->randbytes, 64, keyblk, 256 );
755 if( ret != 0 )
756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100758 return( ret );
759 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
762 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
763 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
764 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
765 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500767 mbedtls_platform_zeroize( handshake->randbytes,
768 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770 /*
771 * Determine the appropriate key, IV and MAC length.
772 */
Paul Bakker68884e32013-01-07 18:20:04 +0100773
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200774 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200777 cipher_info->mode == MBEDTLS_MODE_CCM ||
778 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200780 size_t taglen, explicit_ivlen;
781
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200782 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000783 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200784
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200785 /* All modes haves 96-bit IVs;
786 * GCM and CCM has 4 implicit and 8 explicit bytes
787 * ChachaPoly has all 12 bytes implicit
788 */
Paul Bakker68884e32013-01-07 18:20:04 +0100789 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200790 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
791 transform->fixed_ivlen = 12;
792 else
793 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200794
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200795 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
796 taglen = transform->ciphersuite_info->flags &
797 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
798
799
800 /* Minimum length of encrypted record */
801 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
802 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100803 }
804 else
805 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200806 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
808 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200811 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100812 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200814 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000815 mac_key_len = mbedtls_md_get_size( md_info );
816 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200819 /*
820 * If HMAC is to be truncated, we shall keep the leftmost bytes,
821 * (rfc 6066 page 13 or rfc 2104 section 4),
822 * so we only need to adjust the length here.
823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000827
828#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
829 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000830 * HMAC implementation which also truncates the key
831 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000832 mac_key_len = transform->maclen;
833#endif
834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200836
837 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100838 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200840 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200842 transform->minlen = transform->maclen;
843 else
Paul Bakker68884e32013-01-07 18:20:04 +0100844 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200845 /*
846 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100847 * 1. if EtM is in use: one block plus MAC
848 * otherwise: * first multiple of blocklen greater than maclen
849 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
852 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100853 {
854 transform->minlen = transform->maclen
855 + cipher_info->block_size;
856 }
857 else
858#endif
859 {
860 transform->minlen = transform->maclen
861 + cipher_info->block_size
862 - transform->maclen % cipher_info->block_size;
863 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
867 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200868 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100869 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
872 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
873 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874 {
875 transform->minlen += transform->ivlen;
876 }
877 else
878#endif
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200882 }
Paul Bakker68884e32013-01-07 18:20:04 +0100883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 }
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000887 transform->keylen, transform->minlen, transform->ivlen,
888 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
890 /*
891 * Finally setup the cipher contexts, IVs and MAC secrets.
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200894 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000896 key1 = keyblk + mac_key_len * 2;
897 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Paul Bakker68884e32013-01-07 18:20:04 +0100899 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000900 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000902 /*
903 * This is not used in TLS v1.1.
904 */
Paul Bakker48916f92012-09-16 19:57:18 +0000905 iv_copy_len = ( transform->fixed_ivlen ) ?
906 transform->fixed_ivlen : transform->ivlen;
907 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
908 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000909 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 }
911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_SSL_CLI_C */
913#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000916 key1 = keyblk + mac_key_len * 2 + transform->keylen;
917 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Hanno Becker81c7b182017-11-09 18:39:33 +0000919 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100920 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000922 /*
923 * This is not used in TLS v1.1.
924 */
Paul Bakker48916f92012-09-16 19:57:18 +0000925 iv_copy_len = ( transform->fixed_ivlen ) ?
926 transform->fixed_ivlen : transform->ivlen;
927 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
928 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000929 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100931 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100936 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_SSL_PROTO_SSL3)
939 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100940 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000941 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
944 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100945 }
946
Hanno Becker81c7b182017-11-09 18:39:33 +0000947 memcpy( transform->mac_enc, mac_enc, mac_key_len );
948 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100949 }
950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#endif /* MBEDTLS_SSL_PROTO_SSL3 */
952#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
953 defined(MBEDTLS_SSL_PROTO_TLS1_2)
954 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100955 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100956 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
957 For AEAD-based ciphersuites, there is nothing to do here. */
958 if( mac_key_len != 0 )
959 {
960 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
961 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
962 }
Paul Bakker68884e32013-01-07 18:20:04 +0100963 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200964 else
965#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200969 }
Paul Bakker68884e32013-01-07 18:20:04 +0100970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
972 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000973 {
974 int ret = 0;
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100979 transform->iv_enc, transform->iv_dec,
980 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100981 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000982 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
985 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000986 }
987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000989
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200990#if defined(MBEDTLS_SSL_EXPORT_KEYS)
991 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100992 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200993 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
994 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100996 iv_copy_len );
997 }
998#endif
999
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001000 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001001 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001004 return( ret );
1005 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001006
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001007 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001008 cipher_info ) ) != 0 )
1009 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 return( ret );
1012 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001015 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001019 return( ret );
1020 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001023 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001027 return( ret );
1028 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_CIPHER_MODE_CBC)
1031 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1034 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001037 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001038 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1041 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001044 return( ret );
1045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001049 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001052 // Initialize compression
1053 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001055 {
Paul Bakker16770332013-10-11 09:59:44 +02001056 if( ssl->compress_buf == NULL )
1057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001059 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001060 if( ssl->compress_buf == NULL )
1061 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001063 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001064 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001065 }
1066 }
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001069
Paul Bakker48916f92012-09-16 19:57:18 +00001070 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1071 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001072
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001073 if( deflateInit( &transform->ctx_deflate,
1074 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001075 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1078 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001079 }
1080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
1085 return( 0 );
1086}
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SSL_PROTO_SSL3)
1089void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001090{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001091 mbedtls_md5_context md5;
1092 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 unsigned char pad_1[48];
1094 unsigned char pad_2[48];
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001098 mbedtls_md5_init( &md5 );
1099 mbedtls_sha1_init( &sha1 );
1100
1101 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1102 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Paul Bakker380da532012-04-18 16:10:25 +00001104 memset( pad_1, 0x36, 48 );
1105 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001107 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1108 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1109 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001111 mbedtls_md5_starts_ret( &md5 );
1112 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1113 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1114 mbedtls_md5_update_ret( &md5, hash, 16 );
1115 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001117 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1118 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1119 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001120
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_sha1_starts_ret( &sha1 );
1122 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1123 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1124 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1125 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001129
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001130 mbedtls_md5_free( &md5 );
1131 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001132
Paul Bakker380da532012-04-18 16:10:25 +00001133 return;
1134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1138void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001139{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001140 mbedtls_md5_context md5;
1141 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001144
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001145 mbedtls_md5_init( &md5 );
1146 mbedtls_sha1_init( &sha1 );
1147
1148 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1149 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001150
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001151 mbedtls_md5_finish_ret( &md5, hash );
1152 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001156
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001157 mbedtls_md5_free( &md5 );
1158 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001159
Paul Bakker380da532012-04-18 16:10:25 +00001160 return;
1161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1165#if defined(MBEDTLS_SHA256_C)
1166void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001167{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001168 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001169
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001170 mbedtls_sha256_init( &sha256 );
1171
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001174 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001175 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001179
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001180 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001181
Paul Bakker380da532012-04-18 16:10:25 +00001182 return;
1183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_SHA512_C)
1187void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001188{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001189 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001190
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001191 mbedtls_sha512_init( &sha512 );
1192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001195 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001196 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001201 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 return;
1204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_SHA512_C */
1206#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1209int 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 +02001210{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001211 unsigned char *p = ssl->handshake->premaster;
1212 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001213 const unsigned char *psk = ssl->conf->psk;
1214 size_t psk_len = ssl->conf->psk_len;
1215
1216 /* If the psk callback was called, use its result */
1217 if( ssl->handshake->psk != NULL )
1218 {
1219 psk = ssl->handshake->psk;
1220 psk_len = ssl->handshake->psk_len;
1221 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001222
1223 /*
1224 * PMS = struct {
1225 * opaque other_secret<0..2^16-1>;
1226 * opaque psk<0..2^16-1>;
1227 * };
1228 * with "other_secret" depending on the particular key exchange
1229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1231 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001233 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001235
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001236 *(p++) = (unsigned char)( psk_len >> 8 );
1237 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001238
1239 if( end < p || (size_t)( end - p ) < psk_len )
1240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1241
1242 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001243 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001244 }
1245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1247#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1248 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001249 {
1250 /*
1251 * other_secret already set by the ClientKeyExchange message,
1252 * and is 48 bytes long
1253 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001254 if( end - p < 2 )
1255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1256
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001257 *p++ = 0;
1258 *p++ = 48;
1259 p += 48;
1260 }
1261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1263#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1264 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001266 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001267 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001268
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001269 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001271 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001272 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001275 return( ret );
1276 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001277 *(p++) = (unsigned char)( len >> 8 );
1278 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 p += len;
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001282 }
1283 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1285#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1286 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001287 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001288 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001289 size_t zlen;
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001292 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001293 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001296 return( ret );
1297 }
1298
1299 *(p++) = (unsigned char)( zlen >> 8 );
1300 *(p++) = (unsigned char)( zlen );
1301 p += zlen;
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 }
1305 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001310 }
1311
1312 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001313 if( end - p < 2 )
1314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001315
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001316 *(p++) = (unsigned char)( psk_len >> 8 );
1317 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001318
1319 if( end < p || (size_t)( end - p ) < psk_len )
1320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1321
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001322 memcpy( p, psk, psk_len );
1323 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001324
1325 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1326
1327 return( 0 );
1328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001332/*
1333 * SSLv3.0 MAC functions
1334 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001335#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001336static void ssl_mac( mbedtls_md_context_t *md_ctx,
1337 const unsigned char *secret,
1338 const unsigned char *buf, size_t len,
1339 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001340 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341{
1342 unsigned char header[11];
1343 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001344 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1346 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001347
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001348 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001350 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001351 else
Paul Bakker68884e32013-01-07 18:20:04 +01001352 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 memcpy( header, ctr, 8 );
1355 header[ 8] = (unsigned char) type;
1356 header[ 9] = (unsigned char)( len >> 8 );
1357 header[10] = (unsigned char)( len );
1358
Paul Bakker68884e32013-01-07 18:20:04 +01001359 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_md_starts( md_ctx );
1361 mbedtls_md_update( md_ctx, secret, md_size );
1362 mbedtls_md_update( md_ctx, padding, padlen );
1363 mbedtls_md_update( md_ctx, header, 11 );
1364 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001365 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Paul Bakker68884e32013-01-07 18:20:04 +01001367 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_md_starts( md_ctx );
1369 mbedtls_md_update( md_ctx, secret, md_size );
1370 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001371 mbedtls_md_update( md_ctx, out, md_size );
1372 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1377 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001378 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001379#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001380#endif
1381
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001382/* The function below is only used in the Lucky 13 counter-measure in
1383 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1384#if defined(SSL_SOME_MODES_USE_MAC) && \
1385 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1386 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1387 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1388/* This function makes sure every byte in the memory region is accessed
1389 * (in ascending addresses order) */
1390static void ssl_read_memory( unsigned char *p, size_t len )
1391{
1392 unsigned char acc = 0;
1393 volatile unsigned char force;
1394
1395 for( ; len != 0; p++, len-- )
1396 acc ^= *p;
1397
1398 force = acc;
1399 (void) force;
1400}
1401#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1402
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001403/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001409 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001413 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001417 }
1418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001422 ssl->out_msg, ssl->out_msglen );
1423
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001425 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001427#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 if( mode == MBEDTLS_MODE_STREAM ||
1429 ( mode == MBEDTLS_MODE_CBC
1430#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1431 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001432#endif
1433 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_SSL_PROTO_SSL3)
1436 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001437 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001438 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001439
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001440 ssl_mac( &ssl->transform_out->md_ctx_enc,
1441 ssl->transform_out->mac_enc,
1442 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001443 ssl->out_ctr, ssl->out_msgtype,
1444 mac );
1445
1446 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001447 }
1448 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1451 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1452 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001453 {
Hanno Becker992b6872017-11-09 18:57:39 +00001454 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1457 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1458 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1459 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001460 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001461 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001463
1464 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 }
1466 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001467#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 }
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001474 ssl->out_msg + ssl->out_msglen,
1475 ssl->transform_out->maclen );
1476
1477 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001478 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001479 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001480#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001482 /*
1483 * Encrypt
1484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1486 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001488 int ret;
1489 size_t olen = 0;
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 "including %d bytes of padding",
1493 ssl->out_msglen, 0 ) );
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001496 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001497 ssl->transform_out->ivlen,
1498 ssl->out_msg, ssl->out_msglen,
1499 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001502 return( ret );
1503 }
1504
1505 if( ssl->out_msglen != olen )
1506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 }
Paul Bakker68884e32013-01-07 18:20:04 +01001511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001513#if defined(MBEDTLS_GCM_C) || \
1514 defined(MBEDTLS_CCM_C) || \
1515 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001517 mode == MBEDTLS_MODE_CCM ||
1518 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001519 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001520 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001521 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001522 unsigned char *enc_msg;
1523 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001524 unsigned char iv[12];
1525 mbedtls_ssl_transform *transform = ssl->transform_out;
1526 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001528 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001530 /*
1531 * Prepare additional authenticated data
1532 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533 memcpy( add_data, ssl->out_ctr, 8 );
1534 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001536 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1538 add_data[12] = ssl->out_msglen & 0xFF;
1539
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001540 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541
Paul Bakker68884e32013-01-07 18:20:04 +01001542 /*
1543 * Generate IV
1544 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001545 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1546 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001547 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1549 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1550 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1551
1552 }
1553 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1554 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001555 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001556 unsigned char i;
1557
1558 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1559
1560 for( i = 0; i < 8; i++ )
1561 iv[i+4] ^= ssl->out_ctr[i];
1562 }
1563 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001564 {
1565 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001568 }
1569
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001570 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1571 iv, transform->ivlen );
1572 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1573 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001574
Paul Bakker68884e32013-01-07 18:20:04 +01001575 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001576 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001577 */
1578 enc_msg = ssl->out_msg;
1579 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001583 "including 0 bytes of padding",
1584 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Paul Bakker68884e32013-01-07 18:20:04 +01001586 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001587 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001588 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001589 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1590 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 add_data, 13,
1592 enc_msg, enc_msglen,
1593 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001594 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001597 return( ret );
1598 }
1599
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001600 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001604 }
1605
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001606 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001607 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1613#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001614 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001617 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001619 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001620
Paul Bakker48916f92012-09-16 19:57:18 +00001621 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1622 ssl->transform_out->ivlen;
1623 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 padlen = 0;
1625
1626 for( i = 0; i <= padlen; i++ )
1627 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1628
1629 ssl->out_msglen += padlen + 1;
1630
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631 enc_msglen = ssl->out_msglen;
1632 enc_msg = ssl->out_msg;
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001636 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1637 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640 {
1641 /*
1642 * Generate IV
1643 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001644 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001645 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001646 if( ret != 0 )
1647 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648
Paul Bakker92be97b2013-01-02 17:30:03 +01001649 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001650 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651
1652 /*
1653 * Fix pointer positions and message length with added IV
1654 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001655 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001657 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001662 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001663 ssl->out_msglen, ssl->transform_out->ivlen,
1664 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001667 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001668 ssl->transform_out->ivlen,
1669 enc_msg, enc_msglen,
1670 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001673 return( ret );
1674 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001675
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 if( enc_msglen != olen )
1677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1683 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001684 {
1685 /*
1686 * Save IV in SSL3 and TLS1
1687 */
1688 memcpy( ssl->transform_out->iv_enc,
1689 ssl->transform_out->cipher_ctx_enc.iv,
1690 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001692#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001695 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001696 {
1697 /*
1698 * MAC(MAC_write_key, seq_num +
1699 * TLSCipherText.type +
1700 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001701 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001702 * IV + // except for TLS 1.0
1703 * ENC(content + padding + padding_length));
1704 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001705 unsigned char pseudo_hdr[13];
1706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001708
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1710 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001711 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1712 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1717 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001718 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001720 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001722
1723 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001724 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001730 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001734 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001736 /* Make extra sure authentication was performed, exactly once */
1737 if( auth_done != 1 )
1738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 return( 0 );
1746}
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001752#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001753 size_t padlen = 0, correct = 1;
1754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 }
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765
Paul Bakker48916f92012-09-16 19:57:18 +00001766 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001769 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1774 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001775 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001776 int ret;
1777 size_t olen = 0;
1778
Paul Bakker68884e32013-01-07 18:20:04 +01001779 padlen = 0;
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001782 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001783 ssl->transform_in->ivlen,
1784 ssl->in_msg, ssl->in_msglen,
1785 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001788 return( ret );
1789 }
1790
1791 if( ssl->in_msglen != olen )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 }
Paul Bakker68884e32013-01-07 18:20:04 +01001797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001799#if defined(MBEDTLS_GCM_C) || \
1800 defined(MBEDTLS_CCM_C) || \
1801 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803 mode == MBEDTLS_MODE_CCM ||
1804 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001805 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001806 int ret;
1807 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001808 unsigned char *dec_msg;
1809 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001810 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001811 unsigned char iv[12];
1812 mbedtls_ssl_transform *transform = ssl->transform_in;
1813 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001816
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001817 /*
1818 * Compute and update sizes
1819 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001820 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001823 "+ taglen (%d)", ssl->in_msglen,
1824 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001826 }
1827 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1828
Paul Bakker68884e32013-01-07 18:20:04 +01001829 dec_msg = ssl->in_msg;
1830 dec_msg_result = ssl->in_msg;
1831 ssl->in_msglen = dec_msglen;
1832
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001833 /*
1834 * Prepare additional authenticated data
1835 */
Paul Bakker68884e32013-01-07 18:20:04 +01001836 memcpy( add_data, ssl->in_ctr, 8 );
1837 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001839 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001840 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1841 add_data[12] = ssl->in_msglen & 0xFF;
1842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001844
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 /*
1846 * Prepare IV
1847 */
1848 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1849 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001850 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1852 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001853
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 }
1855 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1856 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001857 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001858 unsigned char i;
1859
1860 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1861
1862 for( i = 0; i < 8; i++ )
1863 iv[i+4] ^= ssl->in_ctr[i];
1864 }
1865 else
1866 {
1867 /* Reminder if we ever add an AEAD mode with a different size */
1868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1870 }
1871
1872 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001874
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001875 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001876 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001880 add_data, 13,
1881 dec_msg, dec_msglen,
1882 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001883 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1888 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001889
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001890 return( ret );
1891 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001892 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001893
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001894 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001898 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1902#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001903 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 {
Paul Bakker45829992013-01-03 14:52:21 +01001906 /*
1907 * Decrypt and check the padding
1908 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001909 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001910 unsigned char *dec_msg;
1911 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001912 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001913 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001914 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 /*
Paul Bakker45829992013-01-03 14:52:21 +01001917 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1920 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001921 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001922#endif
Paul Bakker45829992013-01-03 14:52:21 +01001923
1924 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1925 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001928 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1929 ssl->transform_in->ivlen,
1930 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001932 }
1933
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001934 dec_msglen = ssl->in_msglen;
1935 dec_msg = ssl->in_msg;
1936 dec_msg_result = ssl->in_msg;
1937
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001938 /*
1939 * Authenticate before decrypt if enabled
1940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1942 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001943 {
Hanno Becker992b6872017-11-09 18:57:39 +00001944 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001945 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001948
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001949 dec_msglen -= ssl->transform_in->maclen;
1950 ssl->in_msglen -= ssl->transform_in->maclen;
1951
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001952 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1953 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1954 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1955 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1960 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001962 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001967 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 ssl->transform_in->maclen );
1969
Hanno Becker992b6872017-11-09 18:57:39 +00001970 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1971 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001977 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980
1981 /*
1982 * Check length sanity
1983 */
1984 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001989 }
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001992 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001993 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001997 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001998 dec_msglen -= ssl->transform_in->ivlen;
1999 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000
Paul Bakker48916f92012-09-16 19:57:18 +00002001 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002002 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002007 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002008 ssl->transform_in->ivlen,
2009 dec_msg, dec_msglen,
2010 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002013 return( ret );
2014 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002015
Paul Bakkercca5b812013-08-31 17:40:26 +02002016 if( dec_msglen != olen )
2017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002020 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2023 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 {
2025 /*
2026 * Save IV in SSL3 and TLS1
2027 */
2028 memcpy( ssl->transform_in->iv_dec,
2029 ssl->transform_in->cipher_ctx_dec.iv,
2030 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002035
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002036 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002037 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_SSL_DEBUG_ALL)
2040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002041 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002042#endif
Paul Bakker45829992013-01-03 14:52:21 +01002043 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002044 correct = 0;
2045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#if defined(MBEDTLS_SSL_PROTO_SSL3)
2048 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 {
Paul Bakker48916f92012-09-16 19:57:18 +00002050 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_DEBUG_ALL)
2053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002055 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002056#endif
Paul Bakker45829992013-01-03 14:52:21 +01002057 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059 }
2060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2063 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2064 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
2066 /*
Paul Bakker45829992013-01-03 14:52:21 +01002067 * TLSv1+: always check the padding up to the first failure
2068 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002070 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002071 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002073
Paul Bakker956c9e02013-12-19 14:42:28 +01002074 /*
2075 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002076 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002077 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002078 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002079 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002080 *
2081 * In both cases we reset padding_idx to a safe value (0) to
2082 * prevent out-of-buffer reads.
2083 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002084 correct &= ( padlen <= ssl->in_msglen );
2085 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002086 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002087
2088 padding_idx *= correct;
2089
Angus Grattonb512bc12018-06-19 15:57:50 +10002090 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002091 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002092 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002093 pad_count += real_count *
2094 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2095 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002096
2097 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002100 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002102#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002103 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2107 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002111 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002112
2113 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002117 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002123#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
2128 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002129 * Authenticate if not done yet.
2130 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002132#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002134 {
Hanno Becker992b6872017-11-09 18:57:39 +00002135 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002136
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002137 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002139 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2140 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#if defined(MBEDTLS_SSL_PROTO_SSL3)
2143 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002144 {
2145 ssl_mac( &ssl->transform_in->md_ctx_dec,
2146 ssl->transform_in->mac_dec,
2147 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002148 ssl->in_ctr, ssl->in_msgtype,
2149 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002150 }
2151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2153#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2154 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2155 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002156 {
2157 /*
2158 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002159 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 *
2161 * Known timing attacks:
2162 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2163 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002164 * To compensate for different timings for the MAC calculation
2165 * depending on how much padding was removed (which is determined
2166 * by padlen), process extra_run more blocks through the hash
2167 * function.
2168 *
2169 * The formula in the paper is
2170 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2171 * where L1 is the size of the header plus the decrypted message
2172 * plus CBC padding and L2 is the size of the header plus the
2173 * decrypted message. This is for an underlying hash function
2174 * with 64-byte blocks.
2175 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2176 * correctly. We round down instead of up, so -56 is the correct
2177 * value for our calculations instead of -55.
2178 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002179 * Repeat the formula rather than defining a block_size variable.
2180 * This avoids requiring division by a variable at runtime
2181 * (which would be marginally less efficient and would require
2182 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002183 */
2184 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002185
2186 /*
2187 * The next two sizes are the minimum and maximum values of
2188 * in_msglen over all padlen values.
2189 *
2190 * They're independent of padlen, since we previously did
2191 * in_msglen -= padlen.
2192 *
2193 * Note that max_len + maclen is never more than the buffer
2194 * length, as we previously did in_msglen -= maclen too.
2195 */
2196 const size_t max_len = ssl->in_msglen + padlen;
2197 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2198
Gilles Peskine20b44082018-05-29 14:06:49 +02002199 switch( ssl->transform_in->ciphersuite_info->mac )
2200 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002201#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2202 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 case MBEDTLS_MD_MD5:
2204 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002205 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002206 /* 8 bytes of message size, 64-byte compression blocks */
2207 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2208 ( 13 + ssl->in_msglen + 8 ) / 64;
2209 break;
2210#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002211#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002212 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002213 /* 16 bytes of message size, 128-byte compression blocks */
2214 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2215 ( 13 + ssl->in_msglen + 16 ) / 128;
2216 break;
2217#endif
2218 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2221 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002222
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2226 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2227 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2228 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002229 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002230 /* Make sure we access everything even when padlen > 0. This
2231 * makes the synchronisation requirements for just-in-time
2232 * Prime+Probe attacks much tighter and hopefully impractical. */
2233 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002234 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002235
2236 /* Call mbedtls_md_process at least once due to cache attacks
2237 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002238 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002242
2243 /* Make sure we access all the memory that could contain the MAC,
2244 * before we check it in the next code block. This makes the
2245 * synchronisation requirements for just-in-time Prime+Probe
2246 * attacks much tighter and hopefully impractical. */
2247 ssl_read_memory( ssl->in_msg + min_len,
2248 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002249 }
2250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2252 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002258#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002259 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2260 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2261 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Hanno Becker992b6872017-11-09 18:57:39 +00002264 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2265 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#if defined(MBEDTLS_SSL_DEBUG_ALL)
2268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002269#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002270 correct = 0;
2271 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002272 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002274 /*
2275 * Finally check the correct flag
2276 */
2277 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002279 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002280#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281
2282 /* Make extra sure authentication was performed, exactly once */
2283 if( auth_done != 1 )
2284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
2289 if( ssl->in_msglen == 0 )
2290 {
Angus Gratton34817922018-06-19 15:58:22 +10002291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2292 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2293 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2294 {
2295 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2298 }
2299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2300
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 ssl->nb_zero++;
2302
2303 /*
2304 * Three or more empty messages may be a DoS attack
2305 * (excessive CPU consumption).
2306 */
2307 if( ssl->nb_zero > 3 )
2308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
2313 }
2314 else
2315 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002319 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002320 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002321 }
2322 else
2323#endif
2324 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002326 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2327 if( ++ssl->in_ctr[i - 1] != 0 )
2328 break;
2329
2330 /* The loop goes to its end iff the counter is wrapping */
2331 if( i == ssl_ep_len( ssl ) )
2332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2334 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002335 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 return( 0 );
2341}
2342
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002343#undef MAC_NONE
2344#undef MAC_PLAINTEXT
2345#undef MAC_CIPHERTEXT
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348/*
2349 * Compression/decompression functions
2350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352{
2353 int ret;
2354 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002355 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002357 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002361 if( len_pre == 0 )
2362 return( 0 );
2363
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364 memcpy( msg_pre, ssl->out_msg, len_pre );
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002367 ssl->out_msglen ) );
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370 ssl->out_msg, ssl->out_msglen );
2371
Paul Bakker48916f92012-09-16 19:57:18 +00002372 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2373 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2374 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002375 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376
Paul Bakker48916f92012-09-16 19:57:18 +00002377 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378 if( ret != Z_OK )
2379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2381 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 }
2383
Angus Grattond8213d02016-05-25 20:56:48 +10002384 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002385 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 ssl->out_msglen ) );
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391 ssl->out_msg, ssl->out_msglen );
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
2395 return( 0 );
2396}
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399{
2400 int ret;
2401 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002402 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002404 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002407
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002408 if( len_pre == 0 )
2409 return( 0 );
2410
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411 memcpy( msg_pre, ssl->in_msg, len_pre );
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002414 ssl->in_msglen ) );
2415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 ssl->in_msg, ssl->in_msglen );
2418
Paul Bakker48916f92012-09-16 19:57:18 +00002419 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2420 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2421 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002422 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002423 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424
Paul Bakker48916f92012-09-16 19:57:18 +00002425 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426 if( ret != Z_OK )
2427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2429 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 }
2431
Angus Grattond8213d02016-05-25 20:56:48 +10002432 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002433 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436 ssl->in_msglen ) );
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002439 ssl->in_msg, ssl->in_msglen );
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442
2443 return( 0 );
2444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2448static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SSL_PROTO_DTLS)
2451static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002452{
2453 /* If renegotiation is not enforced, retransmit until we would reach max
2454 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002455 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002456 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002457 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002458 unsigned char doublings = 1;
2459
2460 while( ratio != 0 )
2461 {
2462 ++doublings;
2463 ratio >>= 1;
2464 }
2465
2466 if( ++ssl->renego_records_seen > doublings )
2467 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002469 return( 0 );
2470 }
2471 }
2472
2473 return( ssl_write_hello_request( ssl ) );
2474}
2475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002477
Paul Bakker5121ce52009-01-03 21:22:43 +00002478/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002479 * Fill the input message buffer by appending data to it.
2480 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002481 *
2482 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2483 * available (from this read and/or a previous one). Otherwise, an error code
2484 * is returned (possibly EOF or WANT_READ).
2485 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002486 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2487 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2488 * since we always read a whole datagram at once.
2489 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002490 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002491 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002494{
Paul Bakker23986e52011-04-24 08:57:21 +00002495 int ret;
2496 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002500 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002503 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002505 }
2506
Angus Grattond8213d02016-05-25 20:56:48 +10002507 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002511 }
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002516 uint32_t timeout;
2517
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002518 /* Just to be sure */
2519 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2520 {
2521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2522 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2524 }
2525
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002526 /*
2527 * The point is, we need to always read a full datagram at once, so we
2528 * sometimes read more then requested, and handle the additional data.
2529 * It could be the rest of the current record (while fetching the
2530 * header) and/or some other records in the same datagram.
2531 */
2532
2533 /*
2534 * Move to the next record in the already read datagram if applicable
2535 */
2536 if( ssl->next_record_offset != 0 )
2537 {
2538 if( ssl->in_left < ssl->next_record_offset )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002542 }
2543
2544 ssl->in_left -= ssl->next_record_offset;
2545
2546 if( ssl->in_left != 0 )
2547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002549 ssl->next_record_offset ) );
2550 memmove( ssl->in_hdr,
2551 ssl->in_hdr + ssl->next_record_offset,
2552 ssl->in_left );
2553 }
2554
2555 ssl->next_record_offset = 0;
2556 }
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002560
2561 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002562 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002563 */
2564 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002567 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002568 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002569
2570 /*
2571 * A record can't be split accross datagrams. If we need to read but
2572 * are not at the beginning of a new record, the caller did something
2573 * wrong.
2574 */
2575 if( ssl->in_left != 0 )
2576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002579 }
2580
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002581 /*
2582 * Don't even try to read if time's out already.
2583 * This avoids by-passing the timer when repeatedly receiving messages
2584 * that will end up being dropped.
2585 */
2586 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002587 {
2588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002589 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002590 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002591 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002592 {
Angus Grattond8213d02016-05-25 20:56:48 +10002593 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002596 timeout = ssl->handshake->retransmit_timeout;
2597 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002598 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002601
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002602 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002603 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2604 timeout );
2605 else
2606 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609
2610 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002612 }
2613
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002614 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002617 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002620 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002621 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002624 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 }
2626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002630 return( ret );
2631 }
2632
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002633 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002636 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002638 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002639 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002642 return( ret );
2643 }
2644
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002645 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002648 }
2649
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 if( ret < 0 )
2651 return( ret );
2652
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002653 ssl->in_left = ret;
2654 }
2655 else
2656#endif
2657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002659 ssl->in_left, nb_want ) );
2660
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002661 while( ssl->in_left < nb_want )
2662 {
2663 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002664
2665 if( ssl_check_timer( ssl ) != 0 )
2666 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2667 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002668 {
2669 if( ssl->f_recv_timeout != NULL )
2670 {
2671 ret = ssl->f_recv_timeout( ssl->p_bio,
2672 ssl->in_hdr + ssl->in_left, len,
2673 ssl->conf->read_timeout );
2674 }
2675 else
2676 {
2677 ret = ssl->f_recv( ssl->p_bio,
2678 ssl->in_hdr + ssl->in_left, len );
2679 }
2680 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002683 ssl->in_left, nb_want ) );
2684 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685
2686 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002688
2689 if( ret < 0 )
2690 return( ret );
2691
mohammad160352aecb92018-03-28 23:41:40 -07002692 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002693 {
Darryl Green11999bb2018-03-13 15:22:58 +00002694 MBEDTLS_SSL_DEBUG_MSG( 1,
2695 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002696 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2698 }
2699
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002700 ssl->in_left += ret;
2701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706 return( 0 );
2707}
2708
2709/*
2710 * Flush any data not yet written
2711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002714 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002715 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002719 if( ssl->f_send == NULL )
2720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002722 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002724 }
2725
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002726 /* Avoid incrementing counter if data is flushed */
2727 if( ssl->out_left == 0 )
2728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002730 return( 0 );
2731 }
2732
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 while( ssl->out_left > 0 )
2734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2736 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Hanno Becker2b1e3542018-08-06 11:19:13 +01002738 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002739 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
2743 if( ret <= 0 )
2744 return( ret );
2745
mohammad160352aecb92018-03-28 23:41:40 -07002746 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002747 {
Darryl Green11999bb2018-03-13 15:22:58 +00002748 MBEDTLS_SSL_DEBUG_MSG( 1,
2749 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002750 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2752 }
2753
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 ssl->out_left -= ret;
2755 }
2756
Hanno Becker2b1e3542018-08-06 11:19:13 +01002757#if defined(MBEDTLS_SSL_PROTO_DTLS)
2758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2759 {
2760 ssl->out_hdr = ssl->out_buf;
2761 }
2762 else
2763#endif
2764 {
2765 ssl->out_hdr = ssl->out_buf + 8;
2766 }
2767 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
2771 return( 0 );
2772}
2773
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002774/*
2775 * Functions to handle the DTLS retransmission state machine
2776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002778/*
2779 * Append current handshake message to current outgoing flight
2780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2785 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2786 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002787
2788 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002789 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002790 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794 }
2795
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002796 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002797 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002800 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801 }
2802
2803 /* Copy current handshake message with headers */
2804 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2805 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002806 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002807 msg->next = NULL;
2808
2809 /* Append to the current flight */
2810 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002811 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812 else
2813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815 while( cur->next != NULL )
2816 cur = cur->next;
2817 cur->next = msg;
2818 }
2819
Hanno Becker3b235902018-08-06 09:54:53 +01002820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821 return( 0 );
2822}
2823
2824/*
2825 * Free the current flight of handshake messages
2826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_ssl_flight_item *cur = flight;
2830 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831
2832 while( cur != NULL )
2833 {
2834 next = cur->next;
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 mbedtls_free( cur->p );
2837 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838
2839 cur = next;
2840 }
2841}
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2844static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002845#endif
2846
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002847/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848 * Swap transform_out and out_ctr with the alternative ones
2849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002853 unsigned char tmp_out_ctr[8];
2854
2855 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002858 return;
2859 }
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002862
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002863 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002864 tmp_transform = ssl->transform_out;
2865 ssl->transform_out = ssl->handshake->alt_transform_out;
2866 ssl->handshake->alt_transform_out = tmp_transform;
2867
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002868 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002869 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2870 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002871 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872
2873 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002874 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2877 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002883 }
2884 }
2885#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002886}
2887
2888/*
2889 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002890 */
2891int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2892{
2893 int ret = 0;
2894
2895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2896
2897 ret = mbedtls_ssl_flight_transmit( ssl );
2898
2899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2900
2901 return( ret );
2902}
2903
2904/*
2905 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002906 *
2907 * Need to remember the current message in case flush_output returns
2908 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002909 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002910 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002911int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002912{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002913 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002917 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002919
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002920 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002921 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002922 ssl_swap_epochs( ssl );
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002925 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002926
2927 while( ssl->handshake->cur_msg != NULL )
2928 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002929 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002930 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002931
Hanno Beckere1dcb032018-08-17 16:47:58 +01002932 int const is_finished =
2933 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2934 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2935
Hanno Becker04da1892018-08-14 13:22:10 +01002936 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2937 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2938
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002939 /* Swap epochs before sending Finished: we can't do it after
2940 * sending ChangeCipherSpec, in case write returns WANT_READ.
2941 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002942 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002943 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002945 ssl_swap_epochs( ssl );
2946 }
2947
Hanno Becker67bc7c32018-08-06 11:33:50 +01002948 ret = ssl_get_remaining_payload_in_datagram( ssl );
2949 if( ret < 0 )
2950 return( ret );
2951 max_frag_len = (size_t) ret;
2952
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002953 /* CCS is copied as is, while HS messages may need fragmentation */
2954 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2955 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002956 if( max_frag_len == 0 )
2957 {
2958 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2959 return( ret );
2960
2961 continue;
2962 }
2963
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002964 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002965 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002966 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002967
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002968 /* Update position inside current message */
2969 ssl->handshake->cur_msg_p += cur->len;
2970 }
2971 else
2972 {
2973 const unsigned char * const p = ssl->handshake->cur_msg_p;
2974 const size_t hs_len = cur->len - 12;
2975 const size_t frag_off = p - ( cur->p + 12 );
2976 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002977 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002978
Hanno Beckere1dcb032018-08-17 16:47:58 +01002979 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002980 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002981 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002982 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002983
2984 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2985 return( ret );
2986
2987 continue;
2988 }
2989 max_hs_frag_len = max_frag_len - 12;
2990
2991 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2992 max_hs_frag_len : rem_len;
2993
2994 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002995 {
2996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002997 (unsigned) cur_hs_frag_len,
2998 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002999 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003000
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003001 /* Messages are stored with handshake headers as if not fragmented,
3002 * copy beginning of headers then fill fragmentation fields.
3003 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3004 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003005
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003006 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3007 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3008 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3009
Hanno Becker67bc7c32018-08-06 11:33:50 +01003010 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3011 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3012 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003013
3014 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3015
Hanno Becker67bc7c32018-08-06 11:33:50 +01003016 /* Copy the handshame message content and set records fields */
3017 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3018 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003019 ssl->out_msgtype = cur->type;
3020
3021 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003022 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003023 }
3024
3025 /* If done with the current message move to the next one if any */
3026 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3027 {
3028 if( cur->next != NULL )
3029 {
3030 ssl->handshake->cur_msg = cur->next;
3031 ssl->handshake->cur_msg_p = cur->next->p + 12;
3032 }
3033 else
3034 {
3035 ssl->handshake->cur_msg = NULL;
3036 ssl->handshake->cur_msg_p = NULL;
3037 }
3038 }
3039
3040 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003041 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003044 return( ret );
3045 }
3046 }
3047
Hanno Becker67bc7c32018-08-06 11:33:50 +01003048 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3049 return( ret );
3050
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003051 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3053 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003054 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003057 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3058 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003059
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061
3062 return( 0 );
3063}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003064
3065/*
3066 * To be called when the last message of an incoming flight is received.
3067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003069{
3070 /* We won't need to resend that one any more */
3071 ssl_flight_free( ssl->handshake->flight );
3072 ssl->handshake->flight = NULL;
3073 ssl->handshake->cur_msg = NULL;
3074
3075 /* The next incoming flight will start with this msg_seq */
3076 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3077
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003078 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003079 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003080
Hanno Becker0271f962018-08-16 13:23:47 +01003081 /* Clear future message buffering structure. */
3082 ssl_buffering_free( ssl );
3083
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003084 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003085 ssl_set_timer( ssl, 0 );
3086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3088 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003091 }
3092 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003094}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003095
3096/*
3097 * To be called when the last message of an outgoing flight is send.
3098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003100{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003101 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003102 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3105 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003108 }
3109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003113
Paul Bakker5121ce52009-01-03 21:22:43 +00003114/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003115 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003117
3118/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003119 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003120 *
3121 * - fill in handshake headers
3122 * - update handshake checksum
3123 * - DTLS: save message for resending
3124 * - then pass to the record layer
3125 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003126 * DTLS: except for HelloRequest, messages are only queued, and will only be
3127 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003128 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003129 * Inputs:
3130 * - ssl->out_msglen: 4 + actual handshake message len
3131 * (4 is the size of handshake headers for TLS)
3132 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3133 * - ssl->out_msg + 4: the handshake message body
3134 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003135 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003136 * - ssl->out_msglen: the length of the record contents
3137 * (including handshake headers but excluding record headers)
3138 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003140int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003141{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003142 int ret;
3143 const size_t hs_len = ssl->out_msglen - 4;
3144 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3147
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003148 /*
3149 * Sanity checks
3150 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003151 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3152 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3153 {
3154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003157
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003158 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3159 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3160 ssl->handshake == NULL )
3161 {
3162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3164 }
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003168 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003174#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003175
Hanno Beckerb50a2532018-08-06 11:52:54 +01003176 /* Double-check that we did not exceed the bounds
3177 * of the outgoing record buffer.
3178 * This should never fail as the various message
3179 * writing functions must obey the bounds of the
3180 * outgoing record buffer, but better be safe.
3181 *
3182 * Note: We deliberately do not check for the MTU or MFL here.
3183 */
3184 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3185 {
3186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3187 "size %u, maximum %u",
3188 (unsigned) ssl->out_msglen,
3189 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3191 }
3192
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003193 /*
3194 * Fill handshake headers
3195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003198 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3199 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3200 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003202 /*
3203 * DTLS has additional fields in the Handshake layer,
3204 * between the length field and the actual payload:
3205 * uint16 message_seq;
3206 * uint24 fragment_offset;
3207 * uint24 fragment_length;
3208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003211 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003212 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003213 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003214 {
3215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3216 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003217 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003218 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3220 }
3221
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003222 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003223 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003224
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003225 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003226 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003227 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003228 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3229 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3230 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003231 }
3232 else
3233 {
3234 ssl->out_msg[4] = 0;
3235 ssl->out_msg[5] = 0;
3236 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003237
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003238 /* Handshake hashes are computed without fragmentation,
3239 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003240 memset( ssl->out_msg + 6, 0x00, 3 );
3241 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003242 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003244
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003245 /* Update running hashes of hanshake messages seen */
3246 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3247 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
3249
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003250 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003253 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003254 {
3255 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003258 return( ret );
3259 }
3260 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003261 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003263 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003264 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003265 {
3266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3267 return( ret );
3268 }
3269 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003270
3271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3272
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003273 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003274}
3275
3276/*
3277 * Record layer functions
3278 */
3279
3280/*
3281 * Write current record.
3282 *
3283 * Uses:
3284 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3285 * - ssl->out_msglen: length of the record content (excl headers)
3286 * - ssl->out_msg: record content
3287 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003288int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003289{
3290 int ret, done = 0;
3291 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003292 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003293
3294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003297 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003299 {
3300 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003303 return( ret );
3304 }
3305
3306 len = ssl->out_msglen;
3307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3311 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 ret = mbedtls_ssl_hw_record_write( ssl );
3316 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3319 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003320 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003321
3322 if( ret == 0 )
3323 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003326 if( !done )
3327 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003328 unsigned i;
3329 size_t protected_record_size;
3330
Paul Bakker05ef8352012-05-08 09:17:57 +00003331 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003333 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003334
Hanno Becker19859472018-08-06 09:40:20 +01003335 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003336 ssl->out_len[0] = (unsigned char)( len >> 8 );
3337 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003338
Paul Bakker48916f92012-09-16 19:57:18 +00003339 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003340 {
3341 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003344 return( ret );
3345 }
3346
3347 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003348 ssl->out_len[0] = (unsigned char)( len >> 8 );
3349 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003350 }
3351
Hanno Becker2b1e3542018-08-06 11:19:13 +01003352 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3353
3354#if defined(MBEDTLS_SSL_PROTO_DTLS)
3355 /* In case of DTLS, double-check that we don't exceed
3356 * the remaining space in the datagram. */
3357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3358 {
3359 ret = ssl_get_maximum_datagram_size( ssl );
3360 if( ret < 0 )
3361 return( ret );
3362
3363 if( protected_record_size > (size_t) ret )
3364 {
3365 /* Should never happen */
3366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3367 }
3368 }
3369#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003372 "version = [%d:%d], msglen = %d",
3373 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3374
Paul Bakker05ef8352012-05-08 09:17:57 +00003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003377 ssl->out_hdr, protected_record_size );
3378
3379 ssl->out_left += protected_record_size;
3380 ssl->out_hdr += protected_record_size;
3381 ssl_update_out_pointers( ssl, ssl->transform_out );
3382
Hanno Becker04484622018-08-06 09:49:38 +01003383 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3384 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3385 break;
3386
3387 /* The loop goes to its end iff the counter is wrapping */
3388 if( i == ssl_ep_len( ssl ) )
3389 {
3390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3391 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 }
3394
Hanno Becker67bc7c32018-08-06 11:33:50 +01003395#if defined(MBEDTLS_SSL_PROTO_DTLS)
3396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3397 {
3398 size_t remaining = ssl_get_remaining_payload_in_datagram( ssl );
3399 if( remaining == 0 )
3400 flush = SSL_FORCE_FLUSH;
3401 else
3402 {
3403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Stil %u bytes available in current datagram", (unsigned) remaining ) );
3404 }
3405 }
3406#endif /* MBEDTLS_SSL_PROTO_DTLS */
3407
3408 if( ( flush == SSL_FORCE_FLUSH ) &&
3409 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 return( ret );
3413 }
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
3417 return( 0 );
3418}
3419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003421
3422static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3423{
3424 if( ssl->in_msglen < ssl->in_hslen ||
3425 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3426 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3427 {
3428 return( 1 );
3429 }
3430 return( 0 );
3431}
Hanno Becker44650b72018-08-16 12:51:11 +01003432
3433static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3434{
3435 return( ( ssl->in_msg[9] << 16 ) |
3436 ( ssl->in_msg[10] << 8 ) |
3437 ssl->in_msg[11] );
3438}
3439
3440static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3441{
3442 return( ( ssl->in_msg[6] << 16 ) |
3443 ( ssl->in_msg[7] << 8 ) |
3444 ssl->in_msg[8] );
3445}
3446
3447static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3448{
3449 uint32_t msg_len, frag_off, frag_len;
3450
3451 msg_len = ssl_get_hs_total_len( ssl );
3452 frag_off = ssl_get_hs_frag_off( ssl );
3453 frag_len = ssl_get_hs_frag_len( ssl );
3454
3455 if( frag_off > msg_len )
3456 return( -1 );
3457
3458 if( frag_len > msg_len - frag_off )
3459 return( -1 );
3460
3461 if( frag_len + 12 > ssl->in_msglen )
3462 return( -1 );
3463
3464 return( 0 );
3465}
3466
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003467/*
3468 * Mark bits in bitmask (used for DTLS HS reassembly)
3469 */
3470static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3471{
3472 unsigned int start_bits, end_bits;
3473
3474 start_bits = 8 - ( offset % 8 );
3475 if( start_bits != 8 )
3476 {
3477 size_t first_byte_idx = offset / 8;
3478
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003479 /* Special case */
3480 if( len <= start_bits )
3481 {
3482 for( ; len != 0; len-- )
3483 mask[first_byte_idx] |= 1 << ( start_bits - len );
3484
3485 /* Avoid potential issues with offset or len becoming invalid */
3486 return;
3487 }
3488
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003489 offset += start_bits; /* Now offset % 8 == 0 */
3490 len -= start_bits;
3491
3492 for( ; start_bits != 0; start_bits-- )
3493 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3494 }
3495
3496 end_bits = len % 8;
3497 if( end_bits != 0 )
3498 {
3499 size_t last_byte_idx = ( offset + len ) / 8;
3500
3501 len -= end_bits; /* Now len % 8 == 0 */
3502
3503 for( ; end_bits != 0; end_bits-- )
3504 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3505 }
3506
3507 memset( mask + offset / 8, 0xFF, len / 8 );
3508}
3509
3510/*
3511 * Check that bitmask is full
3512 */
3513static int ssl_bitmask_check( unsigned char *mask, size_t len )
3514{
3515 size_t i;
3516
3517 for( i = 0; i < len / 8; i++ )
3518 if( mask[i] != 0xFF )
3519 return( -1 );
3520
3521 for( i = 0; i < len % 8; i++ )
3522 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3523 return( -1 );
3524
3525 return( 0 );
3526}
3527
Hanno Becker56e205e2018-08-16 09:06:12 +01003528/* msg_len does not include the handshake header */
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003529static size_t ssl_get_reassembly_buffer_size( unsigned msg_len,
3530 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003531{
3532 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003533
3534 alloc_len = 12; /* Handshake header */
3535 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003536
3537 if( add_bitmap )
3538 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003539
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003540 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003541}
3542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003544
Hanno Becker12555c62018-08-16 12:47:53 +01003545static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3546{
3547 return( ( ssl->in_msg[1] << 16 ) |
3548 ( ssl->in_msg[2] << 8 ) |
3549 ssl->in_msg[3] );
3550}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003551
Simon Butcher99000142016-10-13 17:21:01 +01003552int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003557 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003559 }
3560
Hanno Becker12555c62018-08-16 12:47:53 +01003561 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003564 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003565 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003569 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003570 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003571 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003572
Hanno Becker44650b72018-08-16 12:51:11 +01003573 if( ssl_check_hs_header( ssl ) != 0 )
3574 {
3575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3576 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3577 }
3578
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003579 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003580 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3581 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3582 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3583 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003584 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003585 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3586 {
3587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3588 recv_msg_seq,
3589 ssl->handshake->in_msg_seq ) );
3590 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3591 }
3592
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003593 /* Retransmit only on last message from previous flight, to avoid
3594 * too many retransmissions.
3595 * Besides, No sane server ever retransmits HelloVerifyRequest */
3596 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003600 "message_seq = %d, start_of_flight = %d",
3601 recv_msg_seq,
3602 ssl->handshake->in_flight_start_seq ) );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003607 return( ret );
3608 }
3609 }
3610 else
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003613 "message_seq = %d, expected = %d",
3614 recv_msg_seq,
3615 ssl->handshake->in_msg_seq ) );
3616 }
3617
Hanno Becker90333da2017-10-10 11:27:13 +01003618 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003619 }
3620 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003621
Hanno Becker6d97ef52018-08-16 13:09:04 +01003622 /* Message reassembly is handled alongside buffering of future
3623 * messages; the commonality is that both handshake fragments and
3624 * future messages cannot be forwarded immediately to the handshake
3625 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003626 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003629 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003630 }
3631 }
3632 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003634 /* With TLS we don't handle fragmentation (for now) */
3635 if( ssl->in_msglen < ssl->in_hslen )
3636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3638 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003639 }
3640
Simon Butcher99000142016-10-13 17:21:01 +01003641 return( 0 );
3642}
3643
3644void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3645{
Hanno Becker0271f962018-08-16 13:23:47 +01003646 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003647
Hanno Becker0271f962018-08-16 13:23:47 +01003648 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003649 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003650 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003651 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003652
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003653 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003655 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003656 ssl->handshake != NULL )
3657 {
Hanno Becker0271f962018-08-16 13:23:47 +01003658 unsigned offset;
3659 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003660
Hanno Becker0271f962018-08-16 13:23:47 +01003661 /* Increment handshake sequence number */
3662 hs->in_msg_seq++;
3663
3664 /*
3665 * Clear up handshake buffering and reassembly structure.
3666 */
3667
3668 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003669 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003670
3671 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003672 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3673 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003674 offset++, hs_buf++ )
3675 {
3676 *hs_buf = *(hs_buf + 1);
3677 }
3678
3679 /* Create a fresh last entry */
3680 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003681 }
3682#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003683}
3684
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003685/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003686 * DTLS anti-replay: RFC 6347 4.1.2.6
3687 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003688 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3689 * Bit n is set iff record number in_window_top - n has been seen.
3690 *
3691 * Usually, in_window_top is the last record number seen and the lsb of
3692 * in_window is set. The only exception is the initial state (record number 0
3693 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3696static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003697{
3698 ssl->in_window_top = 0;
3699 ssl->in_window = 0;
3700}
3701
3702static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3703{
3704 return( ( (uint64_t) buf[0] << 40 ) |
3705 ( (uint64_t) buf[1] << 32 ) |
3706 ( (uint64_t) buf[2] << 24 ) |
3707 ( (uint64_t) buf[3] << 16 ) |
3708 ( (uint64_t) buf[4] << 8 ) |
3709 ( (uint64_t) buf[5] ) );
3710}
3711
3712/*
3713 * Return 0 if sequence number is acceptable, -1 otherwise
3714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003716{
3717 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3718 uint64_t bit;
3719
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003720 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003721 return( 0 );
3722
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003723 if( rec_seqnum > ssl->in_window_top )
3724 return( 0 );
3725
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003726 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003727
3728 if( bit >= 64 )
3729 return( -1 );
3730
3731 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3732 return( -1 );
3733
3734 return( 0 );
3735}
3736
3737/*
3738 * Update replay window on new validated record
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003741{
3742 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
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;
3746
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003747 if( rec_seqnum > ssl->in_window_top )
3748 {
3749 /* Update window_top and the contents of the window */
3750 uint64_t shift = rec_seqnum - ssl->in_window_top;
3751
3752 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003753 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003754 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003755 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003756 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003757 ssl->in_window |= 1;
3758 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003759
3760 ssl->in_window_top = rec_seqnum;
3761 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003762 else
3763 {
3764 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003765 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003766
3767 if( bit < 64 ) /* Always true, but be extra sure */
3768 ssl->in_window |= (uint64_t) 1 << bit;
3769 }
3770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003772
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003773#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003774/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003775static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3776
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003777/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003778 * Without any SSL context, check if a datagram looks like a ClientHello with
3779 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003780 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003781 *
3782 * - if cookie is valid, return 0
3783 * - if ClientHello looks superficially valid but cookie is not,
3784 * fill obuf and set olen, then
3785 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3786 * - otherwise return a specific error code
3787 */
3788static int ssl_check_dtls_clihlo_cookie(
3789 mbedtls_ssl_cookie_write_t *f_cookie_write,
3790 mbedtls_ssl_cookie_check_t *f_cookie_check,
3791 void *p_cookie,
3792 const unsigned char *cli_id, size_t cli_id_len,
3793 const unsigned char *in, size_t in_len,
3794 unsigned char *obuf, size_t buf_len, size_t *olen )
3795{
3796 size_t sid_len, cookie_len;
3797 unsigned char *p;
3798
3799 if( f_cookie_write == NULL || f_cookie_check == NULL )
3800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3801
3802 /*
3803 * Structure of ClientHello with record and handshake headers,
3804 * and expected values. We don't need to check a lot, more checks will be
3805 * done when actually parsing the ClientHello - skipping those checks
3806 * avoids code duplication and does not make cookie forging any easier.
3807 *
3808 * 0-0 ContentType type; copied, must be handshake
3809 * 1-2 ProtocolVersion version; copied
3810 * 3-4 uint16 epoch; copied, must be 0
3811 * 5-10 uint48 sequence_number; copied
3812 * 11-12 uint16 length; (ignored)
3813 *
3814 * 13-13 HandshakeType msg_type; (ignored)
3815 * 14-16 uint24 length; (ignored)
3816 * 17-18 uint16 message_seq; copied
3817 * 19-21 uint24 fragment_offset; copied, must be 0
3818 * 22-24 uint24 fragment_length; (ignored)
3819 *
3820 * 25-26 ProtocolVersion client_version; (ignored)
3821 * 27-58 Random random; (ignored)
3822 * 59-xx SessionID session_id; 1 byte len + sid_len content
3823 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3824 * ...
3825 *
3826 * Minimum length is 61 bytes.
3827 */
3828 if( in_len < 61 ||
3829 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3830 in[3] != 0 || in[4] != 0 ||
3831 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3832 {
3833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3834 }
3835
3836 sid_len = in[59];
3837 if( sid_len > in_len - 61 )
3838 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3839
3840 cookie_len = in[60 + sid_len];
3841 if( cookie_len > in_len - 60 )
3842 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3843
3844 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3845 cli_id, cli_id_len ) == 0 )
3846 {
3847 /* Valid cookie */
3848 return( 0 );
3849 }
3850
3851 /*
3852 * If we get here, we've got an invalid cookie, let's prepare HVR.
3853 *
3854 * 0-0 ContentType type; copied
3855 * 1-2 ProtocolVersion version; copied
3856 * 3-4 uint16 epoch; copied
3857 * 5-10 uint48 sequence_number; copied
3858 * 11-12 uint16 length; olen - 13
3859 *
3860 * 13-13 HandshakeType msg_type; hello_verify_request
3861 * 14-16 uint24 length; olen - 25
3862 * 17-18 uint16 message_seq; copied
3863 * 19-21 uint24 fragment_offset; copied
3864 * 22-24 uint24 fragment_length; olen - 25
3865 *
3866 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3867 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3868 *
3869 * Minimum length is 28.
3870 */
3871 if( buf_len < 28 )
3872 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3873
3874 /* Copy most fields and adapt others */
3875 memcpy( obuf, in, 25 );
3876 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3877 obuf[25] = 0xfe;
3878 obuf[26] = 0xff;
3879
3880 /* Generate and write actual cookie */
3881 p = obuf + 28;
3882 if( f_cookie_write( p_cookie,
3883 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3884 {
3885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3886 }
3887
3888 *olen = p - obuf;
3889
3890 /* Go back and fill length fields */
3891 obuf[27] = (unsigned char)( *olen - 28 );
3892
3893 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3894 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3895 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3896
3897 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3898 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3899
3900 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3901}
3902
3903/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003904 * Handle possible client reconnect with the same UDP quadruplet
3905 * (RFC 6347 Section 4.2.8).
3906 *
3907 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3908 * that looks like a ClientHello.
3909 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003910 * - if the input looks like a ClientHello without cookies,
3911 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003912 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003913 * - if the input looks like a ClientHello with a valid cookie,
3914 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003915 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003916 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003917 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003918 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003919 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3920 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003921 */
3922static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3923{
3924 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003925 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003926
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003927 ret = ssl_check_dtls_clihlo_cookie(
3928 ssl->conf->f_cookie_write,
3929 ssl->conf->f_cookie_check,
3930 ssl->conf->p_cookie,
3931 ssl->cli_id, ssl->cli_id_len,
3932 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003933 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003934
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003935 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3936
3937 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003938 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003939 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003940 * If the error is permanent we'll catch it later,
3941 * if it's not, then hopefully it'll work next time. */
3942 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3943
3944 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003945 }
3946
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003947 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003948 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003949 /* Got a valid cookie, partially reset context */
3950 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3951 {
3952 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3953 return( ret );
3954 }
3955
3956 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003957 }
3958
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003959 return( ret );
3960}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003961#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003962
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003963/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003964 * ContentType type;
3965 * ProtocolVersion version;
3966 * uint16 epoch; // DTLS only
3967 * uint48 sequence_number; // DTLS only
3968 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003969 *
3970 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003971 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003972 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3973 *
3974 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003975 * 1. proceed with the record if this function returns 0
3976 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3977 * 3. return CLIENT_RECONNECT if this function return that value
3978 * 4. drop the whole datagram if this function returns anything else.
3979 * Point 2 is needed when the peer is resending, and we have already received
3980 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003984 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 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 +02003987
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003989 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003990 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003994 ssl->in_msgtype,
3995 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003997 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3999 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4000 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4001 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004004
4005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004006 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4007 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004008 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4009#endif /* MBEDTLS_SSL_PROTO_DTLS */
4010 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4011 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004014 }
4015
4016 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004017 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4020 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 }
4022
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004023 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4026 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 }
4028
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004029 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004030 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004031 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4034 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004035 }
4036
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004037 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004038 * DTLS-related tests.
4039 * Check epoch before checking length constraint because
4040 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4041 * message gets duplicated before the corresponding Finished message,
4042 * the second ChangeCipherSpec should be discarded because it belongs
4043 * to an old epoch, but not because its length is shorter than
4044 * the minimum record length for packets using the new record transform.
4045 * Note that these two kinds of failures are handled differently,
4046 * as an unexpected record is silently skipped but an invalid
4047 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004048 */
4049#if defined(MBEDTLS_SSL_PROTO_DTLS)
4050 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4051 {
4052 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4053
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004054 /* Check epoch (and sequence number) with DTLS */
4055 if( rec_epoch != ssl->in_epoch )
4056 {
4057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4058 "expected %d, received %d",
4059 ssl->in_epoch, rec_epoch ) );
4060
4061#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4062 /*
4063 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4064 * access the first byte of record content (handshake type), as we
4065 * have an active transform (possibly iv_len != 0), so use the
4066 * fact that the record header len is 13 instead.
4067 */
4068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4069 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4070 rec_epoch == 0 &&
4071 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4072 ssl->in_left > 13 &&
4073 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4074 {
4075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4076 "from the same port" ) );
4077 return( ssl_handle_possible_reconnect( ssl ) );
4078 }
4079 else
4080#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004081 {
4082 /* Consider buffering the record. */
4083 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4084 {
4085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4086 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4087 }
4088
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004089 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004090 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004091 }
4092
4093#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4094 /* Replay detection only works for the current epoch */
4095 if( rec_epoch == ssl->in_epoch &&
4096 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4097 {
4098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4099 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4100 }
4101#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004102
Hanno Becker52c6dc62017-05-26 16:07:36 +01004103 /* Drop unexpected ApplicationData records,
4104 * except at the beginning of renegotiations */
4105 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4106 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4107#if defined(MBEDTLS_SSL_RENEGOTIATION)
4108 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4109 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4110#endif
4111 )
4112 {
4113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4114 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4115 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004116 }
4117#endif /* MBEDTLS_SSL_PROTO_DTLS */
4118
Hanno Becker52c6dc62017-05-26 16:07:36 +01004119
4120 /* Check length against bounds of the current transform and version */
4121 if( ssl->transform_in == NULL )
4122 {
4123 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004124 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004125 {
4126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4128 }
4129 }
4130 else
4131 {
4132 if( ssl->in_msglen < ssl->transform_in->minlen )
4133 {
4134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4135 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4136 }
4137
4138#if defined(MBEDTLS_SSL_PROTO_SSL3)
4139 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004140 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004141 {
4142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4143 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4144 }
4145#endif
4146#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4147 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4148 /*
4149 * TLS encrypted messages can have up to 256 bytes of padding
4150 */
4151 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4152 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004153 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4156 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4157 }
4158#endif
4159 }
4160
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004161 return( 0 );
4162}
Paul Bakker5121ce52009-01-03 21:22:43 +00004163
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004164/*
4165 * If applicable, decrypt (and decompress) record content
4166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004168{
4169 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4172 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4175 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 ret = mbedtls_ssl_hw_record_read( ssl );
4180 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4183 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004184 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004185
4186 if( ret == 0 )
4187 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004190 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 {
4192 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 return( ret );
4196 }
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004199 ssl->in_msg, ssl->in_msglen );
4200
Angus Grattond8213d02016-05-25 20:56:48 +10004201 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4204 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 }
4206 }
4207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004209 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004211 {
4212 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004215 return( ret );
4216 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004224 }
4225#endif
4226
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004227 return( 0 );
4228}
4229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004231
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004232/*
4233 * Read a record.
4234 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004235 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4236 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4237 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004238 */
Hanno Becker1097b342018-08-15 14:09:41 +01004239
4240/* Helper functions for mbedtls_ssl_read_record(). */
4241static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004242static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4243static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004244
Hanno Becker40f50842018-08-15 14:48:01 +01004245#if defined(MBEDTLS_SSL_PROTO_DTLS)
4246static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004247static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004248static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004249static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004250static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4251#endif /* MBEDTLS_SSL_PROTO_DTLS */
4252
Hanno Becker327c93b2018-08-15 13:56:18 +01004253int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004254 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004255{
4256 int ret;
4257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004259
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004260 if( ssl->keep_current_message == 0 )
4261 {
4262 do {
Simon Butcher99000142016-10-13 17:21:01 +01004263
Hanno Becker26994592018-08-15 14:14:59 +01004264 ret = ssl_consume_current_message( ssl );
4265 if( ret != 0 )
4266 return( ret );
4267
Hanno Beckere74d5562018-08-15 14:26:08 +01004268 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004269 {
Hanno Becker40f50842018-08-15 14:48:01 +01004270#if defined(MBEDTLS_SSL_PROTO_DTLS)
4271 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004272
Hanno Becker40f50842018-08-15 14:48:01 +01004273 /* We only check for buffered messages if the
4274 * current datagram is fully consumed. */
4275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4276 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004277 {
Hanno Becker40f50842018-08-15 14:48:01 +01004278 if( ssl_load_buffered_message( ssl ) == 0 )
4279 have_buffered = 1;
4280 }
4281
4282 if( have_buffered == 0 )
4283#endif /* MBEDTLS_SSL_PROTO_DTLS */
4284 {
4285 ret = ssl_get_next_record( ssl );
4286 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4287 continue;
4288
4289 if( ret != 0 )
4290 {
4291 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4292 return( ret );
4293 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004294 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004295 }
4296
4297 ret = mbedtls_ssl_handle_message_type( ssl );
4298
Hanno Becker40f50842018-08-15 14:48:01 +01004299#if defined(MBEDTLS_SSL_PROTO_DTLS)
4300 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4301 {
4302 /* Buffer future message */
4303 ret = ssl_buffer_message( ssl );
4304 if( ret != 0 )
4305 return( ret );
4306
4307 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4308 }
4309#endif /* MBEDTLS_SSL_PROTO_DTLS */
4310
Hanno Becker90333da2017-10-10 11:27:13 +01004311 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4312 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004313
4314 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004315 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004316 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004317 return( ret );
4318 }
4319
Hanno Becker327c93b2018-08-15 13:56:18 +01004320 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004321 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004322 {
4323 mbedtls_ssl_update_handshake_status( ssl );
4324 }
Simon Butcher99000142016-10-13 17:21:01 +01004325 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004326 else
Simon Butcher99000142016-10-13 17:21:01 +01004327 {
Hanno Becker02f59072018-08-15 14:00:24 +01004328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004329 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004330 }
4331
4332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4333
4334 return( 0 );
4335}
4336
Hanno Becker40f50842018-08-15 14:48:01 +01004337#if defined(MBEDTLS_SSL_PROTO_DTLS)
4338static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4339{
4340 if( ssl->in_left > ssl->next_record_offset )
4341 return( 1 );
4342
4343 return( 0 );
4344}
4345
4346static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4347{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004348 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004349 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004350 int ret = 0;
4351
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004352 if( hs == NULL )
4353 return( -1 );
4354
Hanno Beckere00ae372018-08-20 09:39:42 +01004355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4356
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004357 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4358 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4359 {
4360 /* Check if we have seen a ChangeCipherSpec before.
4361 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004362 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004363 {
4364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4365 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004366 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004367 }
4368
4369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4370 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4371 ssl->in_msglen = 1;
4372 ssl->in_msg[0] = 1;
4373
4374 /* As long as they are equal, the exact value doesn't matter. */
4375 ssl->in_left = 0;
4376 ssl->next_record_offset = 0;
4377
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004378 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004379 goto exit;
4380 }
Hanno Becker37f95322018-08-16 13:55:32 +01004381
4382 /* Debug only */
4383 {
4384 unsigned offset;
4385 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4386 {
4387 hs_buf = &hs->buffering.hs[offset];
4388 if( hs_buf->is_valid == 1 )
4389 {
4390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4391 hs->in_msg_seq + offset,
4392 hs_buf->is_complete ? "fully" : "partitially" ) );
4393 }
4394 }
4395 }
4396
4397 /* Check if we have buffered and/or fully reassembled the
4398 * next handshake message. */
4399 hs_buf = &hs->buffering.hs[0];
4400 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4401 {
4402 /* Synthesize a record containing the buffered HS message. */
4403 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4404 ( hs_buf->data[2] << 8 ) |
4405 hs_buf->data[3];
4406
4407 /* Double-check that we haven't accidentally buffered
4408 * a message that doesn't fit into the input buffer. */
4409 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4413 }
4414
4415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4416 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4417 hs_buf->data, msg_len + 12 );
4418
4419 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4420 ssl->in_hslen = msg_len + 12;
4421 ssl->in_msglen = msg_len + 12;
4422 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4423
4424 ret = 0;
4425 goto exit;
4426 }
4427 else
4428 {
4429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4430 hs->in_msg_seq ) );
4431 }
4432
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004433 ret = -1;
4434
4435exit:
4436
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4438 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004439}
4440
4441static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4442{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004443 int ret = 0;
4444 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4445
4446 if( hs == NULL )
4447 return( 0 );
4448
4449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4450
4451 switch( ssl->in_msgtype )
4452 {
4453 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004455
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004456 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004457 break;
4458
4459 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004460 {
4461 unsigned recv_msg_seq_offset;
4462 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4463 mbedtls_ssl_hs_buffer *hs_buf;
4464 size_t msg_len = ssl->in_hslen - 12;
4465
4466 /* We should never receive an old handshake
4467 * message - double-check nonetheless. */
4468 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4469 {
4470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4472 }
4473
4474 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4475 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4476 {
4477 /* Silently ignore -- message too far in the future */
4478 MBEDTLS_SSL_DEBUG_MSG( 2,
4479 ( "Ignore future HS message with sequence number %u, "
4480 "buffering window %u - %u",
4481 recv_msg_seq, ssl->handshake->in_msg_seq,
4482 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4483
4484 goto exit;
4485 }
4486
4487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4488 recv_msg_seq, recv_msg_seq_offset ) );
4489
4490 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4491
4492 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004493 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004494 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004495 size_t reassembly_buf_sz;
4496
Hanno Becker37f95322018-08-16 13:55:32 +01004497 hs_buf->is_fragmented =
4498 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4499
4500 /* We copy the message back into the input buffer
4501 * after reassembly, so check that it's not too large.
4502 * This is an implementation-specific limitation
4503 * and not one from the standard, hence it is not
4504 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004505 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004506 {
4507 /* Ignore message */
4508 goto exit;
4509 }
4510
Hanno Beckere0b150f2018-08-21 15:51:03 +01004511 /* Check if we have enough space to buffer the message. */
4512 if( hs->buffering.total_bytes_buffered >
4513 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4517 }
4518
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004519 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4520 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004521
4522 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4523 hs->buffering.total_bytes_buffered ) )
4524 {
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004525 int offset;
4526
Hanno Beckere0b150f2018-08-21 15:51:03 +01004527 if( recv_msg_seq_offset > 0 )
4528 {
4529 /* If we can't buffer a future message because
4530 * of space limitations -- ignore. */
4531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4532 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4533 (unsigned) hs->buffering.total_bytes_buffered ) );
4534 goto exit;
4535 }
4536
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004537 /* We don't have enough space to buffer the next expected
4538 * handshake message. Remove buffers used for future msgs
4539 * to gain space, starting with the most distant one. */
4540 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4541 offset >= 0; offset-- )
4542 {
4543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4544 offset ) );
4545
4546 ssl_buffering_free_slot( ssl, offset );
4547
4548 /* Check if we have enough space available now. */
4549 if( reassembly_buf_sz <=
4550 ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4551 hs->buffering.total_bytes_buffered ) )
4552 {
4553 break;
4554 }
4555 }
4556
4557 if( offset == -1 )
4558 {
4559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
Hanno Beckere0b150f2018-08-21 15:51:03 +01004560 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4561 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004562 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4563 goto exit;
4564 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004565 }
4566
4567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4568 msg_len ) );
4569
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004570 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4571 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004572 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004573 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004574 goto exit;
4575 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004576 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004577
4578 /* Prepare final header: copy msg_type, length and message_seq,
4579 * then add standardised fragment_offset and fragment_length */
4580 memcpy( hs_buf->data, ssl->in_msg, 6 );
4581 memset( hs_buf->data + 6, 0, 3 );
4582 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4583
4584 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004585
4586 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004587 }
4588 else
4589 {
4590 /* Make sure msg_type and length are consistent */
4591 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4592 {
4593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4594 /* Ignore */
4595 goto exit;
4596 }
4597 }
4598
Hanno Becker4422bbb2018-08-20 09:40:19 +01004599 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004600 {
4601 size_t frag_len, frag_off;
4602 unsigned char * const msg = hs_buf->data + 12;
4603
4604 /*
4605 * Check and copy current fragment
4606 */
4607
4608 /* Validation of header fields already done in
4609 * mbedtls_ssl_prepare_handshake_record(). */
4610 frag_off = ssl_get_hs_frag_off( ssl );
4611 frag_len = ssl_get_hs_frag_len( ssl );
4612
4613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4614 frag_off, frag_len ) );
4615 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4616
4617 if( hs_buf->is_fragmented )
4618 {
4619 unsigned char * const bitmask = msg + msg_len;
4620 ssl_bitmask_set( bitmask, frag_off, frag_len );
4621 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4622 msg_len ) == 0 );
4623 }
4624 else
4625 {
4626 hs_buf->is_complete = 1;
4627 }
4628
4629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4630 hs_buf->is_complete ? "" : "not yet " ) );
4631 }
4632
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004633 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004634 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004635
4636 default:
4637 break;
4638 }
4639
4640exit:
4641
4642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4643 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004644}
4645#endif /* MBEDTLS_SSL_PROTO_DTLS */
4646
Hanno Becker1097b342018-08-15 14:09:41 +01004647static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004648{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004649 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004650 * Consume last content-layer message and potentially
4651 * update in_msglen which keeps track of the contents'
4652 * consumption state.
4653 *
4654 * (1) Handshake messages:
4655 * Remove last handshake message, move content
4656 * and adapt in_msglen.
4657 *
4658 * (2) Alert messages:
4659 * Consume whole record content, in_msglen = 0.
4660 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004661 * (3) Change cipher spec:
4662 * Consume whole record content, in_msglen = 0.
4663 *
4664 * (4) Application data:
4665 * Don't do anything - the record layer provides
4666 * the application data as a stream transport
4667 * and consumes through mbedtls_ssl_read only.
4668 *
4669 */
4670
4671 /* Case (1): Handshake messages */
4672 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004673 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004674 /* Hard assertion to be sure that no application data
4675 * is in flight, as corrupting ssl->in_msglen during
4676 * ssl->in_offt != NULL is fatal. */
4677 if( ssl->in_offt != NULL )
4678 {
4679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4681 }
4682
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004683 /*
4684 * Get next Handshake message in the current record
4685 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004686
Hanno Becker4a810fb2017-05-24 16:27:30 +01004687 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004688 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004689 * current handshake content: If DTLS handshake
4690 * fragmentation is used, that's the fragment
4691 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004692 * size here is faulty and should be changed at
4693 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004694 * (2) While it doesn't seem to cause problems, one
4695 * has to be very careful not to assume that in_hslen
4696 * is always <= in_msglen in a sensible communication.
4697 * Again, it's wrong for DTLS handshake fragmentation.
4698 * The following check is therefore mandatory, and
4699 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004700 * Additionally, ssl->in_hslen might be arbitrarily out of
4701 * bounds after handling a DTLS message with an unexpected
4702 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004703 */
4704 if( ssl->in_hslen < ssl->in_msglen )
4705 {
4706 ssl->in_msglen -= ssl->in_hslen;
4707 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4708 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004709
Hanno Becker4a810fb2017-05-24 16:27:30 +01004710 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4711 ssl->in_msg, ssl->in_msglen );
4712 }
4713 else
4714 {
4715 ssl->in_msglen = 0;
4716 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004717
Hanno Becker4a810fb2017-05-24 16:27:30 +01004718 ssl->in_hslen = 0;
4719 }
4720 /* Case (4): Application data */
4721 else if( ssl->in_offt != NULL )
4722 {
4723 return( 0 );
4724 }
4725 /* Everything else (CCS & Alerts) */
4726 else
4727 {
4728 ssl->in_msglen = 0;
4729 }
4730
Hanno Becker1097b342018-08-15 14:09:41 +01004731 return( 0 );
4732}
4733
Hanno Beckere74d5562018-08-15 14:26:08 +01004734static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4735{
4736 if( ssl->in_msglen > 0 )
4737 return( 1 );
4738
4739 return( 0 );
4740}
4741
Hanno Becker5f066e72018-08-16 14:56:31 +01004742#if defined(MBEDTLS_SSL_PROTO_DTLS)
4743
4744static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4745{
4746 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4747 if( hs == NULL )
4748 return;
4749
4750 mbedtls_free( hs->buffering.future_record.data );
4751 hs->buffering.future_record.data = NULL;
4752}
4753
4754static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4755{
4756 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4757 unsigned char * rec;
4758 size_t rec_len;
4759 unsigned rec_epoch;
4760
4761 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4762 return( 0 );
4763
4764 if( hs == NULL )
4765 return( 0 );
4766
Hanno Becker5f066e72018-08-16 14:56:31 +01004767 rec = hs->buffering.future_record.data;
4768 rec_len = hs->buffering.future_record.len;
4769 rec_epoch = hs->buffering.future_record.epoch;
4770
4771 if( rec == NULL )
4772 return( 0 );
4773
Hanno Becker4cb782d2018-08-20 11:19:05 +01004774 /* Only consider loading future records if the
4775 * input buffer is empty. */
4776 if( ssl_another_record_in_datagram( ssl ) == 1 )
4777 return( 0 );
4778
Hanno Becker5f066e72018-08-16 14:56:31 +01004779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4780
4781 if( rec_epoch != ssl->in_epoch )
4782 {
4783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4784 goto exit;
4785 }
4786
4787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4788
4789 /* Double-check that the record is not too large */
4790 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4791 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4792 {
4793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4795 }
4796
4797 memcpy( ssl->in_hdr, rec, rec_len );
4798 ssl->in_left = rec_len;
4799 ssl->next_record_offset = 0;
4800
4801 ssl_free_buffered_record( ssl );
4802
4803exit:
4804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4805 return( 0 );
4806}
4807
4808static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4809{
4810 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4811 size_t const rec_hdr_len = 13;
4812
4813 /* Don't buffer future records outside handshakes. */
4814 if( hs == NULL )
4815 return( 0 );
4816
4817 /* Only buffer handshake records (we are only interested
4818 * in Finished messages). */
4819 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4820 return( 0 );
4821
4822 /* Don't buffer more than one future epoch record. */
4823 if( hs->buffering.future_record.data != NULL )
4824 return( 0 );
4825
4826 /* Buffer record */
4827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4828 ssl->in_epoch + 1 ) );
4829 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4830 rec_hdr_len + ssl->in_msglen );
4831
4832 /* ssl_parse_record_header() only considers records
4833 * of the next epoch as candidates for buffering. */
4834 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
4835 hs->buffering.future_record.len = rec_hdr_len + ssl->in_msglen;
4836
4837 hs->buffering.future_record.data =
4838 mbedtls_calloc( 1, hs->buffering.future_record.len );
4839 if( hs->buffering.future_record.data == NULL )
4840 {
4841 /* If we run out of RAM trying to buffer a
4842 * record from the next epoch, just ignore. */
4843 return( 0 );
4844 }
4845
4846 memcpy( hs->buffering.future_record.data,
4847 ssl->in_hdr, rec_hdr_len + ssl->in_msglen );
4848
4849 return( 0 );
4850}
4851
4852#endif /* MBEDTLS_SSL_PROTO_DTLS */
4853
Hanno Beckere74d5562018-08-15 14:26:08 +01004854static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004855{
4856 int ret;
4857
Hanno Becker5f066e72018-08-16 14:56:31 +01004858#if defined(MBEDTLS_SSL_PROTO_DTLS)
4859 /* We might have buffered a future record; if so,
4860 * and if the epoch matches now, load it.
4861 * On success, this call will set ssl->in_left to
4862 * the length of the buffered record, so that
4863 * the calls to ssl_fetch_input() below will
4864 * essentially be no-ops. */
4865 ret = ssl_load_buffered_record( ssl );
4866 if( ret != 0 )
4867 return( ret );
4868#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004873 return( ret );
4874 }
4875
4876 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4880 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004881 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004882 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4883 {
4884 ret = ssl_buffer_future_record( ssl );
4885 if( ret != 0 )
4886 return( ret );
4887
4888 /* Fall through to handling of unexpected records */
4889 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4890 }
4891
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004892 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4893 {
4894 /* Skip unexpected record (but not whole datagram) */
4895 ssl->next_record_offset = ssl->in_msglen
4896 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004897
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4899 "(header)" ) );
4900 }
4901 else
4902 {
4903 /* Skip invalid record and the rest of the datagram */
4904 ssl->next_record_offset = 0;
4905 ssl->in_left = 0;
4906
4907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4908 "(header)" ) );
4909 }
4910
4911 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004912 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004913 }
4914#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004915 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004916 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004917
4918 /*
4919 * Read and optionally decrypt the message contents
4920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004921 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4922 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004925 return( ret );
4926 }
4927
4928 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004930 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004933 if( ssl->next_record_offset < ssl->in_left )
4934 {
4935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4936 }
4937 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004938 else
4939#endif
4940 ssl->in_left = 0;
4941
4942 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004945 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004946 {
4947 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4949 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004950 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004951 /* Except when waiting for Finished as a bad mac here
4952 * probably means something went wrong in the handshake
4953 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4954 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4955 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4956 {
4957#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4958 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4959 {
4960 mbedtls_ssl_send_alert_message( ssl,
4961 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4962 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4963 }
4964#endif
4965 return( ret );
4966 }
4967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004969 if( ssl->conf->badmac_limit != 0 &&
4970 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4973 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004974 }
4975#endif
4976
Hanno Becker4a810fb2017-05-24 16:27:30 +01004977 /* As above, invalid records cause
4978 * dismissal of the whole datagram. */
4979
4980 ssl->next_record_offset = 0;
4981 ssl->in_left = 0;
4982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004984 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004985 }
4986
4987 return( ret );
4988 }
4989 else
4990#endif
4991 {
4992 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4994 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 mbedtls_ssl_send_alert_message( ssl,
4997 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4998 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004999 }
5000#endif
5001 return( ret );
5002 }
5003 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005004
Simon Butcher99000142016-10-13 17:21:01 +01005005 return( 0 );
5006}
5007
5008int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5009{
5010 int ret;
5011
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005012 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005013 * Handle particular types of records
5014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005016 {
Simon Butcher99000142016-10-13 17:21:01 +01005017 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5018 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005019 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 }
5022
Hanno Beckere678eaa2018-08-21 14:57:46 +01005023 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005024 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005025 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005026 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5028 ssl->in_msglen ) );
5029 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005030 }
5031
Hanno Beckere678eaa2018-08-21 14:57:46 +01005032 if( ssl->in_msg[0] != 1 )
5033 {
5034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5035 ssl->in_msg[0] ) );
5036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5037 }
5038
5039#if defined(MBEDTLS_SSL_PROTO_DTLS)
5040 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5041 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5042 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5043 {
5044 if( ssl->handshake == NULL )
5045 {
5046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5047 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5048 }
5049
5050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5051 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5052 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005053#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005054 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005057 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005058 if( ssl->in_msglen != 2 )
5059 {
5060 /* Note: Standard allows for more than one 2 byte alert
5061 to be packed in a single message, but Mbed TLS doesn't
5062 currently support this. */
5063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5064 ssl->in_msglen ) );
5065 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5066 }
5067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 ssl->in_msg[0], ssl->in_msg[1] ) );
5070
5071 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005072 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005077 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005079 }
5080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5082 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5085 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005086 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005087
5088#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5089 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5090 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5091 {
Hanno Becker90333da2017-10-10 11:27:13 +01005092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005093 /* Will be handled when trying to parse ServerHello */
5094 return( 0 );
5095 }
5096#endif
5097
5098#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5099 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5100 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5101 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5102 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5103 {
5104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5105 /* Will be handled in mbedtls_ssl_parse_certificate() */
5106 return( 0 );
5107 }
5108#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5109
5110 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005111 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005112 }
5113
Hanno Beckerc76c6192017-06-06 10:03:17 +01005114#if defined(MBEDTLS_SSL_PROTO_DTLS)
5115 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5116 ssl->handshake != NULL &&
5117 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5118 {
5119 ssl_handshake_wrapup_free_hs_transform( ssl );
5120 }
5121#endif
5122
Paul Bakker5121ce52009-01-03 21:22:43 +00005123 return( 0 );
5124}
5125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005127{
5128 int ret;
5129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5131 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5132 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005133 {
5134 return( ret );
5135 }
5136
5137 return( 0 );
5138}
5139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005141 unsigned char level,
5142 unsigned char message )
5143{
5144 int ret;
5145
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005146 if( ssl == NULL || ssl->conf == NULL )
5147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005153 ssl->out_msglen = 2;
5154 ssl->out_msg[0] = level;
5155 ssl->out_msg[1] = message;
5156
Hanno Becker67bc7c32018-08-06 11:33:50 +01005157 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005160 return( ret );
5161 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005163
5164 return( 0 );
5165}
5166
Paul Bakker5121ce52009-01-03 21:22:43 +00005167/*
5168 * Handshake functions
5169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5171 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5172 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5173 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5174 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5175 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5176 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005177/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5185 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005186 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5187 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005190 ssl->state++;
5191 return( 0 );
5192 }
5193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005196}
5197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5205 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005206 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5207 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005210 ssl->state++;
5211 return( 0 );
5212 }
5213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005216}
Gilles Peskinef9828522017-05-03 12:28:43 +02005217
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005218#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005219/* Some certificate support -> implement write and parse */
5220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005224 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 const mbedtls_x509_crt *crt;
5226 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5231 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005232 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5233 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005236 ssl->state++;
5237 return( 0 );
5238 }
5239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005241 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005242 {
5243 if( ssl->client_auth == 0 )
5244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 ssl->state++;
5247 return( 0 );
5248 }
5249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005251 /*
5252 * If using SSLv3 and got no cert, send an Alert message
5253 * (otherwise an empty Certificate message will be sent).
5254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5256 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005257 {
5258 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5260 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5261 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005264 goto write_msg;
5265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268#endif /* MBEDTLS_SSL_CLI_C */
5269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5275 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005276 }
5277 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005281
5282 /*
5283 * 0 . 0 handshake type
5284 * 1 . 3 handshake length
5285 * 4 . 6 length of all certs
5286 * 7 . 9 length of cert. 1
5287 * 10 . n-1 peer certificate
5288 * n . n+2 length of cert. 2
5289 * n+3 . ... upper level cert, etc.
5290 */
5291 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005293
Paul Bakker29087132010-03-21 21:03:34 +00005294 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005295 {
5296 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005297 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005300 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005302 }
5303
5304 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5305 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5306 ssl->out_msg[i + 2] = (unsigned char)( n );
5307
5308 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5309 i += n; crt = crt->next;
5310 }
5311
5312 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5313 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5314 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5315
5316 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5318 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005320#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005321write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005323
5324 ssl->state++;
5325
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005326 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005329 return( ret );
5330 }
5331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Paul Bakkered27a042013-04-18 22:46:23 +02005334 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005335}
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005340 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005342 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005343 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5348 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005349 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5350 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005353 ssl->state++;
5354 return( 0 );
5355 }
5356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005359 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5362 ssl->state++;
5363 return( 0 );
5364 }
5365
5366#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5367 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5368 authmode = ssl->handshake->sni_authmode;
5369#endif
5370
5371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5372 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005373 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005374 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376 ssl->state++;
5377 return( 0 );
5378 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005380
Hanno Becker327c93b2018-08-15 13:56:18 +01005381 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005382 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005383 /* mbedtls_ssl_read_record may have sent an alert already. We
5384 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005386 return( ret );
5387 }
5388
5389 ssl->state++;
5390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391#if defined(MBEDTLS_SSL_SRV_C)
5392#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 /*
5394 * Check if the client sent an empty certificate
5395 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005396 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005399 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5401 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5402 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005406 /* The client was asked for a certificate but didn't send
5407 one. The client should know what's going on, so we
5408 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005409 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005410 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005411 return( 0 );
5412 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 }
5415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5419 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005420 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5424 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5425 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5426 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005429
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005430 /* The client was asked for a certificate but didn't send
5431 one. The client should know what's going on, so we
5432 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005433 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005434 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005435 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005436 else
5437 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 }
5439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5441 MBEDTLS_SSL_PROTO_TLS1_2 */
5442#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005447 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5448 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 }
5451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5453 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005456 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5457 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005459 }
5460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005462
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005466 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005467
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005468 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005472 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5473 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 }
5476
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005477 /* In case we tried to reuse a session but it failed */
5478 if( ssl->session_negotiate->peer_cert != NULL )
5479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5481 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005482 }
5483
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005484 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005486 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005489 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5490 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005491 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 }
5493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005495
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005496 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005497
5498 while( i < ssl->in_hslen )
5499 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005500 if ( i + 3 > ssl->in_hslen ) {
5501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5502 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5503 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5504 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5505 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 if( ssl->in_msg[i] != 0 )
5507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 }
5513
5514 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5515 | (unsigned int) ssl->in_msg[i + 2];
5516 i += 3;
5517
5518 if( n < 128 || i + n > ssl->in_hslen )
5519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005521 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5522 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005524 }
5525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005527 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005528 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005529 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005530 case 0: /*ok*/
5531 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5532 /* Ignore certificate with an unknown algorithm: maybe a
5533 prior certificate was already trusted. */
5534 break;
5535
5536 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5537 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5538 goto crt_parse_der_failed;
5539
5540 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5541 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5542 goto crt_parse_der_failed;
5543
5544 default:
5545 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5546 crt_parse_der_failed:
5547 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005549 return( ret );
5550 }
5551
5552 i += n;
5553 }
5554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005557 /*
5558 * On client, make sure the server cert doesn't change during renego to
5559 * avoid "triple handshake" attack: https://secure-resumption.com/
5560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005562 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005564 {
5565 if( ssl->session->peer_cert == NULL )
5566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005568 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5569 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005571 }
5572
5573 if( ssl->session->peer_cert->raw.len !=
5574 ssl->session_negotiate->peer_cert->raw.len ||
5575 memcmp( ssl->session->peer_cert->raw.p,
5576 ssl->session_negotiate->peer_cert->raw.p,
5577 ssl->session->peer_cert->raw.len ) != 0 )
5578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005580 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5581 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005583 }
5584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005586
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005587 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005588 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005589 mbedtls_x509_crt *ca_chain;
5590 mbedtls_x509_crl *ca_crl;
5591
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005592#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005593 if( ssl->handshake->sni_ca_chain != NULL )
5594 {
5595 ca_chain = ssl->handshake->sni_ca_chain;
5596 ca_crl = ssl->handshake->sni_ca_crl;
5597 }
5598 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005599#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005600 {
5601 ca_chain = ssl->conf->ca_chain;
5602 ca_crl = ssl->conf->ca_crl;
5603 }
5604
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005605 /*
5606 * Main check: verify certificate
5607 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005608 ret = mbedtls_x509_crt_verify_with_profile(
5609 ssl->session_negotiate->peer_cert,
5610 ca_chain, ca_crl,
5611 ssl->conf->cert_profile,
5612 ssl->hostname,
5613 &ssl->session_negotiate->verify_result,
5614 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005615
5616 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005619 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005620
5621 /*
5622 * Secondary checks: always done, but change 'ret' only if it was 0
5623 */
5624
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005625#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005628
5629 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005631 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005632 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005633 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005636 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005638 }
5639 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005640#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005643 ciphersuite_info,
5644 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005645 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005648 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005650 }
5651
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005652 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5653 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5654 * with details encoded in the verification flags. All other kinds
5655 * of error codes, including those from the user provided f_vrfy
5656 * functions, are treated as fatal and lead to a failure of
5657 * ssl_parse_certificate even if verification was optional. */
5658 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5659 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5660 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5661 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005662 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005663 }
5664
5665 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5666 {
5667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5668 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5669 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005670
5671 if( ret != 0 )
5672 {
5673 /* The certificate may have been rejected for several reasons.
5674 Pick one and send the corresponding alert. Which alert to send
5675 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005676 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5677 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5678 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5679 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5680 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5681 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5682 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5683 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5684 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5685 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5686 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5687 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5688 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5689 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5690 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5691 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5692 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5693 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5694 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5695 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005696 else
5697 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5699 alert );
5700 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005701
Hanno Beckere6706e62017-05-15 16:05:15 +01005702#if defined(MBEDTLS_DEBUG_C)
5703 if( ssl->session_negotiate->verify_result != 0 )
5704 {
5705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5706 ssl->session_negotiate->verify_result ) );
5707 }
5708 else
5709 {
5710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5711 }
5712#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 }
5714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005716
5717 return( ret );
5718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5720 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5721 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5722 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5723 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5724 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5725 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005728{
5729 int ret;
5730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005734 ssl->out_msglen = 1;
5735 ssl->out_msg[0] = 1;
5736
Paul Bakker5121ce52009-01-03 21:22:43 +00005737 ssl->state++;
5738
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005739 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 return( ret );
5743 }
5744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005746
5747 return( 0 );
5748}
5749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005751{
5752 int ret;
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005755
Hanno Becker327c93b2018-08-15 13:56:18 +01005756 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005759 return( ret );
5760 }
5761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005765 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5766 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005768 }
5769
Hanno Beckere678eaa2018-08-21 14:57:46 +01005770 /* CCS records are only accepted if they have length 1 and content '1',
5771 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005772
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005773 /*
5774 * Switch to our negotiated transform and session parameters for inbound
5775 * data.
5776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005778 ssl->transform_in = ssl->transform_negotiate;
5779 ssl->session_in = ssl->session_negotiate;
5780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005785 ssl_dtls_replay_reset( ssl );
5786#endif
5787
5788 /* Increment epoch */
5789 if( ++ssl->in_epoch == 0 )
5790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005792 /* This is highly unlikely to happen for legitimate reasons, so
5793 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005795 }
5796 }
5797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005799 memset( ssl->in_ctr, 0, 8 );
5800
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005801 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5804 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5810 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005812 }
5813 }
5814#endif
5815
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 ssl->state++;
5817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005819
5820 return( 0 );
5821}
5822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5824 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005825{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005826 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5829 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5830 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005831 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005832 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5835#if defined(MBEDTLS_SHA512_C)
5836 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005837 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5838 else
5839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840#if defined(MBEDTLS_SHA256_C)
5841 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005842 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005843 else
5844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005848 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005849 }
Paul Bakker380da532012-04-18 16:10:25 +00005850}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005853{
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)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005856 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5857 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5860#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005861 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005864 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005867}
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005870 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5873 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005874 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5875 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5878#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005879 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005882 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005885}
5886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5888 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5889static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005890 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005891{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005892 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5893 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005894}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005895#endif
Paul Bakker380da532012-04-18 16:10:25 +00005896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5898#if defined(MBEDTLS_SHA256_C)
5899static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005900 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005901{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005902 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005903}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005904#endif
Paul Bakker380da532012-04-18 16:10:25 +00005905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906#if defined(MBEDTLS_SHA512_C)
5907static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005908 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005909{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005910 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005911}
Paul Bakker769075d2012-11-24 11:26:46 +01005912#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005916static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005918{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005919 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005920 mbedtls_md5_context md5;
5921 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005922
Paul Bakker5121ce52009-01-03 21:22:43 +00005923 unsigned char padbuf[48];
5924 unsigned char md5sum[16];
5925 unsigned char sha1sum[20];
5926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005928 if( !session )
5929 session = ssl->session;
5930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005932
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005933 mbedtls_md5_init( &md5 );
5934 mbedtls_sha1_init( &sha1 );
5935
5936 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5937 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005938
5939 /*
5940 * SSLv3:
5941 * hash =
5942 * MD5( master + pad2 +
5943 * MD5( handshake + sender + master + pad1 ) )
5944 * + SHA1( master + pad2 +
5945 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005946 */
5947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005949 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5950 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005954 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5955 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005956#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005959 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005960
Paul Bakker1ef83d62012-04-11 12:09:53 +00005961 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005962
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005963 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5964 mbedtls_md5_update_ret( &md5, session->master, 48 );
5965 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5966 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005967
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005968 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5969 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5970 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5971 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005972
Paul Bakker1ef83d62012-04-11 12:09:53 +00005973 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005974
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005975 mbedtls_md5_starts_ret( &md5 );
5976 mbedtls_md5_update_ret( &md5, session->master, 48 );
5977 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5978 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5979 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005980
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005981 mbedtls_sha1_starts_ret( &sha1 );
5982 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5983 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5984 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5985 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005989 mbedtls_md5_free( &md5 );
5990 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005991
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005992 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5993 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5994 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006001static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006003{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006004 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006005 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006006 mbedtls_md5_context md5;
6007 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006008 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006011 if( !session )
6012 session = ssl->session;
6013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006015
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006016 mbedtls_md5_init( &md5 );
6017 mbedtls_sha1_init( &sha1 );
6018
6019 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6020 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006021
Paul Bakker1ef83d62012-04-11 12:09:53 +00006022 /*
6023 * TLSv1:
6024 * hash = PRF( master, finished_label,
6025 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6026 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006029 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6030 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006031#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006034 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6035 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006036#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006039 ? "client finished"
6040 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006041
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006042 mbedtls_md5_finish_ret( &md5, padbuf );
6043 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006044
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006045 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006046 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006049
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006050 mbedtls_md5_free( &md5 );
6051 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006052
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006053 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6060#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006061static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006063{
6064 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006065 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006066 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006067 unsigned char padbuf[32];
6068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006070 if( !session )
6071 session = ssl->session;
6072
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006073 mbedtls_sha256_init( &sha256 );
6074
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006076
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006077 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006078
6079 /*
6080 * TLSv1.2:
6081 * hash = PRF( master, finished_label,
6082 * Hash( handshake ) )[0.11]
6083 */
6084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if !defined(MBEDTLS_SHA256_ALT)
6086 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006087 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006088#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006091 ? "client finished"
6092 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006093
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006094 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006095
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006096 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006097 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006100
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006101 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006102
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006103 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006110static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006112{
6113 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006114 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006115 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006116 unsigned char padbuf[48];
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006119 if( !session )
6120 session = ssl->session;
6121
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006122 mbedtls_sha512_init( &sha512 );
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006125
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006126 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006127
6128 /*
6129 * TLSv1.2:
6130 * hash = PRF( master, finished_label,
6131 * Hash( handshake ) )[0.11]
6132 */
6133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006135 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6136 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006137#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006140 ? "client finished"
6141 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006142
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006143 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006144
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006145 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006146 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006149
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006150 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006151
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006152 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156#endif /* MBEDTLS_SHA512_C */
6157#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006162
6163 /*
6164 * Free our handshake params
6165 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006166 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006168 ssl->handshake = NULL;
6169
6170 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006171 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006172 */
6173 if( ssl->transform )
6174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 mbedtls_ssl_transform_free( ssl->transform );
6176 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006177 }
6178 ssl->transform = ssl->transform_negotiate;
6179 ssl->transform_negotiate = NULL;
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006182}
6183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006185{
6186 int resume = ssl->handshake->resume;
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190#if defined(MBEDTLS_SSL_RENEGOTIATION)
6191 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006194 ssl->renego_records_seen = 0;
6195 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006196#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006197
6198 /*
6199 * Free the previous session and switch in the current one
6200 */
Paul Bakker0a597072012-09-25 21:55:46 +00006201 if( ssl->session )
6202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006204 /* RFC 7366 3.1: keep the EtM state */
6205 ssl->session_negotiate->encrypt_then_mac =
6206 ssl->session->encrypt_then_mac;
6207#endif
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 mbedtls_ssl_session_free( ssl->session );
6210 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006211 }
6212 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006213 ssl->session_negotiate = NULL;
6214
Paul Bakker0a597072012-09-25 21:55:46 +00006215 /*
6216 * Add cache entry
6217 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006218 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006219 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006220 resume == 0 )
6221 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006222 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006224 }
Paul Bakker0a597072012-09-25 21:55:46 +00006225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006228 ssl->handshake->flight != NULL )
6229 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006230 /* Cancel handshake timer */
6231 ssl_set_timer( ssl, 0 );
6232
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006233 /* Keep last flight around in case we need to resend it:
6234 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006236 }
6237 else
6238#endif
6239 ssl_handshake_wrapup_free_hs_transform( ssl );
6240
Paul Bakker48916f92012-09-16 19:57:18 +00006241 ssl->state++;
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006244}
6245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006247{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006248 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006251
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006252 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006253
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006254 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006255
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006256 /*
6257 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6258 * may define some other value. Currently (early 2016), no defined
6259 * ciphersuite does this (and this is unlikely to change as activity has
6260 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006265 ssl->verify_data_len = hash_len;
6266 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006267#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006268
Paul Bakker5121ce52009-01-03 21:22:43 +00006269 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6271 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006272
6273 /*
6274 * In case of session resuming, invert the client and server
6275 * ChangeCipherSpec messages order.
6276 */
Paul Bakker0a597072012-09-25 21:55:46 +00006277 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006284 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 }
6288 else
6289 ssl->state++;
6290
Paul Bakker48916f92012-09-16 19:57:18 +00006291 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006292 * Switch to our negotiated transform and session parameters for outbound
6293 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006299 {
6300 unsigned char i;
6301
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006302 /* Remember current epoch settings for resending */
6303 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006304 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006305
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006306 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006307 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006308
6309 /* Increment epoch */
6310 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006311 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006312 break;
6313
6314 /* The loop goes to its end iff the counter is wrapping */
6315 if( i == 0 )
6316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6318 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006319 }
6320 }
6321 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006323 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006324
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006325 ssl->transform_out = ssl->transform_negotiate;
6326 ssl->session_out = ssl->session_negotiate;
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6329 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6334 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006335 }
6336 }
6337#endif
6338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006342#endif
6343
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006344 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 return( ret );
6348 }
6349
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006350#if defined(MBEDTLS_SSL_PROTO_DTLS)
6351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6352 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6353 {
6354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6355 return( ret );
6356 }
6357#endif
6358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360
6361 return( 0 );
6362}
6363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006365#define SSL_MAX_HASH_LEN 36
6366#else
6367#define SSL_MAX_HASH_LEN 12
6368#endif
6369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006371{
Paul Bakker23986e52011-04-24 08:57:21 +00006372 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006373 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006374 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006378 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006379
Hanno Becker327c93b2018-08-15 13:56:18 +01006380 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 return( ret );
6384 }
6385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006389 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6390 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006392 }
6393
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006394 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if defined(MBEDTLS_SSL_PROTO_SSL3)
6396 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006397 hash_len = 36;
6398 else
6399#endif
6400 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6403 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006406 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6407 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 }
6410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006412 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006415 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6416 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 }
6419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006421 ssl->verify_data_len = hash_len;
6422 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006423#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006424
Paul Bakker0a597072012-09-25 21:55:46 +00006425 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006428 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006434#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006435 }
6436 else
6437 ssl->state++;
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006440 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006442#endif
6443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006445
6446 return( 0 );
6447}
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6454 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6455 mbedtls_md5_init( &handshake->fin_md5 );
6456 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006457 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6458 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6461#if defined(MBEDTLS_SHA256_C)
6462 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006463 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465#if defined(MBEDTLS_SHA512_C)
6466 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006467 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006470
6471 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006472
6473#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6474 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6475 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6476#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_DHM_C)
6479 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481#if defined(MBEDTLS_ECDH_C)
6482 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006483#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006484#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006485 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006486#if defined(MBEDTLS_SSL_CLI_C)
6487 handshake->ecjpake_cache = NULL;
6488 handshake->ecjpake_cache_len = 0;
6489#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006490#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006491
6492#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6493 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6494#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006495}
6496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6502 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 mbedtls_md_init( &transform->md_ctx_enc );
6505 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006506}
6507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006511}
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006514{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006515 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006516 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006518 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006520 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006521 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006522
6523 /*
6524 * Either the pointers are now NULL or cleared properly and can be freed.
6525 * Now allocate missing structures.
6526 */
6527 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006528 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006529 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006530 }
Paul Bakker48916f92012-09-16 19:57:18 +00006531
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006532 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006533 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006534 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006535 }
Paul Bakker48916f92012-09-16 19:57:18 +00006536
Paul Bakker82788fb2014-10-20 13:59:19 +02006537 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006538 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006539 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006540 }
Paul Bakker48916f92012-09-16 19:57:18 +00006541
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006542 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006543 if( ssl->handshake == NULL ||
6544 ssl->transform_negotiate == NULL ||
6545 ssl->session_negotiate == NULL )
6546 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 mbedtls_free( ssl->handshake );
6550 mbedtls_free( ssl->transform_negotiate );
6551 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006552
6553 ssl->handshake = NULL;
6554 ssl->transform_negotiate = NULL;
6555 ssl->session_negotiate = NULL;
6556
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006557 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006558 }
6559
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006560 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006562 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006563 ssl_handshake_params_init( ssl->handshake );
6564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006566 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6567 {
6568 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006569
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006570 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6571 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6572 else
6573 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006574
6575 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006576 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006577#endif
6578
Paul Bakker48916f92012-09-16 19:57:18 +00006579 return( 0 );
6580}
6581
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006582#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006583/* Dummy cookie callbacks for defaults */
6584static int ssl_cookie_write_dummy( void *ctx,
6585 unsigned char **p, unsigned char *end,
6586 const unsigned char *cli_id, size_t cli_id_len )
6587{
6588 ((void) ctx);
6589 ((void) p);
6590 ((void) end);
6591 ((void) cli_id);
6592 ((void) cli_id_len);
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006595}
6596
6597static int ssl_cookie_check_dummy( void *ctx,
6598 const unsigned char *cookie, size_t cookie_len,
6599 const unsigned char *cli_id, size_t cli_id_len )
6600{
6601 ((void) ctx);
6602 ((void) cookie);
6603 ((void) cookie_len);
6604 ((void) cli_id);
6605 ((void) cli_id_len);
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006608}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006609#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006610
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006611/* Once ssl->out_hdr as the address of the beginning of the
6612 * next outgoing record is set, deduce the other pointers.
6613 *
6614 * Note: For TLS, we save the implicit record sequence number
6615 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6616 * and the caller has to make sure there's space for this.
6617 */
6618
6619static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6620 mbedtls_ssl_transform *transform )
6621{
6622#if defined(MBEDTLS_SSL_PROTO_DTLS)
6623 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6624 {
6625 ssl->out_ctr = ssl->out_hdr + 3;
6626 ssl->out_len = ssl->out_hdr + 11;
6627 ssl->out_iv = ssl->out_hdr + 13;
6628 }
6629 else
6630#endif
6631 {
6632 ssl->out_ctr = ssl->out_hdr - 8;
6633 ssl->out_len = ssl->out_hdr + 3;
6634 ssl->out_iv = ssl->out_hdr + 5;
6635 }
6636
6637 /* Adjust out_msg to make space for explicit IV, if used. */
6638 if( transform != NULL &&
6639 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6640 {
6641 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6642 }
6643 else
6644 ssl->out_msg = ssl->out_iv;
6645}
6646
6647/* Once ssl->in_hdr as the address of the beginning of the
6648 * next incoming record is set, deduce the other pointers.
6649 *
6650 * Note: For TLS, we save the implicit record sequence number
6651 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6652 * and the caller has to make sure there's space for this.
6653 */
6654
6655static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6656 mbedtls_ssl_transform *transform )
6657{
6658#if defined(MBEDTLS_SSL_PROTO_DTLS)
6659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6660 {
6661 ssl->in_ctr = ssl->in_hdr + 3;
6662 ssl->in_len = ssl->in_hdr + 11;
6663 ssl->in_iv = ssl->in_hdr + 13;
6664 }
6665 else
6666#endif
6667 {
6668 ssl->in_ctr = ssl->in_hdr - 8;
6669 ssl->in_len = ssl->in_hdr + 3;
6670 ssl->in_iv = ssl->in_hdr + 5;
6671 }
6672
6673 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6674 if( transform != NULL &&
6675 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6676 {
6677 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6678 }
6679 else
6680 ssl->in_msg = ssl->in_iv;
6681}
6682
Paul Bakker5121ce52009-01-03 21:22:43 +00006683/*
6684 * Initialize an SSL context
6685 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006686void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6687{
6688 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6689}
6690
6691/*
6692 * Setup an SSL context
6693 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006694
6695static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6696{
6697 /* Set the incoming and outgoing record pointers. */
6698#if defined(MBEDTLS_SSL_PROTO_DTLS)
6699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6700 {
6701 ssl->out_hdr = ssl->out_buf;
6702 ssl->in_hdr = ssl->in_buf;
6703 }
6704 else
6705#endif /* MBEDTLS_SSL_PROTO_DTLS */
6706 {
6707 ssl->out_hdr = ssl->out_buf + 8;
6708 ssl->in_hdr = ssl->in_buf + 8;
6709 }
6710
6711 /* Derive other internal pointers. */
6712 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6713 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6714}
6715
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006716int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006717 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006718{
Paul Bakker48916f92012-09-16 19:57:18 +00006719 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006720
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006721 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006722
6723 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006724 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006725 */
Angus Grattond8213d02016-05-25 20:56:48 +10006726 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6727 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006728 {
Angus Grattond8213d02016-05-25 20:56:48 +10006729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6730 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6731 }
6732
6733 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6734 if( ssl->out_buf == NULL )
6735 {
6736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006738 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006739 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006740 }
6741
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006742 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006743
Paul Bakker48916f92012-09-16 19:57:18 +00006744 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6745 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006746
6747 return( 0 );
6748}
6749
6750/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006751 * Reset an initialized and used SSL context for re-use while retaining
6752 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006753 *
6754 * If partial is non-zero, keep data in the input buffer and client ID.
6755 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006756 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006757static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006758{
Paul Bakker48916f92012-09-16 19:57:18 +00006759 int ret;
6760
Hanno Becker7e772132018-08-10 12:38:21 +01006761#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6762 !defined(MBEDTLS_SSL_SRV_C)
6763 ((void) partial);
6764#endif
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006767
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006768 /* Cancel any possibly running timer */
6769 ssl_set_timer( ssl, 0 );
6770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#if defined(MBEDTLS_SSL_RENEGOTIATION)
6772 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006773 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006774
6775 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6777 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006778#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006780
Paul Bakker7eb013f2011-10-06 12:37:39 +00006781 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006782 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006783
6784 ssl->in_msgtype = 0;
6785 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006787 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006788 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006791 ssl_dtls_replay_reset( ssl );
6792#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006793
6794 ssl->in_hslen = 0;
6795 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006796
6797 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006798
6799 ssl->out_msgtype = 0;
6800 ssl->out_msglen = 0;
6801 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6803 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006804 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006805#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006806
Hanno Becker19859472018-08-06 09:40:20 +01006807 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6808
Paul Bakker48916f92012-09-16 19:57:18 +00006809 ssl->transform_in = NULL;
6810 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006811
Hanno Becker78640902018-08-13 16:35:15 +01006812 ssl->session_in = NULL;
6813 ssl->session_out = NULL;
6814
Angus Grattond8213d02016-05-25 20:56:48 +10006815 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006816
6817#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006818 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006819#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6820 {
6821 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006822 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006823 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6826 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6829 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6832 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006833 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006834 }
6835#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006836
Paul Bakker48916f92012-09-16 19:57:18 +00006837 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 mbedtls_ssl_transform_free( ssl->transform );
6840 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006841 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006842 }
Paul Bakker48916f92012-09-16 19:57:18 +00006843
Paul Bakkerc0463502013-02-14 11:19:38 +01006844 if( ssl->session )
6845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 mbedtls_ssl_session_free( ssl->session );
6847 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006848 ssl->session = NULL;
6849 }
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006852 ssl->alpn_chosen = NULL;
6853#endif
6854
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006855#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006856#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006857 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006858#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006859 {
6860 mbedtls_free( ssl->cli_id );
6861 ssl->cli_id = NULL;
6862 ssl->cli_id_len = 0;
6863 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006864#endif
6865
Paul Bakker48916f92012-09-16 19:57:18 +00006866 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6867 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006868
6869 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006870}
6871
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006872/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006873 * Reset an initialized and used SSL context for re-use while retaining
6874 * all application-set variables, function pointers and data.
6875 */
6876int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6877{
6878 return( ssl_session_reset_int( ssl, 0 ) );
6879}
6880
6881/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006882 * SSL set accessors
6883 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006884void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006885{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006886 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006887}
6888
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006889void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006890{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006891 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006892}
6893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006895void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006897 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006898}
6899#endif
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006902void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006903{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006904 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006905}
6906#endif
6907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006909
6910void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6911 unsigned allow_packing )
6912{
6913 ssl->disable_datagram_packing = !allow_packing;
6914}
6915
6916void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6917 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006918{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006919 conf->hs_timeout_min = min;
6920 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006921}
6922#endif
6923
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006924void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006926 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006927}
6928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006930void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006931 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006932 void *p_vrfy )
6933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006934 conf->f_vrfy = f_vrfy;
6935 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006938
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006939void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006940 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006941 void *p_rng )
6942{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006943 conf->f_rng = f_rng;
6944 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006945}
6946
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006947void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006948 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 void *p_dbg )
6950{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006951 conf->f_dbg = f_dbg;
6952 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006953}
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006956 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006957 mbedtls_ssl_send_t *f_send,
6958 mbedtls_ssl_recv_t *f_recv,
6959 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006960{
6961 ssl->p_bio = p_bio;
6962 ssl->f_send = f_send;
6963 ssl->f_recv = f_recv;
6964 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006965}
6966
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006967void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006968{
6969 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006970}
6971
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006972void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6973 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006974 mbedtls_ssl_set_timer_t *f_set_timer,
6975 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006976{
6977 ssl->p_timer = p_timer;
6978 ssl->f_set_timer = f_set_timer;
6979 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006980
6981 /* Make sure we start with no timer running */
6982 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006983}
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006986void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006987 void *p_cache,
6988 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6989 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006990{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006991 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006992 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006993 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#if defined(MBEDTLS_SSL_CLI_C)
6998int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006999{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007000 int ret;
7001
7002 if( ssl == NULL ||
7003 session == NULL ||
7004 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007005 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007008 }
7009
7010 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7011 return( ret );
7012
Paul Bakker0a597072012-09-25 21:55:46 +00007013 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007014
7015 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007018
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007019void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007020 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007021{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007022 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7023 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7024 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7025 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007026}
7027
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007028void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007029 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007030 int major, int minor )
7031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007033 return;
7034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007036 return;
7037
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007038 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007039}
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007042void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007043 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007044{
7045 conf->cert_profile = profile;
7046}
7047
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007048/* Append a new keycert entry to a (possibly empty) list */
7049static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7050 mbedtls_x509_crt *cert,
7051 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007052{
niisato8ee24222018-06-25 19:05:48 +09007053 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007054
niisato8ee24222018-06-25 19:05:48 +09007055 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7056 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007057 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007058
niisato8ee24222018-06-25 19:05:48 +09007059 new_cert->cert = cert;
7060 new_cert->key = key;
7061 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007062
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007063 /* Update head is the list was null, else add to the end */
7064 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007065 {
niisato8ee24222018-06-25 19:05:48 +09007066 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007067 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007068 else
7069 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007070 mbedtls_ssl_key_cert *cur = *head;
7071 while( cur->next != NULL )
7072 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007073 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007074 }
7075
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007076 return( 0 );
7077}
7078
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007079int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007080 mbedtls_x509_crt *own_cert,
7081 mbedtls_pk_context *pk_key )
7082{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007083 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007084}
7085
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007086void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007087 mbedtls_x509_crt *ca_chain,
7088 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007089{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007090 conf->ca_chain = ca_chain;
7091 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007094
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7096int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7097 mbedtls_x509_crt *own_cert,
7098 mbedtls_pk_context *pk_key )
7099{
7100 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7101 own_cert, pk_key ) );
7102}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007103
7104void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7105 mbedtls_x509_crt *ca_chain,
7106 mbedtls_x509_crl *ca_crl )
7107{
7108 ssl->handshake->sni_ca_chain = ca_chain;
7109 ssl->handshake->sni_ca_crl = ca_crl;
7110}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007111
7112void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7113 int authmode )
7114{
7115 ssl->handshake->sni_authmode = authmode;
7116}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007117#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7118
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007119#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007120/*
7121 * Set EC J-PAKE password for current handshake
7122 */
7123int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7124 const unsigned char *pw,
7125 size_t pw_len )
7126{
7127 mbedtls_ecjpake_role role;
7128
Janos Follath8eb64132016-06-03 15:40:57 +01007129 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7131
7132 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7133 role = MBEDTLS_ECJPAKE_SERVER;
7134 else
7135 role = MBEDTLS_ECJPAKE_CLIENT;
7136
7137 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7138 role,
7139 MBEDTLS_MD_SHA256,
7140 MBEDTLS_ECP_DP_SECP256R1,
7141 pw, pw_len ) );
7142}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007143#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007146int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007147 const unsigned char *psk, size_t psk_len,
7148 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007149{
Paul Bakker6db455e2013-09-18 17:29:31 +02007150 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007155
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007156 /* Identity len will be encoded on two bytes */
7157 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007158 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007159 {
7160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7161 }
7162
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007163 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007164 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007165 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007166
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007167 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007168 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007169 conf->psk_len = 0;
7170 }
7171 if( conf->psk_identity != NULL )
7172 {
7173 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007174 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007175 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007176 }
7177
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007178 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7179 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007180 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007181 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007182 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007183 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007184 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007185 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007186 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007187
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007188 conf->psk_len = psk_len;
7189 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007190
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007191 memcpy( conf->psk, psk, conf->psk_len );
7192 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007193
7194 return( 0 );
7195}
7196
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007197int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7198 const unsigned char *psk, size_t psk_len )
7199{
7200 if( psk == NULL || ssl->handshake == NULL )
7201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7202
7203 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7205
7206 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007207 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007208 mbedtls_platform_zeroize( ssl->handshake->psk,
7209 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007210 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007211 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007212 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007213
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007214 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007215 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007216
7217 ssl->handshake->psk_len = psk_len;
7218 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7219
7220 return( 0 );
7221}
7222
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007223void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007225 size_t),
7226 void *p_psk )
7227{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007228 conf->f_psk = f_psk;
7229 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007232
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007233#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007234
7235#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007236int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007237{
7238 int ret;
7239
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007240 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7241 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7242 {
7243 mbedtls_mpi_free( &conf->dhm_P );
7244 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007245 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007247
7248 return( 0 );
7249}
Hanno Becker470a8c42017-10-04 15:28:46 +01007250#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Hanno Beckera90658f2017-10-04 15:29:08 +01007252int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7253 const unsigned char *dhm_P, size_t P_len,
7254 const unsigned char *dhm_G, size_t G_len )
7255{
7256 int ret;
7257
7258 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7259 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7260 {
7261 mbedtls_mpi_free( &conf->dhm_P );
7262 mbedtls_mpi_free( &conf->dhm_G );
7263 return( ret );
7264 }
7265
7266 return( 0 );
7267}
Paul Bakker5121ce52009-01-03 21:22:43 +00007268
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007269int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007270{
7271 int ret;
7272
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007273 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7274 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7275 {
7276 mbedtls_mpi_free( &conf->dhm_P );
7277 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007278 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007279 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007280
7281 return( 0 );
7282}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007283#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007284
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007285#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7286/*
7287 * Set the minimum length for Diffie-Hellman parameters
7288 */
7289void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7290 unsigned int bitlen )
7291{
7292 conf->dhm_min_bitlen = bitlen;
7293}
7294#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7295
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007296#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007297/*
7298 * Set allowed/preferred hashes for handshake signatures
7299 */
7300void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7301 const int *hashes )
7302{
7303 conf->sig_hashes = hashes;
7304}
Hanno Becker947194e2017-04-07 13:25:49 +01007305#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007306
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007307#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007308/*
7309 * Set the allowed elliptic curves
7310 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007311void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007312 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007314 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007315}
Hanno Becker947194e2017-04-07 13:25:49 +01007316#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007317
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007318#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007320{
Hanno Becker947194e2017-04-07 13:25:49 +01007321 /* Initialize to suppress unnecessary compiler warning */
7322 size_t hostname_len = 0;
7323
7324 /* Check if new hostname is valid before
7325 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007326 if( hostname != NULL )
7327 {
7328 hostname_len = strlen( hostname );
7329
7330 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7332 }
7333
7334 /* Now it's clear that we will overwrite the old hostname,
7335 * so we can free it safely */
7336
7337 if( ssl->hostname != NULL )
7338 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007339 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007340 mbedtls_free( ssl->hostname );
7341 }
7342
7343 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007344
Paul Bakker5121ce52009-01-03 21:22:43 +00007345 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007346 {
7347 ssl->hostname = NULL;
7348 }
7349 else
7350 {
7351 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007352 if( ssl->hostname == NULL )
7353 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007354
Hanno Becker947194e2017-04-07 13:25:49 +01007355 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007356
Hanno Becker947194e2017-04-07 13:25:49 +01007357 ssl->hostname[hostname_len] = '\0';
7358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007359
7360 return( 0 );
7361}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007362#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007363
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007364#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007365void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007367 const unsigned char *, size_t),
7368 void *p_sni )
7369{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007370 conf->f_sni = f_sni;
7371 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007376int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007377{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007378 size_t cur_len, tot_len;
7379 const char **p;
7380
7381 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007382 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7383 * MUST NOT be truncated."
7384 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007385 */
7386 tot_len = 0;
7387 for( p = protos; *p != NULL; p++ )
7388 {
7389 cur_len = strlen( *p );
7390 tot_len += cur_len;
7391
7392 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007394 }
7395
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007396 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007397
7398 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007399}
7400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007402{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007403 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007406
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007407void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007409 conf->max_major_ver = major;
7410 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007411}
7412
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007413void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007415 conf->min_major_ver = major;
7416 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007417}
7418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007420void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007421{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007422 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007423}
7424#endif
7425
Janos Follath088ce432017-04-10 12:42:31 +01007426#if defined(MBEDTLS_SSL_SRV_C)
7427void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7428 char cert_req_ca_list )
7429{
7430 conf->cert_req_ca_list = cert_req_ca_list;
7431}
7432#endif
7433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007435void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007436{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007437 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007438}
7439#endif
7440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007442void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007444 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007445}
7446#endif
7447
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007448#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007449void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007451 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007452}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007453#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007454
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007455#if defined(MBEDTLS_SSL_PROTO_DTLS)
7456void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7457{
7458 conf->mtu = mtu;
7459}
7460#endif
7461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007463int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007466 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007469 }
7470
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007471 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007472
7473 return( 0 );
7474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007478void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007479{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007480 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007485void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007486{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007487 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007488}
7489#endif
7490
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007491void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007492{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007493 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007494}
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007497void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007499 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007500}
7501
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007502void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007504 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007505}
7506
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007507void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007508 const unsigned char period[8] )
7509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007510 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007515#if defined(MBEDTLS_SSL_CLI_C)
7516void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007517{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007518 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007519}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007520#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007521
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007522#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007523void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7524 mbedtls_ssl_ticket_write_t *f_ticket_write,
7525 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7526 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007527{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007528 conf->f_ticket_write = f_ticket_write;
7529 conf->f_ticket_parse = f_ticket_parse;
7530 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007531}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007534
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007535#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7536void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7537 mbedtls_ssl_export_keys_t *f_export_keys,
7538 void *p_export_keys )
7539{
7540 conf->f_export_keys = f_export_keys;
7541 conf->p_export_keys = p_export_keys;
7542}
7543#endif
7544
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007545#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007546void mbedtls_ssl_conf_async_private_cb(
7547 mbedtls_ssl_config *conf,
7548 mbedtls_ssl_async_sign_t *f_async_sign,
7549 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7550 mbedtls_ssl_async_resume_t *f_async_resume,
7551 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007552 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007553{
7554 conf->f_async_sign_start = f_async_sign;
7555 conf->f_async_decrypt_start = f_async_decrypt;
7556 conf->f_async_resume = f_async_resume;
7557 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007558 conf->p_async_config_data = async_config_data;
7559}
7560
Gilles Peskine8f97af72018-04-26 11:46:10 +02007561void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7562{
7563 return( conf->p_async_config_data );
7564}
7565
Gilles Peskine1febfef2018-04-30 11:54:39 +02007566void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007567{
7568 if( ssl->handshake == NULL )
7569 return( NULL );
7570 else
7571 return( ssl->handshake->user_async_ctx );
7572}
7573
Gilles Peskine1febfef2018-04-30 11:54:39 +02007574void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007575 void *ctx )
7576{
7577 if( ssl->handshake != NULL )
7578 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007579}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007580#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007581
Paul Bakker5121ce52009-01-03 21:22:43 +00007582/*
7583 * SSL get accessors
7584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007586{
7587 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7588}
7589
Hanno Becker8b170a02017-10-10 11:51:19 +01007590int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7591{
7592 /*
7593 * Case A: We're currently holding back
7594 * a message for further processing.
7595 */
7596
7597 if( ssl->keep_current_message == 1 )
7598 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007600 return( 1 );
7601 }
7602
7603 /*
7604 * Case B: Further records are pending in the current datagram.
7605 */
7606
7607#if defined(MBEDTLS_SSL_PROTO_DTLS)
7608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7609 ssl->in_left > ssl->next_record_offset )
7610 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007612 return( 1 );
7613 }
7614#endif /* MBEDTLS_SSL_PROTO_DTLS */
7615
7616 /*
7617 * Case C: A handshake message is being processed.
7618 */
7619
Hanno Becker8b170a02017-10-10 11:51:19 +01007620 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7621 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007623 return( 1 );
7624 }
7625
7626 /*
7627 * Case D: An application data message is being processed
7628 */
7629 if( ssl->in_offt != NULL )
7630 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007632 return( 1 );
7633 }
7634
7635 /*
7636 * In all other cases, the rest of the message can be dropped.
7637 * As in ssl_read_record_layer, this needs to be adapted if
7638 * we implement support for multiple alerts in single records.
7639 */
7640
7641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7642 return( 0 );
7643}
7644
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007645uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007646{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007647 if( ssl->session != NULL )
7648 return( ssl->session->verify_result );
7649
7650 if( ssl->session_negotiate != NULL )
7651 return( ssl->session_negotiate->verify_result );
7652
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007653 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007654}
7655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007657{
Paul Bakker926c8e42013-03-06 10:23:34 +01007658 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007659 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007662}
7663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007668 {
7669 switch( ssl->minor_ver )
7670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007672 return( "DTLSv1.0" );
7673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007675 return( "DTLSv1.2" );
7676
7677 default:
7678 return( "unknown (DTLS)" );
7679 }
7680 }
7681#endif
7682
Paul Bakker43ca69c2011-01-15 17:35:19 +00007683 switch( ssl->minor_ver )
7684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007686 return( "SSLv3.0" );
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007689 return( "TLSv1.0" );
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007692 return( "TLSv1.1" );
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695 return( "TLSv1.2" );
7696
Paul Bakker43ca69c2011-01-15 17:35:19 +00007697 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007698 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007699 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007700}
7701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007703{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007704 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007706 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007707
Hanno Becker78640902018-08-13 16:35:15 +01007708 if( transform == NULL )
7709 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#if defined(MBEDTLS_ZLIB_SUPPORT)
7712 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7713 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007714 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007715#endif
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 case MBEDTLS_MODE_GCM:
7720 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007721 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007723 transform_expansion = transform->minlen;
7724 break;
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007727
7728 block_size = mbedtls_cipher_get_block_size(
7729 &transform->cipher_ctx_enc );
7730
7731#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7732 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7733 {
7734 /* Expansion due to addition of
7735 * - MAC
7736 * - CBC padding (theoretically up to 256 bytes, but
7737 * we never use more than block_size)
7738 * - explicit IV
7739 */
7740 transform_expansion = transform->maclen + 2 * block_size;
7741 }
7742 else
7743#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7744 {
7745 /* No explicit IV prior to TLS 1.1. */
7746 transform_expansion = transform->maclen + block_size;
7747 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007748 break;
7749
7750 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007753 }
7754
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007755 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007756}
7757
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007758#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7759size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7760{
7761 size_t max_len;
7762
7763 /*
7764 * Assume mfl_code is correct since it was checked when set
7765 */
Angus Grattond8213d02016-05-25 20:56:48 +10007766 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007767
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007768 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007769 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007770 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007771 {
Angus Grattond8213d02016-05-25 20:56:48 +10007772 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007773 }
7774
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007775 /* During a handshake, use the value being negotiated */
7776 if( ssl->session_negotiate != NULL &&
7777 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7778 {
7779 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7780 }
7781
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007782 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007783}
7784#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7785
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007786int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7787{
7788 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7789
7790#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7791 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7792
7793 if( max_len > mfl )
7794 max_len = mfl;
7795#endif
7796
7797#if defined(MBEDTLS_SSL_PROTO_DTLS)
7798 if( ssl->conf->mtu != 0 )
7799 {
7800 const size_t mtu = ssl->conf->mtu;
7801 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7802 const size_t overhead = (size_t) ret;
7803
7804 if( ret < 0 )
7805 return( ret );
7806
7807 if( mtu <= overhead )
7808 {
7809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7810 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7811 }
7812
7813 if( max_len > mtu - overhead )
7814 max_len = mtu - overhead;
7815 }
7816#endif
7817
Hanno Becker0defedb2018-08-10 12:35:02 +01007818#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7819 !defined(MBEDTLS_SSL_PROTO_DTLS)
7820 ((void) ssl);
7821#endif
7822
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007823 return( (int) max_len );
7824}
7825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826#if defined(MBEDTLS_X509_CRT_PARSE_C)
7827const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007828{
7829 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007830 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007831
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007832 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#if defined(MBEDTLS_SSL_CLI_C)
7837int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007838{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007839 if( ssl == NULL ||
7840 dst == NULL ||
7841 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007842 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007845 }
7846
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007847 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007850
Paul Bakker5121ce52009-01-03 21:22:43 +00007851/*
Paul Bakker1961b702013-01-25 14:49:24 +01007852 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007857
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007858 if( ssl == NULL || ssl->conf == NULL )
7859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007862 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007866 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007868#endif
7869
Paul Bakker1961b702013-01-25 14:49:24 +01007870 return( ret );
7871}
7872
7873/*
7874 * Perform the SSL handshake
7875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007877{
7878 int ret = 0;
7879
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007880 if( ssl == NULL || ssl->conf == NULL )
7881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007888
7889 if( ret != 0 )
7890 break;
7891 }
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007894
7895 return( ret );
7896}
7897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_RENEGOTIATION)
7899#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007900/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007901 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007904{
7905 int ret;
7906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007908
7909 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7911 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007912
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007913 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007914 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007916 return( ret );
7917 }
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007920
7921 return( 0 );
7922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007924
7925/*
7926 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 * - any side: calling mbedtls_ssl_renegotiate(),
7928 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7929 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007930 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007931 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007935{
7936 int ret;
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007939
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007940 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7941 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007942
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007943 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7944 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007948 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007950 ssl->handshake->out_msg_seq = 1;
7951 else
7952 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007953 }
7954#endif
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7957 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007962 return( ret );
7963 }
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007966
7967 return( 0 );
7968}
7969
7970/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007971 * Renegotiate current connection on client,
7972 * or request renegotiation on server
7973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007977
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007978 if( ssl == NULL || ssl->conf == NULL )
7979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007982 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007983 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007989
7990 /* Did we already try/start sending HelloRequest? */
7991 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007993
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007994 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007999 /*
8000 * On client, either start the renegotiation process or,
8001 * if already in progress, continue the handshake
8002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008007
8008 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008011 return( ret );
8012 }
8013 }
8014 else
8015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008019 return( ret );
8020 }
8021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008023
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008024 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008025}
8026
8027/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008028 * Check record counters and renegotiate if they're above the limit.
8029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008031{
Andres AG2196c7f2016-12-15 17:01:16 +00008032 size_t ep_len = ssl_ep_len( ssl );
8033 int in_ctr_cmp;
8034 int out_ctr_cmp;
8035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8037 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008038 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008039 {
8040 return( 0 );
8041 }
8042
Andres AG2196c7f2016-12-15 17:01:16 +00008043 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8044 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008045 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008046 ssl->conf->renego_period + ep_len, 8 - ep_len );
8047
8048 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008049 {
8050 return( 0 );
8051 }
8052
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008057
8058/*
8059 * Receive application data decrypted from the SSL layer
8060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008062{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008063 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008064 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008065
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008066 if( ssl == NULL || ssl->conf == NULL )
8067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008075 return( ret );
8076
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008077 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008079 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008080 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008081 return( ret );
8082 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008083 }
8084#endif
8085
Hanno Becker4a810fb2017-05-24 16:27:30 +01008086 /*
8087 * Check if renegotiation is necessary and/or handshake is
8088 * in process. If yes, perform/continue, and fall through
8089 * if an unexpected packet is received while the client
8090 * is waiting for the ServerHello.
8091 *
8092 * (There is no equivalent to the last condition on
8093 * the server-side as it is not treated as within
8094 * a handshake while waiting for the ClientHello
8095 * after a renegotiation request.)
8096 */
8097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008099 ret = ssl_check_ctr_renegotiate( ssl );
8100 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8101 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008104 return( ret );
8105 }
8106#endif
8107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008111 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8112 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008115 return( ret );
8116 }
8117 }
8118
Hanno Beckere41158b2017-10-23 13:30:32 +01008119 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008120 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008121 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008122 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008123 if( ssl->f_get_timer != NULL &&
8124 ssl->f_get_timer( ssl->p_timer ) == -1 )
8125 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008126 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008127 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008128
Hanno Becker327c93b2018-08-15 13:56:18 +01008129 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008130 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008131 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8132 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008133
Hanno Becker4a810fb2017-05-24 16:27:30 +01008134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8135 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008136 }
8137
8138 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008140 {
8141 /*
8142 * OpenSSL sends empty messages to randomize the IV
8143 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008144 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008147 return( 0 );
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008150 return( ret );
8151 }
8152 }
8153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008157
Hanno Becker4a810fb2017-05-24 16:27:30 +01008158 /*
8159 * - For client-side, expect SERVER_HELLO_REQUEST.
8160 * - For server-side, expect CLIENT_HELLO.
8161 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8162 */
8163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008165 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008167 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008170
8171 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008174 {
8175 continue;
8176 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008179 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008180#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008181
Hanno Becker4a810fb2017-05-24 16:27:30 +01008182#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008183 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008187
8188 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008190 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008191 {
8192 continue;
8193 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008196 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008197#endif /* MBEDTLS_SSL_SRV_C */
8198
Hanno Becker21df7f92017-10-17 11:03:26 +01008199#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008200 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008201 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8202 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8203 ssl->conf->allow_legacy_renegotiation ==
8204 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8205 {
8206 /*
8207 * Accept renegotiation request
8208 */
Paul Bakker48916f92012-09-16 19:57:18 +00008209
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008210 /* DTLS clients need to know renego is server-initiated */
8211#if defined(MBEDTLS_SSL_PROTO_DTLS)
8212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8213 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8214 {
8215 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8216 }
8217#endif
8218 ret = ssl_start_renegotiation( ssl );
8219 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8220 ret != 0 )
8221 {
8222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8223 return( ret );
8224 }
8225 }
8226 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008227#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008228 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008229 /*
8230 * Refuse renegotiation
8231 */
8232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#if defined(MBEDTLS_SSL_PROTO_SSL3)
8236 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008237 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008238 /* SSLv3 does not have a "no_renegotiation" warning, so
8239 we send a fatal alert and abort the connection. */
8240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8241 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8242 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008243 }
8244 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8246#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8247 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8248 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8251 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8252 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008253 {
8254 return( ret );
8255 }
Paul Bakker48916f92012-09-16 19:57:18 +00008256 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8259 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008263 }
Paul Bakker48916f92012-09-16 19:57:18 +00008264 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008265
Hanno Becker90333da2017-10-10 11:27:13 +01008266 /* At this point, we don't know whether the renegotiation has been
8267 * completed or not. The cases to consider are the following:
8268 * 1) The renegotiation is complete. In this case, no new record
8269 * has been read yet.
8270 * 2) The renegotiation is incomplete because the client received
8271 * an application data record while awaiting the ServerHello.
8272 * 3) The renegotiation is incomplete because the client received
8273 * a non-handshake, non-application data message while awaiting
8274 * the ServerHello.
8275 * In each of these case, looping will be the proper action:
8276 * - For 1), the next iteration will read a new record and check
8277 * if it's application data.
8278 * - For 2), the loop condition isn't satisfied as application data
8279 * is present, hence continue is the same as break
8280 * - For 3), the loop condition is satisfied and read_record
8281 * will re-deliver the message that was held back by the client
8282 * when expecting the ServerHello.
8283 */
8284 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008285 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008286#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008288 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008289 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008290 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008291 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008294 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008296 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008297 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8302 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008305 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008306 }
8307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8311 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 }
8313
8314 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008315
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008316 /* We're going to return something now, cancel timer,
8317 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008319 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008320
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008322 /* If we requested renego but received AppData, resend HelloRequest.
8323 * Do it now, after setting in_offt, to avoid taking this branch
8324 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008326 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008328 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008329 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008332 return( ret );
8333 }
8334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008337 }
8338
8339 n = ( len < ssl->in_msglen )
8340 ? len : ssl->in_msglen;
8341
8342 memcpy( buf, ssl->in_offt, n );
8343 ssl->in_msglen -= n;
8344
8345 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008346 {
8347 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008348 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008349 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008350 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008351 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008352 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008353 /* more data available */
8354 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008358
Paul Bakker23986e52011-04-24 08:57:21 +00008359 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008360}
8361
8362/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008363 * Send application data to be encrypted by the SSL layer, taking care of max
8364 * fragment length and buffer size.
8365 *
8366 * According to RFC 5246 Section 6.2.1:
8367 *
8368 * Zero-length fragments of Application data MAY be sent as they are
8369 * potentially useful as a traffic analysis countermeasure.
8370 *
8371 * Therefore, it is possible that the input message length is 0 and the
8372 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008373 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008374static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008375 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008376{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008377 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8378 const size_t max_len = (size_t) ret;
8379
8380 if( ret < 0 )
8381 {
8382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8383 return( ret );
8384 }
8385
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008386 if( len > max_len )
8387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008389 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008392 "maximum fragment length: %d > %d",
8393 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008395 }
8396 else
8397#endif
8398 len = max_len;
8399 }
Paul Bakker887bd502011-06-08 13:10:54 +00008400
Paul Bakker5121ce52009-01-03 21:22:43 +00008401 if( ssl->out_left != 0 )
8402 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008403 /*
8404 * The user has previously tried to send the data and
8405 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8406 * written. In this case, we expect the high-level write function
8407 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008412 return( ret );
8413 }
8414 }
Paul Bakker887bd502011-06-08 13:10:54 +00008415 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008416 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008417 /*
8418 * The user is trying to send a message the first time, so we need to
8419 * copy the data into the internal buffers and setup the data structure
8420 * to keep track of partial writes
8421 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008422 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008424 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008425
Hanno Becker67bc7c32018-08-06 11:33:50 +01008426 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008429 return( ret );
8430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008431 }
8432
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008433 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008434}
8435
8436/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008437 * Write application data, doing 1/n-1 splitting if necessary.
8438 *
8439 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008440 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008441 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008444static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008445 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008446{
8447 int ret;
8448
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008449 if( ssl->conf->cbc_record_splitting ==
8450 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008451 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8453 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8454 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008455 {
8456 return( ssl_write_real( ssl, buf, len ) );
8457 }
8458
8459 if( ssl->split_done == 0 )
8460 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008461 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008462 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008463 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008464 }
8465
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008466 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8467 return( ret );
8468 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008469
8470 return( ret + 1 );
8471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008473
8474/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008475 * Write application data (public-facing wrapper)
8476 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008477int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008478{
8479 int ret;
8480
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008482
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008483 if( ssl == NULL || ssl->conf == NULL )
8484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8485
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008486#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008487 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8488 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008489 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008490 return( ret );
8491 }
8492#endif
8493
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008494 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008495 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008496 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008497 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008499 return( ret );
8500 }
8501 }
8502
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008503#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008504 ret = ssl_write_split( ssl, buf, len );
8505#else
8506 ret = ssl_write_real( ssl, buf, len );
8507#endif
8508
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008510
8511 return( ret );
8512}
8513
8514/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008515 * Notify the peer that the connection is being closed
8516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008518{
8519 int ret;
8520
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008521 if( ssl == NULL || ssl->conf == NULL )
8522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008525
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008526 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8532 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8533 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008536 return( ret );
8537 }
8538 }
8539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008541
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008542 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008543}
8544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008546{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008547 if( transform == NULL )
8548 return;
8549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008551 deflateEnd( &transform->ctx_deflate );
8552 inflateEnd( &transform->ctx_inflate );
8553#endif
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8556 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 mbedtls_md_free( &transform->md_ctx_enc );
8559 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008560
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008561 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008562}
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#if defined(MBEDTLS_X509_CRT_PARSE_C)
8565static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008568
8569 while( cur != NULL )
8570 {
8571 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008573 cur = next;
8574 }
8575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008577
Hanno Becker0271f962018-08-16 13:23:47 +01008578#if defined(MBEDTLS_SSL_PROTO_DTLS)
8579
8580static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8581{
8582 unsigned offset;
8583 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8584
8585 if( hs == NULL )
8586 return;
8587
8588 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008589 ssl_buffering_free_slot( ssl, offset );
8590}
8591
8592static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8593 uint8_t slot )
8594{
8595 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8596 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
8597 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008598 {
Hanno Beckere605b192018-08-21 15:59:07 +01008599 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8600 mbedtls_free( hs_buf->data );
8601 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008602 }
8603}
8604
8605#endif /* MBEDTLS_SSL_PROTO_DTLS */
8606
Gilles Peskine9b562d52018-04-25 20:32:43 +02008607void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008608{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008609 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8610
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008611 if( handshake == NULL )
8612 return;
8613
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008614#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8615 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8616 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008617 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008618 handshake->async_in_progress = 0;
8619 }
8620#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8621
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008622#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8623 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8624 mbedtls_md5_free( &handshake->fin_md5 );
8625 mbedtls_sha1_free( &handshake->fin_sha1 );
8626#endif
8627#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8628#if defined(MBEDTLS_SHA256_C)
8629 mbedtls_sha256_free( &handshake->fin_sha256 );
8630#endif
8631#if defined(MBEDTLS_SHA512_C)
8632 mbedtls_sha512_free( &handshake->fin_sha512 );
8633#endif
8634#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636#if defined(MBEDTLS_DHM_C)
8637 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008638#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#if defined(MBEDTLS_ECDH_C)
8640 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008641#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008642#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008643 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008644#if defined(MBEDTLS_SSL_CLI_C)
8645 mbedtls_free( handshake->ecjpake_cache );
8646 handshake->ecjpake_cache = NULL;
8647 handshake->ecjpake_cache_len = 0;
8648#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008649#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008650
Janos Follath4ae5c292016-02-10 11:27:43 +00008651#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8652 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008653 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008655#endif
8656
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008657#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8658 if( handshake->psk != NULL )
8659 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008660 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008661 mbedtls_free( handshake->psk );
8662 }
8663#endif
8664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8666 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008667 /*
8668 * Free only the linked list wrapper, not the keys themselves
8669 * since the belong to the SNI callback
8670 */
8671 if( handshake->sni_key_cert != NULL )
8672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008674
8675 while( cur != NULL )
8676 {
8677 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008679 cur = next;
8680 }
8681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684#if defined(MBEDTLS_SSL_PROTO_DTLS)
8685 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008686 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008687 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008688 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008689#endif
8690
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008691 mbedtls_platform_zeroize( handshake,
8692 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008693}
8694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008696{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008697 if( session == NULL )
8698 return;
8699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008701 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 mbedtls_x509_crt_free( session->peer_cert );
8704 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008705 }
Paul Bakkered27a042013-04-18 22:46:23 +02008706#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008707
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008708#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008710#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008711
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008712 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008713}
8714
Paul Bakker5121ce52009-01-03 21:22:43 +00008715/*
8716 * Free an SSL context
8717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008719{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008720 if( ssl == NULL )
8721 return;
8722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008724
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008725 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008726 {
Angus Grattond8213d02016-05-25 20:56:48 +10008727 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008729 }
8730
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008731 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008732 {
Angus Grattond8213d02016-05-25 20:56:48 +10008733 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008735 }
8736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008738 if( ssl->compress_buf != NULL )
8739 {
Angus Grattond8213d02016-05-25 20:56:48 +10008740 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008742 }
8743#endif
8744
Paul Bakker48916f92012-09-16 19:57:18 +00008745 if( ssl->transform )
8746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 mbedtls_ssl_transform_free( ssl->transform );
8748 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008749 }
8750
8751 if( ssl->handshake )
8752 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008753 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8755 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 mbedtls_free( ssl->handshake );
8758 mbedtls_free( ssl->transform_negotiate );
8759 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008760 }
8761
Paul Bakkerc0463502013-02-14 11:19:38 +01008762 if( ssl->session )
8763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 mbedtls_ssl_session_free( ssl->session );
8765 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008766 }
8767
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008768#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008769 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008770 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008771 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008773 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008774#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8777 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8780 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008781 }
8782#endif
8783
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008784#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008786#endif
8787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008789
Paul Bakker86f04f42013-02-14 11:20:09 +01008790 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008791 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008792}
8793
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008794/*
8795 * Initialze mbedtls_ssl_config
8796 */
8797void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8798{
8799 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8800}
8801
Simon Butcherc97b6972015-12-27 23:48:17 +00008802#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008803static int ssl_preset_default_hashes[] = {
8804#if defined(MBEDTLS_SHA512_C)
8805 MBEDTLS_MD_SHA512,
8806 MBEDTLS_MD_SHA384,
8807#endif
8808#if defined(MBEDTLS_SHA256_C)
8809 MBEDTLS_MD_SHA256,
8810 MBEDTLS_MD_SHA224,
8811#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008812#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008813 MBEDTLS_MD_SHA1,
8814#endif
8815 MBEDTLS_MD_NONE
8816};
Simon Butcherc97b6972015-12-27 23:48:17 +00008817#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008818
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008819static int ssl_preset_suiteb_ciphersuites[] = {
8820 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8821 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8822 0
8823};
8824
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008825#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008826static int ssl_preset_suiteb_hashes[] = {
8827 MBEDTLS_MD_SHA256,
8828 MBEDTLS_MD_SHA384,
8829 MBEDTLS_MD_NONE
8830};
8831#endif
8832
8833#if defined(MBEDTLS_ECP_C)
8834static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8835 MBEDTLS_ECP_DP_SECP256R1,
8836 MBEDTLS_ECP_DP_SECP384R1,
8837 MBEDTLS_ECP_DP_NONE
8838};
8839#endif
8840
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008841/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008842 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008843 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008844int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008845 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008846{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008847#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008848 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008849#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008850
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008851 /* Use the functions here so that they are covered in tests,
8852 * but otherwise access member directly for efficiency */
8853 mbedtls_ssl_conf_endpoint( conf, endpoint );
8854 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008855
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008856 /*
8857 * Things that are common to all presets
8858 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008859#if defined(MBEDTLS_SSL_CLI_C)
8860 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8861 {
8862 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8863#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8864 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8865#endif
8866 }
8867#endif
8868
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008869#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008870 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008871#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008872
8873#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8874 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8875#endif
8876
8877#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8878 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8879#endif
8880
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008881#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8882 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8883#endif
8884
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008885#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008886 conf->f_cookie_write = ssl_cookie_write_dummy;
8887 conf->f_cookie_check = ssl_cookie_check_dummy;
8888#endif
8889
8890#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8891 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8892#endif
8893
Janos Follath088ce432017-04-10 12:42:31 +01008894#if defined(MBEDTLS_SSL_SRV_C)
8895 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8896#endif
8897
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008898#if defined(MBEDTLS_SSL_PROTO_DTLS)
8899 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8900 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8901#endif
8902
8903#if defined(MBEDTLS_SSL_RENEGOTIATION)
8904 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008905 memset( conf->renego_period, 0x00, 2 );
8906 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008907#endif
8908
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008909#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8910 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8911 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008912 const unsigned char dhm_p[] =
8913 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8914 const unsigned char dhm_g[] =
8915 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8916
Hanno Beckera90658f2017-10-04 15:29:08 +01008917 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8918 dhm_p, sizeof( dhm_p ),
8919 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008920 {
8921 return( ret );
8922 }
8923 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008924#endif
8925
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008926 /*
8927 * Preset-specific defaults
8928 */
8929 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008930 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008931 /*
8932 * NSA Suite B
8933 */
8934 case MBEDTLS_SSL_PRESET_SUITEB:
8935 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8936 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8937 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8938 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
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 ssl_preset_suiteb_ciphersuites;
8945
8946#if defined(MBEDTLS_X509_CRT_PARSE_C)
8947 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008948#endif
8949
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008950#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008951 conf->sig_hashes = ssl_preset_suiteb_hashes;
8952#endif
8953
8954#if defined(MBEDTLS_ECP_C)
8955 conf->curve_list = ssl_preset_suiteb_curves;
8956#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008957 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008958
8959 /*
8960 * Default
8961 */
8962 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008963 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8964 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8965 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8966 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8967 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8968 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8969 MBEDTLS_SSL_MIN_MINOR_VERSION :
8970 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008971 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8972 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8973
8974#if defined(MBEDTLS_SSL_PROTO_DTLS)
8975 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8976 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8977#endif
8978
8979 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8980 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8981 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8982 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8983 mbedtls_ssl_list_ciphersuites();
8984
8985#if defined(MBEDTLS_X509_CRT_PARSE_C)
8986 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8987#endif
8988
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008989#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008990 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008991#endif
8992
8993#if defined(MBEDTLS_ECP_C)
8994 conf->curve_list = mbedtls_ecp_grp_id_list();
8995#endif
8996
8997#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8998 conf->dhm_min_bitlen = 1024;
8999#endif
9000 }
9001
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009002 return( 0 );
9003}
9004
9005/*
9006 * Free mbedtls_ssl_config
9007 */
9008void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9009{
9010#if defined(MBEDTLS_DHM_C)
9011 mbedtls_mpi_free( &conf->dhm_P );
9012 mbedtls_mpi_free( &conf->dhm_G );
9013#endif
9014
9015#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9016 if( conf->psk != NULL )
9017 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009018 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009019 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009020 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009021 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009022 }
9023
9024 if( conf->psk_identity != NULL )
9025 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009026 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009027 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009028 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009029 conf->psk_identity_len = 0;
9030 }
9031#endif
9032
9033#if defined(MBEDTLS_X509_CRT_PARSE_C)
9034 ssl_key_cert_free( conf->key_cert );
9035#endif
9036
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009037 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009038}
9039
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009040#if defined(MBEDTLS_PK_C) && \
9041 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009042/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047#if defined(MBEDTLS_RSA_C)
9048 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9049 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051#if defined(MBEDTLS_ECDSA_C)
9052 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9053 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009054#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009056}
9057
Hanno Becker7e5437a2017-04-28 17:15:26 +01009058unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9059{
9060 switch( type ) {
9061 case MBEDTLS_PK_RSA:
9062 return( MBEDTLS_SSL_SIG_RSA );
9063 case MBEDTLS_PK_ECDSA:
9064 case MBEDTLS_PK_ECKEY:
9065 return( MBEDTLS_SSL_SIG_ECDSA );
9066 default:
9067 return( MBEDTLS_SSL_SIG_ANON );
9068 }
9069}
9070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009072{
9073 switch( sig )
9074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_RSA_C)
9076 case MBEDTLS_SSL_SIG_RSA:
9077 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079#if defined(MBEDTLS_ECDSA_C)
9080 case MBEDTLS_SSL_SIG_ECDSA:
9081 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009082#endif
9083 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009085 }
9086}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009087#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009088
Hanno Becker7e5437a2017-04-28 17:15:26 +01009089#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9090 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9091
9092/* Find an entry in a signature-hash set matching a given hash algorithm. */
9093mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9094 mbedtls_pk_type_t sig_alg )
9095{
9096 switch( sig_alg )
9097 {
9098 case MBEDTLS_PK_RSA:
9099 return( set->rsa );
9100 case MBEDTLS_PK_ECDSA:
9101 return( set->ecdsa );
9102 default:
9103 return( MBEDTLS_MD_NONE );
9104 }
9105}
9106
9107/* Add a signature-hash-pair to a signature-hash set */
9108void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9109 mbedtls_pk_type_t sig_alg,
9110 mbedtls_md_type_t md_alg )
9111{
9112 switch( sig_alg )
9113 {
9114 case MBEDTLS_PK_RSA:
9115 if( set->rsa == MBEDTLS_MD_NONE )
9116 set->rsa = md_alg;
9117 break;
9118
9119 case MBEDTLS_PK_ECDSA:
9120 if( set->ecdsa == MBEDTLS_MD_NONE )
9121 set->ecdsa = md_alg;
9122 break;
9123
9124 default:
9125 break;
9126 }
9127}
9128
9129/* Allow exactly one hash algorithm for each signature. */
9130void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9131 mbedtls_md_type_t md_alg )
9132{
9133 set->rsa = md_alg;
9134 set->ecdsa = md_alg;
9135}
9136
9137#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9138 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9139
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009140/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009141 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009144{
9145 switch( hash )
9146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147#if defined(MBEDTLS_MD5_C)
9148 case MBEDTLS_SSL_HASH_MD5:
9149 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151#if defined(MBEDTLS_SHA1_C)
9152 case MBEDTLS_SSL_HASH_SHA1:
9153 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155#if defined(MBEDTLS_SHA256_C)
9156 case MBEDTLS_SSL_HASH_SHA224:
9157 return( MBEDTLS_MD_SHA224 );
9158 case MBEDTLS_SSL_HASH_SHA256:
9159 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#if defined(MBEDTLS_SHA512_C)
9162 case MBEDTLS_SSL_HASH_SHA384:
9163 return( MBEDTLS_MD_SHA384 );
9164 case MBEDTLS_SSL_HASH_SHA512:
9165 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009166#endif
9167 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009169 }
9170}
9171
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009172/*
9173 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9174 */
9175unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9176{
9177 switch( md )
9178 {
9179#if defined(MBEDTLS_MD5_C)
9180 case MBEDTLS_MD_MD5:
9181 return( MBEDTLS_SSL_HASH_MD5 );
9182#endif
9183#if defined(MBEDTLS_SHA1_C)
9184 case MBEDTLS_MD_SHA1:
9185 return( MBEDTLS_SSL_HASH_SHA1 );
9186#endif
9187#if defined(MBEDTLS_SHA256_C)
9188 case MBEDTLS_MD_SHA224:
9189 return( MBEDTLS_SSL_HASH_SHA224 );
9190 case MBEDTLS_MD_SHA256:
9191 return( MBEDTLS_SSL_HASH_SHA256 );
9192#endif
9193#if defined(MBEDTLS_SHA512_C)
9194 case MBEDTLS_MD_SHA384:
9195 return( MBEDTLS_SSL_HASH_SHA384 );
9196 case MBEDTLS_MD_SHA512:
9197 return( MBEDTLS_SSL_HASH_SHA512 );
9198#endif
9199 default:
9200 return( MBEDTLS_SSL_HASH_NONE );
9201 }
9202}
9203
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009204#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009205/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009206 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009207 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009208 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009209int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009212
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009213 if( ssl->conf->curve_list == NULL )
9214 return( -1 );
9215
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009216 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009217 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009218 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009219
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009220 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009221}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009222#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009223
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009224#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009225/*
9226 * Check if a hash proposed by the peer is in our list.
9227 * Return 0 if we're willing to use it, -1 otherwise.
9228 */
9229int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9230 mbedtls_md_type_t md )
9231{
9232 const int *cur;
9233
9234 if( ssl->conf->sig_hashes == NULL )
9235 return( -1 );
9236
9237 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9238 if( *cur == (int) md )
9239 return( 0 );
9240
9241 return( -1 );
9242}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009243#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245#if defined(MBEDTLS_X509_CRT_PARSE_C)
9246int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9247 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009248 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009249 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009250{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009251 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009253 int usage = 0;
9254#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009256 const char *ext_oid;
9257 size_t ext_len;
9258#endif
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9261 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009262 ((void) cert);
9263 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009264 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009265#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9268 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009269 {
9270 /* Server part of the key exchange */
9271 switch( ciphersuite->key_exchange )
9272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273 case MBEDTLS_KEY_EXCHANGE_RSA:
9274 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009275 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009276 break;
9277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9279 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9280 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9281 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009282 break;
9283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9285 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009286 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009287 break;
9288
9289 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 case MBEDTLS_KEY_EXCHANGE_NONE:
9291 case MBEDTLS_KEY_EXCHANGE_PSK:
9292 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9293 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009294 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009295 usage = 0;
9296 }
9297 }
9298 else
9299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9301 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009302 }
9303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009305 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009306 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009307 ret = -1;
9308 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009309#else
9310 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9314 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9317 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009318 }
9319 else
9320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9322 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009323 }
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009326 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009327 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009328 ret = -1;
9329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009331
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009332 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009335
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009336/*
9337 * Convert version numbers to/from wire format
9338 * and, for DTLS, to/from TLS equivalent.
9339 *
9340 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009341 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009342 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9343 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009346 unsigned char ver[2] )
9347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_SSL_PROTO_DTLS)
9349 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009352 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9353
9354 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9355 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9356 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009357 else
9358#else
9359 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009360#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009361 {
9362 ver[0] = (unsigned char) major;
9363 ver[1] = (unsigned char) minor;
9364 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009365}
9366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009368 const unsigned char ver[2] )
9369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#if defined(MBEDTLS_SSL_PROTO_DTLS)
9371 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009372 {
9373 *major = 255 - ver[0] + 2;
9374 *minor = 255 - ver[1] + 1;
9375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009377 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9378 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009379 else
9380#else
9381 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009382#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009383 {
9384 *major = ver[0];
9385 *minor = ver[1];
9386 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009387}
9388
Simon Butcher99000142016-10-13 17:21:01 +01009389int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9390{
9391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9392 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9393 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9394
9395 switch( md )
9396 {
9397#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9398#if defined(MBEDTLS_MD5_C)
9399 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009400 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009401#endif
9402#if defined(MBEDTLS_SHA1_C)
9403 case MBEDTLS_SSL_HASH_SHA1:
9404 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9405 break;
9406#endif
9407#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9408#if defined(MBEDTLS_SHA512_C)
9409 case MBEDTLS_SSL_HASH_SHA384:
9410 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9411 break;
9412#endif
9413#if defined(MBEDTLS_SHA256_C)
9414 case MBEDTLS_SSL_HASH_SHA256:
9415 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9416 break;
9417#endif
9418 default:
9419 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9420 }
9421
9422 return 0;
9423#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9424 (void) ssl;
9425 (void) md;
9426
9427 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9428#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9429}
9430
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009431#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9432 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9433int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9434 unsigned char *output,
9435 unsigned char *data, size_t data_len )
9436{
9437 int ret = 0;
9438 mbedtls_md5_context mbedtls_md5;
9439 mbedtls_sha1_context mbedtls_sha1;
9440
9441 mbedtls_md5_init( &mbedtls_md5 );
9442 mbedtls_sha1_init( &mbedtls_sha1 );
9443
9444 /*
9445 * digitally-signed struct {
9446 * opaque md5_hash[16];
9447 * opaque sha_hash[20];
9448 * };
9449 *
9450 * md5_hash
9451 * MD5(ClientHello.random + ServerHello.random
9452 * + ServerParams);
9453 * sha_hash
9454 * SHA(ClientHello.random + ServerHello.random
9455 * + ServerParams);
9456 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009457 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009458 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009460 goto exit;
9461 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009462 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009463 ssl->handshake->randbytes, 64 ) ) != 0 )
9464 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009466 goto exit;
9467 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009468 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009469 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009471 goto exit;
9472 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009473 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009474 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009476 goto exit;
9477 }
9478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009479 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009480 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009482 goto exit;
9483 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009484 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009485 ssl->handshake->randbytes, 64 ) ) != 0 )
9486 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009488 goto exit;
9489 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009490 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009491 data_len ) ) != 0 )
9492 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009494 goto exit;
9495 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009496 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009497 output + 16 ) ) != 0 )
9498 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009500 goto exit;
9501 }
9502
9503exit:
9504 mbedtls_md5_free( &mbedtls_md5 );
9505 mbedtls_sha1_free( &mbedtls_sha1 );
9506
9507 if( ret != 0 )
9508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9509 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9510
9511 return( ret );
9512
9513}
9514#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9515 MBEDTLS_SSL_PROTO_TLS1_1 */
9516
9517#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9518 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9519int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009520 unsigned char *hash, size_t *hashlen,
9521 unsigned char *data, size_t data_len,
9522 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009523{
9524 int ret = 0;
9525 mbedtls_md_context_t ctx;
9526 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009527 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009528
9529 mbedtls_md_init( &ctx );
9530
9531 /*
9532 * digitally-signed struct {
9533 * opaque client_random[32];
9534 * opaque server_random[32];
9535 * ServerDHParams params;
9536 * };
9537 */
9538 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9539 {
9540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9541 goto exit;
9542 }
9543 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9544 {
9545 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9546 goto exit;
9547 }
9548 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9549 {
9550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9551 goto exit;
9552 }
9553 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9554 {
9555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9556 goto exit;
9557 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009558 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009559 {
9560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9561 goto exit;
9562 }
9563
9564exit:
9565 mbedtls_md_free( &ctx );
9566
9567 if( ret != 0 )
9568 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9569 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9570
9571 return( ret );
9572}
9573#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9574 MBEDTLS_SSL_PROTO_TLS1_2 */
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#endif /* MBEDTLS_SSL_TLS_C */