blob: eceac913d21735788dd8790e8d15222b13866665 [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{
Hanno Becker6aeaa052018-08-20 12:53:37 +0100114 uint16_t mtu = ssl->mtu;
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
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é-Gonnard065a2a32018-08-20 11:09:26 +02003135 * Outputs, 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)
Hanno Becker47db8772018-08-21 13:32:13 +01003396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3397 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003398 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003399 size_t remaining;
3400 ret = ssl_get_remaining_payload_in_datagram( ssl );
3401 if( ret < 0 )
3402 {
3403 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3404 ret );
3405 return( ret );
3406 }
3407
3408 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003409 if( remaining == 0 )
3410 flush = SSL_FORCE_FLUSH;
3411 else
3412 {
Hanno Becker513815a2018-08-20 11:56:09 +01003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003414 }
3415 }
3416#endif /* MBEDTLS_SSL_PROTO_DTLS */
3417
3418 if( ( flush == SSL_FORCE_FLUSH ) &&
3419 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 return( ret );
3423 }
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003426
3427 return( 0 );
3428}
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003431
3432static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3433{
3434 if( ssl->in_msglen < ssl->in_hslen ||
3435 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3436 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3437 {
3438 return( 1 );
3439 }
3440 return( 0 );
3441}
Hanno Becker44650b72018-08-16 12:51:11 +01003442
3443static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3444{
3445 return( ( ssl->in_msg[9] << 16 ) |
3446 ( ssl->in_msg[10] << 8 ) |
3447 ssl->in_msg[11] );
3448}
3449
3450static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3451{
3452 return( ( ssl->in_msg[6] << 16 ) |
3453 ( ssl->in_msg[7] << 8 ) |
3454 ssl->in_msg[8] );
3455}
3456
3457static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3458{
3459 uint32_t msg_len, frag_off, frag_len;
3460
3461 msg_len = ssl_get_hs_total_len( ssl );
3462 frag_off = ssl_get_hs_frag_off( ssl );
3463 frag_len = ssl_get_hs_frag_len( ssl );
3464
3465 if( frag_off > msg_len )
3466 return( -1 );
3467
3468 if( frag_len > msg_len - frag_off )
3469 return( -1 );
3470
3471 if( frag_len + 12 > ssl->in_msglen )
3472 return( -1 );
3473
3474 return( 0 );
3475}
3476
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003477/*
3478 * Mark bits in bitmask (used for DTLS HS reassembly)
3479 */
3480static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3481{
3482 unsigned int start_bits, end_bits;
3483
3484 start_bits = 8 - ( offset % 8 );
3485 if( start_bits != 8 )
3486 {
3487 size_t first_byte_idx = offset / 8;
3488
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003489 /* Special case */
3490 if( len <= start_bits )
3491 {
3492 for( ; len != 0; len-- )
3493 mask[first_byte_idx] |= 1 << ( start_bits - len );
3494
3495 /* Avoid potential issues with offset or len becoming invalid */
3496 return;
3497 }
3498
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003499 offset += start_bits; /* Now offset % 8 == 0 */
3500 len -= start_bits;
3501
3502 for( ; start_bits != 0; start_bits-- )
3503 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3504 }
3505
3506 end_bits = len % 8;
3507 if( end_bits != 0 )
3508 {
3509 size_t last_byte_idx = ( offset + len ) / 8;
3510
3511 len -= end_bits; /* Now len % 8 == 0 */
3512
3513 for( ; end_bits != 0; end_bits-- )
3514 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3515 }
3516
3517 memset( mask + offset / 8, 0xFF, len / 8 );
3518}
3519
3520/*
3521 * Check that bitmask is full
3522 */
3523static int ssl_bitmask_check( unsigned char *mask, size_t len )
3524{
3525 size_t i;
3526
3527 for( i = 0; i < len / 8; i++ )
3528 if( mask[i] != 0xFF )
3529 return( -1 );
3530
3531 for( i = 0; i < len % 8; i++ )
3532 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3533 return( -1 );
3534
3535 return( 0 );
3536}
3537
Hanno Becker56e205e2018-08-16 09:06:12 +01003538/* msg_len does not include the handshake header */
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003539static size_t ssl_get_reassembly_buffer_size( unsigned msg_len,
3540 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003541{
3542 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003543
3544 alloc_len = 12; /* Handshake header */
3545 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003546
3547 if( add_bitmap )
3548 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003549
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003550 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003551}
3552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003554
Hanno Becker12555c62018-08-16 12:47:53 +01003555static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3556{
3557 return( ( ssl->in_msg[1] << 16 ) |
3558 ( ssl->in_msg[2] << 8 ) |
3559 ssl->in_msg[3] );
3560}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003561
Simon Butcher99000142016-10-13 17:21:01 +01003562int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003567 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003569 }
3570
Hanno Becker12555c62018-08-16 12:47:53 +01003571 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003574 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003575 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003579 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003580 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003581 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003582
Hanno Becker44650b72018-08-16 12:51:11 +01003583 if( ssl_check_hs_header( ssl ) != 0 )
3584 {
3585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3586 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3587 }
3588
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003589 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003590 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3591 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3592 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3593 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003594 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003595 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3596 {
3597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3598 recv_msg_seq,
3599 ssl->handshake->in_msg_seq ) );
3600 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3601 }
3602
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003603 /* Retransmit only on last message from previous flight, to avoid
3604 * too many retransmissions.
3605 * Besides, No sane server ever retransmits HelloVerifyRequest */
3606 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003610 "message_seq = %d, start_of_flight = %d",
3611 recv_msg_seq,
3612 ssl->handshake->in_flight_start_seq ) );
3613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003617 return( ret );
3618 }
3619 }
3620 else
3621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003623 "message_seq = %d, expected = %d",
3624 recv_msg_seq,
3625 ssl->handshake->in_msg_seq ) );
3626 }
3627
Hanno Becker90333da2017-10-10 11:27:13 +01003628 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003629 }
3630 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003631
Hanno Becker6d97ef52018-08-16 13:09:04 +01003632 /* Message reassembly is handled alongside buffering of future
3633 * messages; the commonality is that both handshake fragments and
3634 * future messages cannot be forwarded immediately to the handshake
3635 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003636 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003639 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003640 }
3641 }
3642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003644 /* With TLS we don't handle fragmentation (for now) */
3645 if( ssl->in_msglen < ssl->in_hslen )
3646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3648 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003649 }
3650
Simon Butcher99000142016-10-13 17:21:01 +01003651 return( 0 );
3652}
3653
3654void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3655{
Hanno Becker0271f962018-08-16 13:23:47 +01003656 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003657
Hanno Becker0271f962018-08-16 13:23:47 +01003658 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003659 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003660 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003661 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003662
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003663 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003666 ssl->handshake != NULL )
3667 {
Hanno Becker0271f962018-08-16 13:23:47 +01003668 unsigned offset;
3669 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003670
Hanno Becker0271f962018-08-16 13:23:47 +01003671 /* Increment handshake sequence number */
3672 hs->in_msg_seq++;
3673
3674 /*
3675 * Clear up handshake buffering and reassembly structure.
3676 */
3677
3678 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003679 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003680
3681 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003682 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3683 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003684 offset++, hs_buf++ )
3685 {
3686 *hs_buf = *(hs_buf + 1);
3687 }
3688
3689 /* Create a fresh last entry */
3690 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003691 }
3692#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003693}
3694
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003695/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003696 * DTLS anti-replay: RFC 6347 4.1.2.6
3697 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003698 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3699 * Bit n is set iff record number in_window_top - n has been seen.
3700 *
3701 * Usually, in_window_top is the last record number seen and the lsb of
3702 * in_window is set. The only exception is the initial state (record number 0
3703 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3706static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003707{
3708 ssl->in_window_top = 0;
3709 ssl->in_window = 0;
3710}
3711
3712static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3713{
3714 return( ( (uint64_t) buf[0] << 40 ) |
3715 ( (uint64_t) buf[1] << 32 ) |
3716 ( (uint64_t) buf[2] << 24 ) |
3717 ( (uint64_t) buf[3] << 16 ) |
3718 ( (uint64_t) buf[4] << 8 ) |
3719 ( (uint64_t) buf[5] ) );
3720}
3721
3722/*
3723 * Return 0 if sequence number is acceptable, -1 otherwise
3724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003726{
3727 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3728 uint64_t bit;
3729
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003730 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003731 return( 0 );
3732
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003733 if( rec_seqnum > ssl->in_window_top )
3734 return( 0 );
3735
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003736 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003737
3738 if( bit >= 64 )
3739 return( -1 );
3740
3741 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3742 return( -1 );
3743
3744 return( 0 );
3745}
3746
3747/*
3748 * Update replay window on new validated record
3749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003751{
3752 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3753
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003754 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003755 return;
3756
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003757 if( rec_seqnum > ssl->in_window_top )
3758 {
3759 /* Update window_top and the contents of the window */
3760 uint64_t shift = rec_seqnum - ssl->in_window_top;
3761
3762 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003763 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003764 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003765 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003766 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003767 ssl->in_window |= 1;
3768 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003769
3770 ssl->in_window_top = rec_seqnum;
3771 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003772 else
3773 {
3774 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003775 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003776
3777 if( bit < 64 ) /* Always true, but be extra sure */
3778 ssl->in_window |= (uint64_t) 1 << bit;
3779 }
3780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003782
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003783#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003784/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003785static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3786
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003787/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003788 * Without any SSL context, check if a datagram looks like a ClientHello with
3789 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003790 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003791 *
3792 * - if cookie is valid, return 0
3793 * - if ClientHello looks superficially valid but cookie is not,
3794 * fill obuf and set olen, then
3795 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3796 * - otherwise return a specific error code
3797 */
3798static int ssl_check_dtls_clihlo_cookie(
3799 mbedtls_ssl_cookie_write_t *f_cookie_write,
3800 mbedtls_ssl_cookie_check_t *f_cookie_check,
3801 void *p_cookie,
3802 const unsigned char *cli_id, size_t cli_id_len,
3803 const unsigned char *in, size_t in_len,
3804 unsigned char *obuf, size_t buf_len, size_t *olen )
3805{
3806 size_t sid_len, cookie_len;
3807 unsigned char *p;
3808
3809 if( f_cookie_write == NULL || f_cookie_check == NULL )
3810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3811
3812 /*
3813 * Structure of ClientHello with record and handshake headers,
3814 * and expected values. We don't need to check a lot, more checks will be
3815 * done when actually parsing the ClientHello - skipping those checks
3816 * avoids code duplication and does not make cookie forging any easier.
3817 *
3818 * 0-0 ContentType type; copied, must be handshake
3819 * 1-2 ProtocolVersion version; copied
3820 * 3-4 uint16 epoch; copied, must be 0
3821 * 5-10 uint48 sequence_number; copied
3822 * 11-12 uint16 length; (ignored)
3823 *
3824 * 13-13 HandshakeType msg_type; (ignored)
3825 * 14-16 uint24 length; (ignored)
3826 * 17-18 uint16 message_seq; copied
3827 * 19-21 uint24 fragment_offset; copied, must be 0
3828 * 22-24 uint24 fragment_length; (ignored)
3829 *
3830 * 25-26 ProtocolVersion client_version; (ignored)
3831 * 27-58 Random random; (ignored)
3832 * 59-xx SessionID session_id; 1 byte len + sid_len content
3833 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3834 * ...
3835 *
3836 * Minimum length is 61 bytes.
3837 */
3838 if( in_len < 61 ||
3839 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3840 in[3] != 0 || in[4] != 0 ||
3841 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3842 {
3843 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3844 }
3845
3846 sid_len = in[59];
3847 if( sid_len > in_len - 61 )
3848 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3849
3850 cookie_len = in[60 + sid_len];
3851 if( cookie_len > in_len - 60 )
3852 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3853
3854 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3855 cli_id, cli_id_len ) == 0 )
3856 {
3857 /* Valid cookie */
3858 return( 0 );
3859 }
3860
3861 /*
3862 * If we get here, we've got an invalid cookie, let's prepare HVR.
3863 *
3864 * 0-0 ContentType type; copied
3865 * 1-2 ProtocolVersion version; copied
3866 * 3-4 uint16 epoch; copied
3867 * 5-10 uint48 sequence_number; copied
3868 * 11-12 uint16 length; olen - 13
3869 *
3870 * 13-13 HandshakeType msg_type; hello_verify_request
3871 * 14-16 uint24 length; olen - 25
3872 * 17-18 uint16 message_seq; copied
3873 * 19-21 uint24 fragment_offset; copied
3874 * 22-24 uint24 fragment_length; olen - 25
3875 *
3876 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3877 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3878 *
3879 * Minimum length is 28.
3880 */
3881 if( buf_len < 28 )
3882 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3883
3884 /* Copy most fields and adapt others */
3885 memcpy( obuf, in, 25 );
3886 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3887 obuf[25] = 0xfe;
3888 obuf[26] = 0xff;
3889
3890 /* Generate and write actual cookie */
3891 p = obuf + 28;
3892 if( f_cookie_write( p_cookie,
3893 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3894 {
3895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3896 }
3897
3898 *olen = p - obuf;
3899
3900 /* Go back and fill length fields */
3901 obuf[27] = (unsigned char)( *olen - 28 );
3902
3903 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3904 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3905 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3906
3907 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3908 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3909
3910 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3911}
3912
3913/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003914 * Handle possible client reconnect with the same UDP quadruplet
3915 * (RFC 6347 Section 4.2.8).
3916 *
3917 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3918 * that looks like a ClientHello.
3919 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003920 * - if the input looks like a ClientHello without cookies,
3921 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003922 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003923 * - if the input looks like a ClientHello with a valid cookie,
3924 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003925 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003926 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003927 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003928 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003929 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3930 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003931 */
3932static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3933{
3934 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003935 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003936
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003937 ret = ssl_check_dtls_clihlo_cookie(
3938 ssl->conf->f_cookie_write,
3939 ssl->conf->f_cookie_check,
3940 ssl->conf->p_cookie,
3941 ssl->cli_id, ssl->cli_id_len,
3942 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003943 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003944
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003945 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3946
3947 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003948 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003949 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003950 * If the error is permanent we'll catch it later,
3951 * if it's not, then hopefully it'll work next time. */
3952 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3953
3954 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003955 }
3956
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003957 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003958 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003959 /* Got a valid cookie, partially reset context */
3960 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3961 {
3962 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3963 return( ret );
3964 }
3965
3966 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003967 }
3968
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003969 return( ret );
3970}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003971#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003972
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003973/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003974 * ContentType type;
3975 * ProtocolVersion version;
3976 * uint16 epoch; // DTLS only
3977 * uint48 sequence_number; // DTLS only
3978 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003979 *
3980 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003981 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003982 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3983 *
3984 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003985 * 1. proceed with the record if this function returns 0
3986 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3987 * 3. return CLIENT_RECONNECT if this function return that value
3988 * 4. drop the whole datagram if this function returns anything else.
3989 * Point 2 is needed when the peer is resending, and we have already received
3990 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003993{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003994 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 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 +02003997
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003999 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004000 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004003 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004004 ssl->in_msgtype,
4005 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004007 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4009 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4010 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4011 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004014
4015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004016 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4017 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004018 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4019#endif /* MBEDTLS_SSL_PROTO_DTLS */
4020 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4021 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004024 }
4025
4026 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004027 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4030 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031 }
4032
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004033 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004037 }
4038
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004039 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004040 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004041 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4044 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004045 }
4046
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004047 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004048 * DTLS-related tests.
4049 * Check epoch before checking length constraint because
4050 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4051 * message gets duplicated before the corresponding Finished message,
4052 * the second ChangeCipherSpec should be discarded because it belongs
4053 * to an old epoch, but not because its length is shorter than
4054 * the minimum record length for packets using the new record transform.
4055 * Note that these two kinds of failures are handled differently,
4056 * as an unexpected record is silently skipped but an invalid
4057 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004058 */
4059#if defined(MBEDTLS_SSL_PROTO_DTLS)
4060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4061 {
4062 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4063
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004064 /* Check epoch (and sequence number) with DTLS */
4065 if( rec_epoch != ssl->in_epoch )
4066 {
4067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4068 "expected %d, received %d",
4069 ssl->in_epoch, rec_epoch ) );
4070
4071#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4072 /*
4073 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4074 * access the first byte of record content (handshake type), as we
4075 * have an active transform (possibly iv_len != 0), so use the
4076 * fact that the record header len is 13 instead.
4077 */
4078 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4079 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4080 rec_epoch == 0 &&
4081 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4082 ssl->in_left > 13 &&
4083 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4084 {
4085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4086 "from the same port" ) );
4087 return( ssl_handle_possible_reconnect( ssl ) );
4088 }
4089 else
4090#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004091 {
4092 /* Consider buffering the record. */
4093 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4094 {
4095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4096 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4097 }
4098
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004099 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004100 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004101 }
4102
4103#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4104 /* Replay detection only works for the current epoch */
4105 if( rec_epoch == ssl->in_epoch &&
4106 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4107 {
4108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4109 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4110 }
4111#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004112
Hanno Becker52c6dc62017-05-26 16:07:36 +01004113 /* Drop unexpected ApplicationData records,
4114 * except at the beginning of renegotiations */
4115 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4116 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4117#if defined(MBEDTLS_SSL_RENEGOTIATION)
4118 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4119 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4120#endif
4121 )
4122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4124 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4125 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004126 }
4127#endif /* MBEDTLS_SSL_PROTO_DTLS */
4128
Hanno Becker52c6dc62017-05-26 16:07:36 +01004129
4130 /* Check length against bounds of the current transform and version */
4131 if( ssl->transform_in == NULL )
4132 {
4133 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004134 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4138 }
4139 }
4140 else
4141 {
4142 if( ssl->in_msglen < ssl->transform_in->minlen )
4143 {
4144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4146 }
4147
4148#if defined(MBEDTLS_SSL_PROTO_SSL3)
4149 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004150 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004151 {
4152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4154 }
4155#endif
4156#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4157 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4158 /*
4159 * TLS encrypted messages can have up to 256 bytes of padding
4160 */
4161 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4162 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004163 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004164 {
4165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4167 }
4168#endif
4169 }
4170
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004171 return( 0 );
4172}
Paul Bakker5121ce52009-01-03 21:22:43 +00004173
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004174/*
4175 * If applicable, decrypt (and decompress) record content
4176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004178{
4179 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4182 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4185 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 ret = mbedtls_ssl_hw_record_read( ssl );
4190 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4193 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004194 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004195
4196 if( ret == 0 )
4197 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004198 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004200 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 {
4202 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 return( ret );
4206 }
4207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004209 ssl->in_msg, ssl->in_msglen );
4210
Angus Grattond8213d02016-05-25 20:56:48 +10004211 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4214 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 }
4216 }
4217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004219 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004221 {
4222 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004225 return( ret );
4226 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004234 }
4235#endif
4236
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004237 return( 0 );
4238}
4239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004241
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004242/*
4243 * Read a record.
4244 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004245 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4246 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4247 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004248 */
Hanno Becker1097b342018-08-15 14:09:41 +01004249
4250/* Helper functions for mbedtls_ssl_read_record(). */
4251static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004252static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4253static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004254
Hanno Becker40f50842018-08-15 14:48:01 +01004255#if defined(MBEDTLS_SSL_PROTO_DTLS)
4256static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004257static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004258static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004259static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004260static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4261#endif /* MBEDTLS_SSL_PROTO_DTLS */
4262
Hanno Becker327c93b2018-08-15 13:56:18 +01004263int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004264 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004265{
4266 int ret;
4267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004269
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004270 if( ssl->keep_current_message == 0 )
4271 {
4272 do {
Simon Butcher99000142016-10-13 17:21:01 +01004273
Hanno Becker26994592018-08-15 14:14:59 +01004274 ret = ssl_consume_current_message( ssl );
4275 if( ret != 0 )
4276 return( ret );
4277
Hanno Beckere74d5562018-08-15 14:26:08 +01004278 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004279 {
Hanno Becker40f50842018-08-15 14:48:01 +01004280#if defined(MBEDTLS_SSL_PROTO_DTLS)
4281 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004282
Hanno Becker40f50842018-08-15 14:48:01 +01004283 /* We only check for buffered messages if the
4284 * current datagram is fully consumed. */
4285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4286 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004287 {
Hanno Becker40f50842018-08-15 14:48:01 +01004288 if( ssl_load_buffered_message( ssl ) == 0 )
4289 have_buffered = 1;
4290 }
4291
4292 if( have_buffered == 0 )
4293#endif /* MBEDTLS_SSL_PROTO_DTLS */
4294 {
4295 ret = ssl_get_next_record( ssl );
4296 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4297 continue;
4298
4299 if( ret != 0 )
4300 {
4301 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4302 return( ret );
4303 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004304 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004305 }
4306
4307 ret = mbedtls_ssl_handle_message_type( ssl );
4308
Hanno Becker40f50842018-08-15 14:48:01 +01004309#if defined(MBEDTLS_SSL_PROTO_DTLS)
4310 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4311 {
4312 /* Buffer future message */
4313 ret = ssl_buffer_message( ssl );
4314 if( ret != 0 )
4315 return( ret );
4316
4317 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4318 }
4319#endif /* MBEDTLS_SSL_PROTO_DTLS */
4320
Hanno Becker90333da2017-10-10 11:27:13 +01004321 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4322 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004323
4324 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004325 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004326 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004327 return( ret );
4328 }
4329
Hanno Becker327c93b2018-08-15 13:56:18 +01004330 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004331 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004332 {
4333 mbedtls_ssl_update_handshake_status( ssl );
4334 }
Simon Butcher99000142016-10-13 17:21:01 +01004335 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004336 else
Simon Butcher99000142016-10-13 17:21:01 +01004337 {
Hanno Becker02f59072018-08-15 14:00:24 +01004338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004339 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004340 }
4341
4342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4343
4344 return( 0 );
4345}
4346
Hanno Becker40f50842018-08-15 14:48:01 +01004347#if defined(MBEDTLS_SSL_PROTO_DTLS)
4348static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4349{
4350 if( ssl->in_left > ssl->next_record_offset )
4351 return( 1 );
4352
4353 return( 0 );
4354}
4355
4356static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4357{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004358 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004359 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004360 int ret = 0;
4361
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004362 if( hs == NULL )
4363 return( -1 );
4364
Hanno Beckere00ae372018-08-20 09:39:42 +01004365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4366
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004367 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4368 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4369 {
4370 /* Check if we have seen a ChangeCipherSpec before.
4371 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004372 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004373 {
4374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4375 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004376 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004377 }
4378
4379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4380 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4381 ssl->in_msglen = 1;
4382 ssl->in_msg[0] = 1;
4383
4384 /* As long as they are equal, the exact value doesn't matter. */
4385 ssl->in_left = 0;
4386 ssl->next_record_offset = 0;
4387
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004388 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004389 goto exit;
4390 }
Hanno Becker37f95322018-08-16 13:55:32 +01004391
4392 /* Debug only */
4393 {
4394 unsigned offset;
4395 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4396 {
4397 hs_buf = &hs->buffering.hs[offset];
4398 if( hs_buf->is_valid == 1 )
4399 {
4400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4401 hs->in_msg_seq + offset,
4402 hs_buf->is_complete ? "fully" : "partitially" ) );
4403 }
4404 }
4405 }
4406
4407 /* Check if we have buffered and/or fully reassembled the
4408 * next handshake message. */
4409 hs_buf = &hs->buffering.hs[0];
4410 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4411 {
4412 /* Synthesize a record containing the buffered HS message. */
4413 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4414 ( hs_buf->data[2] << 8 ) |
4415 hs_buf->data[3];
4416
4417 /* Double-check that we haven't accidentally buffered
4418 * a message that doesn't fit into the input buffer. */
4419 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4423 }
4424
4425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4426 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4427 hs_buf->data, msg_len + 12 );
4428
4429 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4430 ssl->in_hslen = msg_len + 12;
4431 ssl->in_msglen = msg_len + 12;
4432 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4433
4434 ret = 0;
4435 goto exit;
4436 }
4437 else
4438 {
4439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4440 hs->in_msg_seq ) );
4441 }
4442
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004443 ret = -1;
4444
4445exit:
4446
4447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4448 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004449}
4450
Hanno Becker01315ea2018-08-21 17:22:17 +01004451static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004452static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4453 size_t desired )
4454{
4455 int offset;
4456 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4457
Hanno Becker01315ea2018-08-21 17:22:17 +01004458 /* Get rid of future records epoch first, if such exist. */
4459 ssl_free_buffered_record( ssl );
4460
4461 /* Check if we have enough space available now. */
4462 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4463 hs->buffering.total_bytes_buffered ) )
4464 {
4465 return( 0 );
4466 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004467
4468 /* We don't have enough space to buffer the next expected
4469 * handshake message. Remove buffers used for future msgs
4470 * to gain space, starting with the most distant one. */
4471 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4472 offset >= 0; offset-- )
4473 {
4474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4475 offset ) );
4476
4477 ssl_buffering_free_slot( ssl, offset );
4478
4479 /* Check if we have enough space available now. */
4480 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4481 hs->buffering.total_bytes_buffered ) )
4482 {
4483 return( 0 );
4484 }
4485 }
4486
4487 return( -1 );
4488}
4489
Hanno Becker40f50842018-08-15 14:48:01 +01004490static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4491{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004492 int ret = 0;
4493 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4494
4495 if( hs == NULL )
4496 return( 0 );
4497
4498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4499
4500 switch( ssl->in_msgtype )
4501 {
4502 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004504
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004505 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004506 break;
4507
4508 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004509 {
4510 unsigned recv_msg_seq_offset;
4511 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4512 mbedtls_ssl_hs_buffer *hs_buf;
4513 size_t msg_len = ssl->in_hslen - 12;
4514
4515 /* We should never receive an old handshake
4516 * message - double-check nonetheless. */
4517 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4518 {
4519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4521 }
4522
4523 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4524 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4525 {
4526 /* Silently ignore -- message too far in the future */
4527 MBEDTLS_SSL_DEBUG_MSG( 2,
4528 ( "Ignore future HS message with sequence number %u, "
4529 "buffering window %u - %u",
4530 recv_msg_seq, ssl->handshake->in_msg_seq,
4531 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4532
4533 goto exit;
4534 }
4535
4536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4537 recv_msg_seq, recv_msg_seq_offset ) );
4538
4539 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4540
4541 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004542 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004543 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004544 size_t reassembly_buf_sz;
4545
Hanno Becker37f95322018-08-16 13:55:32 +01004546 hs_buf->is_fragmented =
4547 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4548
4549 /* We copy the message back into the input buffer
4550 * after reassembly, so check that it's not too large.
4551 * This is an implementation-specific limitation
4552 * and not one from the standard, hence it is not
4553 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004554 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004555 {
4556 /* Ignore message */
4557 goto exit;
4558 }
4559
Hanno Beckere0b150f2018-08-21 15:51:03 +01004560 /* Check if we have enough space to buffer the message. */
4561 if( hs->buffering.total_bytes_buffered >
4562 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4563 {
4564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4566 }
4567
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004568 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4569 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004570
4571 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4572 hs->buffering.total_bytes_buffered ) )
4573 {
4574 if( recv_msg_seq_offset > 0 )
4575 {
4576 /* If we can't buffer a future message because
4577 * of space limitations -- ignore. */
4578 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",
4579 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4580 (unsigned) hs->buffering.total_bytes_buffered ) );
4581 goto exit;
4582 }
Hanno Beckere1801392018-08-21 16:51:05 +01004583 else
4584 {
4585 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",
4586 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4587 (unsigned) hs->buffering.total_bytes_buffered ) );
4588 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004589
Hanno Beckera02b0b42018-08-21 17:20:27 +01004590 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004591 {
4592 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 +01004593 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4594 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004595 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4596 goto exit;
4597 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004598 }
4599
4600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4601 msg_len ) );
4602
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004603 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4604 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004605 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004606 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004607 goto exit;
4608 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004609 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004610
4611 /* Prepare final header: copy msg_type, length and message_seq,
4612 * then add standardised fragment_offset and fragment_length */
4613 memcpy( hs_buf->data, ssl->in_msg, 6 );
4614 memset( hs_buf->data + 6, 0, 3 );
4615 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4616
4617 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004618
4619 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004620 }
4621 else
4622 {
4623 /* Make sure msg_type and length are consistent */
4624 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4625 {
4626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4627 /* Ignore */
4628 goto exit;
4629 }
4630 }
4631
Hanno Becker4422bbb2018-08-20 09:40:19 +01004632 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004633 {
4634 size_t frag_len, frag_off;
4635 unsigned char * const msg = hs_buf->data + 12;
4636
4637 /*
4638 * Check and copy current fragment
4639 */
4640
4641 /* Validation of header fields already done in
4642 * mbedtls_ssl_prepare_handshake_record(). */
4643 frag_off = ssl_get_hs_frag_off( ssl );
4644 frag_len = ssl_get_hs_frag_len( ssl );
4645
4646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4647 frag_off, frag_len ) );
4648 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4649
4650 if( hs_buf->is_fragmented )
4651 {
4652 unsigned char * const bitmask = msg + msg_len;
4653 ssl_bitmask_set( bitmask, frag_off, frag_len );
4654 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4655 msg_len ) == 0 );
4656 }
4657 else
4658 {
4659 hs_buf->is_complete = 1;
4660 }
4661
4662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4663 hs_buf->is_complete ? "" : "not yet " ) );
4664 }
4665
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004666 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004667 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004668
4669 default:
4670 break;
4671 }
4672
4673exit:
4674
4675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4676 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004677}
4678#endif /* MBEDTLS_SSL_PROTO_DTLS */
4679
Hanno Becker1097b342018-08-15 14:09:41 +01004680static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004681{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004682 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004683 * Consume last content-layer message and potentially
4684 * update in_msglen which keeps track of the contents'
4685 * consumption state.
4686 *
4687 * (1) Handshake messages:
4688 * Remove last handshake message, move content
4689 * and adapt in_msglen.
4690 *
4691 * (2) Alert messages:
4692 * Consume whole record content, in_msglen = 0.
4693 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004694 * (3) Change cipher spec:
4695 * Consume whole record content, in_msglen = 0.
4696 *
4697 * (4) Application data:
4698 * Don't do anything - the record layer provides
4699 * the application data as a stream transport
4700 * and consumes through mbedtls_ssl_read only.
4701 *
4702 */
4703
4704 /* Case (1): Handshake messages */
4705 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004706 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004707 /* Hard assertion to be sure that no application data
4708 * is in flight, as corrupting ssl->in_msglen during
4709 * ssl->in_offt != NULL is fatal. */
4710 if( ssl->in_offt != NULL )
4711 {
4712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4714 }
4715
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004716 /*
4717 * Get next Handshake message in the current record
4718 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004719
Hanno Becker4a810fb2017-05-24 16:27:30 +01004720 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004721 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004722 * current handshake content: If DTLS handshake
4723 * fragmentation is used, that's the fragment
4724 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004725 * size here is faulty and should be changed at
4726 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004727 * (2) While it doesn't seem to cause problems, one
4728 * has to be very careful not to assume that in_hslen
4729 * is always <= in_msglen in a sensible communication.
4730 * Again, it's wrong for DTLS handshake fragmentation.
4731 * The following check is therefore mandatory, and
4732 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004733 * Additionally, ssl->in_hslen might be arbitrarily out of
4734 * bounds after handling a DTLS message with an unexpected
4735 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004736 */
4737 if( ssl->in_hslen < ssl->in_msglen )
4738 {
4739 ssl->in_msglen -= ssl->in_hslen;
4740 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4741 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004742
Hanno Becker4a810fb2017-05-24 16:27:30 +01004743 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4744 ssl->in_msg, ssl->in_msglen );
4745 }
4746 else
4747 {
4748 ssl->in_msglen = 0;
4749 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004750
Hanno Becker4a810fb2017-05-24 16:27:30 +01004751 ssl->in_hslen = 0;
4752 }
4753 /* Case (4): Application data */
4754 else if( ssl->in_offt != NULL )
4755 {
4756 return( 0 );
4757 }
4758 /* Everything else (CCS & Alerts) */
4759 else
4760 {
4761 ssl->in_msglen = 0;
4762 }
4763
Hanno Becker1097b342018-08-15 14:09:41 +01004764 return( 0 );
4765}
4766
Hanno Beckere74d5562018-08-15 14:26:08 +01004767static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4768{
4769 if( ssl->in_msglen > 0 )
4770 return( 1 );
4771
4772 return( 0 );
4773}
4774
Hanno Becker5f066e72018-08-16 14:56:31 +01004775#if defined(MBEDTLS_SSL_PROTO_DTLS)
4776
4777static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4778{
4779 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4780 if( hs == NULL )
4781 return;
4782
Hanno Becker01315ea2018-08-21 17:22:17 +01004783 if( hs->buffering.future_record.data != NULL )
4784 {
4785 hs->buffering.total_bytes_buffered -=
4786 hs->buffering.future_record.len;
4787
4788 mbedtls_free( hs->buffering.future_record.data );
4789 hs->buffering.future_record.data = NULL;
4790 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004791}
4792
4793static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4794{
4795 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4796 unsigned char * rec;
4797 size_t rec_len;
4798 unsigned rec_epoch;
4799
4800 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4801 return( 0 );
4802
4803 if( hs == NULL )
4804 return( 0 );
4805
Hanno Becker5f066e72018-08-16 14:56:31 +01004806 rec = hs->buffering.future_record.data;
4807 rec_len = hs->buffering.future_record.len;
4808 rec_epoch = hs->buffering.future_record.epoch;
4809
4810 if( rec == NULL )
4811 return( 0 );
4812
Hanno Becker4cb782d2018-08-20 11:19:05 +01004813 /* Only consider loading future records if the
4814 * input buffer is empty. */
4815 if( ssl_another_record_in_datagram( ssl ) == 1 )
4816 return( 0 );
4817
Hanno Becker5f066e72018-08-16 14:56:31 +01004818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4819
4820 if( rec_epoch != ssl->in_epoch )
4821 {
4822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4823 goto exit;
4824 }
4825
4826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4827
4828 /* Double-check that the record is not too large */
4829 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4830 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4831 {
4832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4834 }
4835
4836 memcpy( ssl->in_hdr, rec, rec_len );
4837 ssl->in_left = rec_len;
4838 ssl->next_record_offset = 0;
4839
4840 ssl_free_buffered_record( ssl );
4841
4842exit:
4843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4844 return( 0 );
4845}
4846
4847static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4848{
4849 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4850 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004851 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004852
4853 /* Don't buffer future records outside handshakes. */
4854 if( hs == NULL )
4855 return( 0 );
4856
4857 /* Only buffer handshake records (we are only interested
4858 * in Finished messages). */
4859 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4860 return( 0 );
4861
4862 /* Don't buffer more than one future epoch record. */
4863 if( hs->buffering.future_record.data != NULL )
4864 return( 0 );
4865
Hanno Becker01315ea2018-08-21 17:22:17 +01004866 /* Don't buffer record if there's not enough buffering space remaining. */
4867 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4868 hs->buffering.total_bytes_buffered ) )
4869 {
4870 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",
4871 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4872 (unsigned) hs->buffering.total_bytes_buffered ) );
4873 return( 0 );
4874 }
4875
Hanno Becker5f066e72018-08-16 14:56:31 +01004876 /* Buffer record */
4877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4878 ssl->in_epoch + 1 ) );
4879 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4880 rec_hdr_len + ssl->in_msglen );
4881
4882 /* ssl_parse_record_header() only considers records
4883 * of the next epoch as candidates for buffering. */
4884 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004885 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004886
4887 hs->buffering.future_record.data =
4888 mbedtls_calloc( 1, hs->buffering.future_record.len );
4889 if( hs->buffering.future_record.data == NULL )
4890 {
4891 /* If we run out of RAM trying to buffer a
4892 * record from the next epoch, just ignore. */
4893 return( 0 );
4894 }
4895
Hanno Becker01315ea2018-08-21 17:22:17 +01004896 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004897
Hanno Becker01315ea2018-08-21 17:22:17 +01004898 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004899 return( 0 );
4900}
4901
4902#endif /* MBEDTLS_SSL_PROTO_DTLS */
4903
Hanno Beckere74d5562018-08-15 14:26:08 +01004904static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004905{
4906 int ret;
4907
Hanno Becker5f066e72018-08-16 14:56:31 +01004908#if defined(MBEDTLS_SSL_PROTO_DTLS)
4909 /* We might have buffered a future record; if so,
4910 * and if the epoch matches now, load it.
4911 * On success, this call will set ssl->in_left to
4912 * the length of the buffered record, so that
4913 * the calls to ssl_fetch_input() below will
4914 * essentially be no-ops. */
4915 ret = ssl_load_buffered_record( ssl );
4916 if( ret != 0 )
4917 return( ret );
4918#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004923 return( ret );
4924 }
4925
4926 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004929 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4930 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004931 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004932 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4933 {
4934 ret = ssl_buffer_future_record( ssl );
4935 if( ret != 0 )
4936 return( ret );
4937
4938 /* Fall through to handling of unexpected records */
4939 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4940 }
4941
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004942 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4943 {
4944 /* Skip unexpected record (but not whole datagram) */
4945 ssl->next_record_offset = ssl->in_msglen
4946 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004947
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4949 "(header)" ) );
4950 }
4951 else
4952 {
4953 /* Skip invalid record and the rest of the datagram */
4954 ssl->next_record_offset = 0;
4955 ssl->in_left = 0;
4956
4957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4958 "(header)" ) );
4959 }
4960
4961 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004962 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004963 }
4964#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004965 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004966 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004967
4968 /*
4969 * Read and optionally decrypt the message contents
4970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4972 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004975 return( ret );
4976 }
4977
4978 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004980 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004983 if( ssl->next_record_offset < ssl->in_left )
4984 {
4985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4986 }
4987 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004988 else
4989#endif
4990 ssl->in_left = 0;
4991
4992 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004996 {
4997 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4999 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005000 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005001 /* Except when waiting for Finished as a bad mac here
5002 * probably means something went wrong in the handshake
5003 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5004 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5005 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5006 {
5007#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5008 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5009 {
5010 mbedtls_ssl_send_alert_message( ssl,
5011 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5012 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5013 }
5014#endif
5015 return( ret );
5016 }
5017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005019 if( ssl->conf->badmac_limit != 0 &&
5020 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5023 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005024 }
5025#endif
5026
Hanno Becker4a810fb2017-05-24 16:27:30 +01005027 /* As above, invalid records cause
5028 * dismissal of the whole datagram. */
5029
5030 ssl->next_record_offset = 0;
5031 ssl->in_left = 0;
5032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005034 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005035 }
5036
5037 return( ret );
5038 }
5039 else
5040#endif
5041 {
5042 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5044 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 mbedtls_ssl_send_alert_message( ssl,
5047 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5048 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005049 }
5050#endif
5051 return( ret );
5052 }
5053 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005054
Simon Butcher99000142016-10-13 17:21:01 +01005055 return( 0 );
5056}
5057
5058int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5059{
5060 int ret;
5061
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005062 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005063 * Handle particular types of records
5064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005066 {
Simon Butcher99000142016-10-13 17:21:01 +01005067 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5068 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005069 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005070 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 }
5072
Hanno Beckere678eaa2018-08-21 14:57:46 +01005073 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005074 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005075 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5078 ssl->in_msglen ) );
5079 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005080 }
5081
Hanno Beckere678eaa2018-08-21 14:57:46 +01005082 if( ssl->in_msg[0] != 1 )
5083 {
5084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5085 ssl->in_msg[0] ) );
5086 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5087 }
5088
5089#if defined(MBEDTLS_SSL_PROTO_DTLS)
5090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5091 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5092 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5093 {
5094 if( ssl->handshake == NULL )
5095 {
5096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5097 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5098 }
5099
5100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5101 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5102 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005103#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005104 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005108 if( ssl->in_msglen != 2 )
5109 {
5110 /* Note: Standard allows for more than one 2 byte alert
5111 to be packed in a single message, but Mbed TLS doesn't
5112 currently support this. */
5113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5114 ssl->in_msglen ) );
5115 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5116 }
5117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005119 ssl->in_msg[0], ssl->in_msg[1] ) );
5120
5121 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005122 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005127 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005129 }
5130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5132 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5135 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005137
5138#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5139 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5140 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5141 {
Hanno Becker90333da2017-10-10 11:27:13 +01005142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005143 /* Will be handled when trying to parse ServerHello */
5144 return( 0 );
5145 }
5146#endif
5147
5148#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5149 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5150 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5151 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5152 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5155 /* Will be handled in mbedtls_ssl_parse_certificate() */
5156 return( 0 );
5157 }
5158#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5159
5160 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005161 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005162 }
5163
Hanno Beckerc76c6192017-06-06 10:03:17 +01005164#if defined(MBEDTLS_SSL_PROTO_DTLS)
5165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5166 ssl->handshake != NULL &&
5167 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5168 {
5169 ssl_handshake_wrapup_free_hs_transform( ssl );
5170 }
5171#endif
5172
Paul Bakker5121ce52009-01-03 21:22:43 +00005173 return( 0 );
5174}
5175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005177{
5178 int ret;
5179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5181 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5182 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005183 {
5184 return( ret );
5185 }
5186
5187 return( 0 );
5188}
5189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005191 unsigned char level,
5192 unsigned char message )
5193{
5194 int ret;
5195
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005196 if( ssl == NULL || ssl->conf == NULL )
5197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005203 ssl->out_msglen = 2;
5204 ssl->out_msg[0] = level;
5205 ssl->out_msg[1] = message;
5206
Hanno Becker67bc7c32018-08-06 11:33:50 +01005207 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005210 return( ret );
5211 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005213
5214 return( 0 );
5215}
5216
Paul Bakker5121ce52009-01-03 21:22:43 +00005217/*
5218 * Handshake functions
5219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5221 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5222 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5223 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5224 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5225 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5226 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005227/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5235 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005236 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5237 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005240 ssl->state++;
5241 return( 0 );
5242 }
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005246}
5247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5255 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005256 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5257 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005260 ssl->state++;
5261 return( 0 );
5262 }
5263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005266}
Gilles Peskinef9828522017-05-03 12:28:43 +02005267
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005268#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005269/* Some certificate support -> implement write and parse */
5270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005274 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 const mbedtls_x509_crt *crt;
5276 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5281 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005282 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5283 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005286 ssl->state++;
5287 return( 0 );
5288 }
5289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005291 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005292 {
5293 if( ssl->client_auth == 0 )
5294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005296 ssl->state++;
5297 return( 0 );
5298 }
5299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005301 /*
5302 * If using SSLv3 and got no cert, send an Alert message
5303 * (otherwise an empty Certificate message will be sent).
5304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5306 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 {
5308 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5310 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5311 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005314 goto write_msg;
5315 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005317 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318#endif /* MBEDTLS_SSL_CLI_C */
5319#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005320 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5325 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 }
5327 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005331
5332 /*
5333 * 0 . 0 handshake type
5334 * 1 . 3 handshake length
5335 * 4 . 6 length of all certs
5336 * 7 . 9 length of cert. 1
5337 * 10 . n-1 peer certificate
5338 * n . n+2 length of cert. 2
5339 * n+3 . ... upper level cert, etc.
5340 */
5341 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005343
Paul Bakker29087132010-03-21 21:03:34 +00005344 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005345 {
5346 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005347 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005350 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005352 }
5353
5354 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5355 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5356 ssl->out_msg[i + 2] = (unsigned char)( n );
5357
5358 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5359 i += n; crt = crt->next;
5360 }
5361
5362 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5363 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5364 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5365
5366 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5368 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005369
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005370#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005371write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005373
5374 ssl->state++;
5375
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005376 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005377 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005379 return( ret );
5380 }
5381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005383
Paul Bakkered27a042013-04-18 22:46:23 +02005384 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005385}
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005390 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005392 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005393 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5398 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005399 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5400 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005403 ssl->state++;
5404 return( 0 );
5405 }
5406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005408 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005409 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5410 {
5411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5412 ssl->state++;
5413 return( 0 );
5414 }
5415
5416#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5417 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5418 authmode = ssl->handshake->sni_authmode;
5419#endif
5420
5421 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5422 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005424 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 ssl->state++;
5427 return( 0 );
5428 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005429#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005430
Hanno Becker327c93b2018-08-15 13:56:18 +01005431 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005432 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005433 /* mbedtls_ssl_read_record may have sent an alert already. We
5434 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005436 return( ret );
5437 }
5438
5439 ssl->state++;
5440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441#if defined(MBEDTLS_SSL_SRV_C)
5442#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005443 /*
5444 * Check if the client sent an empty certificate
5445 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005446 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005448 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005449 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5451 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5452 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005455
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005456 /* The client was asked for a certificate but didn't send
5457 one. The client should know what's going on, so we
5458 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005459 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005460 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 return( 0 );
5462 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005464 }
5465 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5469 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005470 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5474 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5475 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5476 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005479
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005480 /* The client was asked for a certificate but didn't send
5481 one. The client should know what's going on, so we
5482 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005483 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005484 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005485 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005486 else
5487 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 }
5489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5491 MBEDTLS_SSL_PROTO_TLS1_2 */
5492#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005497 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5498 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 }
5501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5503 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005506 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5507 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 }
5510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005512
Paul Bakker5121ce52009-01-03 21:22:43 +00005513 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005516 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005517
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005518 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005522 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5523 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 }
5526
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005527 /* In case we tried to reuse a session but it failed */
5528 if( ssl->session_negotiate->peer_cert != NULL )
5529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5531 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005532 }
5533
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005534 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005539 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5540 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005542 }
5543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005546 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005547
5548 while( i < ssl->in_hslen )
5549 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005550 if ( i + 3 > ssl->in_hslen ) {
5551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5552 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5553 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5554 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 if( ssl->in_msg[i] != 0 )
5557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005559 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5560 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 }
5563
5564 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5565 | (unsigned int) ssl->in_msg[i + 2];
5566 i += 3;
5567
5568 if( n < 128 || i + n > ssl->in_hslen )
5569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005571 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5572 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005574 }
5575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005577 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005578 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005579 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005580 case 0: /*ok*/
5581 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5582 /* Ignore certificate with an unknown algorithm: maybe a
5583 prior certificate was already trusted. */
5584 break;
5585
5586 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5587 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5588 goto crt_parse_der_failed;
5589
5590 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5591 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5592 goto crt_parse_der_failed;
5593
5594 default:
5595 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5596 crt_parse_der_failed:
5597 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005599 return( ret );
5600 }
5601
5602 i += n;
5603 }
5604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005607 /*
5608 * On client, make sure the server cert doesn't change during renego to
5609 * avoid "triple handshake" attack: https://secure-resumption.com/
5610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005614 {
5615 if( ssl->session->peer_cert == NULL )
5616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005618 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5619 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005621 }
5622
5623 if( ssl->session->peer_cert->raw.len !=
5624 ssl->session_negotiate->peer_cert->raw.len ||
5625 memcmp( ssl->session->peer_cert->raw.p,
5626 ssl->session_negotiate->peer_cert->raw.p,
5627 ssl->session->peer_cert->raw.len ) != 0 )
5628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5631 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005633 }
5634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005636
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005637 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005639 mbedtls_x509_crt *ca_chain;
5640 mbedtls_x509_crl *ca_crl;
5641
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005642#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005643 if( ssl->handshake->sni_ca_chain != NULL )
5644 {
5645 ca_chain = ssl->handshake->sni_ca_chain;
5646 ca_crl = ssl->handshake->sni_ca_crl;
5647 }
5648 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005649#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005650 {
5651 ca_chain = ssl->conf->ca_chain;
5652 ca_crl = ssl->conf->ca_crl;
5653 }
5654
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005655 /*
5656 * Main check: verify certificate
5657 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005658 ret = mbedtls_x509_crt_verify_with_profile(
5659 ssl->session_negotiate->peer_cert,
5660 ca_chain, ca_crl,
5661 ssl->conf->cert_profile,
5662 ssl->hostname,
5663 &ssl->session_negotiate->verify_result,
5664 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005665
5666 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005669 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005670
5671 /*
5672 * Secondary checks: always done, but change 'ret' only if it was 0
5673 */
5674
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005675#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005678
5679 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005681 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005682 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005683 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005686 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005688 }
5689 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005690#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005693 ciphersuite_info,
5694 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005695 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005698 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005700 }
5701
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005702 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5703 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5704 * with details encoded in the verification flags. All other kinds
5705 * of error codes, including those from the user provided f_vrfy
5706 * functions, are treated as fatal and lead to a failure of
5707 * ssl_parse_certificate even if verification was optional. */
5708 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5709 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5710 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5711 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005713 }
5714
5715 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5716 {
5717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5718 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5719 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005720
5721 if( ret != 0 )
5722 {
5723 /* The certificate may have been rejected for several reasons.
5724 Pick one and send the corresponding alert. Which alert to send
5725 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005726 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5727 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5728 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5729 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5730 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5731 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5732 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5733 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5734 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5735 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5736 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5737 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5738 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5739 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5740 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5741 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5742 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5743 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5744 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5745 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005746 else
5747 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005748 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5749 alert );
5750 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005751
Hanno Beckere6706e62017-05-15 16:05:15 +01005752#if defined(MBEDTLS_DEBUG_C)
5753 if( ssl->session_negotiate->verify_result != 0 )
5754 {
5755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5756 ssl->session_negotiate->verify_result ) );
5757 }
5758 else
5759 {
5760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5761 }
5762#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005763 }
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005766
5767 return( ret );
5768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5770 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5771 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5772 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5773 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5774 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5775 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005778{
5779 int ret;
5780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005784 ssl->out_msglen = 1;
5785 ssl->out_msg[0] = 1;
5786
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 ssl->state++;
5788
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005789 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005790 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005792 return( ret );
5793 }
5794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005796
5797 return( 0 );
5798}
5799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005801{
5802 int ret;
5803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005805
Hanno Becker327c93b2018-08-15 13:56:18 +01005806 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 return( ret );
5810 }
5811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005815 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5816 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 }
5819
Hanno Beckere678eaa2018-08-21 14:57:46 +01005820 /* CCS records are only accepted if they have length 1 and content '1',
5821 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005822
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005823 /*
5824 * Switch to our negotiated transform and session parameters for inbound
5825 * data.
5826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005828 ssl->transform_in = ssl->transform_negotiate;
5829 ssl->session_in = ssl->session_negotiate;
5830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005835 ssl_dtls_replay_reset( ssl );
5836#endif
5837
5838 /* Increment epoch */
5839 if( ++ssl->in_epoch == 0 )
5840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005842 /* This is highly unlikely to happen for legitimate reasons, so
5843 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005845 }
5846 }
5847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005849 memset( ssl->in_ctr, 0, 8 );
5850
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005851 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5854 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005859 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5860 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005862 }
5863 }
5864#endif
5865
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 ssl->state++;
5867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005869
5870 return( 0 );
5871}
5872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5874 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005875{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005876 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5879 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5880 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005881 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005882 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5885#if defined(MBEDTLS_SHA512_C)
5886 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005887 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5888 else
5889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890#if defined(MBEDTLS_SHA256_C)
5891 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005892 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005893 else
5894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005898 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005899 }
Paul Bakker380da532012-04-18 16:10:25 +00005900}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5905 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005906 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5907 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5910#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005911 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005912#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005914 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005917}
5918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005920 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5923 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005924 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5925 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005926#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5928#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005929 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005932 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005935}
5936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5938 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5939static void ssl_update_checksum_md5sha1( 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_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5943 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005944}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005945#endif
Paul Bakker380da532012-04-18 16:10:25 +00005946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5948#if defined(MBEDTLS_SHA256_C)
5949static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005950 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005951{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005952 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005953}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005954#endif
Paul Bakker380da532012-04-18 16:10:25 +00005955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#if defined(MBEDTLS_SHA512_C)
5957static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005958 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005959{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005960 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005961}
Paul Bakker769075d2012-11-24 11:26:46 +01005962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005966static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005968{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005969 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005970 mbedtls_md5_context md5;
5971 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005972
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 unsigned char padbuf[48];
5974 unsigned char md5sum[16];
5975 unsigned char sha1sum[20];
5976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005978 if( !session )
5979 session = ssl->session;
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005982
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005983 mbedtls_md5_init( &md5 );
5984 mbedtls_sha1_init( &sha1 );
5985
5986 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5987 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
5989 /*
5990 * SSLv3:
5991 * hash =
5992 * MD5( master + pad2 +
5993 * MD5( handshake + sender + master + pad1 ) )
5994 * + SHA1( master + pad2 +
5995 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 */
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005999 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6000 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006001#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6005 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006006#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006009 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006010
Paul Bakker1ef83d62012-04-11 12:09:53 +00006011 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006013 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6014 mbedtls_md5_update_ret( &md5, session->master, 48 );
6015 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6016 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006017
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006018 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6019 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6020 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6021 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006022
Paul Bakker1ef83d62012-04-11 12:09:53 +00006023 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006024
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006025 mbedtls_md5_starts_ret( &md5 );
6026 mbedtls_md5_update_ret( &md5, session->master, 48 );
6027 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6028 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6029 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006031 mbedtls_sha1_starts_ret( &sha1 );
6032 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6033 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6034 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6035 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006038
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006039 mbedtls_md5_free( &md5 );
6040 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006041
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006042 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6043 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6044 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006047}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006051static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006053{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006054 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006055 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006056 mbedtls_md5_context md5;
6057 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006058 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006061 if( !session )
6062 session = ssl->session;
6063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006066 mbedtls_md5_init( &md5 );
6067 mbedtls_sha1_init( &sha1 );
6068
6069 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6070 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
Paul Bakker1ef83d62012-04-11 12:09:53 +00006072 /*
6073 * TLSv1:
6074 * hash = PRF( master, finished_label,
6075 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6076 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006079 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6080 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006081#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006084 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6085 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006086#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006089 ? "client finished"
6090 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006091
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006092 mbedtls_md5_finish_ret( &md5, padbuf );
6093 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006094
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006095 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006096 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006099
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006100 mbedtls_md5_free( &md5 );
6101 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006102
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006103 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6110#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006111static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006113{
6114 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006115 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006116 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006117 unsigned char padbuf[32];
6118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006120 if( !session )
6121 session = ssl->session;
6122
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006123 mbedtls_sha256_init( &sha256 );
6124
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006126
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006127 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006128
6129 /*
6130 * TLSv1.2:
6131 * hash = PRF( master, finished_label,
6132 * Hash( handshake ) )[0.11]
6133 */
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135#if !defined(MBEDTLS_SHA256_ALT)
6136 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006137 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006138#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006141 ? "client finished"
6142 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006143
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006144 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006145
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006146 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006147 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006150
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006151 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006153 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006160static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006162{
6163 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006164 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006165 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006166 unsigned char padbuf[48];
6167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006169 if( !session )
6170 session = ssl->session;
6171
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006172 mbedtls_sha512_init( &sha512 );
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006175
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006176 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006177
6178 /*
6179 * TLSv1.2:
6180 * hash = PRF( master, finished_label,
6181 * Hash( handshake ) )[0.11]
6182 */
6183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006185 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6186 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006187#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006190 ? "client finished"
6191 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006192
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006193 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006194
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006195 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006196 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006199
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006200 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006201
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006202 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206#endif /* MBEDTLS_SHA512_C */
6207#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006212
6213 /*
6214 * Free our handshake params
6215 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006216 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006218 ssl->handshake = NULL;
6219
6220 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006221 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006222 */
6223 if( ssl->transform )
6224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 mbedtls_ssl_transform_free( ssl->transform );
6226 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006227 }
6228 ssl->transform = ssl->transform_negotiate;
6229 ssl->transform_negotiate = NULL;
6230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006232}
6233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006235{
6236 int resume = ssl->handshake->resume;
6237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240#if defined(MBEDTLS_SSL_RENEGOTIATION)
6241 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006244 ssl->renego_records_seen = 0;
6245 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006246#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006247
6248 /*
6249 * Free the previous session and switch in the current one
6250 */
Paul Bakker0a597072012-09-25 21:55:46 +00006251 if( ssl->session )
6252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006254 /* RFC 7366 3.1: keep the EtM state */
6255 ssl->session_negotiate->encrypt_then_mac =
6256 ssl->session->encrypt_then_mac;
6257#endif
6258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 mbedtls_ssl_session_free( ssl->session );
6260 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006261 }
6262 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006263 ssl->session_negotiate = NULL;
6264
Paul Bakker0a597072012-09-25 21:55:46 +00006265 /*
6266 * Add cache entry
6267 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006268 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006269 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006270 resume == 0 )
6271 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006272 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006274 }
Paul Bakker0a597072012-09-25 21:55:46 +00006275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006278 ssl->handshake->flight != NULL )
6279 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006280 /* Cancel handshake timer */
6281 ssl_set_timer( ssl, 0 );
6282
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006283 /* Keep last flight around in case we need to resend it:
6284 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006286 }
6287 else
6288#endif
6289 ssl_handshake_wrapup_free_hs_transform( ssl );
6290
Paul Bakker48916f92012-09-16 19:57:18 +00006291 ssl->state++;
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006294}
6295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006297{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006298 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006302 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006303
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006304 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006305
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006306 /*
6307 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6308 * may define some other value. Currently (early 2016), no defined
6309 * ciphersuite does this (and this is unlikely to change as activity has
6310 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006315 ssl->verify_data_len = hash_len;
6316 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006317#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006318
Paul Bakker5121ce52009-01-03 21:22:43 +00006319 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6321 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006322
6323 /*
6324 * In case of session resuming, invert the client and server
6325 * ChangeCipherSpec messages order.
6326 */
Paul Bakker0a597072012-09-25 21:55:46 +00006327 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006330 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 }
6338 else
6339 ssl->state++;
6340
Paul Bakker48916f92012-09-16 19:57:18 +00006341 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006342 * Switch to our negotiated transform and session parameters for outbound
6343 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006349 {
6350 unsigned char i;
6351
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006352 /* Remember current epoch settings for resending */
6353 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006354 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006355
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006356 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006357 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006358
6359 /* Increment epoch */
6360 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006361 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006362 break;
6363
6364 /* The loop goes to its end iff the counter is wrapping */
6365 if( i == 0 )
6366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6368 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006369 }
6370 }
6371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006373 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006374
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006375 ssl->transform_out = ssl->transform_negotiate;
6376 ssl->session_out = ssl->session_negotiate;
6377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6379 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6384 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006385 }
6386 }
6387#endif
6388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006390 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006392#endif
6393
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006394 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006395 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006397 return( ret );
6398 }
6399
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006400#if defined(MBEDTLS_SSL_PROTO_DTLS)
6401 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6402 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6403 {
6404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6405 return( ret );
6406 }
6407#endif
6408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006410
6411 return( 0 );
6412}
6413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006415#define SSL_MAX_HASH_LEN 36
6416#else
6417#define SSL_MAX_HASH_LEN 12
6418#endif
6419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006421{
Paul Bakker23986e52011-04-24 08:57:21 +00006422 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006423 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006424 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006427
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006428 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006429
Hanno Becker327c93b2018-08-15 13:56:18 +01006430 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006433 return( ret );
6434 }
6435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6440 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006442 }
6443
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006444 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_PROTO_SSL3)
6446 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006447 hash_len = 36;
6448 else
6449#endif
6450 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6453 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006456 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6457 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006459 }
6460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006462 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006465 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6466 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006468 }
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006471 ssl->verify_data_len = hash_len;
6472 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006473#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006474
Paul Bakker0a597072012-09-25 21:55:46 +00006475 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006478 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006482 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006484#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006485 }
6486 else
6487 ssl->state++;
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006492#endif
6493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006495
6496 return( 0 );
6497}
6498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6504 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6505 mbedtls_md5_init( &handshake->fin_md5 );
6506 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006507 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6508 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6511#if defined(MBEDTLS_SHA256_C)
6512 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006513 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515#if defined(MBEDTLS_SHA512_C)
6516 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006517 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006520
6521 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006522
6523#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6524 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6525 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6526#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528#if defined(MBEDTLS_DHM_C)
6529 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531#if defined(MBEDTLS_ECDH_C)
6532 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006533#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006534#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006535 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006536#if defined(MBEDTLS_SSL_CLI_C)
6537 handshake->ecjpake_cache = NULL;
6538 handshake->ecjpake_cache_len = 0;
6539#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006540#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006541
6542#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6543 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6544#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006545}
6546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6552 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 mbedtls_md_init( &transform->md_ctx_enc );
6555 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006556}
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006561}
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006564{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006565 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006566 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006568 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006570 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006571 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006572
6573 /*
6574 * Either the pointers are now NULL or cleared properly and can be freed.
6575 * Now allocate missing structures.
6576 */
6577 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006578 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006579 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006580 }
Paul Bakker48916f92012-09-16 19:57:18 +00006581
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006582 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006583 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006584 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006585 }
Paul Bakker48916f92012-09-16 19:57:18 +00006586
Paul Bakker82788fb2014-10-20 13:59:19 +02006587 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006588 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006589 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006590 }
Paul Bakker48916f92012-09-16 19:57:18 +00006591
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006592 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006593 if( ssl->handshake == NULL ||
6594 ssl->transform_negotiate == NULL ||
6595 ssl->session_negotiate == NULL )
6596 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 mbedtls_free( ssl->handshake );
6600 mbedtls_free( ssl->transform_negotiate );
6601 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006602
6603 ssl->handshake = NULL;
6604 ssl->transform_negotiate = NULL;
6605 ssl->session_negotiate = NULL;
6606
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006607 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006608 }
6609
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006610 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006612 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006613 ssl_handshake_params_init( ssl->handshake );
6614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6617 {
6618 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006619
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006620 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6621 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6622 else
6623 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006624
6625 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006626 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006627#endif
6628
Paul Bakker48916f92012-09-16 19:57:18 +00006629 return( 0 );
6630}
6631
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006632#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006633/* Dummy cookie callbacks for defaults */
6634static int ssl_cookie_write_dummy( void *ctx,
6635 unsigned char **p, unsigned char *end,
6636 const unsigned char *cli_id, size_t cli_id_len )
6637{
6638 ((void) ctx);
6639 ((void) p);
6640 ((void) end);
6641 ((void) cli_id);
6642 ((void) cli_id_len);
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006645}
6646
6647static int ssl_cookie_check_dummy( void *ctx,
6648 const unsigned char *cookie, size_t cookie_len,
6649 const unsigned char *cli_id, size_t cli_id_len )
6650{
6651 ((void) ctx);
6652 ((void) cookie);
6653 ((void) cookie_len);
6654 ((void) cli_id);
6655 ((void) cli_id_len);
6656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006658}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006659#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006660
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006661/* Once ssl->out_hdr as the address of the beginning of the
6662 * next outgoing record is set, deduce the other pointers.
6663 *
6664 * Note: For TLS, we save the implicit record sequence number
6665 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6666 * and the caller has to make sure there's space for this.
6667 */
6668
6669static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6670 mbedtls_ssl_transform *transform )
6671{
6672#if defined(MBEDTLS_SSL_PROTO_DTLS)
6673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6674 {
6675 ssl->out_ctr = ssl->out_hdr + 3;
6676 ssl->out_len = ssl->out_hdr + 11;
6677 ssl->out_iv = ssl->out_hdr + 13;
6678 }
6679 else
6680#endif
6681 {
6682 ssl->out_ctr = ssl->out_hdr - 8;
6683 ssl->out_len = ssl->out_hdr + 3;
6684 ssl->out_iv = ssl->out_hdr + 5;
6685 }
6686
6687 /* Adjust out_msg to make space for explicit IV, if used. */
6688 if( transform != NULL &&
6689 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6690 {
6691 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6692 }
6693 else
6694 ssl->out_msg = ssl->out_iv;
6695}
6696
6697/* Once ssl->in_hdr as the address of the beginning of the
6698 * next incoming record is set, deduce the other pointers.
6699 *
6700 * Note: For TLS, we save the implicit record sequence number
6701 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6702 * and the caller has to make sure there's space for this.
6703 */
6704
6705static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6706 mbedtls_ssl_transform *transform )
6707{
6708#if defined(MBEDTLS_SSL_PROTO_DTLS)
6709 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6710 {
6711 ssl->in_ctr = ssl->in_hdr + 3;
6712 ssl->in_len = ssl->in_hdr + 11;
6713 ssl->in_iv = ssl->in_hdr + 13;
6714 }
6715 else
6716#endif
6717 {
6718 ssl->in_ctr = ssl->in_hdr - 8;
6719 ssl->in_len = ssl->in_hdr + 3;
6720 ssl->in_iv = ssl->in_hdr + 5;
6721 }
6722
6723 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6724 if( transform != NULL &&
6725 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6726 {
6727 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6728 }
6729 else
6730 ssl->in_msg = ssl->in_iv;
6731}
6732
Paul Bakker5121ce52009-01-03 21:22:43 +00006733/*
6734 * Initialize an SSL context
6735 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006736void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6737{
6738 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6739}
6740
6741/*
6742 * Setup an SSL context
6743 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006744
6745static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6746{
6747 /* Set the incoming and outgoing record pointers. */
6748#if defined(MBEDTLS_SSL_PROTO_DTLS)
6749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6750 {
6751 ssl->out_hdr = ssl->out_buf;
6752 ssl->in_hdr = ssl->in_buf;
6753 }
6754 else
6755#endif /* MBEDTLS_SSL_PROTO_DTLS */
6756 {
6757 ssl->out_hdr = ssl->out_buf + 8;
6758 ssl->in_hdr = ssl->in_buf + 8;
6759 }
6760
6761 /* Derive other internal pointers. */
6762 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6763 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6764}
6765
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006766int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006767 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006768{
Paul Bakker48916f92012-09-16 19:57:18 +00006769 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006770
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006771 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006772
6773 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006774 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006775 */
Angus Grattond8213d02016-05-25 20:56:48 +10006776 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6777 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 {
Angus Grattond8213d02016-05-25 20:56:48 +10006779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6780 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6781 }
6782
6783 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6784 if( ssl->out_buf == NULL )
6785 {
6786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006788 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006789 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 }
6791
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006792 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006793
Paul Bakker48916f92012-09-16 19:57:18 +00006794 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6795 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796
6797 return( 0 );
6798}
6799
6800/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006801 * Reset an initialized and used SSL context for re-use while retaining
6802 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006803 *
6804 * If partial is non-zero, keep data in the input buffer and client ID.
6805 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006806 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006807static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006808{
Paul Bakker48916f92012-09-16 19:57:18 +00006809 int ret;
6810
Hanno Becker7e772132018-08-10 12:38:21 +01006811#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6812 !defined(MBEDTLS_SSL_SRV_C)
6813 ((void) partial);
6814#endif
6815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006817
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006818 /* Cancel any possibly running timer */
6819 ssl_set_timer( ssl, 0 );
6820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821#if defined(MBEDTLS_SSL_RENEGOTIATION)
6822 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006823 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006824
6825 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6827 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006828#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006830
Paul Bakker7eb013f2011-10-06 12:37:39 +00006831 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006832 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006833
6834 ssl->in_msgtype = 0;
6835 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006837 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006838 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006841 ssl_dtls_replay_reset( ssl );
6842#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006843
6844 ssl->in_hslen = 0;
6845 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006846
6847 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006848
6849 ssl->out_msgtype = 0;
6850 ssl->out_msglen = 0;
6851 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6853 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006854 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006855#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006856
Hanno Becker19859472018-08-06 09:40:20 +01006857 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6858
Paul Bakker48916f92012-09-16 19:57:18 +00006859 ssl->transform_in = NULL;
6860 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006861
Hanno Becker78640902018-08-13 16:35:15 +01006862 ssl->session_in = NULL;
6863 ssl->session_out = NULL;
6864
Angus Grattond8213d02016-05-25 20:56:48 +10006865 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006866
6867#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006868 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006869#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6870 {
6871 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006872 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006873 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6876 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6879 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006883 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006884 }
6885#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006886
Paul Bakker48916f92012-09-16 19:57:18 +00006887 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 mbedtls_ssl_transform_free( ssl->transform );
6890 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006891 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006892 }
Paul Bakker48916f92012-09-16 19:57:18 +00006893
Paul Bakkerc0463502013-02-14 11:19:38 +01006894 if( ssl->session )
6895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 mbedtls_ssl_session_free( ssl->session );
6897 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006898 ssl->session = NULL;
6899 }
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006902 ssl->alpn_chosen = NULL;
6903#endif
6904
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006905#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006906#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006907 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006908#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006909 {
6910 mbedtls_free( ssl->cli_id );
6911 ssl->cli_id = NULL;
6912 ssl->cli_id_len = 0;
6913 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006914#endif
6915
Paul Bakker48916f92012-09-16 19:57:18 +00006916 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6917 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006918
6919 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006920}
6921
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006922/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006923 * Reset an initialized and used SSL context for re-use while retaining
6924 * all application-set variables, function pointers and data.
6925 */
6926int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6927{
6928 return( ssl_session_reset_int( ssl, 0 ) );
6929}
6930
6931/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006932 * SSL set accessors
6933 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006934void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006936 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006937}
6938
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006939void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006940{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006941 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006942}
6943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006945void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006946{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006947 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006948}
6949#endif
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006952void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006953{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006954 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006955}
6956#endif
6957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006959
6960void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6961 unsigned allow_packing )
6962{
6963 ssl->disable_datagram_packing = !allow_packing;
6964}
6965
6966void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6967 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006969 conf->hs_timeout_min = min;
6970 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006971}
6972#endif
6973
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006974void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006975{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006976 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006977}
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006980void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006981 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006982 void *p_vrfy )
6983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006984 conf->f_vrfy = f_vrfy;
6985 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006988
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006989void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006990 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 void *p_rng )
6992{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006993 conf->f_rng = f_rng;
6994 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006995}
6996
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006997void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006998 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006999 void *p_dbg )
7000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007001 conf->f_dbg = f_dbg;
7002 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007003}
7004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007006 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007007 mbedtls_ssl_send_t *f_send,
7008 mbedtls_ssl_recv_t *f_recv,
7009 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007010{
7011 ssl->p_bio = p_bio;
7012 ssl->f_send = f_send;
7013 ssl->f_recv = f_recv;
7014 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007015}
7016
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007017#if defined(MBEDTLS_SSL_PROTO_DTLS)
7018void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7019{
7020 ssl->mtu = mtu;
7021}
7022#endif
7023
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007024void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007025{
7026 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007027}
7028
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007029void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7030 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007031 mbedtls_ssl_set_timer_t *f_set_timer,
7032 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007033{
7034 ssl->p_timer = p_timer;
7035 ssl->f_set_timer = f_set_timer;
7036 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007037
7038 /* Make sure we start with no timer running */
7039 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007040}
7041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007043void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007044 void *p_cache,
7045 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7046 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007047{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007048 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007049 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007050 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if defined(MBEDTLS_SSL_CLI_C)
7055int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007056{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007057 int ret;
7058
7059 if( ssl == NULL ||
7060 session == NULL ||
7061 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007062 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007065 }
7066
7067 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7068 return( ret );
7069
Paul Bakker0a597072012-09-25 21:55:46 +00007070 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007071
7072 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007075
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007076void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007077 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007079 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7080 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7081 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7082 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007083}
7084
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007085void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007086 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007087 int major, int minor )
7088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007090 return;
7091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007093 return;
7094
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007095 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007096}
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007099void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007100 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007101{
7102 conf->cert_profile = profile;
7103}
7104
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007105/* Append a new keycert entry to a (possibly empty) list */
7106static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7107 mbedtls_x509_crt *cert,
7108 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007109{
niisato8ee24222018-06-25 19:05:48 +09007110 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007111
niisato8ee24222018-06-25 19:05:48 +09007112 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7113 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007114 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007115
niisato8ee24222018-06-25 19:05:48 +09007116 new_cert->cert = cert;
7117 new_cert->key = key;
7118 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007119
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007120 /* Update head is the list was null, else add to the end */
7121 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007122 {
niisato8ee24222018-06-25 19:05:48 +09007123 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007124 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007125 else
7126 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007127 mbedtls_ssl_key_cert *cur = *head;
7128 while( cur->next != NULL )
7129 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007130 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007131 }
7132
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007133 return( 0 );
7134}
7135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007136int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007137 mbedtls_x509_crt *own_cert,
7138 mbedtls_pk_context *pk_key )
7139{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007140 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007141}
7142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007143void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007144 mbedtls_x509_crt *ca_chain,
7145 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007146{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007147 conf->ca_chain = ca_chain;
7148 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007151
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007152#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7153int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7154 mbedtls_x509_crt *own_cert,
7155 mbedtls_pk_context *pk_key )
7156{
7157 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7158 own_cert, pk_key ) );
7159}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007160
7161void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7162 mbedtls_x509_crt *ca_chain,
7163 mbedtls_x509_crl *ca_crl )
7164{
7165 ssl->handshake->sni_ca_chain = ca_chain;
7166 ssl->handshake->sni_ca_crl = ca_crl;
7167}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007168
7169void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7170 int authmode )
7171{
7172 ssl->handshake->sni_authmode = authmode;
7173}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007174#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7175
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007176#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007177/*
7178 * Set EC J-PAKE password for current handshake
7179 */
7180int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7181 const unsigned char *pw,
7182 size_t pw_len )
7183{
7184 mbedtls_ecjpake_role role;
7185
Janos Follath8eb64132016-06-03 15:40:57 +01007186 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7188
7189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7190 role = MBEDTLS_ECJPAKE_SERVER;
7191 else
7192 role = MBEDTLS_ECJPAKE_CLIENT;
7193
7194 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7195 role,
7196 MBEDTLS_MD_SHA256,
7197 MBEDTLS_ECP_DP_SECP256R1,
7198 pw, pw_len ) );
7199}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007200#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007203int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007204 const unsigned char *psk, size_t psk_len,
7205 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007206{
Paul Bakker6db455e2013-09-18 17:29:31 +02007207 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007212
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007213 /* Identity len will be encoded on two bytes */
7214 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007215 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007216 {
7217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7218 }
7219
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007220 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007221 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007222 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007223
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007224 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007225 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007226 conf->psk_len = 0;
7227 }
7228 if( conf->psk_identity != NULL )
7229 {
7230 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007231 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007232 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007233 }
7234
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007235 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7236 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007237 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007238 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007239 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007240 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007241 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007242 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007243 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007244
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007245 conf->psk_len = psk_len;
7246 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007247
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007248 memcpy( conf->psk, psk, conf->psk_len );
7249 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007250
7251 return( 0 );
7252}
7253
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007254int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7255 const unsigned char *psk, size_t psk_len )
7256{
7257 if( psk == NULL || ssl->handshake == NULL )
7258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7259
7260 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7262
7263 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007264 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007265 mbedtls_platform_zeroize( ssl->handshake->psk,
7266 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007267 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007268 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007269 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007270
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007271 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007272 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007273
7274 ssl->handshake->psk_len = psk_len;
7275 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7276
7277 return( 0 );
7278}
7279
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007280void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007282 size_t),
7283 void *p_psk )
7284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007285 conf->f_psk = f_psk;
7286 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007289
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007290#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007291
7292#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007293int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007294{
7295 int ret;
7296
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007297 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7298 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7299 {
7300 mbedtls_mpi_free( &conf->dhm_P );
7301 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007302 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007303 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007304
7305 return( 0 );
7306}
Hanno Becker470a8c42017-10-04 15:28:46 +01007307#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007308
Hanno Beckera90658f2017-10-04 15:29:08 +01007309int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7310 const unsigned char *dhm_P, size_t P_len,
7311 const unsigned char *dhm_G, size_t G_len )
7312{
7313 int ret;
7314
7315 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7316 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7317 {
7318 mbedtls_mpi_free( &conf->dhm_P );
7319 mbedtls_mpi_free( &conf->dhm_G );
7320 return( ret );
7321 }
7322
7323 return( 0 );
7324}
Paul Bakker5121ce52009-01-03 21:22:43 +00007325
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007326int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007327{
7328 int ret;
7329
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007330 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7331 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7332 {
7333 mbedtls_mpi_free( &conf->dhm_P );
7334 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007335 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007336 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007337
7338 return( 0 );
7339}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007340#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007341
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007342#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7343/*
7344 * Set the minimum length for Diffie-Hellman parameters
7345 */
7346void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7347 unsigned int bitlen )
7348{
7349 conf->dhm_min_bitlen = bitlen;
7350}
7351#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7352
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007353#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007354/*
7355 * Set allowed/preferred hashes for handshake signatures
7356 */
7357void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7358 const int *hashes )
7359{
7360 conf->sig_hashes = hashes;
7361}
Hanno Becker947194e2017-04-07 13:25:49 +01007362#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007363
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007364#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007365/*
7366 * Set the allowed elliptic curves
7367 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007368void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007369 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007371 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007372}
Hanno Becker947194e2017-04-07 13:25:49 +01007373#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007374
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007375#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007377{
Hanno Becker947194e2017-04-07 13:25:49 +01007378 /* Initialize to suppress unnecessary compiler warning */
7379 size_t hostname_len = 0;
7380
7381 /* Check if new hostname is valid before
7382 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007383 if( hostname != NULL )
7384 {
7385 hostname_len = strlen( hostname );
7386
7387 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7389 }
7390
7391 /* Now it's clear that we will overwrite the old hostname,
7392 * so we can free it safely */
7393
7394 if( ssl->hostname != NULL )
7395 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007396 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007397 mbedtls_free( ssl->hostname );
7398 }
7399
7400 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007401
Paul Bakker5121ce52009-01-03 21:22:43 +00007402 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007403 {
7404 ssl->hostname = NULL;
7405 }
7406 else
7407 {
7408 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007409 if( ssl->hostname == NULL )
7410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007411
Hanno Becker947194e2017-04-07 13:25:49 +01007412 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007413
Hanno Becker947194e2017-04-07 13:25:49 +01007414 ssl->hostname[hostname_len] = '\0';
7415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007416
7417 return( 0 );
7418}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007419#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007420
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007421#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007422void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007424 const unsigned char *, size_t),
7425 void *p_sni )
7426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007427 conf->f_sni = f_sni;
7428 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007433int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007434{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007435 size_t cur_len, tot_len;
7436 const char **p;
7437
7438 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007439 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7440 * MUST NOT be truncated."
7441 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007442 */
7443 tot_len = 0;
7444 for( p = protos; *p != NULL; p++ )
7445 {
7446 cur_len = strlen( *p );
7447 tot_len += cur_len;
7448
7449 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007451 }
7452
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007453 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007454
7455 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007456}
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007459{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007460 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007463
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007464void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007466 conf->max_major_ver = major;
7467 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007468}
7469
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007470void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007471{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007472 conf->min_major_ver = major;
7473 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007474}
7475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007477void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007478{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007479 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007480}
7481#endif
7482
Janos Follath088ce432017-04-10 12:42:31 +01007483#if defined(MBEDTLS_SSL_SRV_C)
7484void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7485 char cert_req_ca_list )
7486{
7487 conf->cert_req_ca_list = cert_req_ca_list;
7488}
7489#endif
7490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007492void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007493{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007494 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007495}
7496#endif
7497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007499void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007501 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007502}
7503#endif
7504
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007505#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007506void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007507{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007508 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007509}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007510#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007513int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007516 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007519 }
7520
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007521 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007522
7523 return( 0 );
7524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007528void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007529{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007530 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007535void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007536{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007537 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007538}
7539#endif
7540
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007541void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007542{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007543 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007544}
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007547void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007550}
7551
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007552void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007554 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007555}
7556
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007557void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007558 const unsigned char period[8] )
7559{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007560 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007565#if defined(MBEDTLS_SSL_CLI_C)
7566void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007567{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007568 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007569}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007570#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007571
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007572#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007573void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7574 mbedtls_ssl_ticket_write_t *f_ticket_write,
7575 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7576 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007577{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007578 conf->f_ticket_write = f_ticket_write;
7579 conf->f_ticket_parse = f_ticket_parse;
7580 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007581}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007584
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007585#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7586void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7587 mbedtls_ssl_export_keys_t *f_export_keys,
7588 void *p_export_keys )
7589{
7590 conf->f_export_keys = f_export_keys;
7591 conf->p_export_keys = p_export_keys;
7592}
7593#endif
7594
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007595#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007596void mbedtls_ssl_conf_async_private_cb(
7597 mbedtls_ssl_config *conf,
7598 mbedtls_ssl_async_sign_t *f_async_sign,
7599 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7600 mbedtls_ssl_async_resume_t *f_async_resume,
7601 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007602 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007603{
7604 conf->f_async_sign_start = f_async_sign;
7605 conf->f_async_decrypt_start = f_async_decrypt;
7606 conf->f_async_resume = f_async_resume;
7607 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007608 conf->p_async_config_data = async_config_data;
7609}
7610
Gilles Peskine8f97af72018-04-26 11:46:10 +02007611void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7612{
7613 return( conf->p_async_config_data );
7614}
7615
Gilles Peskine1febfef2018-04-30 11:54:39 +02007616void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007617{
7618 if( ssl->handshake == NULL )
7619 return( NULL );
7620 else
7621 return( ssl->handshake->user_async_ctx );
7622}
7623
Gilles Peskine1febfef2018-04-30 11:54:39 +02007624void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007625 void *ctx )
7626{
7627 if( ssl->handshake != NULL )
7628 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007629}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007630#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007631
Paul Bakker5121ce52009-01-03 21:22:43 +00007632/*
7633 * SSL get accessors
7634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007636{
7637 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7638}
7639
Hanno Becker8b170a02017-10-10 11:51:19 +01007640int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7641{
7642 /*
7643 * Case A: We're currently holding back
7644 * a message for further processing.
7645 */
7646
7647 if( ssl->keep_current_message == 1 )
7648 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007650 return( 1 );
7651 }
7652
7653 /*
7654 * Case B: Further records are pending in the current datagram.
7655 */
7656
7657#if defined(MBEDTLS_SSL_PROTO_DTLS)
7658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7659 ssl->in_left > ssl->next_record_offset )
7660 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007662 return( 1 );
7663 }
7664#endif /* MBEDTLS_SSL_PROTO_DTLS */
7665
7666 /*
7667 * Case C: A handshake message is being processed.
7668 */
7669
Hanno Becker8b170a02017-10-10 11:51:19 +01007670 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7671 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007673 return( 1 );
7674 }
7675
7676 /*
7677 * Case D: An application data message is being processed
7678 */
7679 if( ssl->in_offt != NULL )
7680 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007682 return( 1 );
7683 }
7684
7685 /*
7686 * In all other cases, the rest of the message can be dropped.
7687 * As in ssl_read_record_layer, this needs to be adapted if
7688 * we implement support for multiple alerts in single records.
7689 */
7690
7691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7692 return( 0 );
7693}
7694
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007695uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007696{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007697 if( ssl->session != NULL )
7698 return( ssl->session->verify_result );
7699
7700 if( ssl->session_negotiate != NULL )
7701 return( ssl->session_negotiate->verify_result );
7702
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007703 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007704}
7705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007707{
Paul Bakker926c8e42013-03-06 10:23:34 +01007708 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007709 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007712}
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007718 {
7719 switch( ssl->minor_ver )
7720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007722 return( "DTLSv1.0" );
7723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007725 return( "DTLSv1.2" );
7726
7727 default:
7728 return( "unknown (DTLS)" );
7729 }
7730 }
7731#endif
7732
Paul Bakker43ca69c2011-01-15 17:35:19 +00007733 switch( ssl->minor_ver )
7734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007736 return( "SSLv3.0" );
7737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007739 return( "TLSv1.0" );
7740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007742 return( "TLSv1.1" );
7743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007745 return( "TLSv1.2" );
7746
Paul Bakker43ca69c2011-01-15 17:35:19 +00007747 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007748 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007749 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007750}
7751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007753{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007754 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007756 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007757
Hanno Becker78640902018-08-13 16:35:15 +01007758 if( transform == NULL )
7759 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_ZLIB_SUPPORT)
7762 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7763 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007764#endif
7765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 case MBEDTLS_MODE_GCM:
7769 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007770 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007772 transform_expansion = transform->minlen;
7773 break;
7774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007776
7777 block_size = mbedtls_cipher_get_block_size(
7778 &transform->cipher_ctx_enc );
7779
7780#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7781 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7782 {
7783 /* Expansion due to addition of
7784 * - MAC
7785 * - CBC padding (theoretically up to 256 bytes, but
7786 * we never use more than block_size)
7787 * - explicit IV
7788 */
7789 transform_expansion = transform->maclen + 2 * block_size;
7790 }
7791 else
7792#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7793 {
7794 /* No explicit IV prior to TLS 1.1. */
7795 transform_expansion = transform->maclen + block_size;
7796 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007797 break;
7798
7799 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007802 }
7803
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007804 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007805}
7806
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007807#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7808size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7809{
7810 size_t max_len;
7811
7812 /*
7813 * Assume mfl_code is correct since it was checked when set
7814 */
Angus Grattond8213d02016-05-25 20:56:48 +10007815 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007816
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007817 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007818 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007819 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007820 {
Angus Grattond8213d02016-05-25 20:56:48 +10007821 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007822 }
7823
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007824 /* During a handshake, use the value being negotiated */
7825 if( ssl->session_negotiate != NULL &&
7826 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7827 {
7828 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7829 }
7830
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007831 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007832}
7833#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7834
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007835int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7836{
7837 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7838
7839#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7840 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7841
7842 if( max_len > mfl )
7843 max_len = mfl;
7844#endif
7845
7846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007847 if( ssl->mtu != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007848 {
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007849 const size_t mtu = ssl->mtu;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007850 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7851 const size_t overhead = (size_t) ret;
7852
7853 if( ret < 0 )
7854 return( ret );
7855
7856 if( mtu <= overhead )
7857 {
7858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7859 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7860 }
7861
7862 if( max_len > mtu - overhead )
7863 max_len = mtu - overhead;
7864 }
7865#endif
7866
Hanno Becker0defedb2018-08-10 12:35:02 +01007867#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7868 !defined(MBEDTLS_SSL_PROTO_DTLS)
7869 ((void) ssl);
7870#endif
7871
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007872 return( (int) max_len );
7873}
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_X509_CRT_PARSE_C)
7876const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007877{
7878 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007879 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007880
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007881 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_CLI_C)
7886int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007887{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007888 if( ssl == NULL ||
7889 dst == NULL ||
7890 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007891 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007894 }
7895
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007896 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007899
Paul Bakker5121ce52009-01-03 21:22:43 +00007900/*
Paul Bakker1961b702013-01-25 14:49:24 +01007901 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007906
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007907 if( ssl == NULL || ssl->conf == NULL )
7908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007917#endif
7918
Paul Bakker1961b702013-01-25 14:49:24 +01007919 return( ret );
7920}
7921
7922/*
7923 * Perform the SSL handshake
7924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007926{
7927 int ret = 0;
7928
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007929 if( ssl == NULL || ssl->conf == NULL )
7930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007937
7938 if( ret != 0 )
7939 break;
7940 }
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007943
7944 return( ret );
7945}
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_RENEGOTIATION)
7948#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007949/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007950 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007953{
7954 int ret;
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007957
7958 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7960 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007961
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007962 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007963 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007965 return( ret );
7966 }
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007969
7970 return( 0 );
7971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007973
7974/*
7975 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 * - any side: calling mbedtls_ssl_renegotiate(),
7977 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7978 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007979 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007980 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007984{
7985 int ret;
7986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007988
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007989 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7990 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007991
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007992 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7993 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007997 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007999 ssl->handshake->out_msg_seq = 1;
8000 else
8001 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008002 }
8003#endif
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8006 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008011 return( ret );
8012 }
8013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008015
8016 return( 0 );
8017}
8018
8019/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008020 * Renegotiate current connection on client,
8021 * or request renegotiation on server
8022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008026
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008027 if( ssl == NULL || ssl->conf == NULL )
8028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008031 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008038
8039 /* Did we already try/start sending HelloRequest? */
8040 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008042
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008043 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008044 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008048 /*
8049 * On client, either start the renegotiation process or,
8050 * if already in progress, continue the handshake
8051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008056
8057 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008060 return( ret );
8061 }
8062 }
8063 else
8064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008068 return( ret );
8069 }
8070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008072
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008073 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008074}
8075
8076/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008077 * Check record counters and renegotiate if they're above the limit.
8078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008080{
Andres AG2196c7f2016-12-15 17:01:16 +00008081 size_t ep_len = ssl_ep_len( ssl );
8082 int in_ctr_cmp;
8083 int out_ctr_cmp;
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8086 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008087 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008088 {
8089 return( 0 );
8090 }
8091
Andres AG2196c7f2016-12-15 17:01:16 +00008092 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8093 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008094 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008095 ssl->conf->renego_period + ep_len, 8 - ep_len );
8096
8097 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008098 {
8099 return( 0 );
8100 }
8101
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008106
8107/*
8108 * Receive application data decrypted from the SSL layer
8109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008111{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008112 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008113 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008114
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008115 if( ssl == NULL || ssl->conf == NULL )
8116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008121 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008124 return( ret );
8125
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008126 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008128 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008129 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008130 return( ret );
8131 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008132 }
8133#endif
8134
Hanno Becker4a810fb2017-05-24 16:27:30 +01008135 /*
8136 * Check if renegotiation is necessary and/or handshake is
8137 * in process. If yes, perform/continue, and fall through
8138 * if an unexpected packet is received while the client
8139 * is waiting for the ServerHello.
8140 *
8141 * (There is no equivalent to the last condition on
8142 * the server-side as it is not treated as within
8143 * a handshake while waiting for the ClientHello
8144 * after a renegotiation request.)
8145 */
8146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008148 ret = ssl_check_ctr_renegotiate( ssl );
8149 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8150 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008153 return( ret );
8154 }
8155#endif
8156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008160 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8161 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008164 return( ret );
8165 }
8166 }
8167
Hanno Beckere41158b2017-10-23 13:30:32 +01008168 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008169 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008170 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008171 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008172 if( ssl->f_get_timer != NULL &&
8173 ssl->f_get_timer( ssl->p_timer ) == -1 )
8174 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008175 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008176 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008177
Hanno Becker327c93b2018-08-15 13:56:18 +01008178 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008179 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008180 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8181 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008182
Hanno Becker4a810fb2017-05-24 16:27:30 +01008183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8184 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008185 }
8186
8187 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008189 {
8190 /*
8191 * OpenSSL sends empty messages to randomize the IV
8192 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008193 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008196 return( 0 );
8197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008199 return( ret );
8200 }
8201 }
8202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008206
Hanno Becker4a810fb2017-05-24 16:27:30 +01008207 /*
8208 * - For client-side, expect SERVER_HELLO_REQUEST.
8209 * - For server-side, expect CLIENT_HELLO.
8210 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8211 */
8212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008214 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008216 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008219
8220 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008223 {
8224 continue;
8225 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008228 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008229#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008230
Hanno Becker4a810fb2017-05-24 16:27:30 +01008231#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008232 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008236
8237 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008240 {
8241 continue;
8242 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008245 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008246#endif /* MBEDTLS_SSL_SRV_C */
8247
Hanno Becker21df7f92017-10-17 11:03:26 +01008248#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008249 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008250 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8251 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8252 ssl->conf->allow_legacy_renegotiation ==
8253 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8254 {
8255 /*
8256 * Accept renegotiation request
8257 */
Paul Bakker48916f92012-09-16 19:57:18 +00008258
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008259 /* DTLS clients need to know renego is server-initiated */
8260#if defined(MBEDTLS_SSL_PROTO_DTLS)
8261 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8262 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8263 {
8264 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8265 }
8266#endif
8267 ret = ssl_start_renegotiation( ssl );
8268 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8269 ret != 0 )
8270 {
8271 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8272 return( ret );
8273 }
8274 }
8275 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008276#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008277 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008278 /*
8279 * Refuse renegotiation
8280 */
8281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_SSL_PROTO_SSL3)
8285 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008286 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008287 /* SSLv3 does not have a "no_renegotiation" warning, so
8288 we send a fatal alert and abort the connection. */
8289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8290 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8291 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008292 }
8293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8295#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8296 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8297 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8300 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8301 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008302 {
8303 return( ret );
8304 }
Paul Bakker48916f92012-09-16 19:57:18 +00008305 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008306 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8308 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008312 }
Paul Bakker48916f92012-09-16 19:57:18 +00008313 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008314
Hanno Becker90333da2017-10-10 11:27:13 +01008315 /* At this point, we don't know whether the renegotiation has been
8316 * completed or not. The cases to consider are the following:
8317 * 1) The renegotiation is complete. In this case, no new record
8318 * has been read yet.
8319 * 2) The renegotiation is incomplete because the client received
8320 * an application data record while awaiting the ServerHello.
8321 * 3) The renegotiation is incomplete because the client received
8322 * a non-handshake, non-application data message while awaiting
8323 * the ServerHello.
8324 * In each of these case, looping will be the proper action:
8325 * - For 1), the next iteration will read a new record and check
8326 * if it's application data.
8327 * - For 2), the loop condition isn't satisfied as application data
8328 * is present, hence continue is the same as break
8329 * - For 3), the loop condition is satisfied and read_record
8330 * will re-deliver the message that was held back by the client
8331 * when expecting the ServerHello.
8332 */
8333 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008334 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008335#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008337 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008338 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008339 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008340 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008343 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008345 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008346 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8351 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008354 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008355 }
8356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8360 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008361 }
8362
8363 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008364
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008365 /* We're going to return something now, cancel timer,
8366 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008368 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008369
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008371 /* If we requested renego but received AppData, resend HelloRequest.
8372 * Do it now, after setting in_offt, to avoid taking this branch
8373 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008377 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008378 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008381 return( ret );
8382 }
8383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008385#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008386 }
8387
8388 n = ( len < ssl->in_msglen )
8389 ? len : ssl->in_msglen;
8390
8391 memcpy( buf, ssl->in_offt, n );
8392 ssl->in_msglen -= n;
8393
8394 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008395 {
8396 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008397 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008398 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008400 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008401 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008402 /* more data available */
8403 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008407
Paul Bakker23986e52011-04-24 08:57:21 +00008408 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008409}
8410
8411/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008412 * Send application data to be encrypted by the SSL layer, taking care of max
8413 * fragment length and buffer size.
8414 *
8415 * According to RFC 5246 Section 6.2.1:
8416 *
8417 * Zero-length fragments of Application data MAY be sent as they are
8418 * potentially useful as a traffic analysis countermeasure.
8419 *
8420 * Therefore, it is possible that the input message length is 0 and the
8421 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008422 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008423static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008424 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008425{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008426 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8427 const size_t max_len = (size_t) ret;
8428
8429 if( ret < 0 )
8430 {
8431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8432 return( ret );
8433 }
8434
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008435 if( len > max_len )
8436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008438 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008441 "maximum fragment length: %d > %d",
8442 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008444 }
8445 else
8446#endif
8447 len = max_len;
8448 }
Paul Bakker887bd502011-06-08 13:10:54 +00008449
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 if( ssl->out_left != 0 )
8451 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008452 /*
8453 * The user has previously tried to send the data and
8454 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8455 * written. In this case, we expect the high-level write function
8456 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008461 return( ret );
8462 }
8463 }
Paul Bakker887bd502011-06-08 13:10:54 +00008464 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008465 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008466 /*
8467 * The user is trying to send a message the first time, so we need to
8468 * copy the data into the internal buffers and setup the data structure
8469 * to keep track of partial writes
8470 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008471 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008473 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008474
Hanno Becker67bc7c32018-08-06 11:33:50 +01008475 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008478 return( ret );
8479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008480 }
8481
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008482 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008483}
8484
8485/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008486 * Write application data, doing 1/n-1 splitting if necessary.
8487 *
8488 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008489 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008490 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008492#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008493static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008494 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008495{
8496 int ret;
8497
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008498 if( ssl->conf->cbc_record_splitting ==
8499 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008500 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8502 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8503 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008504 {
8505 return( ssl_write_real( ssl, buf, len ) );
8506 }
8507
8508 if( ssl->split_done == 0 )
8509 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008510 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008511 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008512 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008513 }
8514
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008515 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8516 return( ret );
8517 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008518
8519 return( ret + 1 );
8520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008522
8523/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008524 * Write application data (public-facing wrapper)
8525 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008526int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008527{
8528 int ret;
8529
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008531
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008532 if( ssl == NULL || ssl->conf == NULL )
8533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8534
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008535#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008536 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8537 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008538 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008539 return( ret );
8540 }
8541#endif
8542
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008543 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008544 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008545 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008546 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008548 return( ret );
8549 }
8550 }
8551
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008552#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008553 ret = ssl_write_split( ssl, buf, len );
8554#else
8555 ret = ssl_write_real( ssl, buf, len );
8556#endif
8557
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008559
8560 return( ret );
8561}
8562
8563/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008564 * Notify the peer that the connection is being closed
8565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008567{
8568 int ret;
8569
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008570 if( ssl == NULL || ssl->conf == NULL )
8571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008574
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008575 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8581 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8582 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008585 return( ret );
8586 }
8587 }
8588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008590
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008591 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008592}
8593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008595{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008596 if( transform == NULL )
8597 return;
8598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008600 deflateEnd( &transform->ctx_deflate );
8601 inflateEnd( &transform->ctx_inflate );
8602#endif
8603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8605 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607 mbedtls_md_free( &transform->md_ctx_enc );
8608 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008609
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008610 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008611}
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#if defined(MBEDTLS_X509_CRT_PARSE_C)
8614static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008617
8618 while( cur != NULL )
8619 {
8620 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008622 cur = next;
8623 }
8624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008626
Hanno Becker0271f962018-08-16 13:23:47 +01008627#if defined(MBEDTLS_SSL_PROTO_DTLS)
8628
8629static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8630{
8631 unsigned offset;
8632 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8633
8634 if( hs == NULL )
8635 return;
8636
8637 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008638 ssl_buffering_free_slot( ssl, offset );
8639}
8640
8641static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8642 uint8_t slot )
8643{
8644 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8645 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
8646 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008647 {
Hanno Beckere605b192018-08-21 15:59:07 +01008648 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8649 mbedtls_free( hs_buf->data );
8650 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008651 }
8652}
8653
8654#endif /* MBEDTLS_SSL_PROTO_DTLS */
8655
Gilles Peskine9b562d52018-04-25 20:32:43 +02008656void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008657{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008658 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8659
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008660 if( handshake == NULL )
8661 return;
8662
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008663#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8664 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8665 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008666 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008667 handshake->async_in_progress = 0;
8668 }
8669#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8670
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008671#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8672 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8673 mbedtls_md5_free( &handshake->fin_md5 );
8674 mbedtls_sha1_free( &handshake->fin_sha1 );
8675#endif
8676#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8677#if defined(MBEDTLS_SHA256_C)
8678 mbedtls_sha256_free( &handshake->fin_sha256 );
8679#endif
8680#if defined(MBEDTLS_SHA512_C)
8681 mbedtls_sha512_free( &handshake->fin_sha512 );
8682#endif
8683#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_DHM_C)
8686 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008687#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#if defined(MBEDTLS_ECDH_C)
8689 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008690#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008691#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008692 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008693#if defined(MBEDTLS_SSL_CLI_C)
8694 mbedtls_free( handshake->ecjpake_cache );
8695 handshake->ecjpake_cache = NULL;
8696 handshake->ecjpake_cache_len = 0;
8697#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008698#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008699
Janos Follath4ae5c292016-02-10 11:27:43 +00008700#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8701 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008702 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008704#endif
8705
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008706#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8707 if( handshake->psk != NULL )
8708 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008709 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008710 mbedtls_free( handshake->psk );
8711 }
8712#endif
8713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8715 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008716 /*
8717 * Free only the linked list wrapper, not the keys themselves
8718 * since the belong to the SNI callback
8719 */
8720 if( handshake->sni_key_cert != NULL )
8721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008723
8724 while( cur != NULL )
8725 {
8726 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008728 cur = next;
8729 }
8730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733#if defined(MBEDTLS_SSL_PROTO_DTLS)
8734 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008735 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008736 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008737 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008738#endif
8739
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008740 mbedtls_platform_zeroize( handshake,
8741 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008742}
8743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008745{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008746 if( session == NULL )
8747 return;
8748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008750 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 mbedtls_x509_crt_free( session->peer_cert );
8753 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008754 }
Paul Bakkered27a042013-04-18 22:46:23 +02008755#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008756
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008757#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008759#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008760
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008761 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008762}
8763
Paul Bakker5121ce52009-01-03 21:22:43 +00008764/*
8765 * Free an SSL context
8766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008768{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008769 if( ssl == NULL )
8770 return;
8771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008773
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008774 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008775 {
Angus Grattond8213d02016-05-25 20:56:48 +10008776 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008778 }
8779
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008780 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008781 {
Angus Grattond8213d02016-05-25 20:56:48 +10008782 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008784 }
8785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008787 if( ssl->compress_buf != NULL )
8788 {
Angus Grattond8213d02016-05-25 20:56:48 +10008789 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008791 }
8792#endif
8793
Paul Bakker48916f92012-09-16 19:57:18 +00008794 if( ssl->transform )
8795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796 mbedtls_ssl_transform_free( ssl->transform );
8797 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008798 }
8799
8800 if( ssl->handshake )
8801 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008802 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8804 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 mbedtls_free( ssl->handshake );
8807 mbedtls_free( ssl->transform_negotiate );
8808 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008809 }
8810
Paul Bakkerc0463502013-02-14 11:19:38 +01008811 if( ssl->session )
8812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 mbedtls_ssl_session_free( ssl->session );
8814 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008815 }
8816
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008817#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008818 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008819 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008820 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008822 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8826 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8829 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008830 }
8831#endif
8832
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008833#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008835#endif
8836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008838
Paul Bakker86f04f42013-02-14 11:20:09 +01008839 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008840 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008841}
8842
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008843/*
8844 * Initialze mbedtls_ssl_config
8845 */
8846void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8847{
8848 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8849}
8850
Simon Butcherc97b6972015-12-27 23:48:17 +00008851#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008852static int ssl_preset_default_hashes[] = {
8853#if defined(MBEDTLS_SHA512_C)
8854 MBEDTLS_MD_SHA512,
8855 MBEDTLS_MD_SHA384,
8856#endif
8857#if defined(MBEDTLS_SHA256_C)
8858 MBEDTLS_MD_SHA256,
8859 MBEDTLS_MD_SHA224,
8860#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008861#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008862 MBEDTLS_MD_SHA1,
8863#endif
8864 MBEDTLS_MD_NONE
8865};
Simon Butcherc97b6972015-12-27 23:48:17 +00008866#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008867
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008868static int ssl_preset_suiteb_ciphersuites[] = {
8869 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8870 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8871 0
8872};
8873
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008874#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008875static int ssl_preset_suiteb_hashes[] = {
8876 MBEDTLS_MD_SHA256,
8877 MBEDTLS_MD_SHA384,
8878 MBEDTLS_MD_NONE
8879};
8880#endif
8881
8882#if defined(MBEDTLS_ECP_C)
8883static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8884 MBEDTLS_ECP_DP_SECP256R1,
8885 MBEDTLS_ECP_DP_SECP384R1,
8886 MBEDTLS_ECP_DP_NONE
8887};
8888#endif
8889
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008890/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008891 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008892 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008893int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008894 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008895{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008896#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008897 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008898#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008899
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008900 /* Use the functions here so that they are covered in tests,
8901 * but otherwise access member directly for efficiency */
8902 mbedtls_ssl_conf_endpoint( conf, endpoint );
8903 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008904
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008905 /*
8906 * Things that are common to all presets
8907 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008908#if defined(MBEDTLS_SSL_CLI_C)
8909 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8910 {
8911 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8912#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8913 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8914#endif
8915 }
8916#endif
8917
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008918#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008919 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008920#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008921
8922#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8923 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8924#endif
8925
8926#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8927 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8928#endif
8929
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008930#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8931 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8932#endif
8933
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008934#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008935 conf->f_cookie_write = ssl_cookie_write_dummy;
8936 conf->f_cookie_check = ssl_cookie_check_dummy;
8937#endif
8938
8939#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8940 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8941#endif
8942
Janos Follath088ce432017-04-10 12:42:31 +01008943#if defined(MBEDTLS_SSL_SRV_C)
8944 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8945#endif
8946
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008947#if defined(MBEDTLS_SSL_PROTO_DTLS)
8948 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8949 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8950#endif
8951
8952#if defined(MBEDTLS_SSL_RENEGOTIATION)
8953 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008954 memset( conf->renego_period, 0x00, 2 );
8955 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008956#endif
8957
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008958#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8959 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8960 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008961 const unsigned char dhm_p[] =
8962 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8963 const unsigned char dhm_g[] =
8964 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8965
Hanno Beckera90658f2017-10-04 15:29:08 +01008966 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8967 dhm_p, sizeof( dhm_p ),
8968 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008969 {
8970 return( ret );
8971 }
8972 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008973#endif
8974
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008975 /*
8976 * Preset-specific defaults
8977 */
8978 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008979 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008980 /*
8981 * NSA Suite B
8982 */
8983 case MBEDTLS_SSL_PRESET_SUITEB:
8984 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8985 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8986 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8987 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8988
8989 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8990 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8991 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8992 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8993 ssl_preset_suiteb_ciphersuites;
8994
8995#if defined(MBEDTLS_X509_CRT_PARSE_C)
8996 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008997#endif
8998
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008999#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009000 conf->sig_hashes = ssl_preset_suiteb_hashes;
9001#endif
9002
9003#if defined(MBEDTLS_ECP_C)
9004 conf->curve_list = ssl_preset_suiteb_curves;
9005#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009006 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009007
9008 /*
9009 * Default
9010 */
9011 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009012 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9013 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9014 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9015 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9016 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9017 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9018 MBEDTLS_SSL_MIN_MINOR_VERSION :
9019 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009020 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9021 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9022
9023#if defined(MBEDTLS_SSL_PROTO_DTLS)
9024 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9025 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9026#endif
9027
9028 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9029 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9030 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9031 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9032 mbedtls_ssl_list_ciphersuites();
9033
9034#if defined(MBEDTLS_X509_CRT_PARSE_C)
9035 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9036#endif
9037
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009038#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009039 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009040#endif
9041
9042#if defined(MBEDTLS_ECP_C)
9043 conf->curve_list = mbedtls_ecp_grp_id_list();
9044#endif
9045
9046#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9047 conf->dhm_min_bitlen = 1024;
9048#endif
9049 }
9050
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009051 return( 0 );
9052}
9053
9054/*
9055 * Free mbedtls_ssl_config
9056 */
9057void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9058{
9059#if defined(MBEDTLS_DHM_C)
9060 mbedtls_mpi_free( &conf->dhm_P );
9061 mbedtls_mpi_free( &conf->dhm_G );
9062#endif
9063
9064#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9065 if( conf->psk != NULL )
9066 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009067 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009068 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009069 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009070 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009071 }
9072
9073 if( conf->psk_identity != NULL )
9074 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009075 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009076 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009077 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009078 conf->psk_identity_len = 0;
9079 }
9080#endif
9081
9082#if defined(MBEDTLS_X509_CRT_PARSE_C)
9083 ssl_key_cert_free( conf->key_cert );
9084#endif
9085
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009086 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009087}
9088
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009089#if defined(MBEDTLS_PK_C) && \
9090 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009091/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_RSA_C)
9097 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9098 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100#if defined(MBEDTLS_ECDSA_C)
9101 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9102 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009105}
9106
Hanno Becker7e5437a2017-04-28 17:15:26 +01009107unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9108{
9109 switch( type ) {
9110 case MBEDTLS_PK_RSA:
9111 return( MBEDTLS_SSL_SIG_RSA );
9112 case MBEDTLS_PK_ECDSA:
9113 case MBEDTLS_PK_ECKEY:
9114 return( MBEDTLS_SSL_SIG_ECDSA );
9115 default:
9116 return( MBEDTLS_SSL_SIG_ANON );
9117 }
9118}
9119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009121{
9122 switch( sig )
9123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124#if defined(MBEDTLS_RSA_C)
9125 case MBEDTLS_SSL_SIG_RSA:
9126 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128#if defined(MBEDTLS_ECDSA_C)
9129 case MBEDTLS_SSL_SIG_ECDSA:
9130 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009131#endif
9132 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009134 }
9135}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009136#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009137
Hanno Becker7e5437a2017-04-28 17:15:26 +01009138#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9139 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9140
9141/* Find an entry in a signature-hash set matching a given hash algorithm. */
9142mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9143 mbedtls_pk_type_t sig_alg )
9144{
9145 switch( sig_alg )
9146 {
9147 case MBEDTLS_PK_RSA:
9148 return( set->rsa );
9149 case MBEDTLS_PK_ECDSA:
9150 return( set->ecdsa );
9151 default:
9152 return( MBEDTLS_MD_NONE );
9153 }
9154}
9155
9156/* Add a signature-hash-pair to a signature-hash set */
9157void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9158 mbedtls_pk_type_t sig_alg,
9159 mbedtls_md_type_t md_alg )
9160{
9161 switch( sig_alg )
9162 {
9163 case MBEDTLS_PK_RSA:
9164 if( set->rsa == MBEDTLS_MD_NONE )
9165 set->rsa = md_alg;
9166 break;
9167
9168 case MBEDTLS_PK_ECDSA:
9169 if( set->ecdsa == MBEDTLS_MD_NONE )
9170 set->ecdsa = md_alg;
9171 break;
9172
9173 default:
9174 break;
9175 }
9176}
9177
9178/* Allow exactly one hash algorithm for each signature. */
9179void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9180 mbedtls_md_type_t md_alg )
9181{
9182 set->rsa = md_alg;
9183 set->ecdsa = md_alg;
9184}
9185
9186#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9187 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9188
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009189/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009190 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009193{
9194 switch( hash )
9195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196#if defined(MBEDTLS_MD5_C)
9197 case MBEDTLS_SSL_HASH_MD5:
9198 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200#if defined(MBEDTLS_SHA1_C)
9201 case MBEDTLS_SSL_HASH_SHA1:
9202 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204#if defined(MBEDTLS_SHA256_C)
9205 case MBEDTLS_SSL_HASH_SHA224:
9206 return( MBEDTLS_MD_SHA224 );
9207 case MBEDTLS_SSL_HASH_SHA256:
9208 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009209#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210#if defined(MBEDTLS_SHA512_C)
9211 case MBEDTLS_SSL_HASH_SHA384:
9212 return( MBEDTLS_MD_SHA384 );
9213 case MBEDTLS_SSL_HASH_SHA512:
9214 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009215#endif
9216 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009218 }
9219}
9220
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009221/*
9222 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9223 */
9224unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9225{
9226 switch( md )
9227 {
9228#if defined(MBEDTLS_MD5_C)
9229 case MBEDTLS_MD_MD5:
9230 return( MBEDTLS_SSL_HASH_MD5 );
9231#endif
9232#if defined(MBEDTLS_SHA1_C)
9233 case MBEDTLS_MD_SHA1:
9234 return( MBEDTLS_SSL_HASH_SHA1 );
9235#endif
9236#if defined(MBEDTLS_SHA256_C)
9237 case MBEDTLS_MD_SHA224:
9238 return( MBEDTLS_SSL_HASH_SHA224 );
9239 case MBEDTLS_MD_SHA256:
9240 return( MBEDTLS_SSL_HASH_SHA256 );
9241#endif
9242#if defined(MBEDTLS_SHA512_C)
9243 case MBEDTLS_MD_SHA384:
9244 return( MBEDTLS_SSL_HASH_SHA384 );
9245 case MBEDTLS_MD_SHA512:
9246 return( MBEDTLS_SSL_HASH_SHA512 );
9247#endif
9248 default:
9249 return( MBEDTLS_SSL_HASH_NONE );
9250 }
9251}
9252
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009253#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009254/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009255 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009256 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009257 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009258int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009261
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009262 if( ssl->conf->curve_list == NULL )
9263 return( -1 );
9264
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009265 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009266 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009267 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009268
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009269 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009270}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009271#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009272
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009273#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009274/*
9275 * Check if a hash proposed by the peer is in our list.
9276 * Return 0 if we're willing to use it, -1 otherwise.
9277 */
9278int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9279 mbedtls_md_type_t md )
9280{
9281 const int *cur;
9282
9283 if( ssl->conf->sig_hashes == NULL )
9284 return( -1 );
9285
9286 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9287 if( *cur == (int) md )
9288 return( 0 );
9289
9290 return( -1 );
9291}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009292#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#if defined(MBEDTLS_X509_CRT_PARSE_C)
9295int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9296 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009297 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009298 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009299{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009300 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009302 int usage = 0;
9303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009305 const char *ext_oid;
9306 size_t ext_len;
9307#endif
9308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9310 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009311 ((void) cert);
9312 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009313 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009314#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9317 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009318 {
9319 /* Server part of the key exchange */
9320 switch( ciphersuite->key_exchange )
9321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 case MBEDTLS_KEY_EXCHANGE_RSA:
9323 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009324 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009325 break;
9326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9328 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9329 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9330 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009331 break;
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9334 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009335 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009336 break;
9337
9338 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 case MBEDTLS_KEY_EXCHANGE_NONE:
9340 case MBEDTLS_KEY_EXCHANGE_PSK:
9341 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9342 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009343 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009344 usage = 0;
9345 }
9346 }
9347 else
9348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9350 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009351 }
9352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009354 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009355 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009356 ret = -1;
9357 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009358#else
9359 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9363 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9366 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009367 }
9368 else
9369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9371 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009372 }
9373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009375 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009376 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009377 ret = -1;
9378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009380
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009381 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009384
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009385/*
9386 * Convert version numbers to/from wire format
9387 * and, for DTLS, to/from TLS equivalent.
9388 *
9389 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009390 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009391 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9392 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009395 unsigned char ver[2] )
9396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397#if defined(MBEDTLS_SSL_PROTO_DTLS)
9398 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009401 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9402
9403 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9404 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9405 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009406 else
9407#else
9408 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009409#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009410 {
9411 ver[0] = (unsigned char) major;
9412 ver[1] = (unsigned char) minor;
9413 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009414}
9415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009417 const unsigned char ver[2] )
9418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#if defined(MBEDTLS_SSL_PROTO_DTLS)
9420 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009421 {
9422 *major = 255 - ver[0] + 2;
9423 *minor = 255 - ver[1] + 1;
9424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009426 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9427 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009428 else
9429#else
9430 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009431#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009432 {
9433 *major = ver[0];
9434 *minor = ver[1];
9435 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009436}
9437
Simon Butcher99000142016-10-13 17:21:01 +01009438int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9439{
9440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9441 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9442 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9443
9444 switch( md )
9445 {
9446#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9447#if defined(MBEDTLS_MD5_C)
9448 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009449 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009450#endif
9451#if defined(MBEDTLS_SHA1_C)
9452 case MBEDTLS_SSL_HASH_SHA1:
9453 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9454 break;
9455#endif
9456#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9457#if defined(MBEDTLS_SHA512_C)
9458 case MBEDTLS_SSL_HASH_SHA384:
9459 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9460 break;
9461#endif
9462#if defined(MBEDTLS_SHA256_C)
9463 case MBEDTLS_SSL_HASH_SHA256:
9464 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9465 break;
9466#endif
9467 default:
9468 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9469 }
9470
9471 return 0;
9472#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9473 (void) ssl;
9474 (void) md;
9475
9476 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9478}
9479
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009480#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9481 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9482int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9483 unsigned char *output,
9484 unsigned char *data, size_t data_len )
9485{
9486 int ret = 0;
9487 mbedtls_md5_context mbedtls_md5;
9488 mbedtls_sha1_context mbedtls_sha1;
9489
9490 mbedtls_md5_init( &mbedtls_md5 );
9491 mbedtls_sha1_init( &mbedtls_sha1 );
9492
9493 /*
9494 * digitally-signed struct {
9495 * opaque md5_hash[16];
9496 * opaque sha_hash[20];
9497 * };
9498 *
9499 * md5_hash
9500 * MD5(ClientHello.random + ServerHello.random
9501 * + ServerParams);
9502 * sha_hash
9503 * SHA(ClientHello.random + ServerHello.random
9504 * + ServerParams);
9505 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009506 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009507 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009509 goto exit;
9510 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009511 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009512 ssl->handshake->randbytes, 64 ) ) != 0 )
9513 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009515 goto exit;
9516 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009517 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009518 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009520 goto exit;
9521 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009522 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009523 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009525 goto exit;
9526 }
9527
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009528 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009529 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009531 goto exit;
9532 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009533 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009534 ssl->handshake->randbytes, 64 ) ) != 0 )
9535 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009537 goto exit;
9538 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009539 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009540 data_len ) ) != 0 )
9541 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009543 goto exit;
9544 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009545 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009546 output + 16 ) ) != 0 )
9547 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009549 goto exit;
9550 }
9551
9552exit:
9553 mbedtls_md5_free( &mbedtls_md5 );
9554 mbedtls_sha1_free( &mbedtls_sha1 );
9555
9556 if( ret != 0 )
9557 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9558 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9559
9560 return( ret );
9561
9562}
9563#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9564 MBEDTLS_SSL_PROTO_TLS1_1 */
9565
9566#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9567 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9568int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009569 unsigned char *hash, size_t *hashlen,
9570 unsigned char *data, size_t data_len,
9571 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009572{
9573 int ret = 0;
9574 mbedtls_md_context_t ctx;
9575 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009576 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009577
9578 mbedtls_md_init( &ctx );
9579
9580 /*
9581 * digitally-signed struct {
9582 * opaque client_random[32];
9583 * opaque server_random[32];
9584 * ServerDHParams params;
9585 * };
9586 */
9587 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9588 {
9589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9590 goto exit;
9591 }
9592 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9593 {
9594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9595 goto exit;
9596 }
9597 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9598 {
9599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9600 goto exit;
9601 }
9602 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9603 {
9604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9605 goto exit;
9606 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009607 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009608 {
9609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9610 goto exit;
9611 }
9612
9613exit:
9614 mbedtls_md_free( &ctx );
9615
9616 if( ret != 0 )
9617 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9618 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9619
9620 return( ret );
9621}
9622#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9623 MBEDTLS_SSL_PROTO_TLS1_2 */
9624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625#endif /* MBEDTLS_SSL_TLS_C */