blob: 72be09716e9beefaa6926e7e9c80158afb669a7c [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
Hanno Becker01315ea2018-08-21 17:22:17 +01004441static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004442static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4443 size_t desired )
4444{
4445 int offset;
4446 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4447
Hanno Becker01315ea2018-08-21 17:22:17 +01004448 /* Get rid of future records epoch first, if such exist. */
4449 ssl_free_buffered_record( ssl );
4450
4451 /* Check if we have enough space available now. */
4452 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4453 hs->buffering.total_bytes_buffered ) )
4454 {
4455 return( 0 );
4456 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004457
4458 /* We don't have enough space to buffer the next expected
4459 * handshake message. Remove buffers used for future msgs
4460 * to gain space, starting with the most distant one. */
4461 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4462 offset >= 0; offset-- )
4463 {
4464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4465 offset ) );
4466
4467 ssl_buffering_free_slot( ssl, offset );
4468
4469 /* Check if we have enough space available now. */
4470 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4471 hs->buffering.total_bytes_buffered ) )
4472 {
4473 return( 0 );
4474 }
4475 }
4476
4477 return( -1 );
4478}
4479
Hanno Becker40f50842018-08-15 14:48:01 +01004480static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4481{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004482 int ret = 0;
4483 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4484
4485 if( hs == NULL )
4486 return( 0 );
4487
4488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4489
4490 switch( ssl->in_msgtype )
4491 {
4492 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004494
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004495 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004496 break;
4497
4498 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004499 {
4500 unsigned recv_msg_seq_offset;
4501 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4502 mbedtls_ssl_hs_buffer *hs_buf;
4503 size_t msg_len = ssl->in_hslen - 12;
4504
4505 /* We should never receive an old handshake
4506 * message - double-check nonetheless. */
4507 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4508 {
4509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4511 }
4512
4513 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4514 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4515 {
4516 /* Silently ignore -- message too far in the future */
4517 MBEDTLS_SSL_DEBUG_MSG( 2,
4518 ( "Ignore future HS message with sequence number %u, "
4519 "buffering window %u - %u",
4520 recv_msg_seq, ssl->handshake->in_msg_seq,
4521 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4522
4523 goto exit;
4524 }
4525
4526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4527 recv_msg_seq, recv_msg_seq_offset ) );
4528
4529 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4530
4531 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004532 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004533 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004534 size_t reassembly_buf_sz;
4535
Hanno Becker37f95322018-08-16 13:55:32 +01004536 hs_buf->is_fragmented =
4537 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4538
4539 /* We copy the message back into the input buffer
4540 * after reassembly, so check that it's not too large.
4541 * This is an implementation-specific limitation
4542 * and not one from the standard, hence it is not
4543 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004544 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004545 {
4546 /* Ignore message */
4547 goto exit;
4548 }
4549
Hanno Beckere0b150f2018-08-21 15:51:03 +01004550 /* Check if we have enough space to buffer the message. */
4551 if( hs->buffering.total_bytes_buffered >
4552 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4553 {
4554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4556 }
4557
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004558 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4559 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004560
4561 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4562 hs->buffering.total_bytes_buffered ) )
4563 {
4564 if( recv_msg_seq_offset > 0 )
4565 {
4566 /* If we can't buffer a future message because
4567 * of space limitations -- ignore. */
4568 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",
4569 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4570 (unsigned) hs->buffering.total_bytes_buffered ) );
4571 goto exit;
4572 }
Hanno Beckere1801392018-08-21 16:51:05 +01004573 else
4574 {
4575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4576 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4577 (unsigned) hs->buffering.total_bytes_buffered ) );
4578 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004579
Hanno Beckera02b0b42018-08-21 17:20:27 +01004580 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004581 {
4582 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 +01004583 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4584 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004585 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4586 goto exit;
4587 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004588 }
4589
4590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4591 msg_len ) );
4592
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004593 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4594 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004595 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004596 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004597 goto exit;
4598 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004599 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004600
4601 /* Prepare final header: copy msg_type, length and message_seq,
4602 * then add standardised fragment_offset and fragment_length */
4603 memcpy( hs_buf->data, ssl->in_msg, 6 );
4604 memset( hs_buf->data + 6, 0, 3 );
4605 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4606
4607 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004608
4609 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004610 }
4611 else
4612 {
4613 /* Make sure msg_type and length are consistent */
4614 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4615 {
4616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4617 /* Ignore */
4618 goto exit;
4619 }
4620 }
4621
Hanno Becker4422bbb2018-08-20 09:40:19 +01004622 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004623 {
4624 size_t frag_len, frag_off;
4625 unsigned char * const msg = hs_buf->data + 12;
4626
4627 /*
4628 * Check and copy current fragment
4629 */
4630
4631 /* Validation of header fields already done in
4632 * mbedtls_ssl_prepare_handshake_record(). */
4633 frag_off = ssl_get_hs_frag_off( ssl );
4634 frag_len = ssl_get_hs_frag_len( ssl );
4635
4636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4637 frag_off, frag_len ) );
4638 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4639
4640 if( hs_buf->is_fragmented )
4641 {
4642 unsigned char * const bitmask = msg + msg_len;
4643 ssl_bitmask_set( bitmask, frag_off, frag_len );
4644 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4645 msg_len ) == 0 );
4646 }
4647 else
4648 {
4649 hs_buf->is_complete = 1;
4650 }
4651
4652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4653 hs_buf->is_complete ? "" : "not yet " ) );
4654 }
4655
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004656 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004657 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004658
4659 default:
4660 break;
4661 }
4662
4663exit:
4664
4665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4666 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004667}
4668#endif /* MBEDTLS_SSL_PROTO_DTLS */
4669
Hanno Becker1097b342018-08-15 14:09:41 +01004670static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004671{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004672 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004673 * Consume last content-layer message and potentially
4674 * update in_msglen which keeps track of the contents'
4675 * consumption state.
4676 *
4677 * (1) Handshake messages:
4678 * Remove last handshake message, move content
4679 * and adapt in_msglen.
4680 *
4681 * (2) Alert messages:
4682 * Consume whole record content, in_msglen = 0.
4683 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004684 * (3) Change cipher spec:
4685 * Consume whole record content, in_msglen = 0.
4686 *
4687 * (4) Application data:
4688 * Don't do anything - the record layer provides
4689 * the application data as a stream transport
4690 * and consumes through mbedtls_ssl_read only.
4691 *
4692 */
4693
4694 /* Case (1): Handshake messages */
4695 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004696 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004697 /* Hard assertion to be sure that no application data
4698 * is in flight, as corrupting ssl->in_msglen during
4699 * ssl->in_offt != NULL is fatal. */
4700 if( ssl->in_offt != NULL )
4701 {
4702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4704 }
4705
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004706 /*
4707 * Get next Handshake message in the current record
4708 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004709
Hanno Becker4a810fb2017-05-24 16:27:30 +01004710 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004711 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004712 * current handshake content: If DTLS handshake
4713 * fragmentation is used, that's the fragment
4714 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004715 * size here is faulty and should be changed at
4716 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004717 * (2) While it doesn't seem to cause problems, one
4718 * has to be very careful not to assume that in_hslen
4719 * is always <= in_msglen in a sensible communication.
4720 * Again, it's wrong for DTLS handshake fragmentation.
4721 * The following check is therefore mandatory, and
4722 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004723 * Additionally, ssl->in_hslen might be arbitrarily out of
4724 * bounds after handling a DTLS message with an unexpected
4725 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004726 */
4727 if( ssl->in_hslen < ssl->in_msglen )
4728 {
4729 ssl->in_msglen -= ssl->in_hslen;
4730 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4731 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004732
Hanno Becker4a810fb2017-05-24 16:27:30 +01004733 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4734 ssl->in_msg, ssl->in_msglen );
4735 }
4736 else
4737 {
4738 ssl->in_msglen = 0;
4739 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004740
Hanno Becker4a810fb2017-05-24 16:27:30 +01004741 ssl->in_hslen = 0;
4742 }
4743 /* Case (4): Application data */
4744 else if( ssl->in_offt != NULL )
4745 {
4746 return( 0 );
4747 }
4748 /* Everything else (CCS & Alerts) */
4749 else
4750 {
4751 ssl->in_msglen = 0;
4752 }
4753
Hanno Becker1097b342018-08-15 14:09:41 +01004754 return( 0 );
4755}
4756
Hanno Beckere74d5562018-08-15 14:26:08 +01004757static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4758{
4759 if( ssl->in_msglen > 0 )
4760 return( 1 );
4761
4762 return( 0 );
4763}
4764
Hanno Becker5f066e72018-08-16 14:56:31 +01004765#if defined(MBEDTLS_SSL_PROTO_DTLS)
4766
4767static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4768{
4769 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4770 if( hs == NULL )
4771 return;
4772
Hanno Becker01315ea2018-08-21 17:22:17 +01004773 if( hs->buffering.future_record.data != NULL )
4774 {
4775 hs->buffering.total_bytes_buffered -=
4776 hs->buffering.future_record.len;
4777
4778 mbedtls_free( hs->buffering.future_record.data );
4779 hs->buffering.future_record.data = NULL;
4780 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004781}
4782
4783static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4784{
4785 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4786 unsigned char * rec;
4787 size_t rec_len;
4788 unsigned rec_epoch;
4789
4790 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4791 return( 0 );
4792
4793 if( hs == NULL )
4794 return( 0 );
4795
Hanno Becker5f066e72018-08-16 14:56:31 +01004796 rec = hs->buffering.future_record.data;
4797 rec_len = hs->buffering.future_record.len;
4798 rec_epoch = hs->buffering.future_record.epoch;
4799
4800 if( rec == NULL )
4801 return( 0 );
4802
Hanno Becker4cb782d2018-08-20 11:19:05 +01004803 /* Only consider loading future records if the
4804 * input buffer is empty. */
4805 if( ssl_another_record_in_datagram( ssl ) == 1 )
4806 return( 0 );
4807
Hanno Becker5f066e72018-08-16 14:56:31 +01004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4809
4810 if( rec_epoch != ssl->in_epoch )
4811 {
4812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4813 goto exit;
4814 }
4815
4816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4817
4818 /* Double-check that the record is not too large */
4819 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4820 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4821 {
4822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4823 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4824 }
4825
4826 memcpy( ssl->in_hdr, rec, rec_len );
4827 ssl->in_left = rec_len;
4828 ssl->next_record_offset = 0;
4829
4830 ssl_free_buffered_record( ssl );
4831
4832exit:
4833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4834 return( 0 );
4835}
4836
4837static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4838{
4839 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4840 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004841 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004842
4843 /* Don't buffer future records outside handshakes. */
4844 if( hs == NULL )
4845 return( 0 );
4846
4847 /* Only buffer handshake records (we are only interested
4848 * in Finished messages). */
4849 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4850 return( 0 );
4851
4852 /* Don't buffer more than one future epoch record. */
4853 if( hs->buffering.future_record.data != NULL )
4854 return( 0 );
4855
Hanno Becker01315ea2018-08-21 17:22:17 +01004856 /* Don't buffer record if there's not enough buffering space remaining. */
4857 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4858 hs->buffering.total_bytes_buffered ) )
4859 {
4860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4861 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4862 (unsigned) hs->buffering.total_bytes_buffered ) );
4863 return( 0 );
4864 }
4865
Hanno Becker5f066e72018-08-16 14:56:31 +01004866 /* Buffer record */
4867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4868 ssl->in_epoch + 1 ) );
4869 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4870 rec_hdr_len + ssl->in_msglen );
4871
4872 /* ssl_parse_record_header() only considers records
4873 * of the next epoch as candidates for buffering. */
4874 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004875 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004876
4877 hs->buffering.future_record.data =
4878 mbedtls_calloc( 1, hs->buffering.future_record.len );
4879 if( hs->buffering.future_record.data == NULL )
4880 {
4881 /* If we run out of RAM trying to buffer a
4882 * record from the next epoch, just ignore. */
4883 return( 0 );
4884 }
4885
Hanno Becker01315ea2018-08-21 17:22:17 +01004886 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004887
Hanno Becker01315ea2018-08-21 17:22:17 +01004888 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004889 return( 0 );
4890}
4891
4892#endif /* MBEDTLS_SSL_PROTO_DTLS */
4893
Hanno Beckere74d5562018-08-15 14:26:08 +01004894static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004895{
4896 int ret;
4897
Hanno Becker5f066e72018-08-16 14:56:31 +01004898#if defined(MBEDTLS_SSL_PROTO_DTLS)
4899 /* We might have buffered a future record; if so,
4900 * and if the epoch matches now, load it.
4901 * On success, this call will set ssl->in_left to
4902 * the length of the buffered record, so that
4903 * the calls to ssl_fetch_input() below will
4904 * essentially be no-ops. */
4905 ret = ssl_load_buffered_record( ssl );
4906 if( ret != 0 )
4907 return( ret );
4908#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004913 return( ret );
4914 }
4915
4916 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004919 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4920 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004921 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004922 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4923 {
4924 ret = ssl_buffer_future_record( ssl );
4925 if( ret != 0 )
4926 return( ret );
4927
4928 /* Fall through to handling of unexpected records */
4929 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4930 }
4931
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004932 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4933 {
4934 /* Skip unexpected record (but not whole datagram) */
4935 ssl->next_record_offset = ssl->in_msglen
4936 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004937
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4939 "(header)" ) );
4940 }
4941 else
4942 {
4943 /* Skip invalid record and the rest of the datagram */
4944 ssl->next_record_offset = 0;
4945 ssl->in_left = 0;
4946
4947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4948 "(header)" ) );
4949 }
4950
4951 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004952 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004953 }
4954#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004955 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004956 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004957
4958 /*
4959 * Read and optionally decrypt the message contents
4960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4962 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004965 return( ret );
4966 }
4967
4968 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004970 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004973 if( ssl->next_record_offset < ssl->in_left )
4974 {
4975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4976 }
4977 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004978 else
4979#endif
4980 ssl->in_left = 0;
4981
4982 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004986 {
4987 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4989 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004990 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004991 /* Except when waiting for Finished as a bad mac here
4992 * probably means something went wrong in the handshake
4993 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4994 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4995 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4996 {
4997#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4998 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4999 {
5000 mbedtls_ssl_send_alert_message( ssl,
5001 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5002 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5003 }
5004#endif
5005 return( ret );
5006 }
5007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005009 if( ssl->conf->badmac_limit != 0 &&
5010 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5013 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005014 }
5015#endif
5016
Hanno Becker4a810fb2017-05-24 16:27:30 +01005017 /* As above, invalid records cause
5018 * dismissal of the whole datagram. */
5019
5020 ssl->next_record_offset = 0;
5021 ssl->in_left = 0;
5022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005024 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005025 }
5026
5027 return( ret );
5028 }
5029 else
5030#endif
5031 {
5032 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5034 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 mbedtls_ssl_send_alert_message( ssl,
5037 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5038 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005039 }
5040#endif
5041 return( ret );
5042 }
5043 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005044
Simon Butcher99000142016-10-13 17:21:01 +01005045 return( 0 );
5046}
5047
5048int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5049{
5050 int ret;
5051
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005052 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005053 * Handle particular types of records
5054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 {
Simon Butcher99000142016-10-13 17:21:01 +01005057 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5058 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005059 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005060 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005061 }
5062
Hanno Beckere678eaa2018-08-21 14:57:46 +01005063 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005064 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005065 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005066 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5068 ssl->in_msglen ) );
5069 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005070 }
5071
Hanno Beckere678eaa2018-08-21 14:57:46 +01005072 if( ssl->in_msg[0] != 1 )
5073 {
5074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5075 ssl->in_msg[0] ) );
5076 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5077 }
5078
5079#if defined(MBEDTLS_SSL_PROTO_DTLS)
5080 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5081 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5082 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5083 {
5084 if( ssl->handshake == NULL )
5085 {
5086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5087 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5088 }
5089
5090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5091 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5092 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005094 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005097 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005098 if( ssl->in_msglen != 2 )
5099 {
5100 /* Note: Standard allows for more than one 2 byte alert
5101 to be packed in a single message, but Mbed TLS doesn't
5102 currently support this. */
5103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5104 ssl->in_msglen ) );
5105 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5106 }
5107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005109 ssl->in_msg[0], ssl->in_msg[1] ) );
5110
5111 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005112 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005117 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005119 }
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5122 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5125 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005126 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005127
5128#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5129 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5130 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5131 {
Hanno Becker90333da2017-10-10 11:27:13 +01005132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005133 /* Will be handled when trying to parse ServerHello */
5134 return( 0 );
5135 }
5136#endif
5137
5138#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5139 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5140 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5141 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5142 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5143 {
5144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5145 /* Will be handled in mbedtls_ssl_parse_certificate() */
5146 return( 0 );
5147 }
5148#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5149
5150 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005151 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 }
5153
Hanno Beckerc76c6192017-06-06 10:03:17 +01005154#if defined(MBEDTLS_SSL_PROTO_DTLS)
5155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5156 ssl->handshake != NULL &&
5157 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5158 {
5159 ssl_handshake_wrapup_free_hs_transform( ssl );
5160 }
5161#endif
5162
Paul Bakker5121ce52009-01-03 21:22:43 +00005163 return( 0 );
5164}
5165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005167{
5168 int ret;
5169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5171 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5172 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005173 {
5174 return( ret );
5175 }
5176
5177 return( 0 );
5178}
5179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005181 unsigned char level,
5182 unsigned char message )
5183{
5184 int ret;
5185
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005186 if( ssl == NULL || ssl->conf == NULL )
5187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005193 ssl->out_msglen = 2;
5194 ssl->out_msg[0] = level;
5195 ssl->out_msg[1] = message;
5196
Hanno Becker67bc7c32018-08-06 11:33:50 +01005197 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005200 return( ret );
5201 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005203
5204 return( 0 );
5205}
5206
Paul Bakker5121ce52009-01-03 21:22:43 +00005207/*
5208 * Handshake functions
5209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5211 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5212 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5213 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5214 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5215 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5216 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005217/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5225 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005226 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5227 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005230 ssl->state++;
5231 return( 0 );
5232 }
5233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005236}
5237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5245 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005246 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5247 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005250 ssl->state++;
5251 return( 0 );
5252 }
5253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005256}
Gilles Peskinef9828522017-05-03 12:28:43 +02005257
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005258#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005259/* Some certificate support -> implement write and parse */
5260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005264 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 const mbedtls_x509_crt *crt;
5266 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5271 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005272 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5273 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005276 ssl->state++;
5277 return( 0 );
5278 }
5279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005281 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 {
5283 if( ssl->client_auth == 0 )
5284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005286 ssl->state++;
5287 return( 0 );
5288 }
5289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005291 /*
5292 * If using SSLv3 and got no cert, send an Alert message
5293 * (otherwise an empty Certificate message will be sent).
5294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5296 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005297 {
5298 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5300 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5301 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 goto write_msg;
5305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308#endif /* MBEDTLS_SSL_CLI_C */
5309#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005310 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5315 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005316 }
5317 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321
5322 /*
5323 * 0 . 0 handshake type
5324 * 1 . 3 handshake length
5325 * 4 . 6 length of all certs
5326 * 7 . 9 length of cert. 1
5327 * 10 . n-1 peer certificate
5328 * n . n+2 length of cert. 2
5329 * n+3 . ... upper level cert, etc.
5330 */
5331 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Paul Bakker29087132010-03-21 21:03:34 +00005334 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005335 {
5336 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005337 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005340 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005342 }
5343
5344 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5345 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5346 ssl->out_msg[i + 2] = (unsigned char)( n );
5347
5348 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5349 i += n; crt = crt->next;
5350 }
5351
5352 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5353 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5354 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5355
5356 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5358 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005359
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005360#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005361write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005362#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005363
5364 ssl->state++;
5365
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005366 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005367 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005369 return( ret );
5370 }
5371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005373
Paul Bakkered27a042013-04-18 22:46:23 +02005374 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375}
5376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005380 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005382 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005383 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5388 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005389 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5390 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005393 ssl->state++;
5394 return( 0 );
5395 }
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005399 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5400 {
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5402 ssl->state++;
5403 return( 0 );
5404 }
5405
5406#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5407 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5408 authmode = ssl->handshake->sni_authmode;
5409#endif
5410
5411 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5412 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005413 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005414 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005416 ssl->state++;
5417 return( 0 );
5418 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005419#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005420
Hanno Becker327c93b2018-08-15 13:56:18 +01005421 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005423 /* mbedtls_ssl_read_record may have sent an alert already. We
5424 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 return( ret );
5427 }
5428
5429 ssl->state++;
5430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#if defined(MBEDTLS_SSL_SRV_C)
5432#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005433 /*
5434 * Check if the client sent an empty certificate
5435 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005436 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005439 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5441 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5442 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005445
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005446 /* The client was asked for a certificate but didn't send
5447 one. The client should know what's going on, so we
5448 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005449 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005450 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 return( 0 );
5452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 }
5455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5459 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005460 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5464 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5465 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5466 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005470 /* The client was asked for a certificate but didn't send
5471 one. The client should know what's going on, so we
5472 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005473 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005474 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005476 else
5477 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 }
5479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5481 MBEDTLS_SSL_PROTO_TLS1_2 */
5482#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005487 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5488 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005490 }
5491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5493 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005496 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5497 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 }
5500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005502
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005506 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005507
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005508 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005512 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5513 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 }
5516
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005517 /* In case we tried to reuse a session but it failed */
5518 if( ssl->session_negotiate->peer_cert != NULL )
5519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5521 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005522 }
5523
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005524 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5530 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005531 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005532 }
5533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005536 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005537
5538 while( i < ssl->in_hslen )
5539 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005540 if ( i + 3 > ssl->in_hslen ) {
5541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5542 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5543 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5544 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005546 if( ssl->in_msg[i] != 0 )
5547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005549 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5550 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005552 }
5553
5554 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5555 | (unsigned int) ssl->in_msg[i + 2];
5556 i += 3;
5557
5558 if( n < 128 || i + n > ssl->in_hslen )
5559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5562 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 }
5565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005567 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005568 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005569 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005570 case 0: /*ok*/
5571 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5572 /* Ignore certificate with an unknown algorithm: maybe a
5573 prior certificate was already trusted. */
5574 break;
5575
5576 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5577 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5578 goto crt_parse_der_failed;
5579
5580 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5581 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5582 goto crt_parse_der_failed;
5583
5584 default:
5585 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5586 crt_parse_der_failed:
5587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 return( ret );
5590 }
5591
5592 i += n;
5593 }
5594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005596
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005597 /*
5598 * On client, make sure the server cert doesn't change during renego to
5599 * avoid "triple handshake" attack: https://secure-resumption.com/
5600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005602 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005604 {
5605 if( ssl->session->peer_cert == NULL )
5606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005608 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5609 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005611 }
5612
5613 if( ssl->session->peer_cert->raw.len !=
5614 ssl->session_negotiate->peer_cert->raw.len ||
5615 memcmp( ssl->session->peer_cert->raw.p,
5616 ssl->session_negotiate->peer_cert->raw.p,
5617 ssl->session->peer_cert->raw.len ) != 0 )
5618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005620 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5621 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005623 }
5624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005626
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005627 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005629 mbedtls_x509_crt *ca_chain;
5630 mbedtls_x509_crl *ca_crl;
5631
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005632#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005633 if( ssl->handshake->sni_ca_chain != NULL )
5634 {
5635 ca_chain = ssl->handshake->sni_ca_chain;
5636 ca_crl = ssl->handshake->sni_ca_crl;
5637 }
5638 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005639#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005640 {
5641 ca_chain = ssl->conf->ca_chain;
5642 ca_crl = ssl->conf->ca_crl;
5643 }
5644
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005645 /*
5646 * Main check: verify certificate
5647 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005648 ret = mbedtls_x509_crt_verify_with_profile(
5649 ssl->session_negotiate->peer_cert,
5650 ca_chain, ca_crl,
5651 ssl->conf->cert_profile,
5652 ssl->hostname,
5653 &ssl->session_negotiate->verify_result,
5654 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005655
5656 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005659 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005660
5661 /*
5662 * Secondary checks: always done, but change 'ret' only if it was 0
5663 */
5664
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005665#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005668
5669 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005671 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005672 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005673 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005676 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005678 }
5679 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005680#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005683 ciphersuite_info,
5684 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005685 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005688 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005690 }
5691
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005692 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5693 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5694 * with details encoded in the verification flags. All other kinds
5695 * of error codes, including those from the user provided f_vrfy
5696 * functions, are treated as fatal and lead to a failure of
5697 * ssl_parse_certificate even if verification was optional. */
5698 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5699 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5700 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5701 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005703 }
5704
5705 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5706 {
5707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5708 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5709 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005710
5711 if( ret != 0 )
5712 {
5713 /* The certificate may have been rejected for several reasons.
5714 Pick one and send the corresponding alert. Which alert to send
5715 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005716 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5717 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5718 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5719 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5720 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5721 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5722 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5723 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5724 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5725 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5726 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5727 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5728 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5729 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5730 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5731 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5732 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5733 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5734 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5735 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005736 else
5737 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5739 alert );
5740 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005741
Hanno Beckere6706e62017-05-15 16:05:15 +01005742#if defined(MBEDTLS_DEBUG_C)
5743 if( ssl->session_negotiate->verify_result != 0 )
5744 {
5745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5746 ssl->session_negotiate->verify_result ) );
5747 }
5748 else
5749 {
5750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5751 }
5752#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 }
5754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005756
5757 return( ret );
5758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5760 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5761 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5762 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5763 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5764 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5765 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005768{
5769 int ret;
5770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 ssl->out_msglen = 1;
5775 ssl->out_msg[0] = 1;
5776
Paul Bakker5121ce52009-01-03 21:22:43 +00005777 ssl->state++;
5778
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005779 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 return( ret );
5783 }
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005786
5787 return( 0 );
5788}
5789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005791{
5792 int ret;
5793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005795
Hanno Becker327c93b2018-08-15 13:56:18 +01005796 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005799 return( ret );
5800 }
5801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Hanno Beckere678eaa2018-08-21 14:57:46 +01005810 /* CCS records are only accepted if they have length 1 and content '1',
5811 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005812
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005813 /*
5814 * Switch to our negotiated transform and session parameters for inbound
5815 * data.
5816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005818 ssl->transform_in = ssl->transform_negotiate;
5819 ssl->session_in = ssl->session_negotiate;
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005825 ssl_dtls_replay_reset( ssl );
5826#endif
5827
5828 /* Increment epoch */
5829 if( ++ssl->in_epoch == 0 )
5830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005832 /* This is highly unlikely to happen for legitimate reasons, so
5833 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005835 }
5836 }
5837 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005839 memset( ssl->in_ctr, 0, 8 );
5840
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005841 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5844 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005849 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5850 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005852 }
5853 }
5854#endif
5855
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 ssl->state++;
5857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005859
5860 return( 0 );
5861}
5862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5864 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005865{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005866 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5869 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5870 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005871 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005872 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5875#if defined(MBEDTLS_SHA512_C)
5876 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005877 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5878 else
5879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880#if defined(MBEDTLS_SHA256_C)
5881 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005882 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005883 else
5884#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005888 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005889 }
Paul Bakker380da532012-04-18 16:10:25 +00005890}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5895 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005896 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5897 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005898#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5900#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005901 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005904 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005907}
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005910 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5913 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005914 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5915 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5918#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005919 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005922 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005924#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005925}
5926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5928 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5929static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005930 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005931{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005932 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5933 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005934}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005935#endif
Paul Bakker380da532012-04-18 16:10:25 +00005936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5938#if defined(MBEDTLS_SHA256_C)
5939static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005940 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005941{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005942 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005943}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005944#endif
Paul Bakker380da532012-04-18 16:10:25 +00005945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946#if defined(MBEDTLS_SHA512_C)
5947static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005948 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005949{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005950 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005951}
Paul Bakker769075d2012-11-24 11:26:46 +01005952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005956static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005958{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005959 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005960 mbedtls_md5_context md5;
5961 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005962
Paul Bakker5121ce52009-01-03 21:22:43 +00005963 unsigned char padbuf[48];
5964 unsigned char md5sum[16];
5965 unsigned char sha1sum[20];
5966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005968 if( !session )
5969 session = ssl->session;
5970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005972
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005973 mbedtls_md5_init( &md5 );
5974 mbedtls_sha1_init( &sha1 );
5975
5976 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5977 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005978
5979 /*
5980 * SSLv3:
5981 * hash =
5982 * MD5( master + pad2 +
5983 * MD5( handshake + sender + master + pad1 ) )
5984 * + SHA1( master + pad2 +
5985 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005986 */
5987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005989 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5990 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005994 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5995 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005996#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005999 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006000
Paul Bakker1ef83d62012-04-11 12:09:53 +00006001 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006002
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006003 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6004 mbedtls_md5_update_ret( &md5, session->master, 48 );
6005 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6006 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006007
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006008 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6009 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6010 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6011 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012
Paul Bakker1ef83d62012-04-11 12:09:53 +00006013 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006014
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006015 mbedtls_md5_starts_ret( &md5 );
6016 mbedtls_md5_update_ret( &md5, session->master, 48 );
6017 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6018 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6019 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006020
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006021 mbedtls_sha1_starts_ret( &sha1 );
6022 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6023 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6024 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6025 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006028
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006029 mbedtls_md5_free( &md5 );
6030 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6033 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6034 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006041static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006043{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006044 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006045 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006046 mbedtls_md5_context md5;
6047 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006048 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006051 if( !session )
6052 session = ssl->session;
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006055
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006056 mbedtls_md5_init( &md5 );
6057 mbedtls_sha1_init( &sha1 );
6058
6059 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6060 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006061
Paul Bakker1ef83d62012-04-11 12:09:53 +00006062 /*
6063 * TLSv1:
6064 * hash = PRF( master, finished_label,
6065 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6066 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006069 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6070 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006071#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006074 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6075 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006076#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006079 ? "client finished"
6080 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006081
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006082 mbedtls_md5_finish_ret( &md5, padbuf );
6083 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006084
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006085 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006086 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006089
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006090 mbedtls_md5_free( &md5 );
6091 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006092
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006093 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6100#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006101static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006103{
6104 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006105 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006106 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006107 unsigned char padbuf[32];
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006110 if( !session )
6111 session = ssl->session;
6112
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006113 mbedtls_sha256_init( &sha256 );
6114
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006116
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006117 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006118
6119 /*
6120 * TLSv1.2:
6121 * hash = PRF( master, finished_label,
6122 * Hash( handshake ) )[0.11]
6123 */
6124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125#if !defined(MBEDTLS_SHA256_ALT)
6126 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006127 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006128#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006131 ? "client finished"
6132 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006133
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006134 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006135
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006136 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006137 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006140
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006141 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006142
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006143 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006150static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006152{
6153 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006154 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006155 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006156 unsigned char padbuf[48];
6157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006159 if( !session )
6160 session = ssl->session;
6161
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006162 mbedtls_sha512_init( &sha512 );
6163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006165
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006166 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006167
6168 /*
6169 * TLSv1.2:
6170 * hash = PRF( master, finished_label,
6171 * Hash( handshake ) )[0.11]
6172 */
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006175 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6176 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006177#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006180 ? "client finished"
6181 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006182
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006183 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006184
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006185 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006186 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006189
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006190 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006191
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006192 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196#endif /* MBEDTLS_SHA512_C */
6197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006202
6203 /*
6204 * Free our handshake params
6205 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006206 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006208 ssl->handshake = NULL;
6209
6210 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006211 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006212 */
6213 if( ssl->transform )
6214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 mbedtls_ssl_transform_free( ssl->transform );
6216 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006217 }
6218 ssl->transform = ssl->transform_negotiate;
6219 ssl->transform_negotiate = NULL;
6220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006222}
6223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006225{
6226 int resume = ssl->handshake->resume;
6227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_RENEGOTIATION)
6231 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006234 ssl->renego_records_seen = 0;
6235 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006236#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006237
6238 /*
6239 * Free the previous session and switch in the current one
6240 */
Paul Bakker0a597072012-09-25 21:55:46 +00006241 if( ssl->session )
6242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006244 /* RFC 7366 3.1: keep the EtM state */
6245 ssl->session_negotiate->encrypt_then_mac =
6246 ssl->session->encrypt_then_mac;
6247#endif
6248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 mbedtls_ssl_session_free( ssl->session );
6250 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006251 }
6252 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006253 ssl->session_negotiate = NULL;
6254
Paul Bakker0a597072012-09-25 21:55:46 +00006255 /*
6256 * Add cache entry
6257 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006258 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006259 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006260 resume == 0 )
6261 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006262 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006264 }
Paul Bakker0a597072012-09-25 21:55:46 +00006265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006268 ssl->handshake->flight != NULL )
6269 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006270 /* Cancel handshake timer */
6271 ssl_set_timer( ssl, 0 );
6272
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006273 /* Keep last flight around in case we need to resend it:
6274 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006276 }
6277 else
6278#endif
6279 ssl_handshake_wrapup_free_hs_transform( ssl );
6280
Paul Bakker48916f92012-09-16 19:57:18 +00006281 ssl->state++;
6282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006284}
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006287{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006288 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006291
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006292 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006293
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006294 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006295
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006296 /*
6297 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6298 * may define some other value. Currently (early 2016), no defined
6299 * ciphersuite does this (and this is unlikely to change as activity has
6300 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006305 ssl->verify_data_len = hash_len;
6306 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006307#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006308
Paul Bakker5121ce52009-01-03 21:22:43 +00006309 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6311 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006312
6313 /*
6314 * In case of session resuming, invert the client and server
6315 * ChangeCipherSpec messages order.
6316 */
Paul Bakker0a597072012-09-25 21:55:46 +00006317 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006320 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006324 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006327 }
6328 else
6329 ssl->state++;
6330
Paul Bakker48916f92012-09-16 19:57:18 +00006331 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006332 * Switch to our negotiated transform and session parameters for outbound
6333 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006339 {
6340 unsigned char i;
6341
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006342 /* Remember current epoch settings for resending */
6343 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006344 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006345
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006346 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006347 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006348
6349 /* Increment epoch */
6350 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006351 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006352 break;
6353
6354 /* The loop goes to its end iff the counter is wrapping */
6355 if( i == 0 )
6356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6358 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006359 }
6360 }
6361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006363 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006364
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006365 ssl->transform_out = ssl->transform_negotiate;
6366 ssl->session_out = ssl->session_negotiate;
6367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6369 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6374 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006375 }
6376 }
6377#endif
6378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006380 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006382#endif
6383
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006384 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 return( ret );
6388 }
6389
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006390#if defined(MBEDTLS_SSL_PROTO_DTLS)
6391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6392 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6393 {
6394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6395 return( ret );
6396 }
6397#endif
6398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
6401 return( 0 );
6402}
6403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006405#define SSL_MAX_HASH_LEN 36
6406#else
6407#define SSL_MAX_HASH_LEN 12
6408#endif
6409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006411{
Paul Bakker23986e52011-04-24 08:57:21 +00006412 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006413 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006414 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006417
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006418 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419
Hanno Becker327c93b2018-08-15 13:56:18 +01006420 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 return( ret );
6424 }
6425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006429 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6430 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 }
6433
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006434 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435#if defined(MBEDTLS_SSL_PROTO_SSL3)
6436 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006437 hash_len = 36;
6438 else
6439#endif
6440 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6443 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006446 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6447 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006449 }
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006452 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006455 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006458 }
6459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006461 ssl->verify_data_len = hash_len;
6462 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006463#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006464
Paul Bakker0a597072012-09-25 21:55:46 +00006465 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006472 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 }
6476 else
6477 ssl->state++;
6478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006480 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006482#endif
6483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006485
6486 return( 0 );
6487}
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6494 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6495 mbedtls_md5_init( &handshake->fin_md5 );
6496 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006497 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6498 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6501#if defined(MBEDTLS_SHA256_C)
6502 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006503 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006504#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505#if defined(MBEDTLS_SHA512_C)
6506 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006507 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006510
6511 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006512
6513#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6514 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6515 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6516#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#if defined(MBEDTLS_DHM_C)
6519 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_ECDH_C)
6522 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006523#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006524#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006525 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006526#if defined(MBEDTLS_SSL_CLI_C)
6527 handshake->ecjpake_cache = NULL;
6528 handshake->ecjpake_cache_len = 0;
6529#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006530#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006531
6532#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6533 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6534#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006535}
6536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6542 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 mbedtls_md_init( &transform->md_ctx_enc );
6545 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006546}
6547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006551}
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006554{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006555 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006556 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006558 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006560 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006561 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006562
6563 /*
6564 * Either the pointers are now NULL or cleared properly and can be freed.
6565 * Now allocate missing structures.
6566 */
6567 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006568 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006569 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006570 }
Paul Bakker48916f92012-09-16 19:57:18 +00006571
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006572 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006573 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006574 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006575 }
Paul Bakker48916f92012-09-16 19:57:18 +00006576
Paul Bakker82788fb2014-10-20 13:59:19 +02006577 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006578 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006579 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006580 }
Paul Bakker48916f92012-09-16 19:57:18 +00006581
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006582 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006583 if( ssl->handshake == NULL ||
6584 ssl->transform_negotiate == NULL ||
6585 ssl->session_negotiate == NULL )
6586 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 mbedtls_free( ssl->handshake );
6590 mbedtls_free( ssl->transform_negotiate );
6591 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006592
6593 ssl->handshake = NULL;
6594 ssl->transform_negotiate = NULL;
6595 ssl->session_negotiate = NULL;
6596
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006598 }
6599
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006600 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006602 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006603 ssl_handshake_params_init( ssl->handshake );
6604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6607 {
6608 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006609
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006610 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6611 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6612 else
6613 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006614
6615 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006616 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006617#endif
6618
Paul Bakker48916f92012-09-16 19:57:18 +00006619 return( 0 );
6620}
6621
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006622#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006623/* Dummy cookie callbacks for defaults */
6624static int ssl_cookie_write_dummy( void *ctx,
6625 unsigned char **p, unsigned char *end,
6626 const unsigned char *cli_id, size_t cli_id_len )
6627{
6628 ((void) ctx);
6629 ((void) p);
6630 ((void) end);
6631 ((void) cli_id);
6632 ((void) cli_id_len);
6633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006635}
6636
6637static int ssl_cookie_check_dummy( void *ctx,
6638 const unsigned char *cookie, size_t cookie_len,
6639 const unsigned char *cli_id, size_t cli_id_len )
6640{
6641 ((void) ctx);
6642 ((void) cookie);
6643 ((void) cookie_len);
6644 ((void) cli_id);
6645 ((void) cli_id_len);
6646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006648}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006649#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006650
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006651/* Once ssl->out_hdr as the address of the beginning of the
6652 * next outgoing record is set, deduce the other pointers.
6653 *
6654 * Note: For TLS, we save the implicit record sequence number
6655 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6656 * and the caller has to make sure there's space for this.
6657 */
6658
6659static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6660 mbedtls_ssl_transform *transform )
6661{
6662#if defined(MBEDTLS_SSL_PROTO_DTLS)
6663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6664 {
6665 ssl->out_ctr = ssl->out_hdr + 3;
6666 ssl->out_len = ssl->out_hdr + 11;
6667 ssl->out_iv = ssl->out_hdr + 13;
6668 }
6669 else
6670#endif
6671 {
6672 ssl->out_ctr = ssl->out_hdr - 8;
6673 ssl->out_len = ssl->out_hdr + 3;
6674 ssl->out_iv = ssl->out_hdr + 5;
6675 }
6676
6677 /* Adjust out_msg to make space for explicit IV, if used. */
6678 if( transform != NULL &&
6679 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6680 {
6681 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6682 }
6683 else
6684 ssl->out_msg = ssl->out_iv;
6685}
6686
6687/* Once ssl->in_hdr as the address of the beginning of the
6688 * next incoming record is set, deduce the other pointers.
6689 *
6690 * Note: For TLS, we save the implicit record sequence number
6691 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6692 * and the caller has to make sure there's space for this.
6693 */
6694
6695static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6696 mbedtls_ssl_transform *transform )
6697{
6698#if defined(MBEDTLS_SSL_PROTO_DTLS)
6699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6700 {
6701 ssl->in_ctr = ssl->in_hdr + 3;
6702 ssl->in_len = ssl->in_hdr + 11;
6703 ssl->in_iv = ssl->in_hdr + 13;
6704 }
6705 else
6706#endif
6707 {
6708 ssl->in_ctr = ssl->in_hdr - 8;
6709 ssl->in_len = ssl->in_hdr + 3;
6710 ssl->in_iv = ssl->in_hdr + 5;
6711 }
6712
6713 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6714 if( transform != NULL &&
6715 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6716 {
6717 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6718 }
6719 else
6720 ssl->in_msg = ssl->in_iv;
6721}
6722
Paul Bakker5121ce52009-01-03 21:22:43 +00006723/*
6724 * Initialize an SSL context
6725 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006726void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6727{
6728 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6729}
6730
6731/*
6732 * Setup an SSL context
6733 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006734
6735static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6736{
6737 /* Set the incoming and outgoing record pointers. */
6738#if defined(MBEDTLS_SSL_PROTO_DTLS)
6739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6740 {
6741 ssl->out_hdr = ssl->out_buf;
6742 ssl->in_hdr = ssl->in_buf;
6743 }
6744 else
6745#endif /* MBEDTLS_SSL_PROTO_DTLS */
6746 {
6747 ssl->out_hdr = ssl->out_buf + 8;
6748 ssl->in_hdr = ssl->in_buf + 8;
6749 }
6750
6751 /* Derive other internal pointers. */
6752 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6753 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6754}
6755
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006756int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006757 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006758{
Paul Bakker48916f92012-09-16 19:57:18 +00006759 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006760
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006761 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006762
6763 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006764 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006765 */
Angus Grattond8213d02016-05-25 20:56:48 +10006766 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6767 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 {
Angus Grattond8213d02016-05-25 20:56:48 +10006769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6770 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6771 }
6772
6773 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6774 if( ssl->out_buf == NULL )
6775 {
6776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006778 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006779 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 }
6781
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006782 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006783
Paul Bakker48916f92012-09-16 19:57:18 +00006784 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6785 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006786
6787 return( 0 );
6788}
6789
6790/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006791 * Reset an initialized and used SSL context for re-use while retaining
6792 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006793 *
6794 * If partial is non-zero, keep data in the input buffer and client ID.
6795 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006796 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006797static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006798{
Paul Bakker48916f92012-09-16 19:57:18 +00006799 int ret;
6800
Hanno Becker7e772132018-08-10 12:38:21 +01006801#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6802 !defined(MBEDTLS_SSL_SRV_C)
6803 ((void) partial);
6804#endif
6805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006807
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006808 /* Cancel any possibly running timer */
6809 ssl_set_timer( ssl, 0 );
6810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811#if defined(MBEDTLS_SSL_RENEGOTIATION)
6812 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006813 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006814
6815 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6817 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006820
Paul Bakker7eb013f2011-10-06 12:37:39 +00006821 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006822 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006823
6824 ssl->in_msgtype = 0;
6825 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006827 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006828 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006831 ssl_dtls_replay_reset( ssl );
6832#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006833
6834 ssl->in_hslen = 0;
6835 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006836
6837 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006838
6839 ssl->out_msgtype = 0;
6840 ssl->out_msglen = 0;
6841 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6843 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006844 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006845#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006846
Hanno Becker19859472018-08-06 09:40:20 +01006847 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6848
Paul Bakker48916f92012-09-16 19:57:18 +00006849 ssl->transform_in = NULL;
6850 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006851
Hanno Becker78640902018-08-13 16:35:15 +01006852 ssl->session_in = NULL;
6853 ssl->session_out = NULL;
6854
Angus Grattond8213d02016-05-25 20:56:48 +10006855 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006856
6857#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006858 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006859#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6860 {
6861 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006862 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006863 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6866 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6869 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6872 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006873 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006874 }
6875#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006876
Paul Bakker48916f92012-09-16 19:57:18 +00006877 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 mbedtls_ssl_transform_free( ssl->transform );
6880 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006881 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006882 }
Paul Bakker48916f92012-09-16 19:57:18 +00006883
Paul Bakkerc0463502013-02-14 11:19:38 +01006884 if( ssl->session )
6885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 mbedtls_ssl_session_free( ssl->session );
6887 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006888 ssl->session = NULL;
6889 }
6890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006892 ssl->alpn_chosen = NULL;
6893#endif
6894
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006895#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006896#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006897 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006898#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006899 {
6900 mbedtls_free( ssl->cli_id );
6901 ssl->cli_id = NULL;
6902 ssl->cli_id_len = 0;
6903 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006904#endif
6905
Paul Bakker48916f92012-09-16 19:57:18 +00006906 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6907 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006908
6909 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006910}
6911
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006912/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006913 * Reset an initialized and used SSL context for re-use while retaining
6914 * all application-set variables, function pointers and data.
6915 */
6916int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6917{
6918 return( ssl_session_reset_int( ssl, 0 ) );
6919}
6920
6921/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006922 * SSL set accessors
6923 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006924void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006926 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006927}
6928
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006929void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006931 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006932}
6933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006935void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006937 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006938}
6939#endif
6940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006942void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006943{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006944 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006945}
6946#endif
6947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006949
6950void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6951 unsigned allow_packing )
6952{
6953 ssl->disable_datagram_packing = !allow_packing;
6954}
6955
6956void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6957 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006959 conf->hs_timeout_min = min;
6960 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006961}
6962#endif
6963
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006964void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006966 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006967}
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006970void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006971 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006972 void *p_vrfy )
6973{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006974 conf->f_vrfy = f_vrfy;
6975 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006978
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006979void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006980 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006981 void *p_rng )
6982{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006983 conf->f_rng = f_rng;
6984 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006985}
6986
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006987void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006988 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006989 void *p_dbg )
6990{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006991 conf->f_dbg = f_dbg;
6992 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006993}
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006996 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006997 mbedtls_ssl_send_t *f_send,
6998 mbedtls_ssl_recv_t *f_recv,
6999 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007000{
7001 ssl->p_bio = p_bio;
7002 ssl->f_send = f_send;
7003 ssl->f_recv = f_recv;
7004 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007005}
7006
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007007void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007008{
7009 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007010}
7011
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007012void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7013 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007014 mbedtls_ssl_set_timer_t *f_set_timer,
7015 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007016{
7017 ssl->p_timer = p_timer;
7018 ssl->f_set_timer = f_set_timer;
7019 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007020
7021 /* Make sure we start with no timer running */
7022 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007023}
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007026void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007027 void *p_cache,
7028 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7029 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007030{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007031 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007032 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007033 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037#if defined(MBEDTLS_SSL_CLI_C)
7038int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007039{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007040 int ret;
7041
7042 if( ssl == NULL ||
7043 session == NULL ||
7044 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007045 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007048 }
7049
7050 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7051 return( ret );
7052
Paul Bakker0a597072012-09-25 21:55:46 +00007053 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007054
7055 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007059void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007060 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007061{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007062 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7063 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7064 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7065 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007066}
7067
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007068void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007069 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007070 int major, int minor )
7071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007073 return;
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007076 return;
7077
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007078 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007079}
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007082void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007083 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007084{
7085 conf->cert_profile = profile;
7086}
7087
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007088/* Append a new keycert entry to a (possibly empty) list */
7089static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7090 mbedtls_x509_crt *cert,
7091 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007092{
niisato8ee24222018-06-25 19:05:48 +09007093 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007094
niisato8ee24222018-06-25 19:05:48 +09007095 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7096 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007097 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007098
niisato8ee24222018-06-25 19:05:48 +09007099 new_cert->cert = cert;
7100 new_cert->key = key;
7101 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007102
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007103 /* Update head is the list was null, else add to the end */
7104 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007105 {
niisato8ee24222018-06-25 19:05:48 +09007106 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007107 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007108 else
7109 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007110 mbedtls_ssl_key_cert *cur = *head;
7111 while( cur->next != NULL )
7112 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007113 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007114 }
7115
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007116 return( 0 );
7117}
7118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007119int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007120 mbedtls_x509_crt *own_cert,
7121 mbedtls_pk_context *pk_key )
7122{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007123 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007124}
7125
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007126void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007127 mbedtls_x509_crt *ca_chain,
7128 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007129{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007130 conf->ca_chain = ca_chain;
7131 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007134
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007135#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7136int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7137 mbedtls_x509_crt *own_cert,
7138 mbedtls_pk_context *pk_key )
7139{
7140 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7141 own_cert, pk_key ) );
7142}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007143
7144void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7145 mbedtls_x509_crt *ca_chain,
7146 mbedtls_x509_crl *ca_crl )
7147{
7148 ssl->handshake->sni_ca_chain = ca_chain;
7149 ssl->handshake->sni_ca_crl = ca_crl;
7150}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007151
7152void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7153 int authmode )
7154{
7155 ssl->handshake->sni_authmode = authmode;
7156}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007157#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7158
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007159#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007160/*
7161 * Set EC J-PAKE password for current handshake
7162 */
7163int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7164 const unsigned char *pw,
7165 size_t pw_len )
7166{
7167 mbedtls_ecjpake_role role;
7168
Janos Follath8eb64132016-06-03 15:40:57 +01007169 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7171
7172 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7173 role = MBEDTLS_ECJPAKE_SERVER;
7174 else
7175 role = MBEDTLS_ECJPAKE_CLIENT;
7176
7177 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7178 role,
7179 MBEDTLS_MD_SHA256,
7180 MBEDTLS_ECP_DP_SECP256R1,
7181 pw, pw_len ) );
7182}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007183#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007186int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007187 const unsigned char *psk, size_t psk_len,
7188 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007189{
Paul Bakker6db455e2013-09-18 17:29:31 +02007190 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007195
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007196 /* Identity len will be encoded on two bytes */
7197 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007198 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007199 {
7200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7201 }
7202
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007203 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007204 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007205 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007206
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007207 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007208 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007209 conf->psk_len = 0;
7210 }
7211 if( conf->psk_identity != NULL )
7212 {
7213 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007214 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007215 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007216 }
7217
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007218 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7219 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007220 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007221 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007222 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007223 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007224 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007225 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007226 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007227
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007228 conf->psk_len = psk_len;
7229 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007230
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007231 memcpy( conf->psk, psk, conf->psk_len );
7232 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007233
7234 return( 0 );
7235}
7236
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007237int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7238 const unsigned char *psk, size_t psk_len )
7239{
7240 if( psk == NULL || ssl->handshake == NULL )
7241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7242
7243 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7245
7246 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007247 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007248 mbedtls_platform_zeroize( ssl->handshake->psk,
7249 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007250 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007251 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007252 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007253
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007254 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007256
7257 ssl->handshake->psk_len = psk_len;
7258 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7259
7260 return( 0 );
7261}
7262
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007263void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007265 size_t),
7266 void *p_psk )
7267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007268 conf->f_psk = f_psk;
7269 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007272
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007273#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007274
7275#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007276int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007277{
7278 int ret;
7279
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007280 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7281 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7282 {
7283 mbedtls_mpi_free( &conf->dhm_P );
7284 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007287
7288 return( 0 );
7289}
Hanno Becker470a8c42017-10-04 15:28:46 +01007290#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Hanno Beckera90658f2017-10-04 15:29:08 +01007292int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7293 const unsigned char *dhm_P, size_t P_len,
7294 const unsigned char *dhm_G, size_t G_len )
7295{
7296 int ret;
7297
7298 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7299 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7300 {
7301 mbedtls_mpi_free( &conf->dhm_P );
7302 mbedtls_mpi_free( &conf->dhm_G );
7303 return( ret );
7304 }
7305
7306 return( 0 );
7307}
Paul Bakker5121ce52009-01-03 21:22:43 +00007308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007309int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007310{
7311 int ret;
7312
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007313 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7314 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7315 {
7316 mbedtls_mpi_free( &conf->dhm_P );
7317 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007318 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007319 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007320
7321 return( 0 );
7322}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007323#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007324
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007325#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7326/*
7327 * Set the minimum length for Diffie-Hellman parameters
7328 */
7329void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7330 unsigned int bitlen )
7331{
7332 conf->dhm_min_bitlen = bitlen;
7333}
7334#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7335
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007336#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007337/*
7338 * Set allowed/preferred hashes for handshake signatures
7339 */
7340void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7341 const int *hashes )
7342{
7343 conf->sig_hashes = hashes;
7344}
Hanno Becker947194e2017-04-07 13:25:49 +01007345#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007346
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007347#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007348/*
7349 * Set the allowed elliptic curves
7350 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007351void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007352 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007353{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007354 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007355}
Hanno Becker947194e2017-04-07 13:25:49 +01007356#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007357
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007358#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007360{
Hanno Becker947194e2017-04-07 13:25:49 +01007361 /* Initialize to suppress unnecessary compiler warning */
7362 size_t hostname_len = 0;
7363
7364 /* Check if new hostname is valid before
7365 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007366 if( hostname != NULL )
7367 {
7368 hostname_len = strlen( hostname );
7369
7370 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7372 }
7373
7374 /* Now it's clear that we will overwrite the old hostname,
7375 * so we can free it safely */
7376
7377 if( ssl->hostname != NULL )
7378 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007379 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007380 mbedtls_free( ssl->hostname );
7381 }
7382
7383 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007384
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007386 {
7387 ssl->hostname = NULL;
7388 }
7389 else
7390 {
7391 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007392 if( ssl->hostname == NULL )
7393 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007394
Hanno Becker947194e2017-04-07 13:25:49 +01007395 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007396
Hanno Becker947194e2017-04-07 13:25:49 +01007397 ssl->hostname[hostname_len] = '\0';
7398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007399
7400 return( 0 );
7401}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007402#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007403
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007404#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007405void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007407 const unsigned char *, size_t),
7408 void *p_sni )
7409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007410 conf->f_sni = f_sni;
7411 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007416int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007417{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007418 size_t cur_len, tot_len;
7419 const char **p;
7420
7421 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007422 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7423 * MUST NOT be truncated."
7424 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007425 */
7426 tot_len = 0;
7427 for( p = protos; *p != NULL; p++ )
7428 {
7429 cur_len = strlen( *p );
7430 tot_len += cur_len;
7431
7432 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007434 }
7435
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007436 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007437
7438 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007439}
7440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007442{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007443 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007446
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007447void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007448{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007449 conf->max_major_ver = major;
7450 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007451}
7452
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007453void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007454{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007455 conf->min_major_ver = major;
7456 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007457}
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007460void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007461{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007462 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007463}
7464#endif
7465
Janos Follath088ce432017-04-10 12:42:31 +01007466#if defined(MBEDTLS_SSL_SRV_C)
7467void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7468 char cert_req_ca_list )
7469{
7470 conf->cert_req_ca_list = cert_req_ca_list;
7471}
7472#endif
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007475void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007476{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007477 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007478}
7479#endif
7480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007482void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007483{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007484 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007485}
7486#endif
7487
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007488#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007489void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007491 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007492}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007493#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007494
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007495#if defined(MBEDTLS_SSL_PROTO_DTLS)
7496void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7497{
7498 conf->mtu = mtu;
7499}
7500#endif
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007503int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007506 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007509 }
7510
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007511 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007512
7513 return( 0 );
7514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007518void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007520 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007525void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007526{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007527 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007528}
7529#endif
7530
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007531void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007532{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007533 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007534}
7535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007537void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007539 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007540}
7541
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007542void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007544 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007545}
7546
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007547void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007548 const unsigned char period[8] )
7549{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007550 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007555#if defined(MBEDTLS_SSL_CLI_C)
7556void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007557{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007558 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007559}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007560#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007561
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007562#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007563void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7564 mbedtls_ssl_ticket_write_t *f_ticket_write,
7565 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7566 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007567{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007568 conf->f_ticket_write = f_ticket_write;
7569 conf->f_ticket_parse = f_ticket_parse;
7570 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007571}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007574
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007575#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7576void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7577 mbedtls_ssl_export_keys_t *f_export_keys,
7578 void *p_export_keys )
7579{
7580 conf->f_export_keys = f_export_keys;
7581 conf->p_export_keys = p_export_keys;
7582}
7583#endif
7584
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007585#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007586void mbedtls_ssl_conf_async_private_cb(
7587 mbedtls_ssl_config *conf,
7588 mbedtls_ssl_async_sign_t *f_async_sign,
7589 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7590 mbedtls_ssl_async_resume_t *f_async_resume,
7591 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007592 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007593{
7594 conf->f_async_sign_start = f_async_sign;
7595 conf->f_async_decrypt_start = f_async_decrypt;
7596 conf->f_async_resume = f_async_resume;
7597 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007598 conf->p_async_config_data = async_config_data;
7599}
7600
Gilles Peskine8f97af72018-04-26 11:46:10 +02007601void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7602{
7603 return( conf->p_async_config_data );
7604}
7605
Gilles Peskine1febfef2018-04-30 11:54:39 +02007606void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007607{
7608 if( ssl->handshake == NULL )
7609 return( NULL );
7610 else
7611 return( ssl->handshake->user_async_ctx );
7612}
7613
Gilles Peskine1febfef2018-04-30 11:54:39 +02007614void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007615 void *ctx )
7616{
7617 if( ssl->handshake != NULL )
7618 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007619}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007620#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007621
Paul Bakker5121ce52009-01-03 21:22:43 +00007622/*
7623 * SSL get accessors
7624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007626{
7627 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7628}
7629
Hanno Becker8b170a02017-10-10 11:51:19 +01007630int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7631{
7632 /*
7633 * Case A: We're currently holding back
7634 * a message for further processing.
7635 */
7636
7637 if( ssl->keep_current_message == 1 )
7638 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007640 return( 1 );
7641 }
7642
7643 /*
7644 * Case B: Further records are pending in the current datagram.
7645 */
7646
7647#if defined(MBEDTLS_SSL_PROTO_DTLS)
7648 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7649 ssl->in_left > ssl->next_record_offset )
7650 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007652 return( 1 );
7653 }
7654#endif /* MBEDTLS_SSL_PROTO_DTLS */
7655
7656 /*
7657 * Case C: A handshake message is being processed.
7658 */
7659
Hanno Becker8b170a02017-10-10 11:51:19 +01007660 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7661 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007663 return( 1 );
7664 }
7665
7666 /*
7667 * Case D: An application data message is being processed
7668 */
7669 if( ssl->in_offt != NULL )
7670 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007672 return( 1 );
7673 }
7674
7675 /*
7676 * In all other cases, the rest of the message can be dropped.
7677 * As in ssl_read_record_layer, this needs to be adapted if
7678 * we implement support for multiple alerts in single records.
7679 */
7680
7681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7682 return( 0 );
7683}
7684
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007685uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007686{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007687 if( ssl->session != NULL )
7688 return( ssl->session->verify_result );
7689
7690 if( ssl->session_negotiate != NULL )
7691 return( ssl->session_negotiate->verify_result );
7692
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007693 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007694}
7695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007697{
Paul Bakker926c8e42013-03-06 10:23:34 +01007698 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007699 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007702}
7703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007707 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007708 {
7709 switch( ssl->minor_ver )
7710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007712 return( "DTLSv1.0" );
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007715 return( "DTLSv1.2" );
7716
7717 default:
7718 return( "unknown (DTLS)" );
7719 }
7720 }
7721#endif
7722
Paul Bakker43ca69c2011-01-15 17:35:19 +00007723 switch( ssl->minor_ver )
7724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007726 return( "SSLv3.0" );
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007729 return( "TLSv1.0" );
7730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007732 return( "TLSv1.1" );
7733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007735 return( "TLSv1.2" );
7736
Paul Bakker43ca69c2011-01-15 17:35:19 +00007737 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007738 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007739 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007740}
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007743{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007744 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007746 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007747
Hanno Becker78640902018-08-13 16:35:15 +01007748 if( transform == NULL )
7749 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_ZLIB_SUPPORT)
7752 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7753 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007754 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007755#endif
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 case MBEDTLS_MODE_GCM:
7760 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007761 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007763 transform_expansion = transform->minlen;
7764 break;
7765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007767
7768 block_size = mbedtls_cipher_get_block_size(
7769 &transform->cipher_ctx_enc );
7770
7771#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7772 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7773 {
7774 /* Expansion due to addition of
7775 * - MAC
7776 * - CBC padding (theoretically up to 256 bytes, but
7777 * we never use more than block_size)
7778 * - explicit IV
7779 */
7780 transform_expansion = transform->maclen + 2 * block_size;
7781 }
7782 else
7783#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7784 {
7785 /* No explicit IV prior to TLS 1.1. */
7786 transform_expansion = transform->maclen + block_size;
7787 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007788 break;
7789
7790 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007793 }
7794
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007795 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007796}
7797
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007798#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7799size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7800{
7801 size_t max_len;
7802
7803 /*
7804 * Assume mfl_code is correct since it was checked when set
7805 */
Angus Grattond8213d02016-05-25 20:56:48 +10007806 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007807
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007808 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007809 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007810 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007811 {
Angus Grattond8213d02016-05-25 20:56:48 +10007812 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007813 }
7814
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007815 /* During a handshake, use the value being negotiated */
7816 if( ssl->session_negotiate != NULL &&
7817 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7818 {
7819 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7820 }
7821
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007822 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007823}
7824#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7825
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007826int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7827{
7828 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7829
7830#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7831 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7832
7833 if( max_len > mfl )
7834 max_len = mfl;
7835#endif
7836
7837#if defined(MBEDTLS_SSL_PROTO_DTLS)
7838 if( ssl->conf->mtu != 0 )
7839 {
7840 const size_t mtu = ssl->conf->mtu;
7841 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7842 const size_t overhead = (size_t) ret;
7843
7844 if( ret < 0 )
7845 return( ret );
7846
7847 if( mtu <= overhead )
7848 {
7849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7850 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7851 }
7852
7853 if( max_len > mtu - overhead )
7854 max_len = mtu - overhead;
7855 }
7856#endif
7857
Hanno Becker0defedb2018-08-10 12:35:02 +01007858#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7859 !defined(MBEDTLS_SSL_PROTO_DTLS)
7860 ((void) ssl);
7861#endif
7862
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007863 return( (int) max_len );
7864}
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_X509_CRT_PARSE_C)
7867const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007868{
7869 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007870 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007871
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007872 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_SSL_CLI_C)
7877int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007878{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007879 if( ssl == NULL ||
7880 dst == NULL ||
7881 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007882 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007885 }
7886
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007887 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007890
Paul Bakker5121ce52009-01-03 21:22:43 +00007891/*
Paul Bakker1961b702013-01-25 14:49:24 +01007892 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007897
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007898 if( ssl == NULL || ssl->conf == NULL )
7899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007906 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007908#endif
7909
Paul Bakker1961b702013-01-25 14:49:24 +01007910 return( ret );
7911}
7912
7913/*
7914 * Perform the SSL handshake
7915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007917{
7918 int ret = 0;
7919
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007920 if( ssl == NULL || ssl->conf == NULL )
7921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007928
7929 if( ret != 0 )
7930 break;
7931 }
7932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007934
7935 return( ret );
7936}
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_RENEGOTIATION)
7939#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007940/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007941 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007944{
7945 int ret;
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007948
7949 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7951 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007952
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007953 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007954 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007956 return( ret );
7957 }
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007960
7961 return( 0 );
7962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007964
7965/*
7966 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 * - any side: calling mbedtls_ssl_renegotiate(),
7968 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7969 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007970 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007971 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007975{
7976 int ret;
7977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007979
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007980 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7981 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007982
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007983 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7984 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007988 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007990 ssl->handshake->out_msg_seq = 1;
7991 else
7992 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007993 }
7994#endif
7995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7997 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008002 return( ret );
8003 }
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008006
8007 return( 0 );
8008}
8009
8010/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008011 * Renegotiate current connection on client,
8012 * or request renegotiation on server
8013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008017
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008018 if( ssl == NULL || ssl->conf == NULL )
8019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008022 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008023 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008029
8030 /* Did we already try/start sending HelloRequest? */
8031 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008033
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008034 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008039 /*
8040 * On client, either start the renegotiation process or,
8041 * if already in progress, continue the handshake
8042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008047
8048 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008051 return( ret );
8052 }
8053 }
8054 else
8055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008059 return( ret );
8060 }
8061 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008063
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008064 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008065}
8066
8067/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008068 * Check record counters and renegotiate if they're above the limit.
8069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008071{
Andres AG2196c7f2016-12-15 17:01:16 +00008072 size_t ep_len = ssl_ep_len( ssl );
8073 int in_ctr_cmp;
8074 int out_ctr_cmp;
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8077 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008078 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008079 {
8080 return( 0 );
8081 }
8082
Andres AG2196c7f2016-12-15 17:01:16 +00008083 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8084 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008085 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008086 ssl->conf->renego_period + ep_len, 8 - ep_len );
8087
8088 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008089 {
8090 return( 0 );
8091 }
8092
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008097
8098/*
8099 * Receive application data decrypted from the SSL layer
8100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008102{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008103 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008104 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008105
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008106 if( ssl == NULL || ssl->conf == NULL )
8107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008112 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008115 return( ret );
8116
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008117 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008119 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008120 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008121 return( ret );
8122 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008123 }
8124#endif
8125
Hanno Becker4a810fb2017-05-24 16:27:30 +01008126 /*
8127 * Check if renegotiation is necessary and/or handshake is
8128 * in process. If yes, perform/continue, and fall through
8129 * if an unexpected packet is received while the client
8130 * is waiting for the ServerHello.
8131 *
8132 * (There is no equivalent to the last condition on
8133 * the server-side as it is not treated as within
8134 * a handshake while waiting for the ClientHello
8135 * after a renegotiation request.)
8136 */
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008139 ret = ssl_check_ctr_renegotiate( ssl );
8140 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8141 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008144 return( ret );
8145 }
8146#endif
8147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008151 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8152 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008155 return( ret );
8156 }
8157 }
8158
Hanno Beckere41158b2017-10-23 13:30:32 +01008159 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008160 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008161 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008162 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008163 if( ssl->f_get_timer != NULL &&
8164 ssl->f_get_timer( ssl->p_timer ) == -1 )
8165 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008166 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008167 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008168
Hanno Becker327c93b2018-08-15 13:56:18 +01008169 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008170 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008171 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8172 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008173
Hanno Becker4a810fb2017-05-24 16:27:30 +01008174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8175 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008176 }
8177
8178 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008180 {
8181 /*
8182 * OpenSSL sends empty messages to randomize the IV
8183 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008184 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008187 return( 0 );
8188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008190 return( ret );
8191 }
8192 }
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008197
Hanno Becker4a810fb2017-05-24 16:27:30 +01008198 /*
8199 * - For client-side, expect SERVER_HELLO_REQUEST.
8200 * - For server-side, expect CLIENT_HELLO.
8201 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8202 */
8203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008205 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008207 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008210
8211 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008213 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008214 {
8215 continue;
8216 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008219 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008220#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008221
Hanno Becker4a810fb2017-05-24 16:27:30 +01008222#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008223 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008227
8228 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008231 {
8232 continue;
8233 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008236 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008237#endif /* MBEDTLS_SSL_SRV_C */
8238
Hanno Becker21df7f92017-10-17 11:03:26 +01008239#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008240 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008241 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8242 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8243 ssl->conf->allow_legacy_renegotiation ==
8244 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8245 {
8246 /*
8247 * Accept renegotiation request
8248 */
Paul Bakker48916f92012-09-16 19:57:18 +00008249
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008250 /* DTLS clients need to know renego is server-initiated */
8251#if defined(MBEDTLS_SSL_PROTO_DTLS)
8252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8253 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8254 {
8255 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8256 }
8257#endif
8258 ret = ssl_start_renegotiation( ssl );
8259 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8260 ret != 0 )
8261 {
8262 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8263 return( ret );
8264 }
8265 }
8266 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008267#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008268 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008269 /*
8270 * Refuse renegotiation
8271 */
8272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_SSL_PROTO_SSL3)
8276 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008277 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008278 /* SSLv3 does not have a "no_renegotiation" warning, so
8279 we send a fatal alert and abort the connection. */
8280 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8281 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8282 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008283 }
8284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8286#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8287 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8288 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8291 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8292 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008293 {
8294 return( ret );
8295 }
Paul Bakker48916f92012-09-16 19:57:18 +00008296 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8299 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008303 }
Paul Bakker48916f92012-09-16 19:57:18 +00008304 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008305
Hanno Becker90333da2017-10-10 11:27:13 +01008306 /* At this point, we don't know whether the renegotiation has been
8307 * completed or not. The cases to consider are the following:
8308 * 1) The renegotiation is complete. In this case, no new record
8309 * has been read yet.
8310 * 2) The renegotiation is incomplete because the client received
8311 * an application data record while awaiting the ServerHello.
8312 * 3) The renegotiation is incomplete because the client received
8313 * a non-handshake, non-application data message while awaiting
8314 * the ServerHello.
8315 * In each of these case, looping will be the proper action:
8316 * - For 1), the next iteration will read a new record and check
8317 * if it's application data.
8318 * - For 2), the loop condition isn't satisfied as application data
8319 * is present, hence continue is the same as break
8320 * - For 3), the loop condition is satisfied and read_record
8321 * will re-deliver the message that was held back by the client
8322 * when expecting the ServerHello.
8323 */
8324 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008325 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008326#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008328 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008329 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008330 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008331 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008334 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008336 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008337 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8342 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008345 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008346 }
8347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8351 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008352 }
8353
8354 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008355
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008356 /* We're going to return something now, cancel timer,
8357 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008359 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008360
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008361#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008362 /* If we requested renego but received AppData, resend HelloRequest.
8363 * Do it now, after setting in_offt, to avoid taking this branch
8364 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008368 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008369 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008372 return( ret );
8373 }
8374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008376#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008377 }
8378
8379 n = ( len < ssl->in_msglen )
8380 ? len : ssl->in_msglen;
8381
8382 memcpy( buf, ssl->in_offt, n );
8383 ssl->in_msglen -= n;
8384
8385 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008386 {
8387 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008388 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008389 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008391 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008392 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008393 /* more data available */
8394 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008398
Paul Bakker23986e52011-04-24 08:57:21 +00008399 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008400}
8401
8402/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008403 * Send application data to be encrypted by the SSL layer, taking care of max
8404 * fragment length and buffer size.
8405 *
8406 * According to RFC 5246 Section 6.2.1:
8407 *
8408 * Zero-length fragments of Application data MAY be sent as they are
8409 * potentially useful as a traffic analysis countermeasure.
8410 *
8411 * Therefore, it is possible that the input message length is 0 and the
8412 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008413 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008414static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008415 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008416{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008417 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8418 const size_t max_len = (size_t) ret;
8419
8420 if( ret < 0 )
8421 {
8422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8423 return( ret );
8424 }
8425
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008426 if( len > max_len )
8427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008432 "maximum fragment length: %d > %d",
8433 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008435 }
8436 else
8437#endif
8438 len = max_len;
8439 }
Paul Bakker887bd502011-06-08 13:10:54 +00008440
Paul Bakker5121ce52009-01-03 21:22:43 +00008441 if( ssl->out_left != 0 )
8442 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008443 /*
8444 * The user has previously tried to send the data and
8445 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8446 * written. In this case, we expect the high-level write function
8447 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008452 return( ret );
8453 }
8454 }
Paul Bakker887bd502011-06-08 13:10:54 +00008455 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008456 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008457 /*
8458 * The user is trying to send a message the first time, so we need to
8459 * copy the data into the internal buffers and setup the data structure
8460 * to keep track of partial writes
8461 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008462 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008464 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008465
Hanno Becker67bc7c32018-08-06 11:33:50 +01008466 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008469 return( ret );
8470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008471 }
8472
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008473 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008474}
8475
8476/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008477 * Write application data, doing 1/n-1 splitting if necessary.
8478 *
8479 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008480 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008481 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008484static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008485 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008486{
8487 int ret;
8488
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008489 if( ssl->conf->cbc_record_splitting ==
8490 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008491 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008492 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8493 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8494 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008495 {
8496 return( ssl_write_real( ssl, buf, len ) );
8497 }
8498
8499 if( ssl->split_done == 0 )
8500 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008501 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008502 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008503 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008504 }
8505
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008506 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8507 return( ret );
8508 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008509
8510 return( ret + 1 );
8511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008513
8514/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008515 * Write application data (public-facing wrapper)
8516 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008517int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008518{
8519 int ret;
8520
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008522
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008523 if( ssl == NULL || ssl->conf == NULL )
8524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8525
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008526#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008527 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8528 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008529 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008530 return( ret );
8531 }
8532#endif
8533
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008534 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008535 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008536 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008537 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008539 return( ret );
8540 }
8541 }
8542
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008543#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008544 ret = ssl_write_split( ssl, buf, len );
8545#else
8546 ret = ssl_write_real( ssl, buf, len );
8547#endif
8548
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008550
8551 return( ret );
8552}
8553
8554/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008555 * Notify the peer that the connection is being closed
8556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008558{
8559 int ret;
8560
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008561 if( ssl == NULL || ssl->conf == NULL )
8562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008565
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008566 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8572 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8573 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008576 return( ret );
8577 }
8578 }
8579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008581
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008582 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008583}
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008586{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008587 if( transform == NULL )
8588 return;
8589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008591 deflateEnd( &transform->ctx_deflate );
8592 inflateEnd( &transform->ctx_inflate );
8593#endif
8594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8596 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 mbedtls_md_free( &transform->md_ctx_enc );
8599 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008600
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008601 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008602}
8603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604#if defined(MBEDTLS_X509_CRT_PARSE_C)
8605static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008608
8609 while( cur != NULL )
8610 {
8611 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008613 cur = next;
8614 }
8615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008617
Hanno Becker0271f962018-08-16 13:23:47 +01008618#if defined(MBEDTLS_SSL_PROTO_DTLS)
8619
8620static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8621{
8622 unsigned offset;
8623 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8624
8625 if( hs == NULL )
8626 return;
8627
8628 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008629 ssl_buffering_free_slot( ssl, offset );
8630}
8631
8632static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8633 uint8_t slot )
8634{
8635 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8636 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
8637 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008638 {
Hanno Beckere605b192018-08-21 15:59:07 +01008639 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8640 mbedtls_free( hs_buf->data );
8641 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008642 }
8643}
8644
8645#endif /* MBEDTLS_SSL_PROTO_DTLS */
8646
Gilles Peskine9b562d52018-04-25 20:32:43 +02008647void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008648{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008649 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8650
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008651 if( handshake == NULL )
8652 return;
8653
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008654#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8655 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8656 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008657 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008658 handshake->async_in_progress = 0;
8659 }
8660#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8661
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008662#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8663 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8664 mbedtls_md5_free( &handshake->fin_md5 );
8665 mbedtls_sha1_free( &handshake->fin_sha1 );
8666#endif
8667#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8668#if defined(MBEDTLS_SHA256_C)
8669 mbedtls_sha256_free( &handshake->fin_sha256 );
8670#endif
8671#if defined(MBEDTLS_SHA512_C)
8672 mbedtls_sha512_free( &handshake->fin_sha512 );
8673#endif
8674#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676#if defined(MBEDTLS_DHM_C)
8677 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008678#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679#if defined(MBEDTLS_ECDH_C)
8680 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008681#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008682#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008683 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008684#if defined(MBEDTLS_SSL_CLI_C)
8685 mbedtls_free( handshake->ecjpake_cache );
8686 handshake->ecjpake_cache = NULL;
8687 handshake->ecjpake_cache_len = 0;
8688#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008689#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008690
Janos Follath4ae5c292016-02-10 11:27:43 +00008691#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8692 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008693 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008695#endif
8696
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008697#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8698 if( handshake->psk != NULL )
8699 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008700 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008701 mbedtls_free( handshake->psk );
8702 }
8703#endif
8704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8706 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008707 /*
8708 * Free only the linked list wrapper, not the keys themselves
8709 * since the belong to the SNI callback
8710 */
8711 if( handshake->sni_key_cert != NULL )
8712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008714
8715 while( cur != NULL )
8716 {
8717 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008719 cur = next;
8720 }
8721 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724#if defined(MBEDTLS_SSL_PROTO_DTLS)
8725 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008726 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008727 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008728 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008729#endif
8730
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008731 mbedtls_platform_zeroize( handshake,
8732 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008733}
8734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008736{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008737 if( session == NULL )
8738 return;
8739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008741 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 mbedtls_x509_crt_free( session->peer_cert );
8744 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008745 }
Paul Bakkered27a042013-04-18 22:46:23 +02008746#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008747
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008748#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008750#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008751
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008752 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008753}
8754
Paul Bakker5121ce52009-01-03 21:22:43 +00008755/*
8756 * Free an SSL context
8757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008759{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008760 if( ssl == NULL )
8761 return;
8762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008764
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008765 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008766 {
Angus Grattond8213d02016-05-25 20:56:48 +10008767 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008769 }
8770
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008771 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008772 {
Angus Grattond8213d02016-05-25 20:56:48 +10008773 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008775 }
8776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008778 if( ssl->compress_buf != NULL )
8779 {
Angus Grattond8213d02016-05-25 20:56:48 +10008780 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008782 }
8783#endif
8784
Paul Bakker48916f92012-09-16 19:57:18 +00008785 if( ssl->transform )
8786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 mbedtls_ssl_transform_free( ssl->transform );
8788 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008789 }
8790
8791 if( ssl->handshake )
8792 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008793 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8795 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797 mbedtls_free( ssl->handshake );
8798 mbedtls_free( ssl->transform_negotiate );
8799 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008800 }
8801
Paul Bakkerc0463502013-02-14 11:19:38 +01008802 if( ssl->session )
8803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 mbedtls_ssl_session_free( ssl->session );
8805 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008806 }
8807
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008808#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008809 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008810 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008811 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008813 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008814#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8817 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8820 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008821 }
8822#endif
8823
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008824#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008826#endif
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008829
Paul Bakker86f04f42013-02-14 11:20:09 +01008830 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008831 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008832}
8833
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008834/*
8835 * Initialze mbedtls_ssl_config
8836 */
8837void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8838{
8839 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8840}
8841
Simon Butcherc97b6972015-12-27 23:48:17 +00008842#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008843static int ssl_preset_default_hashes[] = {
8844#if defined(MBEDTLS_SHA512_C)
8845 MBEDTLS_MD_SHA512,
8846 MBEDTLS_MD_SHA384,
8847#endif
8848#if defined(MBEDTLS_SHA256_C)
8849 MBEDTLS_MD_SHA256,
8850 MBEDTLS_MD_SHA224,
8851#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008852#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008853 MBEDTLS_MD_SHA1,
8854#endif
8855 MBEDTLS_MD_NONE
8856};
Simon Butcherc97b6972015-12-27 23:48:17 +00008857#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008858
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008859static int ssl_preset_suiteb_ciphersuites[] = {
8860 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8861 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8862 0
8863};
8864
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008865#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008866static int ssl_preset_suiteb_hashes[] = {
8867 MBEDTLS_MD_SHA256,
8868 MBEDTLS_MD_SHA384,
8869 MBEDTLS_MD_NONE
8870};
8871#endif
8872
8873#if defined(MBEDTLS_ECP_C)
8874static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8875 MBEDTLS_ECP_DP_SECP256R1,
8876 MBEDTLS_ECP_DP_SECP384R1,
8877 MBEDTLS_ECP_DP_NONE
8878};
8879#endif
8880
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008881/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008882 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008883 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008884int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008885 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008886{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008887#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008888 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008889#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008890
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008891 /* Use the functions here so that they are covered in tests,
8892 * but otherwise access member directly for efficiency */
8893 mbedtls_ssl_conf_endpoint( conf, endpoint );
8894 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008895
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008896 /*
8897 * Things that are common to all presets
8898 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008899#if defined(MBEDTLS_SSL_CLI_C)
8900 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8901 {
8902 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8903#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8904 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8905#endif
8906 }
8907#endif
8908
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008909#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008910 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008911#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008912
8913#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8914 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8915#endif
8916
8917#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8918 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8919#endif
8920
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008921#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8922 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8923#endif
8924
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008925#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008926 conf->f_cookie_write = ssl_cookie_write_dummy;
8927 conf->f_cookie_check = ssl_cookie_check_dummy;
8928#endif
8929
8930#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8931 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8932#endif
8933
Janos Follath088ce432017-04-10 12:42:31 +01008934#if defined(MBEDTLS_SSL_SRV_C)
8935 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8936#endif
8937
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008938#if defined(MBEDTLS_SSL_PROTO_DTLS)
8939 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8940 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8941#endif
8942
8943#if defined(MBEDTLS_SSL_RENEGOTIATION)
8944 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008945 memset( conf->renego_period, 0x00, 2 );
8946 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008947#endif
8948
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008949#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8950 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8951 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008952 const unsigned char dhm_p[] =
8953 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8954 const unsigned char dhm_g[] =
8955 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8956
Hanno Beckera90658f2017-10-04 15:29:08 +01008957 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8958 dhm_p, sizeof( dhm_p ),
8959 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008960 {
8961 return( ret );
8962 }
8963 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008964#endif
8965
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008966 /*
8967 * Preset-specific defaults
8968 */
8969 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008970 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008971 /*
8972 * NSA Suite B
8973 */
8974 case MBEDTLS_SSL_PRESET_SUITEB:
8975 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8976 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8977 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8978 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8979
8980 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8981 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8982 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8983 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8984 ssl_preset_suiteb_ciphersuites;
8985
8986#if defined(MBEDTLS_X509_CRT_PARSE_C)
8987 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008988#endif
8989
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008990#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008991 conf->sig_hashes = ssl_preset_suiteb_hashes;
8992#endif
8993
8994#if defined(MBEDTLS_ECP_C)
8995 conf->curve_list = ssl_preset_suiteb_curves;
8996#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008997 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008998
8999 /*
9000 * Default
9001 */
9002 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009003 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9004 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9005 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9006 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9007 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9008 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9009 MBEDTLS_SSL_MIN_MINOR_VERSION :
9010 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009011 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9012 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9013
9014#if defined(MBEDTLS_SSL_PROTO_DTLS)
9015 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9016 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9017#endif
9018
9019 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9020 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9021 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9022 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9023 mbedtls_ssl_list_ciphersuites();
9024
9025#if defined(MBEDTLS_X509_CRT_PARSE_C)
9026 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9027#endif
9028
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009029#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009030 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009031#endif
9032
9033#if defined(MBEDTLS_ECP_C)
9034 conf->curve_list = mbedtls_ecp_grp_id_list();
9035#endif
9036
9037#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9038 conf->dhm_min_bitlen = 1024;
9039#endif
9040 }
9041
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009042 return( 0 );
9043}
9044
9045/*
9046 * Free mbedtls_ssl_config
9047 */
9048void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9049{
9050#if defined(MBEDTLS_DHM_C)
9051 mbedtls_mpi_free( &conf->dhm_P );
9052 mbedtls_mpi_free( &conf->dhm_G );
9053#endif
9054
9055#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9056 if( conf->psk != NULL )
9057 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009058 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009059 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009060 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009061 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009062 }
9063
9064 if( conf->psk_identity != NULL )
9065 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009066 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009067 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009068 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009069 conf->psk_identity_len = 0;
9070 }
9071#endif
9072
9073#if defined(MBEDTLS_X509_CRT_PARSE_C)
9074 ssl_key_cert_free( conf->key_cert );
9075#endif
9076
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009077 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009078}
9079
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009080#if defined(MBEDTLS_PK_C) && \
9081 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009082/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087#if defined(MBEDTLS_RSA_C)
9088 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9089 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_ECDSA_C)
9092 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9093 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009096}
9097
Hanno Becker7e5437a2017-04-28 17:15:26 +01009098unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9099{
9100 switch( type ) {
9101 case MBEDTLS_PK_RSA:
9102 return( MBEDTLS_SSL_SIG_RSA );
9103 case MBEDTLS_PK_ECDSA:
9104 case MBEDTLS_PK_ECKEY:
9105 return( MBEDTLS_SSL_SIG_ECDSA );
9106 default:
9107 return( MBEDTLS_SSL_SIG_ANON );
9108 }
9109}
9110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009112{
9113 switch( sig )
9114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115#if defined(MBEDTLS_RSA_C)
9116 case MBEDTLS_SSL_SIG_RSA:
9117 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#if defined(MBEDTLS_ECDSA_C)
9120 case MBEDTLS_SSL_SIG_ECDSA:
9121 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009122#endif
9123 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009125 }
9126}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009127#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009128
Hanno Becker7e5437a2017-04-28 17:15:26 +01009129#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9130 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9131
9132/* Find an entry in a signature-hash set matching a given hash algorithm. */
9133mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9134 mbedtls_pk_type_t sig_alg )
9135{
9136 switch( sig_alg )
9137 {
9138 case MBEDTLS_PK_RSA:
9139 return( set->rsa );
9140 case MBEDTLS_PK_ECDSA:
9141 return( set->ecdsa );
9142 default:
9143 return( MBEDTLS_MD_NONE );
9144 }
9145}
9146
9147/* Add a signature-hash-pair to a signature-hash set */
9148void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9149 mbedtls_pk_type_t sig_alg,
9150 mbedtls_md_type_t md_alg )
9151{
9152 switch( sig_alg )
9153 {
9154 case MBEDTLS_PK_RSA:
9155 if( set->rsa == MBEDTLS_MD_NONE )
9156 set->rsa = md_alg;
9157 break;
9158
9159 case MBEDTLS_PK_ECDSA:
9160 if( set->ecdsa == MBEDTLS_MD_NONE )
9161 set->ecdsa = md_alg;
9162 break;
9163
9164 default:
9165 break;
9166 }
9167}
9168
9169/* Allow exactly one hash algorithm for each signature. */
9170void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9171 mbedtls_md_type_t md_alg )
9172{
9173 set->rsa = md_alg;
9174 set->ecdsa = md_alg;
9175}
9176
9177#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9178 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9179
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009180/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009181 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009184{
9185 switch( hash )
9186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187#if defined(MBEDTLS_MD5_C)
9188 case MBEDTLS_SSL_HASH_MD5:
9189 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191#if defined(MBEDTLS_SHA1_C)
9192 case MBEDTLS_SSL_HASH_SHA1:
9193 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195#if defined(MBEDTLS_SHA256_C)
9196 case MBEDTLS_SSL_HASH_SHA224:
9197 return( MBEDTLS_MD_SHA224 );
9198 case MBEDTLS_SSL_HASH_SHA256:
9199 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201#if defined(MBEDTLS_SHA512_C)
9202 case MBEDTLS_SSL_HASH_SHA384:
9203 return( MBEDTLS_MD_SHA384 );
9204 case MBEDTLS_SSL_HASH_SHA512:
9205 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009206#endif
9207 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009209 }
9210}
9211
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009212/*
9213 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9214 */
9215unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9216{
9217 switch( md )
9218 {
9219#if defined(MBEDTLS_MD5_C)
9220 case MBEDTLS_MD_MD5:
9221 return( MBEDTLS_SSL_HASH_MD5 );
9222#endif
9223#if defined(MBEDTLS_SHA1_C)
9224 case MBEDTLS_MD_SHA1:
9225 return( MBEDTLS_SSL_HASH_SHA1 );
9226#endif
9227#if defined(MBEDTLS_SHA256_C)
9228 case MBEDTLS_MD_SHA224:
9229 return( MBEDTLS_SSL_HASH_SHA224 );
9230 case MBEDTLS_MD_SHA256:
9231 return( MBEDTLS_SSL_HASH_SHA256 );
9232#endif
9233#if defined(MBEDTLS_SHA512_C)
9234 case MBEDTLS_MD_SHA384:
9235 return( MBEDTLS_SSL_HASH_SHA384 );
9236 case MBEDTLS_MD_SHA512:
9237 return( MBEDTLS_SSL_HASH_SHA512 );
9238#endif
9239 default:
9240 return( MBEDTLS_SSL_HASH_NONE );
9241 }
9242}
9243
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009244#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009245/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009246 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009247 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009248 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009249int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009252
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009253 if( ssl->conf->curve_list == NULL )
9254 return( -1 );
9255
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009256 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009257 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009258 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009259
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009260 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009261}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009262#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009263
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009264#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009265/*
9266 * Check if a hash proposed by the peer is in our list.
9267 * Return 0 if we're willing to use it, -1 otherwise.
9268 */
9269int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9270 mbedtls_md_type_t md )
9271{
9272 const int *cur;
9273
9274 if( ssl->conf->sig_hashes == NULL )
9275 return( -1 );
9276
9277 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9278 if( *cur == (int) md )
9279 return( 0 );
9280
9281 return( -1 );
9282}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009283#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285#if defined(MBEDTLS_X509_CRT_PARSE_C)
9286int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9287 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009288 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009289 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009290{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009291 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009293 int usage = 0;
9294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009296 const char *ext_oid;
9297 size_t ext_len;
9298#endif
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9301 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009302 ((void) cert);
9303 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009304 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009305#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9308 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009309 {
9310 /* Server part of the key exchange */
9311 switch( ciphersuite->key_exchange )
9312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 case MBEDTLS_KEY_EXCHANGE_RSA:
9314 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009315 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009316 break;
9317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9319 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9320 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9321 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009322 break;
9323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9325 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009326 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009327 break;
9328
9329 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 case MBEDTLS_KEY_EXCHANGE_NONE:
9331 case MBEDTLS_KEY_EXCHANGE_PSK:
9332 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9333 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009334 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009335 usage = 0;
9336 }
9337 }
9338 else
9339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9341 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009342 }
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009345 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009346 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009347 ret = -1;
9348 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009349#else
9350 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9354 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9357 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009358 }
9359 else
9360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9362 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009363 }
9364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009366 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009367 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009368 ret = -1;
9369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009371
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009372 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009375
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009376/*
9377 * Convert version numbers to/from wire format
9378 * and, for DTLS, to/from TLS equivalent.
9379 *
9380 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009381 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009382 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9383 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009386 unsigned char ver[2] )
9387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388#if defined(MBEDTLS_SSL_PROTO_DTLS)
9389 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009392 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9393
9394 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9395 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9396 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009397 else
9398#else
9399 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009400#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009401 {
9402 ver[0] = (unsigned char) major;
9403 ver[1] = (unsigned char) minor;
9404 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009405}
9406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009408 const unsigned char ver[2] )
9409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410#if defined(MBEDTLS_SSL_PROTO_DTLS)
9411 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009412 {
9413 *major = 255 - ver[0] + 2;
9414 *minor = 255 - ver[1] + 1;
9415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009417 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9418 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009419 else
9420#else
9421 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009422#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009423 {
9424 *major = ver[0];
9425 *minor = ver[1];
9426 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009427}
9428
Simon Butcher99000142016-10-13 17:21:01 +01009429int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9430{
9431#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9432 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9433 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9434
9435 switch( md )
9436 {
9437#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9438#if defined(MBEDTLS_MD5_C)
9439 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009440 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009441#endif
9442#if defined(MBEDTLS_SHA1_C)
9443 case MBEDTLS_SSL_HASH_SHA1:
9444 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9445 break;
9446#endif
9447#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9448#if defined(MBEDTLS_SHA512_C)
9449 case MBEDTLS_SSL_HASH_SHA384:
9450 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9451 break;
9452#endif
9453#if defined(MBEDTLS_SHA256_C)
9454 case MBEDTLS_SSL_HASH_SHA256:
9455 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9456 break;
9457#endif
9458 default:
9459 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9460 }
9461
9462 return 0;
9463#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9464 (void) ssl;
9465 (void) md;
9466
9467 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9469}
9470
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009471#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9472 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9473int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9474 unsigned char *output,
9475 unsigned char *data, size_t data_len )
9476{
9477 int ret = 0;
9478 mbedtls_md5_context mbedtls_md5;
9479 mbedtls_sha1_context mbedtls_sha1;
9480
9481 mbedtls_md5_init( &mbedtls_md5 );
9482 mbedtls_sha1_init( &mbedtls_sha1 );
9483
9484 /*
9485 * digitally-signed struct {
9486 * opaque md5_hash[16];
9487 * opaque sha_hash[20];
9488 * };
9489 *
9490 * md5_hash
9491 * MD5(ClientHello.random + ServerHello.random
9492 * + ServerParams);
9493 * sha_hash
9494 * SHA(ClientHello.random + ServerHello.random
9495 * + ServerParams);
9496 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009497 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009498 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009500 goto exit;
9501 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009502 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009503 ssl->handshake->randbytes, 64 ) ) != 0 )
9504 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009506 goto exit;
9507 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009508 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009509 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009511 goto exit;
9512 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009513 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009514 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009516 goto exit;
9517 }
9518
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009519 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009520 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009522 goto exit;
9523 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009524 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009525 ssl->handshake->randbytes, 64 ) ) != 0 )
9526 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009528 goto exit;
9529 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009530 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009531 data_len ) ) != 0 )
9532 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009534 goto exit;
9535 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009536 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009537 output + 16 ) ) != 0 )
9538 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009540 goto exit;
9541 }
9542
9543exit:
9544 mbedtls_md5_free( &mbedtls_md5 );
9545 mbedtls_sha1_free( &mbedtls_sha1 );
9546
9547 if( ret != 0 )
9548 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9549 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9550
9551 return( ret );
9552
9553}
9554#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9555 MBEDTLS_SSL_PROTO_TLS1_1 */
9556
9557#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9558 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9559int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009560 unsigned char *hash, size_t *hashlen,
9561 unsigned char *data, size_t data_len,
9562 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009563{
9564 int ret = 0;
9565 mbedtls_md_context_t ctx;
9566 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009567 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009568
9569 mbedtls_md_init( &ctx );
9570
9571 /*
9572 * digitally-signed struct {
9573 * opaque client_random[32];
9574 * opaque server_random[32];
9575 * ServerDHParams params;
9576 * };
9577 */
9578 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9579 {
9580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9581 goto exit;
9582 }
9583 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9584 {
9585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9586 goto exit;
9587 }
9588 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9589 {
9590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9591 goto exit;
9592 }
9593 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9594 {
9595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9596 goto exit;
9597 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009598 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009599 {
9600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9601 goto exit;
9602 }
9603
9604exit:
9605 mbedtls_md_free( &ctx );
9606
9607 if( ret != 0 )
9608 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9609 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9610
9611 return( ret );
9612}
9613#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9614 MBEDTLS_SSL_PROTO_TLS1_2 */
9615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#endif /* MBEDTLS_SSL_TLS_C */